From 58f04ea4effbeec479d4afe165745a41bb7bae01 Mon Sep 17 00:00:00 2001 From: Nikita Bulai Date: Tue, 24 Mar 2020 12:59:36 +0300 Subject: [PATCH 01/17] Update Travis config --- .travis.yml | 11 ++--------- 1 file changed, 2 insertions(+), 9 deletions(-) diff --git a/.travis.yml b/.travis.yml index a1651f2..e8300d9 100644 --- a/.travis.yml +++ b/.travis.yml @@ -10,18 +10,15 @@ before_install: - mkdir ${PWD}/mongodb-linux-x86_64-3.2.18/data # Bind on another port in order to test mongo_mapper separately - ${PWD}/mongodb-linux-x86_64-3.2.18/bin/mongod --dbpath ${PWD}/mongodb-linux-x86_64-3.2.18/data --logpath ${PWD}/mongodb-linux-x86_64-3.2.18/mongodb.log --fork --port 27027 - - gem update --system # Need for Ruby 2.5.0. https://github.com/travis-ci/travis-ci/issues/8978 - gem install bundler -v '~> 1.10' rvm: - - 2.1 - - 2.2 - - 2.3 - 2.4 - 2.5 + - 2.6 + - 2.7 env: - - RAILS=4.2.6 - RAILS=5.0.7 - RAILS=5.1.6 - RAILS=5.2.0 @@ -62,14 +59,10 @@ matrix: env: RAILS=5.2.0 # Mongoid6 requires activemodel ~> 5.0 - - gemfile: gemfiles/Gemfile.mongoid6.rb - env: RAILS=4.2.6 - gemfile: gemfiles/Gemfile.mongoid6.rb rvm: 2.1 # Mongoid7 requires activemodel ~> 5.1 - - gemfile: gemfiles/Gemfile.mongoid7.rb - env: RAILS=4.2.6 - gemfile: gemfiles/Gemfile.mongoid7.rb env: RAILS=5.0.7 - gemfile: gemfiles/Gemfile.mongoid7.rb From 3102600cc80f4eb3a600c8961e2104982173381a Mon Sep 17 00:00:00 2001 From: Nikita Bulai Date: Tue, 24 Mar 2020 15:07:21 +0300 Subject: [PATCH 02/17] Update to support Doorkeeper 5.x, p1 --- Rakefile | 1 + gemfiles/Gemfile.common.rb | 1 + lib/doorkeeper-mongodb.rb | 2 ++ 3 files changed, 4 insertions(+) diff --git a/Rakefile b/Rakefile index 95bc97c..167683b 100644 --- a/Rakefile +++ b/Rakefile @@ -10,6 +10,7 @@ task :load_doorkeeper do end `cp -r -n doorkeeper/spec .` `rm -rf spec/generators/` # we are not ActiveRecord + `rm -rf spec/validators/` `bundle exec rspec` end diff --git a/gemfiles/Gemfile.common.rb b/gemfiles/Gemfile.common.rb index 70bb100..69db975 100644 --- a/gemfiles/Gemfile.common.rb +++ b/gemfiles/Gemfile.common.rb @@ -7,6 +7,7 @@ gem 'rails', "~> #{ENV['RAILS']}" gem 'doorkeeper', "~> #{ENV['DOORKEEPER']}" +gem 'bcrypt' # Older Grape requires Ruby >= 2.2.2 if ENV['RAILS'][0] == '4' diff --git a/lib/doorkeeper-mongodb.rb b/lib/doorkeeper-mongodb.rb index 8cceab4..2a02921 100644 --- a/lib/doorkeeper-mongodb.rb +++ b/lib/doorkeeper-mongodb.rb @@ -1,6 +1,8 @@ require 'doorkeeper-mongodb/version' +require 'active_model' require 'doorkeeper' +require 'doorkeeper/orm/active_record/redirect_uri_validator' require 'doorkeeper-mongodb/compatible' require 'doorkeeper-mongodb/shared/scopes' From 765602c5b8af673b134612e96f93d9b455eac25d Mon Sep 17 00:00:00 2001 From: Nikita Bulai Date: Tue, 24 Mar 2020 16:05:43 +0300 Subject: [PATCH 03/17] Add support for Doorkeeper 5.x, p. 2 --- Rakefile | 1 + config/locales/en.yml | 8 ++ doorkeeper-mongodb.gemspec | 1 + gemfiles/Gemfile.common.rb | 1 + gemfiles/Gemfile.mongo_mapper.rb | 1 + gemfiles/Gemfile.mongoid4.rb | 1 + gemfiles/Gemfile.mongoid5.rb | 1 + gemfiles/Gemfile.mongoid6.rb | 9 +- gemfiles/Gemfile.mongoid7.rb | 1 + lib/doorkeeper-mongodb.rb | 1 + lib/doorkeeper-mongodb/compatible.rb | 10 ++ .../mixins/mongo_mapper/access_grant_mixin.rb | 1 + .../mixins/mongo_mapper/access_token_mixin.rb | 1 + .../mixins/mongo_mapper/application_mixin.rb | 1 + .../mixins/mongo_mapper/base_mixin.rb | 1 + .../mixins/mongoid/access_grant_mixin.rb | 27 +++- .../mixins/mongoid/access_token_mixin.rb | 128 ++++++++++++++---- .../mixins/mongoid/application_mixin.rb | 62 ++++++++- .../mixins/mongoid/base_mixin.rb | 7 + lib/doorkeeper-mongodb/shared/scopes.rb | 1 + lib/doorkeeper-mongodb/version.rb | 1 + .../mongo_mapper/indexes_generator.rb | 1 + .../mongo_mapper/templates/indexes.rb | 1 + lib/doorkeeper/orm/mongo_mapper.rb | 1 + .../orm/mongo_mapper/access_grant.rb | 1 + .../orm/mongo_mapper/access_token.rb | 1 + .../orm/mongo_mapper/application.rb | 1 + lib/doorkeeper/orm/mongoid4.rb | 1 + lib/doorkeeper/orm/mongoid4/access_grant.rb | 2 + lib/doorkeeper/orm/mongoid4/access_token.rb | 2 + lib/doorkeeper/orm/mongoid4/application.rb | 1 + lib/doorkeeper/orm/mongoid5.rb | 1 + lib/doorkeeper/orm/mongoid5/access_grant.rb | 2 + lib/doorkeeper/orm/mongoid5/access_token.rb | 2 + lib/doorkeeper/orm/mongoid5/application.rb | 1 + lib/doorkeeper/orm/mongoid6.rb | 1 + lib/doorkeeper/orm/mongoid6/access_grant.rb | 2 + lib/doorkeeper/orm/mongoid6/access_token.rb | 72 +++++----- lib/doorkeeper/orm/mongoid6/application.rb | 65 ++++----- lib/doorkeeper/orm/mongoid7.rb | 1 + lib/doorkeeper/orm/mongoid7/access_grant.rb | 2 + lib/doorkeeper/orm/mongoid7/access_token.rb | 2 + lib/doorkeeper/orm/mongoid7/application.rb | 1 + spec/dummy/app/models/user.rb | 1 + spec/support/orm/mongo_mapper.rb | 1 + spec/support/orm/mongoid.rb | 1 + spec/support/orm/mongoid4.rb | 1 + spec/support/orm/mongoid5.rb | 1 + spec/support/orm/mongoid6.rb | 1 + spec/support/orm/mongoid7.rb | 1 + 50 files changed, 329 insertions(+), 107 deletions(-) diff --git a/Rakefile b/Rakefile index 167683b..1108d73 100644 --- a/Rakefile +++ b/Rakefile @@ -1,3 +1,4 @@ +# frozen_string_literal: true require 'bundler/setup' require 'rspec/core/rake_task' diff --git a/config/locales/en.yml b/config/locales/en.yml index b365193..d1aea41 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -1,4 +1,11 @@ en: + activerecord: + errors: + models: + doorkeeper/application: + attributes: + redirect_uri: + unspecified_scheme: 'must specify a scheme.' orm: &orm attributes: doorkeeper/application: @@ -14,6 +21,7 @@ en: relative_uri: 'must be an absolute URI.' secured_uri: 'must be an HTTPS/SSL URI.' forbidden_uri: 'is forbidden by the server.' + unspecified_scheme: 'must specify a scheme.' scopes: not_match_configured: "doesn't match configured on the server." diff --git a/doorkeeper-mongodb.gemspec b/doorkeeper-mongodb.gemspec index a0fb9ab..425be9b 100644 --- a/doorkeeper-mongodb.gemspec +++ b/doorkeeper-mongodb.gemspec @@ -1,3 +1,4 @@ +# frozen_string_literal: true $:.push File.expand_path('../lib', __FILE__) # Maintain your gem's version: diff --git a/gemfiles/Gemfile.common.rb b/gemfiles/Gemfile.common.rb index 69db975..e70985b 100644 --- a/gemfiles/Gemfile.common.rb +++ b/gemfiles/Gemfile.common.rb @@ -1,3 +1,4 @@ +# frozen_string_literal: true ENV['RAILS'] ||= '4.2' ENV['DOORKEEPER'] ||= '5.0' diff --git a/gemfiles/Gemfile.mongo_mapper.rb b/gemfiles/Gemfile.mongo_mapper.rb index 5329e4b..8aa4d19 100644 --- a/gemfiles/Gemfile.mongo_mapper.rb +++ b/gemfiles/Gemfile.mongo_mapper.rb @@ -1,3 +1,4 @@ +# frozen_string_literal: true gemfile = File.expand_path('../Gemfile.common.rb', __FILE__) instance_eval IO.read(gemfile), gemfile diff --git a/gemfiles/Gemfile.mongoid4.rb b/gemfiles/Gemfile.mongoid4.rb index d02164a..c1558e4 100644 --- a/gemfiles/Gemfile.mongoid4.rb +++ b/gemfiles/Gemfile.mongoid4.rb @@ -1,3 +1,4 @@ +# frozen_string_literal: true gemfile = File.expand_path('../Gemfile.common.rb', __FILE__) instance_eval IO.read(gemfile), gemfile diff --git a/gemfiles/Gemfile.mongoid5.rb b/gemfiles/Gemfile.mongoid5.rb index f653e35..de1dc8e 100644 --- a/gemfiles/Gemfile.mongoid5.rb +++ b/gemfiles/Gemfile.mongoid5.rb @@ -1,3 +1,4 @@ +# frozen_string_literal: true gemfile = File.expand_path('../Gemfile.common.rb', __FILE__) instance_eval IO.read(gemfile), gemfile diff --git a/gemfiles/Gemfile.mongoid6.rb b/gemfiles/Gemfile.mongoid6.rb index 40116bb..b745ecb 100644 --- a/gemfiles/Gemfile.mongoid6.rb +++ b/gemfiles/Gemfile.mongoid6.rb @@ -1,4 +1,5 @@ -gemfile = File.expand_path('../Gemfile.common.rb', __FILE__) -instance_eval IO.read(gemfile), gemfile - -gem 'mongoid', '~> 6' +# frozen_string_literal: true +gemfile = File.expand_path('../Gemfile.common.rb', __FILE__) +instance_eval IO.read(gemfile), gemfile + +gem 'mongoid', '~> 6' diff --git a/gemfiles/Gemfile.mongoid7.rb b/gemfiles/Gemfile.mongoid7.rb index fdb0a46..cf049ca 100644 --- a/gemfiles/Gemfile.mongoid7.rb +++ b/gemfiles/Gemfile.mongoid7.rb @@ -1,3 +1,4 @@ +# frozen_string_literal: true gemfile = File.expand_path('../Gemfile.common.rb', __FILE__) instance_eval IO.read(gemfile), gemfile diff --git a/lib/doorkeeper-mongodb.rb b/lib/doorkeeper-mongodb.rb index 2a02921..c0a41f4 100644 --- a/lib/doorkeeper-mongodb.rb +++ b/lib/doorkeeper-mongodb.rb @@ -1,3 +1,4 @@ +# frozen_string_literal: true require 'doorkeeper-mongodb/version' require 'active_model' diff --git a/lib/doorkeeper-mongodb/compatible.rb b/lib/doorkeeper-mongodb/compatible.rb index 54e6fa0..ef8abd0 100644 --- a/lib/doorkeeper-mongodb/compatible.rb +++ b/lib/doorkeeper-mongodb/compatible.rb @@ -1,3 +1,4 @@ +# frozen_string_literal: true module DoorkeeperMongodb module Compatible extend ActiveSupport::Concern @@ -22,9 +23,18 @@ def transaction(options = {}, &block) self.class.transaction(options, &block) end + def update_column(column, value) + update(column => value) + end + def lock!(_ = true) reload if persisted? self end + + def with_lock(&_block) + lock! + yield + end end end diff --git a/lib/doorkeeper-mongodb/mixins/mongo_mapper/access_grant_mixin.rb b/lib/doorkeeper-mongodb/mixins/mongo_mapper/access_grant_mixin.rb index ddf7b7b..3c1e9fb 100644 --- a/lib/doorkeeper-mongodb/mixins/mongo_mapper/access_grant_mixin.rb +++ b/lib/doorkeeper-mongodb/mixins/mongo_mapper/access_grant_mixin.rb @@ -1,3 +1,4 @@ +# frozen_string_literal: true module DoorkeeperMongodb module Mixins module MongoMapper diff --git a/lib/doorkeeper-mongodb/mixins/mongo_mapper/access_token_mixin.rb b/lib/doorkeeper-mongodb/mixins/mongo_mapper/access_token_mixin.rb index 9590ab8..7f9b693 100644 --- a/lib/doorkeeper-mongodb/mixins/mongo_mapper/access_token_mixin.rb +++ b/lib/doorkeeper-mongodb/mixins/mongo_mapper/access_token_mixin.rb @@ -1,3 +1,4 @@ +# frozen_string_literal: true module DoorkeeperMongodb module Mixins module MongoMapper diff --git a/lib/doorkeeper-mongodb/mixins/mongo_mapper/application_mixin.rb b/lib/doorkeeper-mongodb/mixins/mongo_mapper/application_mixin.rb index 109b38e..23c45e9 100644 --- a/lib/doorkeeper-mongodb/mixins/mongo_mapper/application_mixin.rb +++ b/lib/doorkeeper-mongodb/mixins/mongo_mapper/application_mixin.rb @@ -1,3 +1,4 @@ +# frozen_string_literal: true module DoorkeeperMongodb module Mixins module MongoMapper diff --git a/lib/doorkeeper-mongodb/mixins/mongo_mapper/base_mixin.rb b/lib/doorkeeper-mongodb/mixins/mongo_mapper/base_mixin.rb index a7da347..c79e822 100644 --- a/lib/doorkeeper-mongodb/mixins/mongo_mapper/base_mixin.rb +++ b/lib/doorkeeper-mongodb/mixins/mongo_mapper/base_mixin.rb @@ -1,3 +1,4 @@ +# frozen_string_literal: true module DoorkeeperMongodb module Mixins module MongoMapper diff --git a/lib/doorkeeper-mongodb/mixins/mongoid/access_grant_mixin.rb b/lib/doorkeeper-mongodb/mixins/mongoid/access_grant_mixin.rb index 2f56bca..cba0a6c 100644 --- a/lib/doorkeeper-mongodb/mixins/mongoid/access_grant_mixin.rb +++ b/lib/doorkeeper-mongodb/mixins/mongoid/access_grant_mixin.rb @@ -1,3 +1,4 @@ +# frozen_string_literal: true module DoorkeeperMongodb module Mixins module Mongoid @@ -9,6 +10,8 @@ module AccessGrantMixin include Doorkeeper::Models::Revocable include Doorkeeper::Models::Accessible include Doorkeeper::Models::Scopes + include Doorkeeper::Models::SecretStorable + include Doorkeeper::Models::ResourceOwnerable include BaseMixin included do @@ -24,6 +27,10 @@ module AccessGrantMixin belongs_to :application, belongs_to_opts + if Doorkeeper.config.polymorphic_resource_owner? + belongs_to :resource_owner, polymorphic: true + end + validates :resource_owner_id, :application_id, :token, :expires_in, :redirect_uri, presence: true validates :token, uniqueness: true @@ -39,6 +46,22 @@ def pkce_supported? respond_to? :code_challenge end + def plaintext_token + if secret_strategy.allows_restoring_secrets? + secret_strategy.restore_secret(self, :token) + else + @raw_token + end + end + + def secret_strategy + ::Doorkeeper.configuration.token_secret_strategy + end + + def fallback_secret_strategy + ::Doorkeeper.configuration.token_secret_fallback_strategy + end + module ClassMethods # Searches for Doorkeeper::AccessGrant record with the # specific token value. @@ -122,7 +145,9 @@ def pkce_supported? # @return [String] token value # def generate_token - self.token = UniqueToken.generate + return nil unless self[:token].nil? + @raw_token = UniqueToken.generate + secret_strategy.store_secret(self, :token, @raw_token) end end end diff --git a/lib/doorkeeper-mongodb/mixins/mongoid/access_token_mixin.rb b/lib/doorkeeper-mongodb/mixins/mongoid/access_token_mixin.rb index 1a868ce..a01b1a2 100644 --- a/lib/doorkeeper-mongodb/mixins/mongoid/access_token_mixin.rb +++ b/lib/doorkeeper-mongodb/mixins/mongoid/access_token_mixin.rb @@ -1,3 +1,4 @@ +# frozen_string_literal: true module DoorkeeperMongodb module Mixins module Mongoid @@ -7,8 +8,11 @@ module AccessTokenMixin include Doorkeeper::OAuth::Helpers include Doorkeeper::Models::Expirable include Doorkeeper::Models::Revocable + include Doorkeeper::Models::Reusable include Doorkeeper::Models::Accessible include Doorkeeper::Models::Scopes + include Doorkeeper::Models::SecretStorable + include Doorkeeper::Models::ResourceOwnerable include BaseMixin included do @@ -24,6 +28,16 @@ module AccessTokenMixin belongs_to :application, belongs_to_opts + if Doorkeeper.config.polymorphic_resource_owner? + opts = { polymorphic: true } + + if ::Mongoid::VERSION[0].to_i >= 6 + opts[:optional] = true + end + + belongs_to :resource_owner, opts + end + validates :token, presence: true, uniqueness: true validates :refresh_token, uniqueness: true, if: :use_refresh_token? @@ -48,7 +62,7 @@ module ClassMethods # if there is no record with such token # def by_token(token) - where(token: token.to_s).first + find_by_plaintext_token(:token, token) end @@ -62,7 +76,7 @@ def by_token(token) # if there is no record with such refresh token # def by_refresh_token(refresh_token) - where(refresh_token: refresh_token.to_s).first + find_by_plaintext_token(:refresh_token, refresh_token) end # Revokes AccessToken records that have not been revoked and associated @@ -74,10 +88,14 @@ def by_refresh_token(refresh_token) # instance of the Resource Owner model # def revoke_all_for(application_id, resource_owner, clock = Time) - where(application_id: application_id, - resource_owner_id: resource_owner.id, - revoked_at: nil). - update_all(revoked_at: clock.now.utc) + by_resource_owner(resource_owner). + where(application_id: application_id, + revoked_at: nil). + update_all(revoked_at: clock.now.utc) + end + + def by_previous_refresh_token(previous_refresh_token) + where(refresh_token: previous_refresh_token).first end # Looking for not revoked Access Token with a matching set of scopes @@ -93,15 +111,17 @@ def revoke_all_for(application_id, resource_owner, clock = Time) # @return [Doorkeeper::AccessToken, nil] Access Token instance or # nil if matching record was not found # - def matching_token_for(application, resource_owner_or_id, scopes) - resource_owner_id = if resource_owner_or_id.respond_to?(:to_key) - resource_owner_or_id.id - else - resource_owner_or_id - end - - tokens = authorized_tokens_for(application.try(:id), resource_owner_id) - tokens.detect do |token| + def matching_token_for(application, resource_owner, scopes) + tokens = authorized_tokens_for(application&.id, resource_owner) + find_matching_token(tokens, application, scopes) + end + + def find_access_token_in_batches(relation, *args, &block) + relation.all.each(&block) + end + + def find_matching_token(relation, application, scopes) + relation.detect do |token| scopes_match?(token.scopes, scopes, application.try(:scopes)) end end @@ -126,9 +146,9 @@ def scopes_match?(token_scopes, param_scopes, app_scopes) (token_scopes.sort == param_scopes.sort) && Doorkeeper::OAuth::Helpers::ScopeChecker.valid?( - param_scopes.to_s, - Doorkeeper.configuration.scopes, - app_scopes + scope_str: param_scopes.to_s, + server_scopes: Doorkeeper.configuration.scopes, + app_scopes: app_scopes, ) end @@ -149,20 +169,27 @@ def scopes_match?(token_scopes, param_scopes, app_scopes) # # @return [Doorkeeper::AccessToken] existing record or a new one # - def find_or_create_for(application, resource_owner_id, scopes, expires_in, use_refresh_token) + def find_or_create_for(application, resource_owner, scopes, expires_in, use_refresh_token) if Doorkeeper.configuration.reuse_access_token - access_token = matching_token_for(application, resource_owner_id, scopes) + access_token = matching_token_for(application, resource_owner, scopes) - return access_token if access_token && !access_token.expired? + return access_token if access_token&.reusable? end - create!( + attributes = { application_id: application.try(:id), - resource_owner_id: resource_owner_id, scopes: scopes.to_s, expires_in: expires_in, use_refresh_token: use_refresh_token - ) + } + + if Doorkeeper.config.polymorphic_resource_owner? + attributes[:resource_owner] = resource_owner + else + attributes[:resource_owner_id] = resource_owner_id_for(resource_owner) + end + + create!(**attributes) end # Looking for not revoked Access Token records that belongs to specific @@ -175,11 +202,11 @@ def find_or_create_for(application, resource_owner_id, scopes, expires_in, use_r # # @return [Doorkeeper::AccessToken] array of matching AccessToken objects # - def authorized_tokens_for(application_id, resource_owner_id) + def authorized_tokens_for(application_id, resource_owner) send(order_method, created_at_desc). - where(application_id: application_id, - resource_owner_id: resource_owner_id, - revoked_at: nil) + by_resource_owner(resource_owner). + where(application_id: application_id, + revoked_at: nil) end # Convenience method for backwards-compatibility, return the last @@ -196,6 +223,14 @@ def authorized_tokens_for(application_id, resource_owner_id) def last_authorized_token_for(application_id, resource_owner_id) authorized_tokens_for(application_id, resource_owner_id).first end + + def secret_strategy + ::Doorkeeper.configuration.token_secret_strategy + end + + def fallback_secret_strategy + ::Doorkeeper.configuration.token_secret_fallback_strategy + end end @@ -248,14 +283,45 @@ def acceptable?(scopes) accessible? && includes_scope?(*scopes) end + def plaintext_refresh_token + if secret_strategy.allows_restoring_secrets? + secret_strategy.restore_secret(self, :refresh_token) + else + @raw_refresh_token + end + end + + def plaintext_token + if secret_strategy.allows_restoring_secrets? + secret_strategy.restore_secret(self, :token) + else + @raw_token + end + end + + # Revokes token with `:refresh_token` equal to `:previous_refresh_token` + # and clears `:previous_refresh_token` attribute. + # + def revoke_previous_refresh_token! + return unless self.class.refresh_token_revoked_on_use? + + old_refresh_token&.revoke + update(previous_refresh_token: "") + end + private + def old_refresh_token + @old_refresh_token ||= self.class.by_previous_refresh_token(previous_refresh_token) + end + # Generates refresh token with UniqueToken generator. # # @return [String] refresh token value # def generate_refresh_token - self.refresh_token = UniqueToken.generate + @raw_refresh_token = UniqueToken.generate + secret_strategy.store_secret(self, :refresh_token, @raw_refresh_token) end # Generates and sets the token value with the @@ -271,13 +337,15 @@ def generate_refresh_token def generate_token self.created_at ||= Time.now.utc - self.token = token_generator.generate( + @raw_token = token_generator.generate( resource_owner_id: resource_owner_id, scopes: scopes, application: application, expires_in: expires_in, created_at: created_at ) + secret_strategy.store_secret(self, :token, @raw_token) + @raw_token end def token_generator diff --git a/lib/doorkeeper-mongodb/mixins/mongoid/application_mixin.rb b/lib/doorkeeper-mongodb/mixins/mongoid/application_mixin.rb index d9e0751..bcee7e1 100644 --- a/lib/doorkeeper-mongodb/mixins/mongoid/application_mixin.rb +++ b/lib/doorkeeper-mongodb/mixins/mongoid/application_mixin.rb @@ -1,3 +1,4 @@ +# frozen_string_literal: true module DoorkeeperMongodb module Mixins module Mongoid @@ -6,6 +7,7 @@ module ApplicationMixin include Doorkeeper::OAuth::Helpers include Doorkeeper::Models::Scopes + include Doorkeeper::Models::SecretStorable include BaseMixin included do @@ -48,7 +50,7 @@ def by_uid_and_secret(uid, secret) app = by_uid(uid) return unless app return app if secret.blank? && !app.confidential? - return unless app.secret == secret + return unless app.secret_matches?(secret) app end @@ -63,6 +65,14 @@ def by_uid(uid) where(uid: uid.to_s).first end + def secret_strategy + ::Doorkeeper.configuration.application_secret_strategy + end + + def fallback_secret_strategy + ::Doorkeeper.configuration.application_secret_fallback_strategy + end + # Revokes AccessToken and AccessGrant records that have not been revoked and # associated with the specific Application and Resource Owner. # @@ -75,6 +85,23 @@ def revoke_tokens_and_grants_for(id, resource_owner) end end + def secret_matches?(input) + # return false if either is nil, since secure_compare depends on strings + # but Application secrets MAY be nil depending on confidentiality. + return false if input.nil? || secret.nil? + + # When matching the secret by comparer function, all is well. + return true if secret_strategy.secret_matches?(input, secret) + + # When fallback lookup is enabled, ensure applications + # with plain secrets can still be found + if fallback_secret_strategy + fallback_secret_strategy.secret_matches?(input, secret) + else + false + end + end + # Set an application's valid redirect URIs. # # @param uris [String, Array] Newline-separated string or array the URI(s) @@ -84,6 +111,30 @@ def redirect_uri=(uris) super(uris.is_a?(Array) ? uris.join("\n") : uris) end + def renew_secret + @raw_secret = Doorkeeper::OAuth::Helpers::UniqueToken.generate + secret_strategy.store_secret(self, :secret, @raw_secret) + end + + def plaintext_secret + if secret_strategy.allows_restoring_secrets? + secret_strategy.restore_secret(self, :secret) + else + @raw_secret + end + end + + def as_json(options = {}) + hash = super + + hash["secret"] = plaintext_secret if hash.key?("secret") + hash + end + + def authorized_for_resource_owner?(resource_owner) + Doorkeeper.configuration.authorize_resource_owner_for_client.call(self, resource_owner) + end + private def generate_uid @@ -93,14 +144,15 @@ def generate_uid end def generate_secret - if secret.blank? - self.secret = UniqueToken.generate - end + return unless secret.blank? + + @raw_secret = UniqueToken.generate + secret_strategy.store_secret(self, :secret, @raw_secret) end def scopes_match_configured if scopes.present? && - !ScopeChecker.valid?(scopes.to_s, Doorkeeper.configuration.scopes) + !ScopeChecker.valid?(scope_str: scopes.to_s, server_scopes: Doorkeeper.configuration.scopes) errors.add(:scopes, :not_match_configured) end end diff --git a/lib/doorkeeper-mongodb/mixins/mongoid/base_mixin.rb b/lib/doorkeeper-mongodb/mixins/mongoid/base_mixin.rb index 37096ff..05f819a 100644 --- a/lib/doorkeeper-mongodb/mixins/mongoid/base_mixin.rb +++ b/lib/doorkeeper-mongodb/mixins/mongoid/base_mixin.rb @@ -1,3 +1,4 @@ +# frozen_string_literal: true module DoorkeeperMongodb module Mixins module Mongoid @@ -8,6 +9,12 @@ module ClassMethods def ordered_by(attribute, direction = :asc) order_by(attribute => direction.to_sym) end + + def find_by(*args) + super(*args) + rescue ::Mongoid::Errors::DocumentNotFound + nil + end end def as_json(*args) diff --git a/lib/doorkeeper-mongodb/shared/scopes.rb b/lib/doorkeeper-mongodb/shared/scopes.rb index bb0fca2..a431858 100644 --- a/lib/doorkeeper-mongodb/shared/scopes.rb +++ b/lib/doorkeeper-mongodb/shared/scopes.rb @@ -1,3 +1,4 @@ +# frozen_string_literal: true module DoorkeeperMongodb module Shared module Scopes diff --git a/lib/doorkeeper-mongodb/version.rb b/lib/doorkeeper-mongodb/version.rb index 768f8d0..cf2b468 100644 --- a/lib/doorkeeper-mongodb/version.rb +++ b/lib/doorkeeper-mongodb/version.rb @@ -1,3 +1,4 @@ +# frozen_string_literal: true module DoorkeeperMongodb def self.gem_version Gem::Version.new VERSION::STRING diff --git a/lib/doorkeeper/generators/doorkeeper/mongo_mapper/indexes_generator.rb b/lib/doorkeeper/generators/doorkeeper/mongo_mapper/indexes_generator.rb index bd62954..d157aa8 100644 --- a/lib/doorkeeper/generators/doorkeeper/mongo_mapper/indexes_generator.rb +++ b/lib/doorkeeper/generators/doorkeeper/mongo_mapper/indexes_generator.rb @@ -1,3 +1,4 @@ +# frozen_string_literal: true module Doorkeeper module MongoMapper class IndexesGenerator < ::Rails::Generators::Base diff --git a/lib/doorkeeper/generators/doorkeeper/mongo_mapper/templates/indexes.rb b/lib/doorkeeper/generators/doorkeeper/mongo_mapper/templates/indexes.rb index 51c8152..b9787a5 100644 --- a/lib/doorkeeper/generators/doorkeeper/mongo_mapper/templates/indexes.rb +++ b/lib/doorkeeper/generators/doorkeeper/mongo_mapper/templates/indexes.rb @@ -1,3 +1,4 @@ +# frozen_string_literal: true Doorkeeper::Application.create_indexes Doorkeeper::AccessGrant.create_indexes Doorkeeper::AccessToken.create_indexes diff --git a/lib/doorkeeper/orm/mongo_mapper.rb b/lib/doorkeeper/orm/mongo_mapper.rb index c1c53e0..063e9ea 100644 --- a/lib/doorkeeper/orm/mongo_mapper.rb +++ b/lib/doorkeeper/orm/mongo_mapper.rb @@ -1,3 +1,4 @@ +# frozen_string_literal: true require 'active_support/lazy_load_hooks' module Doorkeeper diff --git a/lib/doorkeeper/orm/mongo_mapper/access_grant.rb b/lib/doorkeeper/orm/mongo_mapper/access_grant.rb index ff729ae..77f48bd 100644 --- a/lib/doorkeeper/orm/mongo_mapper/access_grant.rb +++ b/lib/doorkeeper/orm/mongo_mapper/access_grant.rb @@ -1,3 +1,4 @@ +# frozen_string_literal: true module Doorkeeper class AccessGrant include DoorkeeperMongodb::Compatible diff --git a/lib/doorkeeper/orm/mongo_mapper/access_token.rb b/lib/doorkeeper/orm/mongo_mapper/access_token.rb index 30b29fc..a3ba6ad 100644 --- a/lib/doorkeeper/orm/mongo_mapper/access_token.rb +++ b/lib/doorkeeper/orm/mongo_mapper/access_token.rb @@ -1,3 +1,4 @@ +# frozen_string_literal: true module Doorkeeper class AccessToken include DoorkeeperMongodb::Compatible diff --git a/lib/doorkeeper/orm/mongo_mapper/application.rb b/lib/doorkeeper/orm/mongo_mapper/application.rb index 0730f9d..235f659 100644 --- a/lib/doorkeeper/orm/mongo_mapper/application.rb +++ b/lib/doorkeeper/orm/mongo_mapper/application.rb @@ -1,3 +1,4 @@ +# frozen_string_literal: true module Doorkeeper class Application include DoorkeeperMongodb::Compatible diff --git a/lib/doorkeeper/orm/mongoid4.rb b/lib/doorkeeper/orm/mongoid4.rb index f2f3edb..5a4431a 100644 --- a/lib/doorkeeper/orm/mongoid4.rb +++ b/lib/doorkeeper/orm/mongoid4.rb @@ -1,3 +1,4 @@ +# frozen_string_literal: true require 'active_support/lazy_load_hooks' module Doorkeeper diff --git a/lib/doorkeeper/orm/mongoid4/access_grant.rb b/lib/doorkeeper/orm/mongoid4/access_grant.rb index 52cd58d..9b9e63c 100644 --- a/lib/doorkeeper/orm/mongoid4/access_grant.rb +++ b/lib/doorkeeper/orm/mongoid4/access_grant.rb @@ -1,3 +1,4 @@ +# frozen_string_literal: true module Doorkeeper class AccessGrant include Mongoid::Document @@ -11,6 +12,7 @@ class AccessGrant store_in collection: :oauth_access_grants field :resource_owner_id, type: BSON::ObjectId + field :resource_owner_type, type: String field :token, type: String field :expires_in, type: Integer field :redirect_uri, type: String diff --git a/lib/doorkeeper/orm/mongoid4/access_token.rb b/lib/doorkeeper/orm/mongoid4/access_token.rb index 4bf34fe..1328be0 100644 --- a/lib/doorkeeper/orm/mongoid4/access_token.rb +++ b/lib/doorkeeper/orm/mongoid4/access_token.rb @@ -1,3 +1,4 @@ +# frozen_string_literal: true module Doorkeeper class AccessToken include Mongoid::Document @@ -11,6 +12,7 @@ class AccessToken store_in collection: :oauth_access_tokens field :resource_owner_id, type: BSON::ObjectId + field :resource_owner_type, type: String field :token, type: String field :refresh_token, type: String field :previous_refresh_token, type: String diff --git a/lib/doorkeeper/orm/mongoid4/application.rb b/lib/doorkeeper/orm/mongoid4/application.rb index a62e037..eb4f660 100644 --- a/lib/doorkeeper/orm/mongoid4/application.rb +++ b/lib/doorkeeper/orm/mongoid4/application.rb @@ -1,3 +1,4 @@ +# frozen_string_literal: true module Doorkeeper class Application include Mongoid::Document diff --git a/lib/doorkeeper/orm/mongoid5.rb b/lib/doorkeeper/orm/mongoid5.rb index d73ac2a..90306f0 100644 --- a/lib/doorkeeper/orm/mongoid5.rb +++ b/lib/doorkeeper/orm/mongoid5.rb @@ -1,3 +1,4 @@ +# frozen_string_literal: true require 'active_support/lazy_load_hooks' module Doorkeeper diff --git a/lib/doorkeeper/orm/mongoid5/access_grant.rb b/lib/doorkeeper/orm/mongoid5/access_grant.rb index 52cd58d..9b9e63c 100644 --- a/lib/doorkeeper/orm/mongoid5/access_grant.rb +++ b/lib/doorkeeper/orm/mongoid5/access_grant.rb @@ -1,3 +1,4 @@ +# frozen_string_literal: true module Doorkeeper class AccessGrant include Mongoid::Document @@ -11,6 +12,7 @@ class AccessGrant store_in collection: :oauth_access_grants field :resource_owner_id, type: BSON::ObjectId + field :resource_owner_type, type: String field :token, type: String field :expires_in, type: Integer field :redirect_uri, type: String diff --git a/lib/doorkeeper/orm/mongoid5/access_token.rb b/lib/doorkeeper/orm/mongoid5/access_token.rb index 4bf34fe..1328be0 100644 --- a/lib/doorkeeper/orm/mongoid5/access_token.rb +++ b/lib/doorkeeper/orm/mongoid5/access_token.rb @@ -1,3 +1,4 @@ +# frozen_string_literal: true module Doorkeeper class AccessToken include Mongoid::Document @@ -11,6 +12,7 @@ class AccessToken store_in collection: :oauth_access_tokens field :resource_owner_id, type: BSON::ObjectId + field :resource_owner_type, type: String field :token, type: String field :refresh_token, type: String field :previous_refresh_token, type: String diff --git a/lib/doorkeeper/orm/mongoid5/application.rb b/lib/doorkeeper/orm/mongoid5/application.rb index a62e037..eb4f660 100644 --- a/lib/doorkeeper/orm/mongoid5/application.rb +++ b/lib/doorkeeper/orm/mongoid5/application.rb @@ -1,3 +1,4 @@ +# frozen_string_literal: true module Doorkeeper class Application include Mongoid::Document diff --git a/lib/doorkeeper/orm/mongoid6.rb b/lib/doorkeeper/orm/mongoid6.rb index 2b9f496..50d4c02 100644 --- a/lib/doorkeeper/orm/mongoid6.rb +++ b/lib/doorkeeper/orm/mongoid6.rb @@ -1,3 +1,4 @@ +# frozen_string_literal: true require 'active_support/lazy_load_hooks' module Doorkeeper diff --git a/lib/doorkeeper/orm/mongoid6/access_grant.rb b/lib/doorkeeper/orm/mongoid6/access_grant.rb index 52cd58d..9b9e63c 100644 --- a/lib/doorkeeper/orm/mongoid6/access_grant.rb +++ b/lib/doorkeeper/orm/mongoid6/access_grant.rb @@ -1,3 +1,4 @@ +# frozen_string_literal: true module Doorkeeper class AccessGrant include Mongoid::Document @@ -11,6 +12,7 @@ class AccessGrant store_in collection: :oauth_access_grants field :resource_owner_id, type: BSON::ObjectId + field :resource_owner_type, type: String field :token, type: String field :expires_in, type: Integer field :redirect_uri, type: String diff --git a/lib/doorkeeper/orm/mongoid6/access_token.rb b/lib/doorkeeper/orm/mongoid6/access_token.rb index fa25627..1328be0 100644 --- a/lib/doorkeeper/orm/mongoid6/access_token.rb +++ b/lib/doorkeeper/orm/mongoid6/access_token.rb @@ -1,35 +1,37 @@ -module Doorkeeper - class AccessToken - include Mongoid::Document - include Mongoid::Timestamps - - include DoorkeeperMongodb::Compatible - - include DoorkeeperMongodb::Shared::Scopes - include DoorkeeperMongodb::Mixins::Mongoid::AccessTokenMixin - - store_in collection: :oauth_access_tokens - - field :resource_owner_id, type: BSON::ObjectId - field :token, type: String - field :refresh_token, type: String - field :previous_refresh_token, type: String - field :expires_in, type: Integer - field :revoked_at, type: DateTime - - index({ token: 1 }, unique: true) - index({ refresh_token: 1 }, unique: true, sparse: true) - - def self.order_method - :order_by - end - - def self.refresh_token_revoked_on_use? - fields.collect { |field| field[0] }.include?('previous_refresh_token') - end - - def self.created_at_desc - %i[created_at desc] - end - end -end +# frozen_string_literal: true +module Doorkeeper + class AccessToken + include Mongoid::Document + include Mongoid::Timestamps + + include DoorkeeperMongodb::Compatible + + include DoorkeeperMongodb::Shared::Scopes + include DoorkeeperMongodb::Mixins::Mongoid::AccessTokenMixin + + store_in collection: :oauth_access_tokens + + field :resource_owner_id, type: BSON::ObjectId + field :resource_owner_type, type: String + field :token, type: String + field :refresh_token, type: String + field :previous_refresh_token, type: String + field :expires_in, type: Integer + field :revoked_at, type: DateTime + + index({ token: 1 }, unique: true) + index({ refresh_token: 1 }, unique: true, sparse: true) + + def self.order_method + :order_by + end + + def self.refresh_token_revoked_on_use? + fields.collect { |field| field[0] }.include?('previous_refresh_token') + end + + def self.created_at_desc + %i[created_at desc] + end + end +end diff --git a/lib/doorkeeper/orm/mongoid6/application.rb b/lib/doorkeeper/orm/mongoid6/application.rb index 5a9df48..eb4f660 100644 --- a/lib/doorkeeper/orm/mongoid6/application.rb +++ b/lib/doorkeeper/orm/mongoid6/application.rb @@ -1,32 +1,33 @@ -module Doorkeeper - class Application - include Mongoid::Document - include Mongoid::Timestamps - - include DoorkeeperMongodb::Compatible - - include DoorkeeperMongodb::Shared::Scopes - include DoorkeeperMongodb::Mixins::Mongoid::ApplicationMixin - - store_in collection: :oauth_applications - - field :name, type: String - field :uid, type: String - field :secret, type: String - field :redirect_uri, type: String - field :confidential, type: Boolean, default: true - - index({ uid: 1 }, unique: true) - - has_many :authorized_tokens, class_name: 'Doorkeeper::AccessToken' - - def self.authorized_for(resource_owner) - ids = AccessToken.where( - resource_owner_id: resource_owner.id, - revoked_at: nil - ).map(&:application_id) - - find(ids) - end - end -end +# frozen_string_literal: true +module Doorkeeper + class Application + include Mongoid::Document + include Mongoid::Timestamps + + include DoorkeeperMongodb::Compatible + + include DoorkeeperMongodb::Shared::Scopes + include DoorkeeperMongodb::Mixins::Mongoid::ApplicationMixin + + store_in collection: :oauth_applications + + field :name, type: String + field :uid, type: String + field :secret, type: String + field :redirect_uri, type: String + field :confidential, type: Boolean, default: true + + index({ uid: 1 }, unique: true) + + has_many :authorized_tokens, class_name: 'Doorkeeper::AccessToken' + + def self.authorized_for(resource_owner) + ids = AccessToken.where( + resource_owner_id: resource_owner.id, + revoked_at: nil + ).map(&:application_id) + + find(ids) + end + end +end diff --git a/lib/doorkeeper/orm/mongoid7.rb b/lib/doorkeeper/orm/mongoid7.rb index 6fd56d6..1e08974 100644 --- a/lib/doorkeeper/orm/mongoid7.rb +++ b/lib/doorkeeper/orm/mongoid7.rb @@ -1,3 +1,4 @@ +# frozen_string_literal: true require 'active_support/lazy_load_hooks' module Doorkeeper diff --git a/lib/doorkeeper/orm/mongoid7/access_grant.rb b/lib/doorkeeper/orm/mongoid7/access_grant.rb index 52cd58d..9b9e63c 100644 --- a/lib/doorkeeper/orm/mongoid7/access_grant.rb +++ b/lib/doorkeeper/orm/mongoid7/access_grant.rb @@ -1,3 +1,4 @@ +# frozen_string_literal: true module Doorkeeper class AccessGrant include Mongoid::Document @@ -11,6 +12,7 @@ class AccessGrant store_in collection: :oauth_access_grants field :resource_owner_id, type: BSON::ObjectId + field :resource_owner_type, type: String field :token, type: String field :expires_in, type: Integer field :redirect_uri, type: String diff --git a/lib/doorkeeper/orm/mongoid7/access_token.rb b/lib/doorkeeper/orm/mongoid7/access_token.rb index 4bf34fe..1328be0 100644 --- a/lib/doorkeeper/orm/mongoid7/access_token.rb +++ b/lib/doorkeeper/orm/mongoid7/access_token.rb @@ -1,3 +1,4 @@ +# frozen_string_literal: true module Doorkeeper class AccessToken include Mongoid::Document @@ -11,6 +12,7 @@ class AccessToken store_in collection: :oauth_access_tokens field :resource_owner_id, type: BSON::ObjectId + field :resource_owner_type, type: String field :token, type: String field :refresh_token, type: String field :previous_refresh_token, type: String diff --git a/lib/doorkeeper/orm/mongoid7/application.rb b/lib/doorkeeper/orm/mongoid7/application.rb index a62e037..eb4f660 100644 --- a/lib/doorkeeper/orm/mongoid7/application.rb +++ b/lib/doorkeeper/orm/mongoid7/application.rb @@ -1,3 +1,4 @@ +# frozen_string_literal: true module Doorkeeper class Application include Mongoid::Document diff --git a/spec/dummy/app/models/user.rb b/spec/dummy/app/models/user.rb index d274316..3386b39 100644 --- a/spec/dummy/app/models/user.rb +++ b/spec/dummy/app/models/user.rb @@ -1,3 +1,4 @@ +# frozen_string_literal: true case DOORKEEPER_ORM.to_s when /mongoid/ class User diff --git a/spec/support/orm/mongo_mapper.rb b/spec/support/orm/mongo_mapper.rb index 8f4733a..f9c591e 100644 --- a/spec/support/orm/mongo_mapper.rb +++ b/spec/support/orm/mongo_mapper.rb @@ -1,3 +1,4 @@ +# frozen_string_literal: true DatabaseCleaner[:mongo_mapper].strategy = :truncation DatabaseCleaner[:mongo_mapper].clean_with :truncation diff --git a/spec/support/orm/mongoid.rb b/spec/support/orm/mongoid.rb index 2073842..0b1c089 100644 --- a/spec/support/orm/mongoid.rb +++ b/spec/support/orm/mongoid.rb @@ -1,3 +1,4 @@ +# frozen_string_literal: true DatabaseCleaner[:mongoid].strategy = :truncation DatabaseCleaner[:mongoid].clean_with :truncation diff --git a/spec/support/orm/mongoid4.rb b/spec/support/orm/mongoid4.rb index 8f79aff..3d89325 100644 --- a/spec/support/orm/mongoid4.rb +++ b/spec/support/orm/mongoid4.rb @@ -1 +1,2 @@ +# frozen_string_literal: true require_relative "mongoid.rb" diff --git a/spec/support/orm/mongoid5.rb b/spec/support/orm/mongoid5.rb index 03e9df1..8881c43 100644 --- a/spec/support/orm/mongoid5.rb +++ b/spec/support/orm/mongoid5.rb @@ -1,3 +1,4 @@ +# frozen_string_literal: true require_relative "mongoid.rb" Mongoid.logger.level = Logger::ERROR diff --git a/spec/support/orm/mongoid6.rb b/spec/support/orm/mongoid6.rb index 03e9df1..8881c43 100644 --- a/spec/support/orm/mongoid6.rb +++ b/spec/support/orm/mongoid6.rb @@ -1,3 +1,4 @@ +# frozen_string_literal: true require_relative "mongoid.rb" Mongoid.logger.level = Logger::ERROR diff --git a/spec/support/orm/mongoid7.rb b/spec/support/orm/mongoid7.rb index e98c15e..560dd62 100644 --- a/spec/support/orm/mongoid7.rb +++ b/spec/support/orm/mongoid7.rb @@ -1,3 +1,4 @@ +# frozen_string_literal: true require_relative 'mongoid.rb' Mongoid.logger.level = Logger::ERROR From 75c8c9fe8ca3d0eb2f5978254352059b2e1590b6 Mon Sep 17 00:00:00 2001 From: Nikita Bulai Date: Tue, 24 Mar 2020 16:51:38 +0300 Subject: [PATCH 04/17] Update Doorkeeper submodule --- .rake_tasks~ | 11 +++++++++++ doorkeeper | 2 +- 2 files changed, 12 insertions(+), 1 deletion(-) create mode 100755 .rake_tasks~ diff --git a/.rake_tasks~ b/.rake_tasks~ new file mode 100755 index 0000000..657bbfc --- /dev/null +++ b/.rake_tasks~ @@ -0,0 +1,11 @@ +build +default +install +install:local +load_doorkeeper +release +release:guard_clean +release:rubygem_push +release:source_control_push +spec +update_submodules diff --git a/doorkeeper b/doorkeeper index 04ef043..e780d84 160000 --- a/doorkeeper +++ b/doorkeeper @@ -1 +1 @@ -Subproject commit 04ef0438f7432cbe53d9043a9a4df88cddcfe913 +Subproject commit e780d84e2646ac1ce798633b2a20a08f9c5cafc8 From 7af1a34d994d3b35b8c87bd587d545ec27ddd832 Mon Sep 17 00:00:00 2001 From: Nikita Bulai Date: Tue, 24 Mar 2020 16:58:35 +0300 Subject: [PATCH 05/17] Update it once again --- doorkeeper | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doorkeeper b/doorkeeper index e780d84..6427f0c 160000 --- a/doorkeeper +++ b/doorkeeper @@ -1 +1 @@ -Subproject commit e780d84e2646ac1ce798633b2a20a08f9c5cafc8 +Subproject commit 6427f0ca9d8072945e11ff04dcf6ba4adb933354 From 04ee1bf9fe642bd9643716ca77f59ba8be033762 Mon Sep 17 00:00:00 2001 From: Nikita Bulai Date: Tue, 24 Mar 2020 17:21:16 +0300 Subject: [PATCH 06/17] Drop MongoMapper support --- .gitignore | 4 +- .hound.yml | 12 +- .rubocop.yml | 55 ++++ .rubocop_todo.yml | 23 ++ .travis.yml | 11 - Gemfile | 6 + Gemfile.lock | 49 +++ README.md | 26 +- Rakefile | 14 +- config/locales/en.yml | 3 - doorkeeper-mongodb.gemspec | 40 +-- gemfiles/Gemfile.common.rb | 18 +- gemfiles/Gemfile.mongo_mapper.rb | 11 - gemfiles/Gemfile.mongoid4.rb | 4 +- gemfiles/Gemfile.mongoid5.rb | 4 +- gemfiles/Gemfile.mongoid6.rb | 4 +- gemfiles/Gemfile.mongoid7.rb | 4 +- lib/doorkeeper-mongodb.rb | 44 ++- .../mixins/mongo_mapper/access_grant_mixin.rb | 125 -------- .../mixins/mongo_mapper/access_token_mixin.rb | 291 ------------------ .../mixins/mongo_mapper/application_mixin.rb | 106 ------- .../mixins/mongo_mapper/base_mixin.rb | 24 -- .../mixins/mongoid/access_grant_mixin.rb | 4 +- .../mixins/mongoid/access_token_mixin.rb | 4 +- .../mixins/mongoid/application_mixin.rb | 4 +- lib/doorkeeper-mongodb/version.rb | 5 +- .../mongo_mapper/indexes_generator.rb | 13 - .../mongo_mapper/templates/indexes.rb | 4 - .../orm/concerns/mongo_mapper/ownership.rb | 24 -- lib/doorkeeper/orm/mongo_mapper.rb | 45 --- .../orm/mongo_mapper/access_grant.rb | 37 --- .../orm/mongo_mapper/access_token.rb | 53 ---- .../orm/mongo_mapper/application.rb | 65 ---- .../orm/mongo_mapper/stale_records_cleaner.rb | 21 -- lib/doorkeeper/orm/mongoid4.rb | 12 +- lib/doorkeeper/orm/mongoid4/access_token.rb | 2 +- lib/doorkeeper/orm/mongoid4/application.rb | 2 +- lib/doorkeeper/orm/mongoid5.rb | 12 +- lib/doorkeeper/orm/mongoid5/access_token.rb | 2 +- lib/doorkeeper/orm/mongoid5/application.rb | 2 +- lib/doorkeeper/orm/mongoid6.rb | 12 +- lib/doorkeeper/orm/mongoid6/access_token.rb | 2 +- lib/doorkeeper/orm/mongoid6/application.rb | 2 +- lib/doorkeeper/orm/mongoid7.rb | 12 +- lib/doorkeeper/orm/mongoid7/access_token.rb | 2 +- lib/doorkeeper/orm/mongoid7/application.rb | 2 +- spec/dummy/app/models/user.rb | 23 +- spec/support/orm/mongo_mapper.rb | 11 - spec/support/orm/mongoid7.rb | 2 +- 49 files changed, 251 insertions(+), 1006 deletions(-) create mode 100644 .rubocop.yml create mode 100644 .rubocop_todo.yml create mode 100755 Gemfile create mode 100755 Gemfile.lock delete mode 100644 gemfiles/Gemfile.mongo_mapper.rb delete mode 100644 lib/doorkeeper-mongodb/mixins/mongo_mapper/access_grant_mixin.rb delete mode 100644 lib/doorkeeper-mongodb/mixins/mongo_mapper/access_token_mixin.rb delete mode 100644 lib/doorkeeper-mongodb/mixins/mongo_mapper/application_mixin.rb delete mode 100644 lib/doorkeeper-mongodb/mixins/mongo_mapper/base_mixin.rb delete mode 100644 lib/doorkeeper/generators/doorkeeper/mongo_mapper/indexes_generator.rb delete mode 100644 lib/doorkeeper/generators/doorkeeper/mongo_mapper/templates/indexes.rb delete mode 100644 lib/doorkeeper/orm/concerns/mongo_mapper/ownership.rb delete mode 100644 lib/doorkeeper/orm/mongo_mapper.rb delete mode 100644 lib/doorkeeper/orm/mongo_mapper/access_grant.rb delete mode 100644 lib/doorkeeper/orm/mongo_mapper/access_token.rb delete mode 100644 lib/doorkeeper/orm/mongo_mapper/application.rb delete mode 100644 lib/doorkeeper/orm/mongo_mapper/stale_records_cleaner.rb delete mode 100644 spec/support/orm/mongo_mapper.rb diff --git a/.gitignore b/.gitignore index 4af8bd4..9411916 100644 --- a/.gitignore +++ b/.gitignore @@ -4,7 +4,6 @@ log/*.log pkg/ spec/dummy/db/*.sqlite3 -Gemfile.lock gemfiles/*.lock .rvmrc *.swp @@ -14,7 +13,6 @@ spec !spec/support/orm/mongo* *.gem -/Gemfile coverage -.idea/ \ No newline at end of file +.idea/ diff --git a/.hound.yml b/.hound.yml index 27ef9c1..6bfee7f 100644 --- a/.hound.yml +++ b/.hound.yml @@ -1,9 +1,3 @@ -LineLength: - Exclude: - - spec/**/* - -StringLiterals: - Enabled: false - -TrailingBlankLines: - Enabled: true +rubocop: + config_file: .rubocop.yml + version: 0.75.0 diff --git a/.rubocop.yml b/.rubocop.yml new file mode 100644 index 0000000..d24f04a --- /dev/null +++ b/.rubocop.yml @@ -0,0 +1,55 @@ +inherit_from: .rubocop_todo.yml +require: rubocop-performance +AllCops: + TargetRubyVersion: 2.4 + Exclude: + - "spec/dummy/db/*" + - "spec/dummy/config/*" + - "Dangerfile" + - "gemfiles/*.gemfile" + +Metrics/BlockLength: + Exclude: + - spec/**/* + - doorkeeper-mongodb.gemspec + +Metrics/MethodLength: + Exclude: + - spec/dummy/db/**/* + +Style/StringLiterals: + EnforcedStyle: double_quotes +Style/StringLiteralsInInterpolation: + EnforcedStyle: double_quotes +Style/FrozenStringLiteralComment: + Enabled: true + +Style/TrailingCommaInHashLiteral: + EnforcedStyleForMultiline: consistent_comma +Style/TrailingCommaInArrayLiteral: + EnforcedStyleForMultiline: consistent_comma +Style/TrailingCommaInArguments: + EnforcedStyleForMultiline: consistent_comma + +Style/SymbolArray: + MinSize: 3 +Style/WordArray: + MinSize: 3 + +Style/ClassAndModuleChildren: + Enabled: false +Style/NumericPredicate: + Enabled: false +Style/DoubleNegation: + Enabled: false + +Layout/MultilineMethodCallIndentation: + EnforcedStyle: indented +Layout/TrailingEmptyLines: + Enabled: true +Layout/DotPosition: + EnforcedStyle: leading +Layout/LineLength: + Exclude: + - spec/**/* + Max: 100 diff --git a/.rubocop_todo.yml b/.rubocop_todo.yml new file mode 100644 index 0000000..7269f08 --- /dev/null +++ b/.rubocop_todo.yml @@ -0,0 +1,23 @@ +# This configuration was generated by +# `rubocop --auto-gen-config` +# on 2020-01-29 11:31:56 +0300 using RuboCop version 0.78.0. +# The point is for the user to remove these configuration records +# one by one as the offenses are removed from the code base. +# Note that changes in the inspected code, or installation of new +# versions of RuboCop, may require this file to be generated again. + +# Offense count: 1 +# Configuration parameters: CountComments, ExcludedMethods. +# ExcludedMethods: refine +Metrics/BlockLength: + Max: 58 + +# Offense count: 2 +# Configuration parameters: CountComments. +Metrics/ClassLength: + Max: 214 + +# Offense count: 4 +# Configuration parameters: CountComments. +Metrics/ModuleLength: + Max: 470 diff --git a/.travis.yml b/.travis.yml index e8300d9..4ebca51 100644 --- a/.travis.yml +++ b/.travis.yml @@ -3,13 +3,6 @@ sudo: false cache: bundler before_install: - # MongoMapper works only with MongoDB <= 3.2.x - - wget http://fastdl.mongodb.org/linux/mongodb-linux-x86_64-3.2.18.tgz - - tar xzf mongodb-linux-x86_64-3.2.18.tgz - - ${PWD}/mongodb-linux-x86_64-3.2.18/bin/mongod --version - - mkdir ${PWD}/mongodb-linux-x86_64-3.2.18/data - # Bind on another port in order to test mongo_mapper separately - - ${PWD}/mongodb-linux-x86_64-3.2.18/bin/mongod --dbpath ${PWD}/mongodb-linux-x86_64-3.2.18/data --logpath ${PWD}/mongodb-linux-x86_64-3.2.18/mongodb.log --fork --port 27027 - gem install bundler -v '~> 1.10' rvm: @@ -28,7 +21,6 @@ gemfile: - gemfiles/Gemfile.mongoid5.rb - gemfiles/Gemfile.mongoid6.rb - gemfiles/Gemfile.mongoid7.rb - - gemfiles/Gemfile.mongo_mapper.rb services: - mongodb @@ -67,6 +59,3 @@ matrix: env: RAILS=5.0.7 - gemfile: gemfiles/Gemfile.mongoid7.rb rvm: 2.1 - - - gemfile: gemfiles/Gemfile.mongo_mapper.rb - env: RAILS=5.2.0 diff --git a/Gemfile b/Gemfile new file mode 100755 index 0000000..c3c471c --- /dev/null +++ b/Gemfile @@ -0,0 +1,6 @@ +# frozen_string_literal: true +source "https://rubygems.org" + +gem "mongoid" +gem "rake" +gem "rspec" diff --git a/Gemfile.lock b/Gemfile.lock new file mode 100755 index 0000000..c923d67 --- /dev/null +++ b/Gemfile.lock @@ -0,0 +1,49 @@ +GEM + remote: https://rubygems.org/ + specs: + activemodel (5.2.4.2) + activesupport (= 5.2.4.2) + activesupport (5.2.4.2) + concurrent-ruby (~> 1.0, >= 1.0.2) + i18n (>= 0.7, < 2) + minitest (~> 5.1) + tzinfo (~> 1.1) + bson (4.8.2) + concurrent-ruby (1.1.6) + diff-lcs (1.3) + i18n (1.8.2) + concurrent-ruby (~> 1.0) + minitest (5.14.0) + mongo (2.11.4) + bson (>= 4.4.2, < 5.0.0) + mongoid (6.4.4) + activemodel (>= 5.1, < 6.0.0) + mongo (>= 2.5.1, < 3.0.0) + rake (13.0.1) + rspec (3.9.0) + rspec-core (~> 3.9.0) + rspec-expectations (~> 3.9.0) + rspec-mocks (~> 3.9.0) + rspec-core (3.9.1) + rspec-support (~> 3.9.1) + rspec-expectations (3.9.1) + diff-lcs (>= 1.2.0, < 2.0) + rspec-support (~> 3.9.0) + rspec-mocks (3.9.1) + diff-lcs (>= 1.2.0, < 2.0) + rspec-support (~> 3.9.0) + rspec-support (3.9.2) + thread_safe (0.3.6) + tzinfo (1.2.6) + thread_safe (~> 0.1) + +PLATFORMS + ruby + +DEPENDENCIES + mongoid + rake + rspec + +BUNDLED WITH + 1.17.3 diff --git a/README.md b/README.md index a2682f8..70151b7 100644 --- a/README.md +++ b/README.md @@ -1,11 +1,10 @@ # Doorkeeper MongoDB extension -[![Gem Version](https://badge.fury.io/rb/doorkeeper-mongodb.svg)](http://badge.fury.io/rb/doorkeeper-mongodb) +[![Gem Version](https://badge.fury.io/rb/doorkeeper-mongodb.svg)](https://badge.fury.io/rb/doorkeeper-mongodb) [![Build Status](https://travis-ci.org/doorkeeper-gem/doorkeeper-mongodb.svg?branch=master)](https://travis-ci.org/doorkeeper-gem/doorkeeper-mongodb) ## Installation -`doorkeeper-mongodb` provides [Doorkeeper](https://github.com/doorkeeper-gem/doorkeeper) support to -[MongoMapper](https://github.com/mongomapper/mongomapper) and [Mongoid](https://github.com/mongodb/mongoid) +`doorkeeper-mongodb` provides [Doorkeeper](https://github.com/doorkeeper-gem/doorkeeper) support to [Mongoid](https://github.com/mongodb/mongoid) (2, 3, 4 and 5 for doorkeeper-mongodb `3.0` and 4, 5, 6 and 7 for version `4.0` and higher). To start using it, add to your Gemfile: @@ -39,22 +38,13 @@ Set the ORM configuration: ``` ruby Doorkeeper.configure do - orm :mongoid6 # or :mongoid7, :mongoid4, :mongoid5, :mongo_mapper + orm :mongoid7 # or any other version of mongoid end ``` ## MongoMapper -**NOTE**: `mongo_mapper` gem works properly with MongoDB <= 3.2, on older versions it throws -`Database command 'insert' failed: Unknown option to insert command: w` exception. This problem -requires `mongo_mapper` gem update. - -Also if you want to use `mongo_mapper` with Rails >= 5.0, then you need to add `activemodel-serializers-xml` gem -to your `Gemfile` (or `gems.rb`): - -```ruby -gem 'activemodel-serializers-xml' -``` +**NOTE**: Support of MongoMapper gem was dropped. ## Indexes @@ -64,14 +54,6 @@ Make sure you create indexes for doorkeeper models. You can do this either by running `rake db:mongoid:create_indexes` or (if you're using Mongoid 2) by adding `autocreate_indexes: true` to your `config/mongoid.yml` - -### MongoMapper - -Generate the `db/indexes.rb` file and create indexes for the doorkeeper models: - - rails generate doorkeeper:mongo_mapper:indexes - rake db:index - ## Tests To run tests, clone this repository and run `rake`. It will copy and run diff --git a/Rakefile b/Rakefile index 1108d73..f9825eb 100644 --- a/Rakefile +++ b/Rakefile @@ -1,11 +1,11 @@ # frozen_string_literal: true -require 'bundler/setup' -require 'rspec/core/rake_task' +require "bundler/setup" +require "rspec/core/rake_task" task :load_doorkeeper do `rm -rf spec/` `git checkout spec` - unless Dir.exist?('doorkeeper') + unless Dir.exist?("doorkeeper") `git submodule init` `git submodule update` end @@ -15,17 +15,17 @@ task :load_doorkeeper do `bundle exec rspec` end -desc 'Update Git submodules.' +desc "Update Git submodules." task :update_submodules do - Rake::Task['load_doorkeeper'].invoke if Dir['doorkeeper/*'].empty? + Rake::Task["load_doorkeeper"].invoke if Dir["doorkeeper/*"].empty? `git submodule foreach git pull origin master` end -desc 'Default: run specs.' +desc "Default: run specs." task default: :spec -desc 'Clone down doorkeeper specs' +desc "Clone down doorkeeper specs" task spec: :load_doorkeeper RSpec::Core::RakeTask.new(:spec) do |config| diff --git a/config/locales/en.yml b/config/locales/en.yml index d1aea41..41982fd 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -27,6 +27,3 @@ en: mongoid: <<: *orm - - mongo_mapper: - <<: *orm diff --git a/doorkeeper-mongodb.gemspec b/doorkeeper-mongodb.gemspec index 425be9b..a4d2ab0 100644 --- a/doorkeeper-mongodb.gemspec +++ b/doorkeeper-mongodb.gemspec @@ -1,31 +1,31 @@ # frozen_string_literal: true -$:.push File.expand_path('../lib', __FILE__) +$:.push File.expand_path("../lib", __FILE__) # Maintain your gem's version: -require 'doorkeeper-mongodb/version' +require "doorkeeper-mongodb/version" # Describe your gem and declare its dependencies: Gem::Specification.new do |gem| - gem.name = 'doorkeeper-mongodb' + gem.name = "doorkeeper-mongodb" gem.version = DoorkeeperMongodb.gem_version - gem.authors = ['jasl', 'Nikita Bulai'] - gem.email = ['bulaj.nikita@gmail.com'] - gem.homepage = 'http://github.com/doorkeeper-gem/doorkeeper-mongodb' - gem.summary = 'Doorkeeper mongoid and mongo_mapper ORMs' - gem.description = 'Doorkeeper mongoid and mongo_mapper ORMs' - gem.license = 'MIT' + gem.authors = ["jasl", "Nikita Bulai"] + gem.email = ["bulaj.nikita@gmail.com"] + gem.homepage = "http://github.com/doorkeeper-gem/doorkeeper-mongodb" + gem.summary = "Doorkeeper Mongoid ORM extension" + gem.description = "Doorkeeper Mongoid ORM extension" + gem.license = "MIT" - gem.files = Dir['lib/**/*', 'config/**/*', 'MIT-LICENSE', 'Rakefile', 'README.md'] - gem.test_files = Dir['spec/**/*'] + gem.files = Dir["lib/**/*", "config/**/*", "MIT-LICENSE", "Rakefile", "README.md"] + gem.test_files = Dir["spec/**/*"] - gem.add_dependency 'doorkeeper', '>= 5.0', '< 6.0' + gem.add_dependency "doorkeeper", ">= 5.0", "< 6.0" - gem.add_development_dependency 'grape' - gem.add_development_dependency 'coveralls' - gem.add_development_dependency 'sqlite3', '~> 1.3.5' - gem.add_development_dependency 'rspec-rails', '~> 3.7' - gem.add_development_dependency 'capybara', '~> 2.17' - gem.add_development_dependency 'generator_spec', '~> 0.9.4' - gem.add_development_dependency 'factory_bot', '~> 4.8' - gem.add_development_dependency 'database_cleaner', '~> 1.6.0' + gem.add_development_dependency "grape" + gem.add_development_dependency "coveralls" + gem.add_development_dependency "sqlite3", "~> 1.3.5" + gem.add_development_dependency "rspec-rails", "~> 3.7" + gem.add_development_dependency "capybara", "~> 2.17" + gem.add_development_dependency "generator_spec", "~> 0.9.4" + gem.add_development_dependency "factory_bot", "~> 4.8" + gem.add_development_dependency "database_cleaner", "~> 1.6.0" end diff --git a/gemfiles/Gemfile.common.rb b/gemfiles/Gemfile.common.rb index e70985b..6e5de66 100644 --- a/gemfiles/Gemfile.common.rb +++ b/gemfiles/Gemfile.common.rb @@ -1,16 +1,16 @@ # frozen_string_literal: true -ENV['RAILS'] ||= '4.2' -ENV['DOORKEEPER'] ||= '5.0' +ENV["RAILS"] ||= "4.2" +ENV["DOORKEEPER"] ||= "5.0" -source 'https://rubygems.org' +source "https://rubygems.org" -gemspec path: '../' +gemspec path: "../" -gem 'rails', "~> #{ENV['RAILS']}" -gem 'doorkeeper', "~> #{ENV['DOORKEEPER']}" -gem 'bcrypt' +gem "rails", "~> #{ENV['RAILS']}" +gem "doorkeeper", "~> #{ENV['DOORKEEPER']}" +gem "bcrypt" # Older Grape requires Ruby >= 2.2.2 -if ENV['RAILS'][0] == '4' - gem 'grape', '~> 0.16', '< 0.19.2' +if ENV["RAILS"][0] == "4" + gem "grape", "~> 0.16", "< 0.19.2" end diff --git a/gemfiles/Gemfile.mongo_mapper.rb b/gemfiles/Gemfile.mongo_mapper.rb deleted file mode 100644 index 8aa4d19..0000000 --- a/gemfiles/Gemfile.mongo_mapper.rb +++ /dev/null @@ -1,11 +0,0 @@ -# frozen_string_literal: true -gemfile = File.expand_path('../Gemfile.common.rb', __FILE__) -instance_eval IO.read(gemfile), gemfile - -gem 'mongo_mapper' -gem 'bson_ext' - -# Rails >= 5 -if ENV['RAILS'][0].to_i >= 5 - gem 'activemodel-serializers-xml' -end diff --git a/gemfiles/Gemfile.mongoid4.rb b/gemfiles/Gemfile.mongoid4.rb index c1558e4..4c70e11 100644 --- a/gemfiles/Gemfile.mongoid4.rb +++ b/gemfiles/Gemfile.mongoid4.rb @@ -1,5 +1,5 @@ # frozen_string_literal: true -gemfile = File.expand_path('../Gemfile.common.rb', __FILE__) +gemfile = File.expand_path("../Gemfile.common.rb", __FILE__) instance_eval IO.read(gemfile), gemfile -gem 'mongoid', '~> 4' +gem "mongoid", "~> 4" diff --git a/gemfiles/Gemfile.mongoid5.rb b/gemfiles/Gemfile.mongoid5.rb index de1dc8e..d9dc37f 100644 --- a/gemfiles/Gemfile.mongoid5.rb +++ b/gemfiles/Gemfile.mongoid5.rb @@ -1,5 +1,5 @@ # frozen_string_literal: true -gemfile = File.expand_path('../Gemfile.common.rb', __FILE__) +gemfile = File.expand_path("../Gemfile.common.rb", __FILE__) instance_eval IO.read(gemfile), gemfile -gem 'mongoid', '~> 5' +gem "mongoid", "~> 5" diff --git a/gemfiles/Gemfile.mongoid6.rb b/gemfiles/Gemfile.mongoid6.rb index b745ecb..69a2fb5 100644 --- a/gemfiles/Gemfile.mongoid6.rb +++ b/gemfiles/Gemfile.mongoid6.rb @@ -1,5 +1,5 @@ # frozen_string_literal: true -gemfile = File.expand_path('../Gemfile.common.rb', __FILE__) +gemfile = File.expand_path("../Gemfile.common.rb", __FILE__) instance_eval IO.read(gemfile), gemfile -gem 'mongoid', '~> 6' +gem "mongoid", "~> 6" diff --git a/gemfiles/Gemfile.mongoid7.rb b/gemfiles/Gemfile.mongoid7.rb index cf049ca..8e0c40a 100644 --- a/gemfiles/Gemfile.mongoid7.rb +++ b/gemfiles/Gemfile.mongoid7.rb @@ -1,5 +1,5 @@ # frozen_string_literal: true -gemfile = File.expand_path('../Gemfile.common.rb', __FILE__) +gemfile = File.expand_path("../Gemfile.common.rb", __FILE__) instance_eval IO.read(gemfile), gemfile -gem 'mongoid', '~> 7.0' +gem "mongoid", "~> 7.0" diff --git a/lib/doorkeeper-mongodb.rb b/lib/doorkeeper-mongodb.rb index c0a41f4..967ddce 100644 --- a/lib/doorkeeper-mongodb.rb +++ b/lib/doorkeeper-mongodb.rb @@ -1,39 +1,31 @@ # frozen_string_literal: true -require 'doorkeeper-mongodb/version' +require "doorkeeper-mongodb/version" -require 'active_model' -require 'doorkeeper' -require 'doorkeeper/orm/active_record/redirect_uri_validator' +require "active_model" +require "doorkeeper" +require "doorkeeper/orm/active_record/redirect_uri_validator" -require 'doorkeeper-mongodb/compatible' -require 'doorkeeper-mongodb/shared/scopes' +require "doorkeeper-mongodb/compatible" +require "doorkeeper-mongodb/shared/scopes" -require 'doorkeeper/orm/concerns/mongo_mapper/ownership' -require 'doorkeeper/orm/concerns/mongoid/ownership' +require "doorkeeper/orm/concerns/mongoid/ownership" -require 'doorkeeper-mongodb/mixins/mongo_mapper/base_mixin' -require 'doorkeeper-mongodb/mixins/mongo_mapper/access_grant_mixin' -require 'doorkeeper-mongodb/mixins/mongo_mapper/access_token_mixin' -require 'doorkeeper-mongodb/mixins/mongo_mapper/application_mixin' - -require 'doorkeeper/orm/mongo_mapper' - -require 'doorkeeper-mongodb/mixins/mongoid/base_mixin' -require 'doorkeeper-mongodb/mixins/mongoid/access_grant_mixin' -require 'doorkeeper-mongodb/mixins/mongoid/access_token_mixin' -require 'doorkeeper-mongodb/mixins/mongoid/application_mixin' -require 'doorkeeper-mongodb/mixins/mongoid/stale_records_cleaner_mixin' +require "doorkeeper-mongodb/mixins/mongoid/base_mixin" +require "doorkeeper-mongodb/mixins/mongoid/access_grant_mixin" +require "doorkeeper-mongodb/mixins/mongoid/access_token_mixin" +require "doorkeeper-mongodb/mixins/mongoid/application_mixin" +require "doorkeeper-mongodb/mixins/mongoid/stale_records_cleaner_mixin" # Maybe we need to squash this into one? With backward compatibility -require 'doorkeeper/orm/mongoid4' -require 'doorkeeper/orm/mongoid5' -require 'doorkeeper/orm/mongoid6' -require 'doorkeeper/orm/mongoid7' +require "doorkeeper/orm/mongoid4" +require "doorkeeper/orm/mongoid5" +require "doorkeeper/orm/mongoid6" +require "doorkeeper/orm/mongoid7" module DoorkeeperMongodb def load_locales - locales_dir = File.expand_path('../../config/locales', __FILE__) - locales = Dir[File.join(locales_dir, '*.yml')] + locales_dir = File.expand_path("../../config/locales", __FILE__) + locales = Dir[File.join(locales_dir, "*.yml")] I18n.load_path |= locales end diff --git a/lib/doorkeeper-mongodb/mixins/mongo_mapper/access_grant_mixin.rb b/lib/doorkeeper-mongodb/mixins/mongo_mapper/access_grant_mixin.rb deleted file mode 100644 index 3c1e9fb..0000000 --- a/lib/doorkeeper-mongodb/mixins/mongo_mapper/access_grant_mixin.rb +++ /dev/null @@ -1,125 +0,0 @@ -# frozen_string_literal: true -module DoorkeeperMongodb - module Mixins - module MongoMapper - module AccessGrantMixin - extend ActiveSupport::Concern - - include Doorkeeper::OAuth::Helpers - include Doorkeeper::Models::Expirable - include Doorkeeper::Models::Revocable - include Doorkeeper::Models::Accessible - include Doorkeeper::Models::Scopes - include BaseMixin - - included do - belongs_to :application, class_name: 'Doorkeeper::Application' - - validates :resource_owner_id, :application_id, :token, :expires_in, :redirect_uri, presence: true - validates :token, uniqueness: true - - before_validation :generate_token, on: :create - end - - # never uses pkce, if pkce migrations were not generated - def uses_pkce? - pkce_supported? && code_challenge.present? - end - - def pkce_supported? - respond_to? :code_challenge - end - - module ClassMethods - # Searches for Doorkeeper::AccessGrant record with the - # specific token value. - # - # @param token [#to_s] token value (any object that responds to `#to_s`) - # - # @return [Doorkeeper::AccessGrant, nil] AccessGrant object or nil - # if there is no record with such token - # - def by_token(token) - where(token: token.to_s).first - end - - # Revokes AccessGrant records that have not been revoked and associated - # with the specific Application and Resource Owner. - # - # @param application_id [Integer] - # ID of the Application - # @param resource_owner [ActiveRecord::Base] - # instance of the Resource Owner model - # - def revoke_all_for(application_id, resource_owner, clock = Time) - set( - { - application_id: application_id, - resource_owner_id: resource_owner.id, - revoked_at: nil - }, - revoked_at: clock.now.utc - ) - end - - # Implements PKCE code_challenge encoding without base64 padding as described in the spec. - # https://tools.ietf.org/html/rfc7636#appendix-A - # Appendix A. Notes on Implementing Base64url Encoding without Padding - # - # This appendix describes how to implement a base64url-encoding - # function without padding, based upon the standard base64-encoding - # function that uses padding. - # - # To be concrete, example C# code implementing these functions is shown - # below. Similar code could be used in other languages. - # - # static string base64urlencode(byte [] arg) - # { - # string s = Convert.ToBase64String(arg); // Regular base64 encoder - # s = s.Split('=')[0]; // Remove any trailing '='s - # s = s.Replace('+', '-'); // 62nd char of encoding - # s = s.Replace('/', '_'); // 63rd char of encoding - # return s; - # } - # - # An example correspondence between unencoded and encoded values - # follows. The octet sequence below encodes into the string below, - # which when decoded, reproduces the octet sequence. - # - # 3 236 255 224 193 - # - # A-z_4ME - # - # https://ruby-doc.org/stdlib-2.1.3/libdoc/base64/rdoc/Base64.html#method-i-urlsafe_encode64 - # - # urlsafe_encode64(bin) - # Returns the Base64-encoded version of bin. This method complies with - # "Base 64 Encoding with URL and Filename Safe Alphabet" in RFC 4648. - # The alphabet uses '-' instead of '+' and '_' instead of '/'. - - # @param code_verifier [#to_s] a one time use value (any object that responds to `#to_s`) - # - # @return [#to_s] An encoded code challenge based on the provided verifier suitable for PKCE validation - def generate_code_challenge(code_verifier) - padded_result = Base64.urlsafe_encode64(Digest::SHA256.digest(code_verifier)) - padded_result.split('=')[0] # Remove any trailing '=' - end - - def pkce_supported? - new.pkce_supported? - end - end - - private - - # Generates token value with UniqueToken class. - # - # @return [String] token value - # - def generate_token - self.token = UniqueToken.generate - end - end - end - end -end diff --git a/lib/doorkeeper-mongodb/mixins/mongo_mapper/access_token_mixin.rb b/lib/doorkeeper-mongodb/mixins/mongo_mapper/access_token_mixin.rb deleted file mode 100644 index 7f9b693..0000000 --- a/lib/doorkeeper-mongodb/mixins/mongo_mapper/access_token_mixin.rb +++ /dev/null @@ -1,291 +0,0 @@ -# frozen_string_literal: true -module DoorkeeperMongodb - module Mixins - module MongoMapper - module AccessTokenMixin - extend ActiveSupport::Concern - - include Doorkeeper::OAuth::Helpers - include Doorkeeper::Models::Expirable - include Doorkeeper::Models::Revocable - include Doorkeeper::Models::Accessible - include Doorkeeper::Models::Scopes - include BaseMixin - - included do - belongs_to :application, class_name: 'Doorkeeper::Application' - - validates :token, presence: true, uniqueness: true - validates :refresh_token, uniqueness: true, if: :use_refresh_token? - - # @attr_writer [Boolean, nil] use_refresh_token - # indicates the possibility of using refresh token - attr_writer :use_refresh_token - - before_validation :generate_token, on: :create - before_validation :generate_refresh_token, - on: :create, - if: :use_refresh_token? - end - - module ClassMethods - # Returns an instance of the Doorkeeper::AccessToken with - # specific token value. - # - # @param token [#to_s] - # token value (any object that responds to `#to_s`) - # - # @return [Doorkeeper::AccessToken, nil] AccessToken object or nil - # if there is no record with such token - # - def by_token(token) - where(token: token.to_s).first - end - - - # Returns an instance of the Doorkeeper::AccessToken - # with specific token value. - # - # @param refresh_token [#to_s] - # refresh token value (any object that responds to `#to_s`) - # - # @return [Doorkeeper::AccessToken, nil] AccessToken object or nil - # if there is no record with such refresh token - # - def by_refresh_token(refresh_token) - where(refresh_token: refresh_token.to_s).first - end - - # Revokes AccessToken records that have not been revoked and associated - # with the specific Application and Resource Owner. - # - # @param application_id [Integer] - # ID of the Application - # @param resource_owner [ActiveRecord::Base] - # instance of the Resource Owner model - # - def revoke_all_for(application_id, resource_owner, clock = Time) - set( - { - application_id: application_id, - resource_owner_id: resource_owner.id, - revoked_at: nil - }, - revoked_at: clock.now.utc - ) - end - - # Looking for not revoked Access Token with a matching set of scopes - # that belongs to specific Application and Resource Owner. - # - # @param application [Doorkeeper::Application] - # Application instance - # @param resource_owner_or_id [ActiveRecord::Base, Integer] - # Resource Owner model instance or it's ID - # @param scopes [String, Doorkeeper::OAuth::Scopes] - # set of scopes - # - # @return [Doorkeeper::AccessToken, nil] Access Token instance or - # nil if matching record was not found - # - def matching_token_for(application, resource_owner_or_id, scopes) - resource_owner_id = if resource_owner_or_id.respond_to?(:to_key) - resource_owner_or_id.id - else - resource_owner_or_id - end - - tokens = authorized_tokens_for(application.try(:id), resource_owner_id) - tokens.detect do |token| - scopes_match?(token.scopes, scopes, application.try(:scopes)) - end - end - - # Checks whether the token scopes match the scopes from the parameters or - # Application scopes (if present). - # - # @param token_scopes [#to_s] - # set of scopes (any object that responds to `#to_s`) - # @param param_scopes [Doorkeeper::OAuth::Scopes] - # scopes from params - # @param app_scopes [Doorkeeper::OAuth::Scopes] - # Application scopes - # - # @return [Boolean] true if the param scopes match the token scopes, - # and all the param scopes are defined in the application (or in the - # server configuration if the application doesn't define any scopes), - # and false in other cases - # - def scopes_match?(token_scopes, param_scopes, app_scopes) - return true if token_scopes.empty? && param_scopes.empty? - - (token_scopes.sort == param_scopes.sort) && - Doorkeeper::OAuth::Helpers::ScopeChecker.valid?( - param_scopes.to_s, - Doorkeeper.configuration.scopes, - app_scopes - ) - end - - # Looking for not expired AccessToken record with a matching set of - # scopes that belongs to specific Application and Resource Owner. - # If it doesn't exists - then creates it. - # - # @param application [Doorkeeper::Application] - # Application instance - # @param resource_owner_id [ActiveRecord::Base, Integer] - # Resource Owner model instance or it's ID - # @param scopes [#to_s] - # set of scopes (any object that responds to `#to_s`) - # @param expires_in [Integer] - # token lifetime in seconds - # @param use_refresh_token [Boolean] - # whether to use the refresh token - # - # @return [Doorkeeper::AccessToken] existing record or a new one - # - def find_or_create_for(application, resource_owner_id, scopes, expires_in, use_refresh_token) - if Doorkeeper.configuration.reuse_access_token - access_token = matching_token_for(application, resource_owner_id, scopes) - - return access_token if access_token && !access_token.expired? - end - - create!( - application_id: application.try(:id), - resource_owner_id: resource_owner_id, - scopes: scopes.to_s, - expires_in: expires_in, - use_refresh_token: use_refresh_token - ) - end - - # Looking for not revoked Access Token records that belongs to specific - # Application and Resource Owner. - # - # @param application_id [Integer] - # ID of the Application model instance - # @param resource_owner_id [Integer] - # ID of the Resource Owner model instance - # - # @return [Doorkeeper::AccessToken] array of matching AccessToken objects - # - def authorized_tokens_for(application_id, resource_owner_id) - send(order_method, created_at_desc). - where(application_id: application_id, - resource_owner_id: resource_owner_id, - revoked_at: nil) - end - - # Convenience method for backwards-compatibility, return the last - # matching token for the given Application and Resource Owner. - # - # @param application_id [Integer] - # ID of the Application model instance - # @param resource_owner_id [Integer] - # ID of the Resource Owner model instance - # - # @return [Doorkeeper::AccessToken, nil] matching AccessToken object or - # nil if nothing was found - # - def last_authorized_token_for(application_id, resource_owner_id) - authorized_tokens_for(application_id, resource_owner_id).first - end - end - - - # Access Token type: Bearer. - # @see https://tools.ietf.org/html/rfc6750 - # The OAuth 2.0 Authorization Framework: Bearer Token Usage - # - def token_type - 'Bearer' - end - - def use_refresh_token? - @use_refresh_token ||= false - !!@use_refresh_token - end - - # JSON representation of the Access Token instance. - # - # @return [Hash] hash with token data - def as_json(_options = {}) - { - resource_owner_id: resource_owner_id, - scope: scopes, - expires_in: expires_in_seconds, - application: { uid: application.try(:uid) }, - created_at: created_at.to_i - } - end - - # Indicates whether the token instance have the same credential - # as the other Access Token. - # - # @param access_token [Doorkeeper::AccessToken] other token - # - # @return [Boolean] true if credentials are same of false in other cases - # - def same_credential?(access_token) - application_id == access_token.application_id && - resource_owner_id == access_token.resource_owner_id - end - - # Indicates if token is acceptable for specific scopes. - # - # @param scopes [Array] scopes - # - # @return [Boolean] true if record is accessible and includes scopes or - # false in other cases - # - def acceptable?(scopes) - accessible? && includes_scope?(*scopes) - end - - private - - # Generates refresh token with UniqueToken generator. - # - # @return [String] refresh token value - # - def generate_refresh_token - self.refresh_token = UniqueToken.generate - end - - # Generates and sets the token value with the - # configured Generator class (see Doorkeeper.configuration). - # - # @return [String] generated token value - # - # @raise [Doorkeeper::Errors::UnableToGenerateToken] - # custom class doesn't implement .generate method - # @raise [Doorkeeper::Errors::TokenGeneratorNotFound] - # custom class doesn't exist - # - def generate_token - self.created_at ||= Time.now.utc - - self.token = token_generator.generate( - resource_owner_id: resource_owner_id, - scopes: scopes, - application: application, - expires_in: expires_in, - created_at: created_at - ) - end - - def token_generator - generator_name = Doorkeeper.configuration.access_token_generator - generator = generator_name.constantize - - return generator if generator.respond_to?(:generate) - - raise Doorkeeper::Errors::UnableToGenerateToken, "#{generator} does not respond to `.generate`." - rescue NameError - raise Doorkeeper::Errors::TokenGeneratorNotFound, "#{generator_name} not found" - end - end - end - end -end diff --git a/lib/doorkeeper-mongodb/mixins/mongo_mapper/application_mixin.rb b/lib/doorkeeper-mongodb/mixins/mongo_mapper/application_mixin.rb deleted file mode 100644 index 23c45e9..0000000 --- a/lib/doorkeeper-mongodb/mixins/mongo_mapper/application_mixin.rb +++ /dev/null @@ -1,106 +0,0 @@ -# frozen_string_literal: true -module DoorkeeperMongodb - module Mixins - module MongoMapper - module ApplicationMixin - extend ActiveSupport::Concern - - include Doorkeeper::OAuth::Helpers - include Doorkeeper::Models::Scopes - include BaseMixin - - included do - many :access_grants, dependent: :destroy, class_name: 'Doorkeeper::AccessGrant' - many :access_tokens, dependent: :destroy, class_name: 'Doorkeeper::AccessToken' - - validates :name, :secret, :uid, presence: true - validates :uid, uniqueness: true - validates :redirect_uri, redirect_uri: true - validates :confidential, inclusion: { in: [true, false] } - - validate :scopes_match_configured, if: :enforce_scopes? - - before_validation :generate_uid, :generate_secret, on: :create - end - - module ClassMethods - # Returns an instance of the Doorkeeper::Application with - # specific UID and secret. - # - # Public/Non-confidential applications will only find by uid if secret is - # blank. - # - # @param uid [#to_s] UID (any object that responds to `#to_s`) - # @param secret [#to_s] secret (any object that responds to `#to_s`) - # - # @return [Doorkeeper::Application, nil] Application instance or nil - # if there is no record with such credentials - # - def by_uid_and_secret(uid, secret) - app = by_uid(uid) - return unless app - return app if secret.blank? && !app.confidential? - return unless app.secret == secret - app - end - - # Returns an instance of the Doorkeeper::Application with specific UID. - # - # @param uid [#to_s] UID (any object that responds to `#to_s`) - # - # @return [Doorkeeper::Application, nil] Application instance or nil - # if there is no record with such UID - # - def by_uid(uid) - where(uid: uid.to_s).first - end - - # Revokes AccessToken and AccessGrant records that have not been revoked and - # associated with the specific Application and Resource Owner. - # - # @param resource_owner [ActiveRecord::Base] - # instance of the Resource Owner model - # - def revoke_tokens_and_grants_for(id, resource_owner) - Doorkeeper::AccessToken.revoke_all_for(id, resource_owner) - Doorkeeper::AccessGrant.revoke_all_for(id, resource_owner) - end - end - - # Set an application's valid redirect URIs. - # - # @param uris [String, Array] Newline-separated string or array the URI(s) - # - # @return [String] The redirect URI(s) seperated by newlines. - def redirect_uri=(uris) - super(uris.is_a?(Array) ? uris.join("\n") : uris) - end - - private - - def generate_uid - if uid.blank? - self.uid = UniqueToken.generate - end - end - - def generate_secret - if secret.blank? - self.secret = UniqueToken.generate - end - end - - def scopes_match_configured - if scopes.present? && - !ScopeChecker.valid?(scopes.to_s, Doorkeeper.configuration.scopes) - errors.add(:scopes, :not_match_configured) - end - end - - def enforce_scopes? - Doorkeeper.configuration.enforce_configured_scopes? - end - end - end - end -end diff --git a/lib/doorkeeper-mongodb/mixins/mongo_mapper/base_mixin.rb b/lib/doorkeeper-mongodb/mixins/mongo_mapper/base_mixin.rb deleted file mode 100644 index c79e822..0000000 --- a/lib/doorkeeper-mongodb/mixins/mongo_mapper/base_mixin.rb +++ /dev/null @@ -1,24 +0,0 @@ -# frozen_string_literal: true -module DoorkeeperMongodb - module Mixins - module MongoMapper - module BaseMixin - extend ActiveSupport::Concern - - module ClassMethods - def ordered_by(attribute, direction = :asc) - sort(attribute.to_sym.send(direction.to_sym)) - end - end - - def as_json(*args) - json_response = super - json_response['id'] = id.to_s - json_response['scopes'] = scopes.to_s - - json_response - end - end - end - end -end diff --git a/lib/doorkeeper-mongodb/mixins/mongoid/access_grant_mixin.rb b/lib/doorkeeper-mongodb/mixins/mongoid/access_grant_mixin.rb index cba0a6c..2b6f245 100644 --- a/lib/doorkeeper-mongodb/mixins/mongoid/access_grant_mixin.rb +++ b/lib/doorkeeper-mongodb/mixins/mongoid/access_grant_mixin.rb @@ -16,7 +16,7 @@ module AccessGrantMixin included do belongs_to_opts = { - class_name: 'Doorkeeper::Application', + class_name: "Doorkeeper::Application", inverse_of: :access_grants } @@ -130,7 +130,7 @@ def revoke_all_for(application_id, resource_owner, clock = Time) # @return [#to_s] An encoded code challenge based on the provided verifier suitable for PKCE validation def generate_code_challenge(code_verifier) padded_result = Base64.urlsafe_encode64(Digest::SHA256.digest(code_verifier)) - padded_result.split('=')[0] # Remove any trailing '=' + padded_result.split("=")[0] # Remove any trailing '=' end def pkce_supported? diff --git a/lib/doorkeeper-mongodb/mixins/mongoid/access_token_mixin.rb b/lib/doorkeeper-mongodb/mixins/mongoid/access_token_mixin.rb index a01b1a2..f97a735 100644 --- a/lib/doorkeeper-mongodb/mixins/mongoid/access_token_mixin.rb +++ b/lib/doorkeeper-mongodb/mixins/mongoid/access_token_mixin.rb @@ -17,7 +17,7 @@ module AccessTokenMixin included do belongs_to_opts = { - class_name: 'Doorkeeper::Application', + class_name: "Doorkeeper::Application", inverse_of: :access_tokens } @@ -239,7 +239,7 @@ def fallback_secret_strategy # The OAuth 2.0 Authorization Framework: Bearer Token Usage # def token_type - 'Bearer' + "Bearer" end def use_refresh_token? diff --git a/lib/doorkeeper-mongodb/mixins/mongoid/application_mixin.rb b/lib/doorkeeper-mongodb/mixins/mongoid/application_mixin.rb index bcee7e1..526095c 100644 --- a/lib/doorkeeper-mongodb/mixins/mongoid/application_mixin.rb +++ b/lib/doorkeeper-mongodb/mixins/mongoid/application_mixin.rb @@ -20,8 +20,8 @@ module ApplicationMixin has_many_options[:dependent] = :delete_all end - has_many :access_grants, has_many_options.merge(class_name: 'Doorkeeper::AccessGrant') - has_many :access_tokens, has_many_options.merge(class_name: 'Doorkeeper::AccessToken') + has_many :access_grants, has_many_options.merge(class_name: "Doorkeeper::AccessGrant") + has_many :access_tokens, has_many_options.merge(class_name: "Doorkeeper::AccessToken") validates :name, :secret, :uid, presence: true validates :uid, uniqueness: true diff --git a/lib/doorkeeper-mongodb/version.rb b/lib/doorkeeper-mongodb/version.rb index cf2b468..b802e0b 100644 --- a/lib/doorkeeper-mongodb/version.rb +++ b/lib/doorkeeper-mongodb/version.rb @@ -1,4 +1,5 @@ # frozen_string_literal: true + module DoorkeeperMongodb def self.gem_version Gem::Version.new VERSION::STRING @@ -7,10 +8,10 @@ def self.gem_version module VERSION # Semver MAJOR = 5 - MINOR = 0 + MINOR = 1 TINY = 0 # Full version number - STRING = [MAJOR, MINOR, TINY].compact.join('.') + STRING = [MAJOR, MINOR, TINY].compact.join(".") end end diff --git a/lib/doorkeeper/generators/doorkeeper/mongo_mapper/indexes_generator.rb b/lib/doorkeeper/generators/doorkeeper/mongo_mapper/indexes_generator.rb deleted file mode 100644 index d157aa8..0000000 --- a/lib/doorkeeper/generators/doorkeeper/mongo_mapper/indexes_generator.rb +++ /dev/null @@ -1,13 +0,0 @@ -# frozen_string_literal: true -module Doorkeeper - module MongoMapper - class IndexesGenerator < ::Rails::Generators::Base - source_root File.expand_path('templates', __FILE__) - desc "'Creates an indexes file for use with MongoMapper's rake db:index'" - - def install - template 'indexes.rb', 'db/indexes.rb' - end - end - end -end diff --git a/lib/doorkeeper/generators/doorkeeper/mongo_mapper/templates/indexes.rb b/lib/doorkeeper/generators/doorkeeper/mongo_mapper/templates/indexes.rb deleted file mode 100644 index b9787a5..0000000 --- a/lib/doorkeeper/generators/doorkeeper/mongo_mapper/templates/indexes.rb +++ /dev/null @@ -1,4 +0,0 @@ -# frozen_string_literal: true -Doorkeeper::Application.create_indexes -Doorkeeper::AccessGrant.create_indexes -Doorkeeper::AccessToken.create_indexes diff --git a/lib/doorkeeper/orm/concerns/mongo_mapper/ownership.rb b/lib/doorkeeper/orm/concerns/mongo_mapper/ownership.rb deleted file mode 100644 index 3eb3766..0000000 --- a/lib/doorkeeper/orm/concerns/mongo_mapper/ownership.rb +++ /dev/null @@ -1,24 +0,0 @@ -# frozen_string_literal: true - -module Doorkeeper - module Orm - module Concerns - module MongoMapper - module Ownership - extend ActiveSupport::Concern - - included do - belongs_to_options = { polymorphic: true } - - belongs_to :owner, belongs_to_options - validates :owner, presence: true, if: :validate_owner? - end - - def validate_owner? - Doorkeeper.configuration.confirm_application_owner? - end - end - end - end - end -end diff --git a/lib/doorkeeper/orm/mongo_mapper.rb b/lib/doorkeeper/orm/mongo_mapper.rb deleted file mode 100644 index 063e9ea..0000000 --- a/lib/doorkeeper/orm/mongo_mapper.rb +++ /dev/null @@ -1,45 +0,0 @@ -# frozen_string_literal: true -require 'active_support/lazy_load_hooks' - -module Doorkeeper - module Orm - module MongoMapper - def self.initialize_models! - install_dependencies! - - lazy_load do - require 'doorkeeper/orm/mongo_mapper/access_grant' - require 'doorkeeper/orm/mongo_mapper/access_token' - require 'doorkeeper/orm/mongo_mapper/application' - require 'doorkeeper/orm/mongo_mapper/stale_records_cleaner' - end - end - - def self.initialize_application_owner! - lazy_load do - require 'doorkeeper/orm/concerns/mongo_mapper/ownership' - - Doorkeeper::Application.send :include, Doorkeeper::Orm::Concerns::MongoMapper::Ownership - end - end - - def self.check_requirements!(_config); end - - def self.install_dependencies! - if ::ActiveModel::VERSION::MAJOR >= 5 - begin - require 'activemodel-serializers-xml' - rescue LoadError - $stderr.print 'Failed to load ActiveModel::Serializers::Xml. ' \ - "You need to add 'activemodel-serializers-xml' gem to your Gemfile." - raise - end - end - end - - def self.lazy_load(&block) - ActiveSupport.on_load(:mongo_mapper, {}, &block) - end - end - end -end diff --git a/lib/doorkeeper/orm/mongo_mapper/access_grant.rb b/lib/doorkeeper/orm/mongo_mapper/access_grant.rb deleted file mode 100644 index 77f48bd..0000000 --- a/lib/doorkeeper/orm/mongo_mapper/access_grant.rb +++ /dev/null @@ -1,37 +0,0 @@ -# frozen_string_literal: true -module Doorkeeper - class AccessGrant - include DoorkeeperMongodb::Compatible - - include MongoMapper::Document - - include DoorkeeperMongodb::Mixins::MongoMapper::AccessGrantMixin - - safe - timestamps! - - set_collection_name 'oauth_access_grants' - - key :resource_owner_id, ObjectId - key :application_id, ObjectId - key :token, String - key :scopes, String - key :expires_in, Integer - key :redirect_uri, String - key :revoked_at, Time - key :code_challenge, String - key :code_challenge_method, String - - def self.create_indexes - ensure_index :token, unique: true - end - - def save!(options = {}) - if options.key?(:validate) - super(options.merge(safe: options.delete(:validate))) - else - super - end - end - end -end diff --git a/lib/doorkeeper/orm/mongo_mapper/access_token.rb b/lib/doorkeeper/orm/mongo_mapper/access_token.rb deleted file mode 100644 index a3ba6ad..0000000 --- a/lib/doorkeeper/orm/mongo_mapper/access_token.rb +++ /dev/null @@ -1,53 +0,0 @@ -# frozen_string_literal: true -module Doorkeeper - class AccessToken - include DoorkeeperMongodb::Compatible - - include MongoMapper::Document - - include DoorkeeperMongodb::Mixins::MongoMapper::AccessTokenMixin - - safe - timestamps! - - set_collection_name 'oauth_access_tokens' - - key :resource_owner_id, ObjectId - key :application_id, ObjectId - key :token, String - key :refresh_token, String - key :previous_refresh_token, String - key :expires_in, Integer - key :revoked_at, Time - key :scopes, String - - def self.last - sort(:created_at).last - end - - def self.create_indexes - ensure_index :token, unique: true - ensure_index [[:refresh_token, 1]], unique: true, sparse: true - end - - def self.refresh_token_revoked_on_use? - keys.keys.include?('previous_refresh_token') - end - - def self.order_method - :sort - end - - def self.created_at_desc - :created_at.desc - end - - def save!(options = {}) - if options.key?(:validate) - super(options.merge(safe: options.delete(:validate))) - else - super - end - end - end -end diff --git a/lib/doorkeeper/orm/mongo_mapper/application.rb b/lib/doorkeeper/orm/mongo_mapper/application.rb deleted file mode 100644 index 235f659..0000000 --- a/lib/doorkeeper/orm/mongo_mapper/application.rb +++ /dev/null @@ -1,65 +0,0 @@ -# frozen_string_literal: true -module Doorkeeper - class Application - include DoorkeeperMongodb::Compatible - - include MongoMapper::Document - - include DoorkeeperMongodb::Mixins::MongoMapper::ApplicationMixin - - safe - timestamps! - - set_collection_name 'oauth_applications' - - many :authorized_tokens, class_name: 'Doorkeeper::AccessToken', dependent: :destroy - - key :name, String - key :uid, String - key :secret, String - key :redirect_uri, String - key :confidential, Boolean, default: true - key :scopes, String - - def self.authorized_for(resource_owner) - ids = AccessToken.where( - resource_owner_id: resource_owner.id, - revoked_at: nil - ).map(&:application_id) - - find(ids) - end - - def self.create_indexes - ensure_index :uid, unique: true - end - - # Due to lack of proper ORM independence in Doorkeeper gem :( - # - def update(attributes = {}) - self.attributes = attributes - save - end - - def create_or_update(options = {}) - run_callbacks(:save) do - result = persisted? ? _update(options) : create(options) - result != false - end - end - - def save!(options = {}) - if options.key?(:validate) - super(options.merge(safe: options.delete(:validate))) - else - super - end - end - - private - - def _update(options = {}) - save_to_collection(options.reverse_merge(:persistence_method => :save)) - end - end -end diff --git a/lib/doorkeeper/orm/mongo_mapper/stale_records_cleaner.rb b/lib/doorkeeper/orm/mongo_mapper/stale_records_cleaner.rb deleted file mode 100644 index b3b35eb..0000000 --- a/lib/doorkeeper/orm/mongo_mapper/stale_records_cleaner.rb +++ /dev/null @@ -1,21 +0,0 @@ -# frozen_string_literal: true - -module Doorkeeper - module Orm - module MongoMapper - class StaleRecordsCleaner - def initialize(base_scope) - @base_scope = base_scope - end - - def clean_revoked - @base_scope.where(:revoked_at.ne => nil, :revoked_at.lt => Time.current).delete_all - end - - def clean_expired(ttl) - @base_scope.where(:created_at.lt => Time.current - ttl).delete_all - end - end - end - end -end diff --git a/lib/doorkeeper/orm/mongoid4.rb b/lib/doorkeeper/orm/mongoid4.rb index 5a4431a..40530ca 100644 --- a/lib/doorkeeper/orm/mongoid4.rb +++ b/lib/doorkeeper/orm/mongoid4.rb @@ -1,21 +1,21 @@ # frozen_string_literal: true -require 'active_support/lazy_load_hooks' +require "active_support/lazy_load_hooks" module Doorkeeper module Orm module Mongoid4 def self.initialize_models! lazy_load do - require 'doorkeeper/orm/mongoid4/access_grant' - require 'doorkeeper/orm/mongoid4/access_token' - require 'doorkeeper/orm/mongoid4/application' - require 'doorkeeper/orm/mongoid4/stale_records_cleaner' + require "doorkeeper/orm/mongoid4/access_grant" + require "doorkeeper/orm/mongoid4/access_token" + require "doorkeeper/orm/mongoid4/application" + require "doorkeeper/orm/mongoid4/stale_records_cleaner" end end def self.initialize_application_owner! lazy_load do - require 'doorkeeper/orm/concerns/mongoid/ownership' + require "doorkeeper/orm/concerns/mongoid/ownership" Doorkeeper::Application.send :include, Doorkeeper::Orm::Concerns::Mongoid::Ownership end diff --git a/lib/doorkeeper/orm/mongoid4/access_token.rb b/lib/doorkeeper/orm/mongoid4/access_token.rb index 1328be0..efced2b 100644 --- a/lib/doorkeeper/orm/mongoid4/access_token.rb +++ b/lib/doorkeeper/orm/mongoid4/access_token.rb @@ -27,7 +27,7 @@ def self.order_method end def self.refresh_token_revoked_on_use? - fields.collect { |field| field[0] }.include?('previous_refresh_token') + fields.collect { |field| field[0] }.include?("previous_refresh_token") end def self.created_at_desc diff --git a/lib/doorkeeper/orm/mongoid4/application.rb b/lib/doorkeeper/orm/mongoid4/application.rb index eb4f660..65427b7 100644 --- a/lib/doorkeeper/orm/mongoid4/application.rb +++ b/lib/doorkeeper/orm/mongoid4/application.rb @@ -19,7 +19,7 @@ class Application index({ uid: 1 }, unique: true) - has_many :authorized_tokens, class_name: 'Doorkeeper::AccessToken' + has_many :authorized_tokens, class_name: "Doorkeeper::AccessToken" def self.authorized_for(resource_owner) ids = AccessToken.where( diff --git a/lib/doorkeeper/orm/mongoid5.rb b/lib/doorkeeper/orm/mongoid5.rb index 90306f0..d32a0f2 100644 --- a/lib/doorkeeper/orm/mongoid5.rb +++ b/lib/doorkeeper/orm/mongoid5.rb @@ -1,21 +1,21 @@ # frozen_string_literal: true -require 'active_support/lazy_load_hooks' +require "active_support/lazy_load_hooks" module Doorkeeper module Orm module Mongoid5 def self.initialize_models! lazy_load do - require 'doorkeeper/orm/mongoid5/access_grant' - require 'doorkeeper/orm/mongoid5/access_token' - require 'doorkeeper/orm/mongoid5/application' - require 'doorkeeper/orm/mongoid5/stale_records_cleaner' + require "doorkeeper/orm/mongoid5/access_grant" + require "doorkeeper/orm/mongoid5/access_token" + require "doorkeeper/orm/mongoid5/application" + require "doorkeeper/orm/mongoid5/stale_records_cleaner" end end def self.initialize_application_owner! lazy_load do - require 'doorkeeper/orm/concerns/mongoid/ownership' + require "doorkeeper/orm/concerns/mongoid/ownership" Doorkeeper::Application.send :include, Doorkeeper::Orm::Concerns::Mongoid::Ownership end diff --git a/lib/doorkeeper/orm/mongoid5/access_token.rb b/lib/doorkeeper/orm/mongoid5/access_token.rb index 1328be0..efced2b 100644 --- a/lib/doorkeeper/orm/mongoid5/access_token.rb +++ b/lib/doorkeeper/orm/mongoid5/access_token.rb @@ -27,7 +27,7 @@ def self.order_method end def self.refresh_token_revoked_on_use? - fields.collect { |field| field[0] }.include?('previous_refresh_token') + fields.collect { |field| field[0] }.include?("previous_refresh_token") end def self.created_at_desc diff --git a/lib/doorkeeper/orm/mongoid5/application.rb b/lib/doorkeeper/orm/mongoid5/application.rb index eb4f660..65427b7 100644 --- a/lib/doorkeeper/orm/mongoid5/application.rb +++ b/lib/doorkeeper/orm/mongoid5/application.rb @@ -19,7 +19,7 @@ class Application index({ uid: 1 }, unique: true) - has_many :authorized_tokens, class_name: 'Doorkeeper::AccessToken' + has_many :authorized_tokens, class_name: "Doorkeeper::AccessToken" def self.authorized_for(resource_owner) ids = AccessToken.where( diff --git a/lib/doorkeeper/orm/mongoid6.rb b/lib/doorkeeper/orm/mongoid6.rb index 50d4c02..a96bc1b 100644 --- a/lib/doorkeeper/orm/mongoid6.rb +++ b/lib/doorkeeper/orm/mongoid6.rb @@ -1,21 +1,21 @@ # frozen_string_literal: true -require 'active_support/lazy_load_hooks' +require "active_support/lazy_load_hooks" module Doorkeeper module Orm module Mongoid6 def self.initialize_models! lazy_load do - require 'doorkeeper/orm/mongoid6/access_grant' - require 'doorkeeper/orm/mongoid6/access_token' - require 'doorkeeper/orm/mongoid6/application' - require 'doorkeeper/orm/mongoid6/stale_records_cleaner' + require "doorkeeper/orm/mongoid6/access_grant" + require "doorkeeper/orm/mongoid6/access_token" + require "doorkeeper/orm/mongoid6/application" + require "doorkeeper/orm/mongoid6/stale_records_cleaner" end end def self.initialize_application_owner! lazy_load do - require 'doorkeeper/orm/concerns/mongoid/ownership' + require "doorkeeper/orm/concerns/mongoid/ownership" Doorkeeper::Application.send :include, Doorkeeper::Orm::Concerns::Mongoid::Ownership end diff --git a/lib/doorkeeper/orm/mongoid6/access_token.rb b/lib/doorkeeper/orm/mongoid6/access_token.rb index 1328be0..efced2b 100644 --- a/lib/doorkeeper/orm/mongoid6/access_token.rb +++ b/lib/doorkeeper/orm/mongoid6/access_token.rb @@ -27,7 +27,7 @@ def self.order_method end def self.refresh_token_revoked_on_use? - fields.collect { |field| field[0] }.include?('previous_refresh_token') + fields.collect { |field| field[0] }.include?("previous_refresh_token") end def self.created_at_desc diff --git a/lib/doorkeeper/orm/mongoid6/application.rb b/lib/doorkeeper/orm/mongoid6/application.rb index eb4f660..65427b7 100644 --- a/lib/doorkeeper/orm/mongoid6/application.rb +++ b/lib/doorkeeper/orm/mongoid6/application.rb @@ -19,7 +19,7 @@ class Application index({ uid: 1 }, unique: true) - has_many :authorized_tokens, class_name: 'Doorkeeper::AccessToken' + has_many :authorized_tokens, class_name: "Doorkeeper::AccessToken" def self.authorized_for(resource_owner) ids = AccessToken.where( diff --git a/lib/doorkeeper/orm/mongoid7.rb b/lib/doorkeeper/orm/mongoid7.rb index 1e08974..8675714 100644 --- a/lib/doorkeeper/orm/mongoid7.rb +++ b/lib/doorkeeper/orm/mongoid7.rb @@ -1,21 +1,21 @@ # frozen_string_literal: true -require 'active_support/lazy_load_hooks' +require "active_support/lazy_load_hooks" module Doorkeeper module Orm module Mongoid7 def self.initialize_models! lazy_load do - require 'doorkeeper/orm/mongoid7/access_grant' - require 'doorkeeper/orm/mongoid7/access_token' - require 'doorkeeper/orm/mongoid7/application' - require 'doorkeeper/orm/mongoid7/stale_records_cleaner' + require "doorkeeper/orm/mongoid7/access_grant" + require "doorkeeper/orm/mongoid7/access_token" + require "doorkeeper/orm/mongoid7/application" + require "doorkeeper/orm/mongoid7/stale_records_cleaner" end end def self.initialize_application_owner! lazy_load do - require 'doorkeeper/orm/concerns/mongoid/ownership' + require "doorkeeper/orm/concerns/mongoid/ownership" Doorkeeper::Application.send :include, Doorkeeper::Orm::Concerns::Mongoid::Ownership end diff --git a/lib/doorkeeper/orm/mongoid7/access_token.rb b/lib/doorkeeper/orm/mongoid7/access_token.rb index 1328be0..efced2b 100644 --- a/lib/doorkeeper/orm/mongoid7/access_token.rb +++ b/lib/doorkeeper/orm/mongoid7/access_token.rb @@ -27,7 +27,7 @@ def self.order_method end def self.refresh_token_revoked_on_use? - fields.collect { |field| field[0] }.include?('previous_refresh_token') + fields.collect { |field| field[0] }.include?("previous_refresh_token") end def self.created_at_desc diff --git a/lib/doorkeeper/orm/mongoid7/application.rb b/lib/doorkeeper/orm/mongoid7/application.rb index eb4f660..65427b7 100644 --- a/lib/doorkeeper/orm/mongoid7/application.rb +++ b/lib/doorkeeper/orm/mongoid7/application.rb @@ -19,7 +19,7 @@ class Application index({ uid: 1 }, unique: true) - has_many :authorized_tokens, class_name: 'Doorkeeper::AccessToken' + has_many :authorized_tokens, class_name: "Doorkeeper::AccessToken" def self.authorized_for(resource_owner) ids = AccessToken.where( diff --git a/spec/dummy/app/models/user.rb b/spec/dummy/app/models/user.rb index 3386b39..7aa96d4 100644 --- a/spec/dummy/app/models/user.rb +++ b/spec/dummy/app/models/user.rb @@ -1,24 +1,13 @@ # frozen_string_literal: true -case DOORKEEPER_ORM.to_s -when /mongoid/ - class User - include Mongoid::Document - include Mongoid::Timestamps - field :name, type: String - field :password, type: String - end -when "mongo_mapper" - class User - include MongoMapper::Document - timestamps! +# [NOTE] MongoMapper support was dropped +class User + include Mongoid::Document + include Mongoid::Timestamps - key :name, String - key :password, String - end -end + field :name, type: String + field :password, type: String -class User if ::Rails.version.to_i < 4 || defined?(::ProtectedAttributes) attr_accessible :name, :password end diff --git a/spec/support/orm/mongo_mapper.rb b/spec/support/orm/mongo_mapper.rb deleted file mode 100644 index f9c591e..0000000 --- a/spec/support/orm/mongo_mapper.rb +++ /dev/null @@ -1,11 +0,0 @@ -# frozen_string_literal: true -DatabaseCleaner[:mongo_mapper].strategy = :truncation -DatabaseCleaner[:mongo_mapper].clean_with :truncation - -RSpec.configure do |config| - config.before :suite do - Doorkeeper::Application.create_indexes - Doorkeeper::AccessGrant.create_indexes - Doorkeeper::AccessToken.create_indexes - end -end diff --git a/spec/support/orm/mongoid7.rb b/spec/support/orm/mongoid7.rb index 560dd62..8881c43 100644 --- a/spec/support/orm/mongoid7.rb +++ b/spec/support/orm/mongoid7.rb @@ -1,5 +1,5 @@ # frozen_string_literal: true -require_relative 'mongoid.rb' +require_relative "mongoid.rb" Mongoid.logger.level = Logger::ERROR Mongo::Logger.logger.level = Logger::ERROR From 56032330461766130d46c1116bc05c7d19c12985 Mon Sep 17 00:00:00 2001 From: Nikita Bulai Date: Tue, 24 Mar 2020 17:35:16 +0300 Subject: [PATCH 07/17] Fixes --- .rubocop.yml | 2 +- .travis.yml | 33 +++++---------- Gemfile | 1 + Rakefile | 1 + doorkeeper-mongodb.gemspec | 1 + gemfiles/Gemfile.common.rb | 1 + gemfiles/Gemfile.mongoid4.rb | 1 + gemfiles/Gemfile.mongoid5.rb | 1 + gemfiles/Gemfile.mongoid6.rb | 1 + gemfiles/Gemfile.mongoid7.rb | 1 + lib/doorkeeper-mongodb.rb | 1 + lib/doorkeeper-mongodb/compatible.rb | 1 + .../mixins/mongoid/access_grant_mixin.rb | 10 ++--- .../mixins/mongoid/access_token_mixin.rb | 41 +++++++++++++++---- .../mixins/mongoid/application_mixin.rb | 1 + .../mixins/mongoid/base_mixin.rb | 1 + lib/doorkeeper-mongodb/shared/scopes.rb | 1 + lib/doorkeeper/orm/mongoid4.rb | 3 +- lib/doorkeeper/orm/mongoid4/access_grant.rb | 1 + lib/doorkeeper/orm/mongoid4/access_token.rb | 1 + lib/doorkeeper/orm/mongoid4/application.rb | 3 +- lib/doorkeeper/orm/mongoid5.rb | 3 +- lib/doorkeeper/orm/mongoid5/access_grant.rb | 1 + lib/doorkeeper/orm/mongoid5/access_token.rb | 1 + lib/doorkeeper/orm/mongoid5/application.rb | 3 +- lib/doorkeeper/orm/mongoid6.rb | 3 +- lib/doorkeeper/orm/mongoid6/access_grant.rb | 1 + lib/doorkeeper/orm/mongoid6/access_token.rb | 1 + lib/doorkeeper/orm/mongoid6/application.rb | 3 +- lib/doorkeeper/orm/mongoid7.rb | 3 +- lib/doorkeeper/orm/mongoid7/access_grant.rb | 1 + lib/doorkeeper/orm/mongoid7/access_token.rb | 1 + lib/doorkeeper/orm/mongoid7/application.rb | 3 +- spec/support/orm/mongoid.rb | 1 + spec/support/orm/mongoid4.rb | 1 + spec/support/orm/mongoid5.rb | 1 + spec/support/orm/mongoid6.rb | 1 + spec/support/orm/mongoid7.rb | 1 + 38 files changed, 90 insertions(+), 46 deletions(-) diff --git a/.rubocop.yml b/.rubocop.yml index d24f04a..8d13005 100644 --- a/.rubocop.yml +++ b/.rubocop.yml @@ -5,7 +5,7 @@ AllCops: Exclude: - "spec/dummy/db/*" - "spec/dummy/config/*" - - "Dangerfile" + - "doorkeepper/**/*" - "gemfiles/*.gemfile" Metrics/BlockLength: diff --git a/.travis.yml b/.travis.yml index 4ebca51..07afcf6 100644 --- a/.travis.yml +++ b/.travis.yml @@ -12,9 +12,9 @@ rvm: - 2.7 env: - - RAILS=5.0.7 - - RAILS=5.1.6 - - RAILS=5.2.0 + - RAILS=5.0 + - RAILS=5.1 + - RAILS=5.2 gemfile: - gemfiles/Gemfile.mongoid4.rb @@ -27,35 +27,22 @@ services: matrix: exclude: - - env: RAILS=5.0.7 - rvm: 2.1 - - env: RAILS=5.1.6 - rvm: 2.1 - - env: RAILS=5.2.0 - rvm: 2.1 - # Mongoid4 requires activemodel ~> 4.0 - gemfile: gemfiles/Gemfile.mongoid4.rb - env: RAILS=5.0.7 + env: RAILS=5.0 - gemfile: gemfiles/Gemfile.mongoid4.rb - env: RAILS=5.1.6 + env: RAILS=5.1 - gemfile: gemfiles/Gemfile.mongoid4.rb - env: RAILS=5.2.0 + env: RAILS=5.2 # Mongoid5 requires activemodel ~> 4.0 - gemfile: gemfiles/Gemfile.mongoid5.rb - env: RAILS=5.0.7 + env: RAILS=5.0 - gemfile: gemfiles/Gemfile.mongoid5.rb - env: RAILS=5.1.6 + env: RAILS=5.1 - gemfile: gemfiles/Gemfile.mongoid5.rb - env: RAILS=5.2.0 - - # Mongoid6 requires activemodel ~> 5.0 - - gemfile: gemfiles/Gemfile.mongoid6.rb - rvm: 2.1 + env: RAILS=5.2 # Mongoid7 requires activemodel ~> 5.1 - gemfile: gemfiles/Gemfile.mongoid7.rb - env: RAILS=5.0.7 - - gemfile: gemfiles/Gemfile.mongoid7.rb - rvm: 2.1 + env: RAILS=5.0 diff --git a/Gemfile b/Gemfile index c3c471c..9cdf872 100755 --- a/Gemfile +++ b/Gemfile @@ -1,4 +1,5 @@ # frozen_string_literal: true + source "https://rubygems.org" gem "mongoid" diff --git a/Rakefile b/Rakefile index f9825eb..eeaa52b 100644 --- a/Rakefile +++ b/Rakefile @@ -1,4 +1,5 @@ # frozen_string_literal: true + require "bundler/setup" require "rspec/core/rake_task" diff --git a/doorkeeper-mongodb.gemspec b/doorkeeper-mongodb.gemspec index a4d2ab0..62c1c2a 100644 --- a/doorkeeper-mongodb.gemspec +++ b/doorkeeper-mongodb.gemspec @@ -1,4 +1,5 @@ # frozen_string_literal: true + $:.push File.expand_path("../lib", __FILE__) # Maintain your gem's version: diff --git a/gemfiles/Gemfile.common.rb b/gemfiles/Gemfile.common.rb index 6e5de66..c9608a2 100644 --- a/gemfiles/Gemfile.common.rb +++ b/gemfiles/Gemfile.common.rb @@ -1,4 +1,5 @@ # frozen_string_literal: true + ENV["RAILS"] ||= "4.2" ENV["DOORKEEPER"] ||= "5.0" diff --git a/gemfiles/Gemfile.mongoid4.rb b/gemfiles/Gemfile.mongoid4.rb index 4c70e11..17f636b 100644 --- a/gemfiles/Gemfile.mongoid4.rb +++ b/gemfiles/Gemfile.mongoid4.rb @@ -1,4 +1,5 @@ # frozen_string_literal: true + gemfile = File.expand_path("../Gemfile.common.rb", __FILE__) instance_eval IO.read(gemfile), gemfile diff --git a/gemfiles/Gemfile.mongoid5.rb b/gemfiles/Gemfile.mongoid5.rb index d9dc37f..acecddf 100644 --- a/gemfiles/Gemfile.mongoid5.rb +++ b/gemfiles/Gemfile.mongoid5.rb @@ -1,4 +1,5 @@ # frozen_string_literal: true + gemfile = File.expand_path("../Gemfile.common.rb", __FILE__) instance_eval IO.read(gemfile), gemfile diff --git a/gemfiles/Gemfile.mongoid6.rb b/gemfiles/Gemfile.mongoid6.rb index 69a2fb5..67e73fd 100644 --- a/gemfiles/Gemfile.mongoid6.rb +++ b/gemfiles/Gemfile.mongoid6.rb @@ -1,4 +1,5 @@ # frozen_string_literal: true + gemfile = File.expand_path("../Gemfile.common.rb", __FILE__) instance_eval IO.read(gemfile), gemfile diff --git a/gemfiles/Gemfile.mongoid7.rb b/gemfiles/Gemfile.mongoid7.rb index 8e0c40a..8e5421e 100644 --- a/gemfiles/Gemfile.mongoid7.rb +++ b/gemfiles/Gemfile.mongoid7.rb @@ -1,4 +1,5 @@ # frozen_string_literal: true + gemfile = File.expand_path("../Gemfile.common.rb", __FILE__) instance_eval IO.read(gemfile), gemfile diff --git a/lib/doorkeeper-mongodb.rb b/lib/doorkeeper-mongodb.rb index 967ddce..b980dca 100644 --- a/lib/doorkeeper-mongodb.rb +++ b/lib/doorkeeper-mongodb.rb @@ -1,4 +1,5 @@ # frozen_string_literal: true + require "doorkeeper-mongodb/version" require "active_model" diff --git a/lib/doorkeeper-mongodb/compatible.rb b/lib/doorkeeper-mongodb/compatible.rb index ef8abd0..319d928 100644 --- a/lib/doorkeeper-mongodb/compatible.rb +++ b/lib/doorkeeper-mongodb/compatible.rb @@ -1,4 +1,5 @@ # frozen_string_literal: true + module DoorkeeperMongodb module Compatible extend ActiveSupport::Concern diff --git a/lib/doorkeeper-mongodb/mixins/mongoid/access_grant_mixin.rb b/lib/doorkeeper-mongodb/mixins/mongoid/access_grant_mixin.rb index 2b6f245..5ae59ab 100644 --- a/lib/doorkeeper-mongodb/mixins/mongoid/access_grant_mixin.rb +++ b/lib/doorkeeper-mongodb/mixins/mongoid/access_grant_mixin.rb @@ -1,4 +1,5 @@ # frozen_string_literal: true + module DoorkeeperMongodb module Mixins module Mongoid @@ -80,14 +81,13 @@ def by_token(token) # # @param application_id [Integer] # ID of the Application - # @param resource_owner [ActiveRecord::Base] + # @param resource_owner [Mongoid::Document, Integer] # instance of the Resource Owner model # def revoke_all_for(application_id, resource_owner, clock = Time) - where(application_id: application_id, - resource_owner_id: resource_owner.id, - revoked_at: nil) - .update_all(revoked_at: clock.now.utc) + by_resource_owner(resource_owner) + .where(application_id: application_id, revoked_at: nil) + .update_all(revoked_at: clock.now.utc) end # Implements PKCE code_challenge encoding without base64 padding as described in the spec. diff --git a/lib/doorkeeper-mongodb/mixins/mongoid/access_token_mixin.rb b/lib/doorkeeper-mongodb/mixins/mongoid/access_token_mixin.rb index f97a735..48c12af 100644 --- a/lib/doorkeeper-mongodb/mixins/mongoid/access_token_mixin.rb +++ b/lib/doorkeeper-mongodb/mixins/mongoid/access_token_mixin.rb @@ -1,4 +1,5 @@ # frozen_string_literal: true + module DoorkeeperMongodb module Mixins module Mongoid @@ -90,7 +91,7 @@ def by_refresh_token(refresh_token) def revoke_all_for(application_id, resource_owner, clock = Time) by_resource_owner(resource_owner). where(application_id: application_id, - revoked_at: nil). + revoked_at: nil,). update_all(revoked_at: clock.now.utc) end @@ -158,7 +159,7 @@ def scopes_match?(token_scopes, param_scopes, app_scopes) # # @param application [Doorkeeper::Application] # Application instance - # @param resource_owner_id [ActiveRecord::Base, Integer] + # @param resource_owner [Mongoid::Document, Integer] # Resource Owner model instance or it's ID # @param scopes [#to_s] # set of scopes (any object that responds to `#to_s`) @@ -197,7 +198,7 @@ def find_or_create_for(application, resource_owner, scopes, expires_in, use_refr # # @param application_id [Integer] # ID of the Application model instance - # @param resource_owner_id [Integer] + # @param resource_owner [Mongoid::Document] # ID of the Resource Owner model instance # # @return [Doorkeeper::AccessToken] array of matching AccessToken objects @@ -205,8 +206,7 @@ def find_or_create_for(application, resource_owner, scopes, expires_in, use_refr def authorized_tokens_for(application_id, resource_owner) send(order_method, created_at_desc). by_resource_owner(resource_owner). - where(application_id: application_id, - revoked_at: nil) + where(application_id: application_id, revoked_at: nil) end # Convenience method for backwards-compatibility, return the last @@ -214,14 +214,14 @@ def authorized_tokens_for(application_id, resource_owner) # # @param application_id [Integer] # ID of the Application model instance - # @param resource_owner_id [Integer] + # @param resource_owner [Mongoid::Document, Integer] # ID of the Resource Owner model instance # # @return [Doorkeeper::AccessToken, nil] matching AccessToken object or # nil if nothing was found # - def last_authorized_token_for(application_id, resource_owner_id) - authorized_tokens_for(application_id, resource_owner_id).first + def last_authorized_token_for(application_id, resource_owner) + authorized_tokens_for(application_id, resource_owner).first end def secret_strategy @@ -269,7 +269,22 @@ def as_json(_options = {}) # def same_credential?(access_token) application_id == access_token.application_id && + same_resource_owner?(access_token) + end + + # Indicates whether the token instance have the same credential + # as the other Access Token. + # + # @param access_token [Doorkeeper::AccessToken] other token + # + # @return [Boolean] true if credentials are same of false in other cases + # + def same_resource_owner?(access_token) + if Doorkeeper.configuration.polymorphic_resource_owner? + resource_owner == access_token.resource_owner + else resource_owner_id == access_token.resource_owner_id + end end # Indicates if token is acceptable for specific scopes. @@ -283,6 +298,8 @@ def acceptable?(scopes) accessible? && includes_scope?(*scopes) end + # We keep a volatile copy of the raw refresh token for initial communication + # The stored refresh_token may be mapped and not available in cleartext. def plaintext_refresh_token if secret_strategy.allows_restoring_secrets? secret_strategy.restore_secret(self, :refresh_token) @@ -291,6 +308,12 @@ def plaintext_refresh_token end end + # We keep a volatile copy of the raw token for initial communication + # The stored refresh_token may be mapped and not available in cleartext. + # + # Some strategies allow restoring stored secrets (e.g. symmetric encryption) + # while hashing strategies do not, so you cannot rely on this value + # returning a present value for persisted tokens. def plaintext_token if secret_strategy.allows_restoring_secrets? secret_strategy.restore_secret(self, :token) @@ -342,7 +365,7 @@ def generate_token scopes: scopes, application: application, expires_in: expires_in, - created_at: created_at + created_at: created_at, ) secret_strategy.store_secret(self, :token, @raw_token) @raw_token diff --git a/lib/doorkeeper-mongodb/mixins/mongoid/application_mixin.rb b/lib/doorkeeper-mongodb/mixins/mongoid/application_mixin.rb index 526095c..f5935b2 100644 --- a/lib/doorkeeper-mongodb/mixins/mongoid/application_mixin.rb +++ b/lib/doorkeeper-mongodb/mixins/mongoid/application_mixin.rb @@ -1,4 +1,5 @@ # frozen_string_literal: true + module DoorkeeperMongodb module Mixins module Mongoid diff --git a/lib/doorkeeper-mongodb/mixins/mongoid/base_mixin.rb b/lib/doorkeeper-mongodb/mixins/mongoid/base_mixin.rb index 05f819a..9d3f3ed 100644 --- a/lib/doorkeeper-mongodb/mixins/mongoid/base_mixin.rb +++ b/lib/doorkeeper-mongodb/mixins/mongoid/base_mixin.rb @@ -1,4 +1,5 @@ # frozen_string_literal: true + module DoorkeeperMongodb module Mixins module Mongoid diff --git a/lib/doorkeeper-mongodb/shared/scopes.rb b/lib/doorkeeper-mongodb/shared/scopes.rb index a431858..188d8d1 100644 --- a/lib/doorkeeper-mongodb/shared/scopes.rb +++ b/lib/doorkeeper-mongodb/shared/scopes.rb @@ -1,4 +1,5 @@ # frozen_string_literal: true + module DoorkeeperMongodb module Shared module Scopes diff --git a/lib/doorkeeper/orm/mongoid4.rb b/lib/doorkeeper/orm/mongoid4.rb index 40530ca..de98aa2 100644 --- a/lib/doorkeeper/orm/mongoid4.rb +++ b/lib/doorkeeper/orm/mongoid4.rb @@ -1,4 +1,5 @@ # frozen_string_literal: true + require "active_support/lazy_load_hooks" module Doorkeeper @@ -17,7 +18,7 @@ def self.initialize_application_owner! lazy_load do require "doorkeeper/orm/concerns/mongoid/ownership" - Doorkeeper::Application.send :include, Doorkeeper::Orm::Concerns::Mongoid::Ownership + Doorkeeper::Application.include Doorkeeper::Orm::Concerns::Mongoid::Ownership end end diff --git a/lib/doorkeeper/orm/mongoid4/access_grant.rb b/lib/doorkeeper/orm/mongoid4/access_grant.rb index 9b9e63c..b8af3d8 100644 --- a/lib/doorkeeper/orm/mongoid4/access_grant.rb +++ b/lib/doorkeeper/orm/mongoid4/access_grant.rb @@ -1,4 +1,5 @@ # frozen_string_literal: true + module Doorkeeper class AccessGrant include Mongoid::Document diff --git a/lib/doorkeeper/orm/mongoid4/access_token.rb b/lib/doorkeeper/orm/mongoid4/access_token.rb index efced2b..45801ec 100644 --- a/lib/doorkeeper/orm/mongoid4/access_token.rb +++ b/lib/doorkeeper/orm/mongoid4/access_token.rb @@ -1,4 +1,5 @@ # frozen_string_literal: true + module Doorkeeper class AccessToken include Mongoid::Document diff --git a/lib/doorkeeper/orm/mongoid4/application.rb b/lib/doorkeeper/orm/mongoid4/application.rb index 65427b7..a74496f 100644 --- a/lib/doorkeeper/orm/mongoid4/application.rb +++ b/lib/doorkeeper/orm/mongoid4/application.rb @@ -1,4 +1,5 @@ # frozen_string_literal: true + module Doorkeeper class Application include Mongoid::Document @@ -24,7 +25,7 @@ class Application def self.authorized_for(resource_owner) ids = AccessToken.where( resource_owner_id: resource_owner.id, - revoked_at: nil + revoked_at: nil, ).map(&:application_id) find(ids) diff --git a/lib/doorkeeper/orm/mongoid5.rb b/lib/doorkeeper/orm/mongoid5.rb index d32a0f2..28a7c38 100644 --- a/lib/doorkeeper/orm/mongoid5.rb +++ b/lib/doorkeeper/orm/mongoid5.rb @@ -1,4 +1,5 @@ # frozen_string_literal: true + require "active_support/lazy_load_hooks" module Doorkeeper @@ -17,7 +18,7 @@ def self.initialize_application_owner! lazy_load do require "doorkeeper/orm/concerns/mongoid/ownership" - Doorkeeper::Application.send :include, Doorkeeper::Orm::Concerns::Mongoid::Ownership + Doorkeeper::Application.include Doorkeeper::Orm::Concerns::Mongoid::Ownership end end diff --git a/lib/doorkeeper/orm/mongoid5/access_grant.rb b/lib/doorkeeper/orm/mongoid5/access_grant.rb index 9b9e63c..b8af3d8 100644 --- a/lib/doorkeeper/orm/mongoid5/access_grant.rb +++ b/lib/doorkeeper/orm/mongoid5/access_grant.rb @@ -1,4 +1,5 @@ # frozen_string_literal: true + module Doorkeeper class AccessGrant include Mongoid::Document diff --git a/lib/doorkeeper/orm/mongoid5/access_token.rb b/lib/doorkeeper/orm/mongoid5/access_token.rb index efced2b..45801ec 100644 --- a/lib/doorkeeper/orm/mongoid5/access_token.rb +++ b/lib/doorkeeper/orm/mongoid5/access_token.rb @@ -1,4 +1,5 @@ # frozen_string_literal: true + module Doorkeeper class AccessToken include Mongoid::Document diff --git a/lib/doorkeeper/orm/mongoid5/application.rb b/lib/doorkeeper/orm/mongoid5/application.rb index 65427b7..a74496f 100644 --- a/lib/doorkeeper/orm/mongoid5/application.rb +++ b/lib/doorkeeper/orm/mongoid5/application.rb @@ -1,4 +1,5 @@ # frozen_string_literal: true + module Doorkeeper class Application include Mongoid::Document @@ -24,7 +25,7 @@ class Application def self.authorized_for(resource_owner) ids = AccessToken.where( resource_owner_id: resource_owner.id, - revoked_at: nil + revoked_at: nil, ).map(&:application_id) find(ids) diff --git a/lib/doorkeeper/orm/mongoid6.rb b/lib/doorkeeper/orm/mongoid6.rb index a96bc1b..9b211df 100644 --- a/lib/doorkeeper/orm/mongoid6.rb +++ b/lib/doorkeeper/orm/mongoid6.rb @@ -1,4 +1,5 @@ # frozen_string_literal: true + require "active_support/lazy_load_hooks" module Doorkeeper @@ -17,7 +18,7 @@ def self.initialize_application_owner! lazy_load do require "doorkeeper/orm/concerns/mongoid/ownership" - Doorkeeper::Application.send :include, Doorkeeper::Orm::Concerns::Mongoid::Ownership + Doorkeeper::Application.include Doorkeeper::Orm::Concerns::Mongoid::Ownership end end diff --git a/lib/doorkeeper/orm/mongoid6/access_grant.rb b/lib/doorkeeper/orm/mongoid6/access_grant.rb index 9b9e63c..b8af3d8 100644 --- a/lib/doorkeeper/orm/mongoid6/access_grant.rb +++ b/lib/doorkeeper/orm/mongoid6/access_grant.rb @@ -1,4 +1,5 @@ # frozen_string_literal: true + module Doorkeeper class AccessGrant include Mongoid::Document diff --git a/lib/doorkeeper/orm/mongoid6/access_token.rb b/lib/doorkeeper/orm/mongoid6/access_token.rb index efced2b..45801ec 100644 --- a/lib/doorkeeper/orm/mongoid6/access_token.rb +++ b/lib/doorkeeper/orm/mongoid6/access_token.rb @@ -1,4 +1,5 @@ # frozen_string_literal: true + module Doorkeeper class AccessToken include Mongoid::Document diff --git a/lib/doorkeeper/orm/mongoid6/application.rb b/lib/doorkeeper/orm/mongoid6/application.rb index 65427b7..a74496f 100644 --- a/lib/doorkeeper/orm/mongoid6/application.rb +++ b/lib/doorkeeper/orm/mongoid6/application.rb @@ -1,4 +1,5 @@ # frozen_string_literal: true + module Doorkeeper class Application include Mongoid::Document @@ -24,7 +25,7 @@ class Application def self.authorized_for(resource_owner) ids = AccessToken.where( resource_owner_id: resource_owner.id, - revoked_at: nil + revoked_at: nil, ).map(&:application_id) find(ids) diff --git a/lib/doorkeeper/orm/mongoid7.rb b/lib/doorkeeper/orm/mongoid7.rb index 8675714..9df460b 100644 --- a/lib/doorkeeper/orm/mongoid7.rb +++ b/lib/doorkeeper/orm/mongoid7.rb @@ -1,4 +1,5 @@ # frozen_string_literal: true + require "active_support/lazy_load_hooks" module Doorkeeper @@ -17,7 +18,7 @@ def self.initialize_application_owner! lazy_load do require "doorkeeper/orm/concerns/mongoid/ownership" - Doorkeeper::Application.send :include, Doorkeeper::Orm::Concerns::Mongoid::Ownership + Doorkeeper::Application.include Doorkeeper::Orm::Concerns::Mongoid::Ownership end end diff --git a/lib/doorkeeper/orm/mongoid7/access_grant.rb b/lib/doorkeeper/orm/mongoid7/access_grant.rb index 9b9e63c..b8af3d8 100644 --- a/lib/doorkeeper/orm/mongoid7/access_grant.rb +++ b/lib/doorkeeper/orm/mongoid7/access_grant.rb @@ -1,4 +1,5 @@ # frozen_string_literal: true + module Doorkeeper class AccessGrant include Mongoid::Document diff --git a/lib/doorkeeper/orm/mongoid7/access_token.rb b/lib/doorkeeper/orm/mongoid7/access_token.rb index efced2b..45801ec 100644 --- a/lib/doorkeeper/orm/mongoid7/access_token.rb +++ b/lib/doorkeeper/orm/mongoid7/access_token.rb @@ -1,4 +1,5 @@ # frozen_string_literal: true + module Doorkeeper class AccessToken include Mongoid::Document diff --git a/lib/doorkeeper/orm/mongoid7/application.rb b/lib/doorkeeper/orm/mongoid7/application.rb index 65427b7..a74496f 100644 --- a/lib/doorkeeper/orm/mongoid7/application.rb +++ b/lib/doorkeeper/orm/mongoid7/application.rb @@ -1,4 +1,5 @@ # frozen_string_literal: true + module Doorkeeper class Application include Mongoid::Document @@ -24,7 +25,7 @@ class Application def self.authorized_for(resource_owner) ids = AccessToken.where( resource_owner_id: resource_owner.id, - revoked_at: nil + revoked_at: nil, ).map(&:application_id) find(ids) diff --git a/spec/support/orm/mongoid.rb b/spec/support/orm/mongoid.rb index 0b1c089..61d8013 100644 --- a/spec/support/orm/mongoid.rb +++ b/spec/support/orm/mongoid.rb @@ -1,4 +1,5 @@ # frozen_string_literal: true + DatabaseCleaner[:mongoid].strategy = :truncation DatabaseCleaner[:mongoid].clean_with :truncation diff --git a/spec/support/orm/mongoid4.rb b/spec/support/orm/mongoid4.rb index 3d89325..5336314 100644 --- a/spec/support/orm/mongoid4.rb +++ b/spec/support/orm/mongoid4.rb @@ -1,2 +1,3 @@ # frozen_string_literal: true + require_relative "mongoid.rb" diff --git a/spec/support/orm/mongoid5.rb b/spec/support/orm/mongoid5.rb index 8881c43..7b3b58c 100644 --- a/spec/support/orm/mongoid5.rb +++ b/spec/support/orm/mongoid5.rb @@ -1,4 +1,5 @@ # frozen_string_literal: true + require_relative "mongoid.rb" Mongoid.logger.level = Logger::ERROR diff --git a/spec/support/orm/mongoid6.rb b/spec/support/orm/mongoid6.rb index 8881c43..7b3b58c 100644 --- a/spec/support/orm/mongoid6.rb +++ b/spec/support/orm/mongoid6.rb @@ -1,4 +1,5 @@ # frozen_string_literal: true + require_relative "mongoid.rb" Mongoid.logger.level = Logger::ERROR diff --git a/spec/support/orm/mongoid7.rb b/spec/support/orm/mongoid7.rb index 8881c43..7b3b58c 100644 --- a/spec/support/orm/mongoid7.rb +++ b/spec/support/orm/mongoid7.rb @@ -1,4 +1,5 @@ # frozen_string_literal: true + require_relative "mongoid.rb" Mongoid.logger.level = Logger::ERROR From 7c5f8170040aa92efaaa136ba66236606792b25a Mon Sep 17 00:00:00 2001 From: Nikita Bulai Date: Tue, 24 Mar 2020 18:07:56 +0300 Subject: [PATCH 08/17] Fixes, p.2 --- gemfiles/Gemfile.common.rb | 2 +- lib/doorkeeper-mongodb.rb | 6 +++++- lib/doorkeeper-mongodb/mixins/mongoid/access_grant_mixin.rb | 2 +- lib/doorkeeper-mongodb/mixins/mongoid/application_mixin.rb | 3 +++ 4 files changed, 10 insertions(+), 3 deletions(-) diff --git a/gemfiles/Gemfile.common.rb b/gemfiles/Gemfile.common.rb index c9608a2..4f4533b 100644 --- a/gemfiles/Gemfile.common.rb +++ b/gemfiles/Gemfile.common.rb @@ -1,6 +1,6 @@ # frozen_string_literal: true -ENV["RAILS"] ||= "4.2" +ENV["RAILS"] ||= "5.0" ENV["DOORKEEPER"] ||= "5.0" source "https://rubygems.org" diff --git a/lib/doorkeeper-mongodb.rb b/lib/doorkeeper-mongodb.rb index b980dca..a79f1fe 100644 --- a/lib/doorkeeper-mongodb.rb +++ b/lib/doorkeeper-mongodb.rb @@ -4,7 +4,11 @@ require "active_model" require "doorkeeper" -require "doorkeeper/orm/active_record/redirect_uri_validator" +begin + require "doorkeeper/orm/active_record/redirect_uri_validator" +rescue LoadError + # for old Doorkeeper version before this change +end require "doorkeeper-mongodb/compatible" require "doorkeeper-mongodb/shared/scopes" diff --git a/lib/doorkeeper-mongodb/mixins/mongoid/access_grant_mixin.rb b/lib/doorkeeper-mongodb/mixins/mongoid/access_grant_mixin.rb index 5ae59ab..e8a34e9 100644 --- a/lib/doorkeeper-mongodb/mixins/mongoid/access_grant_mixin.rb +++ b/lib/doorkeeper-mongodb/mixins/mongoid/access_grant_mixin.rb @@ -73,7 +73,7 @@ module ClassMethods # if there is no record with such token # def by_token(token) - where(token: token.to_s).first + find_by_plaintext_token(:token, token) end # Revokes AccessGrant records that have not been revoked and associated diff --git a/lib/doorkeeper-mongodb/mixins/mongoid/application_mixin.rb b/lib/doorkeeper-mongodb/mixins/mongoid/application_mixin.rb index f5935b2..84d73e3 100644 --- a/lib/doorkeeper-mongodb/mixins/mongoid/application_mixin.rb +++ b/lib/doorkeeper-mongodb/mixins/mongoid/application_mixin.rb @@ -128,6 +128,9 @@ def plaintext_secret def as_json(options = {}) hash = super + if hash.key?("_id") || (options && Array.wrap(options[:only]).include?(:id)) + hash["id"] = id.to_s + end hash["secret"] = plaintext_secret if hash.key?("secret") hash end From 90b9e846cd868883e733b4d1ff6cd49f6ce6f028 Mon Sep 17 00:00:00 2001 From: Nikita Bulai Date: Tue, 24 Mar 2020 18:36:10 +0300 Subject: [PATCH 09/17] Updates to support Doorkeeper 5.x --- doorkeeper-mongodb.gemspec | 2 +- lib/doorkeeper-mongodb/compatible.rb | 2 +- .../mixins/mongoid/access_grant_mixin.rb | 2 +- .../mixins/mongoid/access_token_mixin.rb | 6 +++--- lib/doorkeeper-mongodb/shared/scopes.rb | 12 +++++++++++- lib/doorkeeper-mongodb/version.rb | 2 +- spec/support/orm/mongoid.rb | 8 ++++++++ 7 files changed, 26 insertions(+), 8 deletions(-) diff --git a/doorkeeper-mongodb.gemspec b/doorkeeper-mongodb.gemspec index 62c1c2a..40b04ef 100644 --- a/doorkeeper-mongodb.gemspec +++ b/doorkeeper-mongodb.gemspec @@ -19,7 +19,7 @@ Gem::Specification.new do |gem| gem.files = Dir["lib/**/*", "config/**/*", "MIT-LICENSE", "Rakefile", "README.md"] gem.test_files = Dir["spec/**/*"] - gem.add_dependency "doorkeeper", ">= 5.0", "< 6.0" + gem.add_dependency "doorkeeper", ">= 5.2", "< 6.0" gem.add_development_dependency "grape" gem.add_development_dependency "coveralls" diff --git a/lib/doorkeeper-mongodb/compatible.rb b/lib/doorkeeper-mongodb/compatible.rb index 319d928..383f62b 100644 --- a/lib/doorkeeper-mongodb/compatible.rb +++ b/lib/doorkeeper-mongodb/compatible.rb @@ -25,7 +25,7 @@ def transaction(options = {}, &block) end def update_column(column, value) - update(column => value) + update_attribute(column, value) end def lock!(_ = true) diff --git a/lib/doorkeeper-mongodb/mixins/mongoid/access_grant_mixin.rb b/lib/doorkeeper-mongodb/mixins/mongoid/access_grant_mixin.rb index e8a34e9..003b942 100644 --- a/lib/doorkeeper-mongodb/mixins/mongoid/access_grant_mixin.rb +++ b/lib/doorkeeper-mongodb/mixins/mongoid/access_grant_mixin.rb @@ -28,7 +28,7 @@ module AccessGrantMixin belongs_to :application, belongs_to_opts - if Doorkeeper.config.polymorphic_resource_owner? + if Doorkeeper::VERSION::MINOR > 3 && Doorkeeper.config.polymorphic_resource_owner? belongs_to :resource_owner, polymorphic: true end diff --git a/lib/doorkeeper-mongodb/mixins/mongoid/access_token_mixin.rb b/lib/doorkeeper-mongodb/mixins/mongoid/access_token_mixin.rb index 48c12af..d732288 100644 --- a/lib/doorkeeper-mongodb/mixins/mongoid/access_token_mixin.rb +++ b/lib/doorkeeper-mongodb/mixins/mongoid/access_token_mixin.rb @@ -29,7 +29,7 @@ module AccessTokenMixin belongs_to :application, belongs_to_opts - if Doorkeeper.config.polymorphic_resource_owner? + if Doorkeeper::VERSION::MINOR > 3 && Doorkeeper.config.polymorphic_resource_owner? opts = { polymorphic: true } if ::Mongoid::VERSION[0].to_i >= 6 @@ -184,7 +184,7 @@ def find_or_create_for(application, resource_owner, scopes, expires_in, use_refr use_refresh_token: use_refresh_token } - if Doorkeeper.config.polymorphic_resource_owner? + if Doorkeeper::VERSION::MINOR > 3 && Doorkeeper.config.polymorphic_resource_owner? attributes[:resource_owner] = resource_owner else attributes[:resource_owner_id] = resource_owner_id_for(resource_owner) @@ -280,7 +280,7 @@ def same_credential?(access_token) # @return [Boolean] true if credentials are same of false in other cases # def same_resource_owner?(access_token) - if Doorkeeper.configuration.polymorphic_resource_owner? + if Doorkeeper::VERSION::MINOR > 3 && Doorkeeper.config.polymorphic_resource_owner? resource_owner == access_token.resource_owner else resource_owner_id == access_token.resource_owner_id diff --git a/lib/doorkeeper-mongodb/shared/scopes.rb b/lib/doorkeeper-mongodb/shared/scopes.rb index 188d8d1..9a91f7d 100644 --- a/lib/doorkeeper-mongodb/shared/scopes.rb +++ b/lib/doorkeeper-mongodb/shared/scopes.rb @@ -10,7 +10,17 @@ module Scopes end def scopes=(value) - write_attribute :scopes, value + scopes = if value.is_a?(Array) + Doorkeeper::OAuth::Scopes.from_array(value).to_s + else + Doorkeeper::OAuth::Scopes.from_string(value.to_s).to_s + end + + write_attribute :scopes, scopes + end + + def scopes_string + self[:scopes] end end end diff --git a/lib/doorkeeper-mongodb/version.rb b/lib/doorkeeper-mongodb/version.rb index b802e0b..eac6d70 100644 --- a/lib/doorkeeper-mongodb/version.rb +++ b/lib/doorkeeper-mongodb/version.rb @@ -8,7 +8,7 @@ def self.gem_version module VERSION # Semver MAJOR = 5 - MINOR = 1 + MINOR = 2 TINY = 0 # Full version number diff --git a/spec/support/orm/mongoid.rb b/spec/support/orm/mongoid.rb index 61d8013..cdd543b 100644 --- a/spec/support/orm/mongoid.rb +++ b/spec/support/orm/mongoid.rb @@ -3,6 +3,14 @@ DatabaseCleaner[:mongoid].strategy = :truncation DatabaseCleaner[:mongoid].clean_with :truncation +# Monkey-patch for origin Doorkeeper specs that +# has `resource_owner.id + 1` :( +class BSON::ObjectId + def +(_other) + BSON::ObjectId.new + end +end + RSpec.configure do |config| config.before do Doorkeeper::Application.create_indexes From 4b2d6984fcce6223e937023a443220d734012c4b Mon Sep 17 00:00:00 2001 From: Nikita Bulai Date: Tue, 24 Mar 2020 18:40:50 +0300 Subject: [PATCH 10/17] Update submodule --- doorkeeper | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doorkeeper b/doorkeeper index 6427f0c..c44c7f8 160000 --- a/doorkeeper +++ b/doorkeeper @@ -1 +1 @@ -Subproject commit 6427f0ca9d8072945e11ff04dcf6ba4adb933354 +Subproject commit c44c7f894a79b4673549e4c720f5dd7482b7106c From 39fdd0ec08522cf03662e3b9bb0a9b7007513427 Mon Sep 17 00:00:00 2001 From: Nikita Bulai Date: Tue, 24 Mar 2020 19:46:54 +0300 Subject: [PATCH 11/17] Add Rails 6 --- .travis.yml | 4 ++++ lib/doorkeeper-mongodb/mixins/mongoid/access_grant_mixin.rb | 2 +- lib/doorkeeper-mongodb/mixins/mongoid/access_token_mixin.rb | 2 +- 3 files changed, 6 insertions(+), 2 deletions(-) diff --git a/.travis.yml b/.travis.yml index 07afcf6..b2dc7b5 100644 --- a/.travis.yml +++ b/.travis.yml @@ -15,6 +15,7 @@ env: - RAILS=5.0 - RAILS=5.1 - RAILS=5.2 + - RAILS=6.0 gemfile: - gemfiles/Gemfile.mongoid4.rb @@ -27,6 +28,9 @@ services: matrix: exclude: + - env: RAILS=6.0 + rvm: 2.4 + # Mongoid4 requires activemodel ~> 4.0 - gemfile: gemfiles/Gemfile.mongoid4.rb env: RAILS=5.0 diff --git a/lib/doorkeeper-mongodb/mixins/mongoid/access_grant_mixin.rb b/lib/doorkeeper-mongodb/mixins/mongoid/access_grant_mixin.rb index 003b942..0612800 100644 --- a/lib/doorkeeper-mongodb/mixins/mongoid/access_grant_mixin.rb +++ b/lib/doorkeeper-mongodb/mixins/mongoid/access_grant_mixin.rb @@ -145,7 +145,7 @@ def pkce_supported? # @return [String] token value # def generate_token - return nil unless self[:token].nil? + return if self[:token].present? @raw_token = UniqueToken.generate secret_strategy.store_secret(self, :token, @raw_token) end diff --git a/lib/doorkeeper-mongodb/mixins/mongoid/access_token_mixin.rb b/lib/doorkeeper-mongodb/mixins/mongoid/access_token_mixin.rb index d732288..87c76cc 100644 --- a/lib/doorkeeper-mongodb/mixins/mongoid/access_token_mixin.rb +++ b/lib/doorkeeper-mongodb/mixins/mongoid/access_token_mixin.rb @@ -104,7 +104,7 @@ def by_previous_refresh_token(previous_refresh_token) # # @param application [Doorkeeper::Application] # Application instance - # @param resource_owner_or_id [ActiveRecord::Base, Integer] + # @param resource_owner [Mongoid::Document, Integer] # Resource Owner model instance or it's ID # @param scopes [String, Doorkeeper::OAuth::Scopes] # set of scopes From d5c567750ee76d71884609d10b78ca8a498d10f6 Mon Sep 17 00:00:00 2001 From: Nikita Bulai Date: Tue, 24 Mar 2020 19:49:30 +0300 Subject: [PATCH 12/17] Travis fix --- .travis.yml | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index b2dc7b5..7ac8938 100644 --- a/.travis.yml +++ b/.travis.yml @@ -38,6 +38,8 @@ matrix: env: RAILS=5.1 - gemfile: gemfiles/Gemfile.mongoid4.rb env: RAILS=5.2 + - gemfile: gemfiles/Gemfile.mongoid4.rb + env: RAILS=6.0 # Mongoid5 requires activemodel ~> 4.0 - gemfile: gemfiles/Gemfile.mongoid5.rb @@ -46,7 +48,9 @@ matrix: env: RAILS=5.1 - gemfile: gemfiles/Gemfile.mongoid5.rb env: RAILS=5.2 + - gemfile: gemfiles/Gemfile.mongoid5.rb + env: RAILS=6.0 - # Mongoid7 requires activemodel ~> 5.1 + # Mongoid7 requires activemodel < 6.0.0, >= 5.1 - gemfile: gemfiles/Gemfile.mongoid7.rb env: RAILS=5.0 From 1995640996ad2ab1877f5f1a4c6bdaa4f2e7c244 Mon Sep 17 00:00:00 2001 From: Nikita Bulai Date: Tue, 24 Mar 2020 19:52:35 +0300 Subject: [PATCH 13/17] Travis fix, once again --- .travis.yml | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/.travis.yml b/.travis.yml index 7ac8938..c743394 100644 --- a/.travis.yml +++ b/.travis.yml @@ -51,6 +51,12 @@ matrix: - gemfile: gemfiles/Gemfile.mongoid5.rb env: RAILS=6.0 + # Mongoid6 requires activemodel >= 5.1, < 6.0.0 + - gemfile: gemfiles/Gemfile.mongoid6.rb + env: RAILS=5.0 + - gemfile: gemfiles/Gemfile.mongoid6.rb + env: RAILS=6.0 + # Mongoid7 requires activemodel < 6.0.0, >= 5.1 - gemfile: gemfiles/Gemfile.mongoid7.rb env: RAILS=5.0 From a45ac2f842b3df7e28a7cc93fcc5c6b419740f86 Mon Sep 17 00:00:00 2001 From: Nikita Bulai Date: Tue, 24 Mar 2020 21:04:48 +0300 Subject: [PATCH 14/17] Backport to Doorkeeper 5.2 --- lib/doorkeeper-mongodb.rb | 8 +++ .../mixins/mongoid/access_grant_mixin.rb | 2 +- .../mixins/mongoid/access_token_mixin.rb | 17 +++--- .../concerns/mongoid/resource_ownerable.rb | 52 +++++++++++++++++++ 4 files changed, 69 insertions(+), 10 deletions(-) create mode 100644 lib/doorkeeper/orm/concerns/mongoid/resource_ownerable.rb diff --git a/lib/doorkeeper-mongodb.rb b/lib/doorkeeper-mongodb.rb index a79f1fe..12378af 100644 --- a/lib/doorkeeper-mongodb.rb +++ b/lib/doorkeeper-mongodb.rb @@ -14,6 +14,7 @@ require "doorkeeper-mongodb/shared/scopes" require "doorkeeper/orm/concerns/mongoid/ownership" +require "doorkeeper/orm/concerns/mongoid/resource_ownerable" require "doorkeeper-mongodb/mixins/mongoid/base_mixin" require "doorkeeper-mongodb/mixins/mongoid/access_grant_mixin" @@ -36,6 +37,13 @@ def load_locales end module_function :load_locales + + def doorkeeper_version?(major, minor) + Doorkeeper::VERSION::MAJOR >= major && + Doorkeeper::VERSION::MINOR >= minor + end + + module_function :doorkeeper_version? end DoorkeeperMongodb.load_locales diff --git a/lib/doorkeeper-mongodb/mixins/mongoid/access_grant_mixin.rb b/lib/doorkeeper-mongodb/mixins/mongoid/access_grant_mixin.rb index 0612800..17eac3d 100644 --- a/lib/doorkeeper-mongodb/mixins/mongoid/access_grant_mixin.rb +++ b/lib/doorkeeper-mongodb/mixins/mongoid/access_grant_mixin.rb @@ -12,7 +12,7 @@ module AccessGrantMixin include Doorkeeper::Models::Accessible include Doorkeeper::Models::Scopes include Doorkeeper::Models::SecretStorable - include Doorkeeper::Models::ResourceOwnerable + include Doorkeeper::Orm::Concerns::Mongoid::ResourceOwnerable include BaseMixin included do diff --git a/lib/doorkeeper-mongodb/mixins/mongoid/access_token_mixin.rb b/lib/doorkeeper-mongodb/mixins/mongoid/access_token_mixin.rb index 87c76cc..0713d3e 100644 --- a/lib/doorkeeper-mongodb/mixins/mongoid/access_token_mixin.rb +++ b/lib/doorkeeper-mongodb/mixins/mongoid/access_token_mixin.rb @@ -13,7 +13,7 @@ module AccessTokenMixin include Doorkeeper::Models::Accessible include Doorkeeper::Models::Scopes include Doorkeeper::Models::SecretStorable - include Doorkeeper::Models::ResourceOwnerable + include Doorkeeper::Orm::Concerns::Mongoid::ResourceOwnerable include BaseMixin included do @@ -85,14 +85,13 @@ def by_refresh_token(refresh_token) # # @param application_id [Integer] # ID of the Application - # @param resource_owner [ActiveRecord::Base] + # @param resource_owner [Mongoid::Document] # instance of the Resource Owner model # def revoke_all_for(application_id, resource_owner, clock = Time) - by_resource_owner(resource_owner). - where(application_id: application_id, - revoked_at: nil,). - update_all(revoked_at: clock.now.utc) + by_resource_owner(resource_owner) + .where(application_id: application_id, revoked_at: nil) + .update_all(revoked_at: clock.now.utc) end def by_previous_refresh_token(previous_refresh_token) @@ -204,9 +203,9 @@ def find_or_create_for(application, resource_owner, scopes, expires_in, use_refr # @return [Doorkeeper::AccessToken] array of matching AccessToken objects # def authorized_tokens_for(application_id, resource_owner) - send(order_method, created_at_desc). - by_resource_owner(resource_owner). - where(application_id: application_id, revoked_at: nil) + send(order_method, created_at_desc) + .by_resource_owner(resource_owner) + .where(application_id: application_id, revoked_at: nil) end # Convenience method for backwards-compatibility, return the last diff --git a/lib/doorkeeper/orm/concerns/mongoid/resource_ownerable.rb b/lib/doorkeeper/orm/concerns/mongoid/resource_ownerable.rb new file mode 100644 index 0000000..f1192f3 --- /dev/null +++ b/lib/doorkeeper/orm/concerns/mongoid/resource_ownerable.rb @@ -0,0 +1,52 @@ +# frozen_string_literal: true + +module Doorkeeper + module Orm + module Concerns + module Mongoid + module ResourceOwnerable + extend ActiveSupport::Concern + + module ClassMethods + # Searches for record by Resource Owner considering Doorkeeper + # configuration for resource owner association. + # + # @param resource_owner [Mongoid::Document, Integer] + # resource owner + # + # @return [Doorkeeper::AccessGrant, Doorkeeper::AccessToken] + # collection of records + # + def by_resource_owner(resource_owner) + if DoorkeeperMongodb.doorkeeper_version?(5, 3) && + Doorkeeper.configuration.polymorphic_resource_owner? + where(resource_owner: resource_owner) + else + where(resource_owner_id: resource_owner_id_for(resource_owner)) + end + end + + protected + + # Backward compatible way to retrieve resource owner itself (if + # polymorphic association enabled) or just it's ID. + # + # @param resource_owner [Mongoid::Document, Integer] + # resource owner + # + # @return [ActiveRecord::Base, Integer] + # instance of Resource Owner or it's ID + # + def resource_owner_id_for(resource_owner) + if resource_owner.respond_to?(:to_key) + resource_owner.id + else + resource_owner + end + end + end + end + end + end + end +end From 37b06c7dc1e97f7cd81273cfb973e29f4ef97078 Mon Sep 17 00:00:00 2001 From: Nikita Bulai Date: Tue, 24 Mar 2020 21:44:46 +0300 Subject: [PATCH 15/17] Fix AccessGrant secrets --- Gemfile | 1 + Gemfile.lock | 45 +++++++++++++++++++ .../mixins/mongoid/access_grant_mixin.rb | 22 +++++---- 3 files changed, 60 insertions(+), 8 deletions(-) diff --git a/Gemfile b/Gemfile index 9cdf872..f9c92b5 100755 --- a/Gemfile +++ b/Gemfile @@ -2,6 +2,7 @@ source "https://rubygems.org" +gem "doorkeeper", path: "./doorkeeper" gem "mongoid" gem "rake" gem "rspec" diff --git a/Gemfile.lock b/Gemfile.lock index c923d67..ff9fb21 100755 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -1,6 +1,25 @@ +PATH + remote: doorkeeper + specs: + doorkeeper (5.3.0) + railties (>= 5) + GEM remote: https://rubygems.org/ specs: + actionpack (5.2.4.2) + actionview (= 5.2.4.2) + activesupport (= 5.2.4.2) + rack (~> 2.0, >= 2.0.8) + rack-test (>= 0.6.3) + rails-dom-testing (~> 2.0) + rails-html-sanitizer (~> 1.0, >= 1.0.2) + actionview (5.2.4.2) + activesupport (= 5.2.4.2) + builder (~> 3.1) + erubi (~> 1.4) + rails-dom-testing (~> 2.0) + rails-html-sanitizer (~> 1.0, >= 1.0.3) activemodel (5.2.4.2) activesupport (= 5.2.4.2) activesupport (5.2.4.2) @@ -9,16 +28,40 @@ GEM minitest (~> 5.1) tzinfo (~> 1.1) bson (4.8.2) + builder (3.2.4) concurrent-ruby (1.1.6) + crass (1.0.6) diff-lcs (1.3) + erubi (1.9.0) i18n (1.8.2) concurrent-ruby (~> 1.0) + loofah (2.4.0) + crass (~> 1.0.2) + nokogiri (>= 1.5.9) + method_source (1.0.0) + mini_portile2 (2.4.0) minitest (5.14.0) mongo (2.11.4) bson (>= 4.4.2, < 5.0.0) mongoid (6.4.4) activemodel (>= 5.1, < 6.0.0) mongo (>= 2.5.1, < 3.0.0) + nokogiri (1.10.9) + mini_portile2 (~> 2.4.0) + rack (2.2.2) + rack-test (1.1.0) + rack (>= 1.0, < 3) + rails-dom-testing (2.0.3) + activesupport (>= 4.2.0) + nokogiri (>= 1.6) + rails-html-sanitizer (1.3.0) + loofah (~> 2.3) + railties (5.2.4.2) + actionpack (= 5.2.4.2) + activesupport (= 5.2.4.2) + method_source + rake (>= 0.8.7) + thor (>= 0.19.0, < 2.0) rake (13.0.1) rspec (3.9.0) rspec-core (~> 3.9.0) @@ -33,6 +76,7 @@ GEM diff-lcs (>= 1.2.0, < 2.0) rspec-support (~> 3.9.0) rspec-support (3.9.2) + thor (1.0.1) thread_safe (0.3.6) tzinfo (1.2.6) thread_safe (~> 0.1) @@ -41,6 +85,7 @@ PLATFORMS ruby DEPENDENCIES + doorkeeper! mongoid rake rspec diff --git a/lib/doorkeeper-mongodb/mixins/mongoid/access_grant_mixin.rb b/lib/doorkeeper-mongodb/mixins/mongoid/access_grant_mixin.rb index 17eac3d..0751482 100644 --- a/lib/doorkeeper-mongodb/mixins/mongoid/access_grant_mixin.rb +++ b/lib/doorkeeper-mongodb/mixins/mongoid/access_grant_mixin.rb @@ -55,14 +55,6 @@ def plaintext_token end end - def secret_strategy - ::Doorkeeper.configuration.token_secret_strategy - end - - def fallback_secret_strategy - ::Doorkeeper.configuration.token_secret_fallback_strategy - end - module ClassMethods # Searches for Doorkeeper::AccessGrant record with the # specific token value. @@ -136,6 +128,20 @@ def generate_code_challenge(code_verifier) def pkce_supported? new.pkce_supported? end + + ## + # Determines the secret storing transformer + # Unless configured otherwise, uses the plain secret strategy + def secret_strategy + ::Doorkeeper.config.token_secret_strategy + end + + ## + # Determine the fallback storing strategy + # Unless configured, there will be no fallback + def fallback_secret_strategy + ::Doorkeeper.config.token_secret_fallback_strategy + end end private From cc82ea714dc90baddde611808b907f4810e32466 Mon Sep 17 00:00:00 2001 From: Nikita Bulai Date: Tue, 24 Mar 2020 23:54:32 +0300 Subject: [PATCH 16/17] Update RSpec --- Gemfile | 6 +++++- doorkeeper-mongodb.gemspec | 2 +- gemfiles/Gemfile.common.rb | 6 ++++++ 3 files changed, 12 insertions(+), 2 deletions(-) diff --git a/Gemfile b/Gemfile index f9c92b5..434cfb6 100755 --- a/Gemfile +++ b/Gemfile @@ -5,4 +5,8 @@ source "https://rubygems.org" gem "doorkeeper", path: "./doorkeeper" gem "mongoid" gem "rake" -gem "rspec" +gem "rspec-core", git: "https://github.com/rspec/rspec-core.git" +gem "rspec-expectations", git: "https://github.com/rspec/rspec-expectations.git" +gem "rspec-mocks", git: "https://github.com/rspec/rspec-mocks.git" +gem "rspec-rails", "4.0.0.beta3" +gem "rspec-support", git: "https://github.com/rspec/rspec-support.git" diff --git a/doorkeeper-mongodb.gemspec b/doorkeeper-mongodb.gemspec index 40b04ef..e3ce68d 100644 --- a/doorkeeper-mongodb.gemspec +++ b/doorkeeper-mongodb.gemspec @@ -24,7 +24,7 @@ Gem::Specification.new do |gem| gem.add_development_dependency "grape" gem.add_development_dependency "coveralls" gem.add_development_dependency "sqlite3", "~> 1.3.5" - gem.add_development_dependency "rspec-rails", "~> 3.7" + gem.add_development_dependency "rspec-rails" gem.add_development_dependency "capybara", "~> 2.17" gem.add_development_dependency "generator_spec", "~> 0.9.4" gem.add_development_dependency "factory_bot", "~> 4.8" diff --git a/gemfiles/Gemfile.common.rb b/gemfiles/Gemfile.common.rb index 4f4533b..65c1133 100644 --- a/gemfiles/Gemfile.common.rb +++ b/gemfiles/Gemfile.common.rb @@ -11,6 +11,12 @@ gem "doorkeeper", "~> #{ENV['DOORKEEPER']}" gem "bcrypt" +gem "rspec-core", git: "https://github.com/rspec/rspec-core.git" +gem "rspec-expectations", git: "https://github.com/rspec/rspec-expectations.git" +gem "rspec-mocks", git: "https://github.com/rspec/rspec-mocks.git" +gem "rspec-rails", "4.0.0.rc1" +gem "rspec-support", git: "https://github.com/rspec/rspec-support.git" + # Older Grape requires Ruby >= 2.2.2 if ENV["RAILS"][0] == "4" gem "grape", "~> 0.16", "< 0.19.2" From febfced32eb8a5f00ca973c72ea9bf64970923bb Mon Sep 17 00:00:00 2001 From: Nikita Bulai Date: Wed, 25 Mar 2020 00:22:59 +0300 Subject: [PATCH 17/17] Update rubocop rules, small changes --- Gemfile.lock | 56 ++++++++++++++----- doorkeeper-mongodb.gemspec | 13 ++--- gemfiles/Gemfile.common.rb | 8 +-- gemfiles/Gemfile.mongoid4.rb | 2 +- gemfiles/Gemfile.mongoid5.rb | 2 +- gemfiles/Gemfile.mongoid6.rb | 2 +- gemfiles/Gemfile.mongoid7.rb | 2 +- lib/doorkeeper-mongodb.rb | 2 +- .../mixins/mongoid/access_grant_mixin.rb | 7 ++- .../mixins/mongoid/access_token_mixin.rb | 34 +++++------ .../mixins/mongoid/application_mixin.rb | 9 ++- .../concerns/mongoid/resource_ownerable.rb | 2 +- lib/doorkeeper/orm/mongoid4/application.rb | 10 +++- lib/doorkeeper/orm/mongoid5/application.rb | 10 +++- lib/doorkeeper/orm/mongoid6/application.rb | 10 +++- lib/doorkeeper/orm/mongoid7/application.rb | 10 +++- 16 files changed, 121 insertions(+), 58 deletions(-) diff --git a/Gemfile.lock b/Gemfile.lock index ff9fb21..e81a245 100755 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -1,3 +1,32 @@ +GIT + remote: https://github.com/rspec/rspec-core.git + revision: 002ebcb65324bca7b3ee044e22f4c02133fce14d + specs: + rspec-core (3.10.0.pre) + rspec-support (= 3.10.0.pre) + +GIT + remote: https://github.com/rspec/rspec-expectations.git + revision: 9f224eaeb5c6d7eaaab0f7f6e355efd9329328f8 + specs: + rspec-expectations (3.10.0.pre) + diff-lcs (>= 1.2.0, < 2.0) + rspec-support (= 3.10.0.pre) + +GIT + remote: https://github.com/rspec/rspec-mocks.git + revision: 81c17e89b2722cb225d602e08b600e4a4f6148a4 + specs: + rspec-mocks (3.10.0.pre) + diff-lcs (>= 1.2.0, < 2.0) + rspec-support (= 3.10.0.pre) + +GIT + remote: https://github.com/rspec/rspec-support.git + revision: 63c03e5e6f0ac858398933f820d37087f57c315a + specs: + rspec-support (3.10.0.pre) + PATH remote: doorkeeper specs: @@ -63,19 +92,14 @@ GEM rake (>= 0.8.7) thor (>= 0.19.0, < 2.0) rake (13.0.1) - rspec (3.9.0) - rspec-core (~> 3.9.0) - rspec-expectations (~> 3.9.0) - rspec-mocks (~> 3.9.0) - rspec-core (3.9.1) - rspec-support (~> 3.9.1) - rspec-expectations (3.9.1) - diff-lcs (>= 1.2.0, < 2.0) - rspec-support (~> 3.9.0) - rspec-mocks (3.9.1) - diff-lcs (>= 1.2.0, < 2.0) - rspec-support (~> 3.9.0) - rspec-support (3.9.2) + rspec-rails (4.0.0.beta3) + actionpack (>= 4.2) + activesupport (>= 4.2) + railties (>= 4.2) + rspec-core (~> 3.8) + rspec-expectations (~> 3.8) + rspec-mocks (~> 3.8) + rspec-support (~> 3.8) thor (1.0.1) thread_safe (0.3.6) tzinfo (1.2.6) @@ -88,7 +112,11 @@ DEPENDENCIES doorkeeper! mongoid rake - rspec + rspec-core! + rspec-expectations! + rspec-mocks! + rspec-rails (= 4.0.0.beta3) + rspec-support! BUNDLED WITH 1.17.3 diff --git a/doorkeeper-mongodb.gemspec b/doorkeeper-mongodb.gemspec index e3ce68d..7681f7e 100644 --- a/doorkeeper-mongodb.gemspec +++ b/doorkeeper-mongodb.gemspec @@ -1,6 +1,6 @@ # frozen_string_literal: true -$:.push File.expand_path("../lib", __FILE__) +$LOAD_PATH.push File.expand_path("lib", __dir__) # Maintain your gem's version: require "doorkeeper-mongodb/version" @@ -21,12 +21,11 @@ Gem::Specification.new do |gem| gem.add_dependency "doorkeeper", ">= 5.2", "< 6.0" - gem.add_development_dependency "grape" - gem.add_development_dependency "coveralls" - gem.add_development_dependency "sqlite3", "~> 1.3.5" - gem.add_development_dependency "rspec-rails" gem.add_development_dependency "capybara", "~> 2.17" - gem.add_development_dependency "generator_spec", "~> 0.9.4" - gem.add_development_dependency "factory_bot", "~> 4.8" + gem.add_development_dependency "coveralls" gem.add_development_dependency "database_cleaner", "~> 1.6.0" + gem.add_development_dependency "factory_bot", "~> 4.8" + gem.add_development_dependency "generator_spec", "~> 0.9.4" + gem.add_development_dependency "grape" + gem.add_development_dependency "rspec-rails" end diff --git a/gemfiles/Gemfile.common.rb b/gemfiles/Gemfile.common.rb index 65c1133..f8b8deb 100644 --- a/gemfiles/Gemfile.common.rb +++ b/gemfiles/Gemfile.common.rb @@ -7,8 +7,8 @@ gemspec path: "../" -gem "rails", "~> #{ENV['RAILS']}" -gem "doorkeeper", "~> #{ENV['DOORKEEPER']}" +gem "rails", "~> #{ENV["RAILS"]}" +gem "doorkeeper", "~> #{ENV["DOORKEEPER"]}" gem "bcrypt" gem "rspec-core", git: "https://github.com/rspec/rspec-core.git" @@ -18,6 +18,4 @@ gem "rspec-support", git: "https://github.com/rspec/rspec-support.git" # Older Grape requires Ruby >= 2.2.2 -if ENV["RAILS"][0] == "4" - gem "grape", "~> 0.16", "< 0.19.2" -end +gem "grape", "~> 0.16", "< 0.19.2" if ENV["RAILS"][0] == "4" diff --git a/gemfiles/Gemfile.mongoid4.rb b/gemfiles/Gemfile.mongoid4.rb index 17f636b..1e30657 100644 --- a/gemfiles/Gemfile.mongoid4.rb +++ b/gemfiles/Gemfile.mongoid4.rb @@ -1,6 +1,6 @@ # frozen_string_literal: true -gemfile = File.expand_path("../Gemfile.common.rb", __FILE__) +gemfile = File.expand_path("Gemfile.common.rb", __dir__) instance_eval IO.read(gemfile), gemfile gem "mongoid", "~> 4" diff --git a/gemfiles/Gemfile.mongoid5.rb b/gemfiles/Gemfile.mongoid5.rb index acecddf..48bc4cc 100644 --- a/gemfiles/Gemfile.mongoid5.rb +++ b/gemfiles/Gemfile.mongoid5.rb @@ -1,6 +1,6 @@ # frozen_string_literal: true -gemfile = File.expand_path("../Gemfile.common.rb", __FILE__) +gemfile = File.expand_path("Gemfile.common.rb", __dir__) instance_eval IO.read(gemfile), gemfile gem "mongoid", "~> 5" diff --git a/gemfiles/Gemfile.mongoid6.rb b/gemfiles/Gemfile.mongoid6.rb index 67e73fd..bd320f4 100644 --- a/gemfiles/Gemfile.mongoid6.rb +++ b/gemfiles/Gemfile.mongoid6.rb @@ -1,6 +1,6 @@ # frozen_string_literal: true -gemfile = File.expand_path("../Gemfile.common.rb", __FILE__) +gemfile = File.expand_path("Gemfile.common.rb", __dir__) instance_eval IO.read(gemfile), gemfile gem "mongoid", "~> 6" diff --git a/gemfiles/Gemfile.mongoid7.rb b/gemfiles/Gemfile.mongoid7.rb index 8e5421e..7268ce1 100644 --- a/gemfiles/Gemfile.mongoid7.rb +++ b/gemfiles/Gemfile.mongoid7.rb @@ -1,6 +1,6 @@ # frozen_string_literal: true -gemfile = File.expand_path("../Gemfile.common.rb", __FILE__) +gemfile = File.expand_path("Gemfile.common.rb", __dir__) instance_eval IO.read(gemfile), gemfile gem "mongoid", "~> 7.0" diff --git a/lib/doorkeeper-mongodb.rb b/lib/doorkeeper-mongodb.rb index 12378af..da86129 100644 --- a/lib/doorkeeper-mongodb.rb +++ b/lib/doorkeeper-mongodb.rb @@ -30,7 +30,7 @@ module DoorkeeperMongodb def load_locales - locales_dir = File.expand_path("../../config/locales", __FILE__) + locales_dir = File.expand_path("../config/locales", __dir__) locales = Dir[File.join(locales_dir, "*.yml")] I18n.load_path |= locales diff --git a/lib/doorkeeper-mongodb/mixins/mongoid/access_grant_mixin.rb b/lib/doorkeeper-mongodb/mixins/mongoid/access_grant_mixin.rb index 0751482..092de5f 100644 --- a/lib/doorkeeper-mongodb/mixins/mongoid/access_grant_mixin.rb +++ b/lib/doorkeeper-mongodb/mixins/mongoid/access_grant_mixin.rb @@ -18,9 +18,13 @@ module AccessGrantMixin included do belongs_to_opts = { class_name: "Doorkeeper::Application", - inverse_of: :access_grants + inverse_of: :access_grants, } + if DoorkeeperMongodb.doorkeeper_version?(5, 3) + belongs_to_opts[:class_name] = Doorkeeper.config.application_class + end + # optional associations added in Mongoid 6 if ::Mongoid::VERSION[0].to_i >= 6 belongs_to_opts[:optional] = true @@ -152,6 +156,7 @@ def fallback_secret_strategy # def generate_token return if self[:token].present? + @raw_token = UniqueToken.generate secret_strategy.store_secret(self, :token, @raw_token) end diff --git a/lib/doorkeeper-mongodb/mixins/mongoid/access_token_mixin.rb b/lib/doorkeeper-mongodb/mixins/mongoid/access_token_mixin.rb index 0713d3e..d7d459d 100644 --- a/lib/doorkeeper-mongodb/mixins/mongoid/access_token_mixin.rb +++ b/lib/doorkeeper-mongodb/mixins/mongoid/access_token_mixin.rb @@ -19,9 +19,13 @@ module AccessTokenMixin included do belongs_to_opts = { class_name: "Doorkeeper::Application", - inverse_of: :access_tokens + inverse_of: :access_tokens, } + if DoorkeeperMongodb.doorkeeper_version?(5, 3) + belongs_to_opts[:class_name] = Doorkeeper.config.application_class + end + # optional associations added in Mongoid 6 if ::Mongoid::VERSION[0].to_i >= 6 belongs_to_opts[:optional] = true @@ -66,7 +70,6 @@ def by_token(token) find_by_plaintext_token(:token, token) end - # Returns an instance of the Doorkeeper::AccessToken # with specific token value. # @@ -116,7 +119,7 @@ def matching_token_for(application, resource_owner, scopes) find_matching_token(tokens, application, scopes) end - def find_access_token_in_batches(relation, *args, &block) + def find_access_token_in_batches(relation, *_args, &block) relation.all.each(&block) end @@ -177,10 +180,10 @@ def find_or_create_for(application, resource_owner, scopes, expires_in, use_refr end attributes = { - application_id: application.try(:id), - scopes: scopes.to_s, - expires_in: expires_in, - use_refresh_token: use_refresh_token + application_id: application.try(:id), + scopes: scopes.to_s, + expires_in: expires_in, + use_refresh_token: use_refresh_token, } if Doorkeeper::VERSION::MINOR > 3 && Doorkeeper.config.polymorphic_resource_owner? @@ -232,7 +235,6 @@ def fallback_secret_strategy end end - # Access Token type: Bearer. # @see https://tools.ietf.org/html/rfc6750 # The OAuth 2.0 Authorization Framework: Bearer Token Usage @@ -252,10 +254,10 @@ def use_refresh_token? def as_json(_options = {}) { resource_owner_id: resource_owner_id, - scope: scopes, - expires_in: expires_in_seconds, - application: { uid: application.try(:uid) }, - created_at: created_at.to_i + scope: scopes, + expires_in: expires_in_seconds, + application: { uid: application.try(:uid) }, + created_at: created_at.to_i, } end @@ -361,10 +363,10 @@ def generate_token @raw_token = token_generator.generate( resource_owner_id: resource_owner_id, - scopes: scopes, - application: application, - expires_in: expires_in, - created_at: created_at, + scopes: scopes, + application: application, + expires_in: expires_in, + created_at: created_at, ) secret_strategy.store_secret(self, :token, @raw_token) @raw_token diff --git a/lib/doorkeeper-mongodb/mixins/mongoid/application_mixin.rb b/lib/doorkeeper-mongodb/mixins/mongoid/application_mixin.rb index 84d73e3..2e3bd5b 100644 --- a/lib/doorkeeper-mongodb/mixins/mongoid/application_mixin.rb +++ b/lib/doorkeeper-mongodb/mixins/mongoid/application_mixin.rb @@ -13,7 +13,7 @@ module ApplicationMixin included do has_many_options = { - dependent: :delete + dependent: :delete, } # Mongoid7 dropped :delete option @@ -52,6 +52,7 @@ def by_uid_and_secret(uid, secret) return unless app return app if secret.blank? && !app.confidential? return unless app.secret_matches?(secret) + app end @@ -142,9 +143,7 @@ def authorized_for_resource_owner?(resource_owner) private def generate_uid - if uid.blank? - self.uid = UniqueToken.generate - end + self.uid = UniqueToken.generate if uid.blank? end def generate_secret @@ -156,7 +155,7 @@ def generate_secret def scopes_match_configured if scopes.present? && - !ScopeChecker.valid?(scope_str: scopes.to_s, server_scopes: Doorkeeper.configuration.scopes) + !ScopeChecker.valid?(scope_str: scopes.to_s, server_scopes: Doorkeeper.configuration.scopes) errors.add(:scopes, :not_match_configured) end end diff --git a/lib/doorkeeper/orm/concerns/mongoid/resource_ownerable.rb b/lib/doorkeeper/orm/concerns/mongoid/resource_ownerable.rb index f1192f3..a933315 100644 --- a/lib/doorkeeper/orm/concerns/mongoid/resource_ownerable.rb +++ b/lib/doorkeeper/orm/concerns/mongoid/resource_ownerable.rb @@ -19,7 +19,7 @@ module ClassMethods # def by_resource_owner(resource_owner) if DoorkeeperMongodb.doorkeeper_version?(5, 3) && - Doorkeeper.configuration.polymorphic_resource_owner? + Doorkeeper.configuration.polymorphic_resource_owner? where(resource_owner: resource_owner) else where(resource_owner_id: resource_owner_id_for(resource_owner)) diff --git a/lib/doorkeeper/orm/mongoid4/application.rb b/lib/doorkeeper/orm/mongoid4/application.rb index a74496f..f6f5c12 100644 --- a/lib/doorkeeper/orm/mongoid4/application.rb +++ b/lib/doorkeeper/orm/mongoid4/application.rb @@ -20,7 +20,15 @@ class Application index({ uid: 1 }, unique: true) - has_many :authorized_tokens, class_name: "Doorkeeper::AccessToken" + has_many_opts = { + class_name: "Doorkeeper::AccessToken", + } + + if DoorkeeperMongodb.doorkeeper_version?(5, 3) + has_many_opts[:class_name] = Doorkeeper.config.access_token_class + end + + has_many :authorized_tokens, has_many_opts def self.authorized_for(resource_owner) ids = AccessToken.where( diff --git a/lib/doorkeeper/orm/mongoid5/application.rb b/lib/doorkeeper/orm/mongoid5/application.rb index a74496f..f6f5c12 100644 --- a/lib/doorkeeper/orm/mongoid5/application.rb +++ b/lib/doorkeeper/orm/mongoid5/application.rb @@ -20,7 +20,15 @@ class Application index({ uid: 1 }, unique: true) - has_many :authorized_tokens, class_name: "Doorkeeper::AccessToken" + has_many_opts = { + class_name: "Doorkeeper::AccessToken", + } + + if DoorkeeperMongodb.doorkeeper_version?(5, 3) + has_many_opts[:class_name] = Doorkeeper.config.access_token_class + end + + has_many :authorized_tokens, has_many_opts def self.authorized_for(resource_owner) ids = AccessToken.where( diff --git a/lib/doorkeeper/orm/mongoid6/application.rb b/lib/doorkeeper/orm/mongoid6/application.rb index a74496f..f6f5c12 100644 --- a/lib/doorkeeper/orm/mongoid6/application.rb +++ b/lib/doorkeeper/orm/mongoid6/application.rb @@ -20,7 +20,15 @@ class Application index({ uid: 1 }, unique: true) - has_many :authorized_tokens, class_name: "Doorkeeper::AccessToken" + has_many_opts = { + class_name: "Doorkeeper::AccessToken", + } + + if DoorkeeperMongodb.doorkeeper_version?(5, 3) + has_many_opts[:class_name] = Doorkeeper.config.access_token_class + end + + has_many :authorized_tokens, has_many_opts def self.authorized_for(resource_owner) ids = AccessToken.where( diff --git a/lib/doorkeeper/orm/mongoid7/application.rb b/lib/doorkeeper/orm/mongoid7/application.rb index a74496f..f6f5c12 100644 --- a/lib/doorkeeper/orm/mongoid7/application.rb +++ b/lib/doorkeeper/orm/mongoid7/application.rb @@ -20,7 +20,15 @@ class Application index({ uid: 1 }, unique: true) - has_many :authorized_tokens, class_name: "Doorkeeper::AccessToken" + has_many_opts = { + class_name: "Doorkeeper::AccessToken", + } + + if DoorkeeperMongodb.doorkeeper_version?(5, 3) + has_many_opts[:class_name] = Doorkeeper.config.access_token_class + end + + has_many :authorized_tokens, has_many_opts def self.authorized_for(resource_owner) ids = AccessToken.where(