diff --git a/lib/doorkeeper/models/access_token_mixin.rb b/lib/doorkeeper/models/access_token_mixin.rb index 7ab79a786..bafb2ba46 100644 --- a/lib/doorkeeper/models/access_token_mixin.rb +++ b/lib/doorkeeper/models/access_token_mixin.rb @@ -10,9 +10,16 @@ module AccessTokenMixin include ActiveModel::MassAssignmentSecurity if defined?(::ProtectedAttributes) included do - belongs_to :application, - class_name: 'Doorkeeper::Application', - inverse_of: :access_tokens + belongs_to_options = { + class_name: 'Doorkeeper::Application', + inverse_of: :access_tokens + } + + if defined?(ActiveRecord) && defined?(ActiveRecord::Base) && self < ActiveRecord::Base + belongs_to_options.merge! :optional => true if ActiveRecord::VERSION::MAJOR >= 5 + end + + belongs_to :application, belongs_to_options validates :token, presence: true, uniqueness: true validates :refresh_token, uniqueness: true, if: :use_refresh_token? diff --git a/spec/models/doorkeeper/access_token_spec.rb b/spec/models/doorkeeper/access_token_spec.rb index ff0e9ba3c..e28796726 100644 --- a/spec/models/doorkeeper/access_token_spec.rb +++ b/spec/models/doorkeeper/access_token_spec.rb @@ -143,6 +143,12 @@ module NoGenerate subject.resource_owner_id = nil expect(subject).to be_valid end + + it 'is valid without application_id' do + # For resource owner credentials flow + subject.application_id = nil + expect(subject).to be_valid + end end describe '#same_credential?' do