From d9426a5d7ad3143a59082cb04f64f9b9323a920e Mon Sep 17 00:00:00 2001 From: Nikita Bulai Date: Wed, 1 Apr 2020 10:36:12 +0300 Subject: [PATCH] Add support for Doorkeeper 5.4 #create_for method --- Gemfile | 1 + Gemfile.lock | 86 +++++++++++++++++++ doorkeeper | 2 +- .../mixins/mongoid/access_token_mixin.rb | 59 +++++++++++-- 4 files changed, 139 insertions(+), 9 deletions(-) diff --git a/Gemfile b/Gemfile index 1b28e32..b7bd4fc 100755 --- a/Gemfile +++ b/Gemfile @@ -3,6 +3,7 @@ source "https://rubygems.org" gem "doorkeeper", path: "./doorkeeper" +gemspec gem "mongoid" gem "rake" gem "rspec-core" diff --git a/Gemfile.lock b/Gemfile.lock index 05690c6..38edb49 100755 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -1,3 +1,9 @@ +PATH + remote: . + specs: + doorkeeper-mongodb (5.2.1) + doorkeeper (>= 5.2, < 6.0) + PATH remote: doorkeeper specs: @@ -27,18 +33,71 @@ GEM i18n (>= 0.7, < 2) minitest (~> 5.1) tzinfo (~> 1.1) + addressable (2.7.0) + public_suffix (>= 2.0.2, < 5.0) bson (4.8.2) builder (3.2.4) + capybara (2.18.0) + addressable + mini_mime (>= 0.1.3) + nokogiri (>= 1.3.3) + rack (>= 1.0.0) + rack-test (>= 0.5.4) + xpath (>= 2.0, < 4.0) concurrent-ruby (1.1.6) + coveralls (0.8.23) + json (>= 1.8, < 3) + simplecov (~> 0.16.1) + term-ansicolor (~> 1.3) + thor (>= 0.19.4, < 2.0) + tins (~> 1.6) crass (1.0.6) + database_cleaner (1.6.2) diff-lcs (1.3) + docile (1.3.2) + dry-configurable (0.11.5) + concurrent-ruby (~> 1.0) + dry-core (~> 0.4, >= 0.4.7) + dry-equalizer (~> 0.2) + dry-container (0.7.2) + concurrent-ruby (~> 1.0) + dry-configurable (~> 0.1, >= 0.1.3) + dry-core (0.4.9) + concurrent-ruby (~> 1.0) + dry-equalizer (0.3.0) + dry-inflector (0.2.0) + dry-logic (1.0.6) + concurrent-ruby (~> 1.0) + dry-core (~> 0.2) + dry-equalizer (~> 0.2) + dry-types (1.4.0) + concurrent-ruby (~> 1.0) + dry-container (~> 0.3) + dry-core (~> 0.4, >= 0.4.4) + dry-equalizer (~> 0.3) + dry-inflector (~> 0.1, >= 0.1.2) + dry-logic (~> 1.0, >= 1.0.2) erubi (1.9.0) + factory_bot (4.11.1) + activesupport (>= 3.0.0) + generator_spec (0.9.4) + activesupport (>= 3.0.0) + railties (>= 3.0.0) + grape (1.3.1) + activesupport + builder + dry-types (>= 1.1) + mustermann-grape (~> 1.0.0) + rack (>= 1.3.0) + rack-accept i18n (1.8.2) concurrent-ruby (~> 1.0) + json (2.3.0) loofah (2.4.0) crass (~> 1.0.2) nokogiri (>= 1.5.9) method_source (1.0.0) + mini_mime (1.0.2) mini_portile2 (2.4.0) minitest (5.14.0) mongo (2.11.4) @@ -46,9 +105,16 @@ GEM mongoid (6.4.4) activemodel (>= 5.1, < 6.0.0) mongo (>= 2.5.1, < 3.0.0) + mustermann (1.1.1) + ruby2_keywords (~> 0.0.1) + mustermann-grape (1.0.1) + mustermann (>= 1.0.0) nokogiri (1.10.9) mini_portile2 (~> 2.4.0) + public_suffix (4.0.3) rack (2.2.2) + rack-accept (0.4.5) + rack (>= 0.4) rack-test (1.1.0) rack (>= 1.0, < 3) rails-dom-testing (2.0.3) @@ -80,16 +146,36 @@ GEM rspec-mocks (~> 3.9) rspec-support (~> 3.9) rspec-support (3.9.2) + ruby2_keywords (0.0.2) + simplecov (0.16.1) + docile (~> 1.1) + json (>= 1.8, < 3) + simplecov-html (~> 0.10.0) + simplecov-html (0.10.2) + sync (0.5.0) + term-ansicolor (1.7.1) + tins (~> 1.0) thor (1.0.1) thread_safe (0.3.6) + tins (1.24.1) + sync tzinfo (1.2.6) thread_safe (~> 0.1) + xpath (3.2.0) + nokogiri (~> 1.8) PLATFORMS ruby DEPENDENCIES + capybara (~> 2.17) + coveralls + database_cleaner (~> 1.6.0) doorkeeper! + doorkeeper-mongodb! + factory_bot (~> 4.8) + generator_spec (~> 0.9.4) + grape mongoid rake rspec-core diff --git a/doorkeeper b/doorkeeper index c44c7f8..ac40a8d 160000 --- a/doorkeeper +++ b/doorkeeper @@ -1 +1 @@ -Subproject commit c44c7f894a79b4673549e4c720f5dd7482b7106c +Subproject commit ac40a8dacd9581b994b63e5787d837c556259df6 diff --git a/lib/doorkeeper-mongodb/mixins/mongoid/access_token_mixin.rb b/lib/doorkeeper-mongodb/mixins/mongoid/access_token_mixin.rb index 10eaed1..f58f8ab 100644 --- a/lib/doorkeeper-mongodb/mixins/mongoid/access_token_mixin.rb +++ b/lib/doorkeeper-mongodb/mixins/mongoid/access_token_mixin.rb @@ -169,27 +169,70 @@ 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, scopes, expires_in, use_refresh_token) + def find_or_create_for(*args) + # [NOTE]: For backward compatibility with Doorkeeper < 5.4 + attributes = if args.size > 1 + { + application: args[0], + resource_owner: args[1], + scopes: args[2], + expires_in: args[3], + use_refresh_token: args[4], + } + else + args.first + end + + application = attributes[:application] + resource_owner = attributes[:resource_owner] + scopes = attributes[:scopes] + expires_in = attributes[:expires_in] + use_refresh_token = attributes[:use_refresh_token] + if Doorkeeper.configuration.reuse_access_token access_token = matching_token_for(application, resource_owner, scopes) return access_token if access_token&.reusable? end - attributes = { - application_id: application.try(:id), - scopes: scopes.to_s, + create_for( + application: application, + resource_owner: resource_owner, + scopes: scopes, expires_in: expires_in, use_refresh_token: use_refresh_token, - } + ) + end + + # Creates a not expired AccessToken record with a matching set of + # scopes that belongs to specific Application and Resource Owner. + # + # @param application [Doorkeeper::Application] + # Application instance + # @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`) + # @param token_attributes [Hash] + # Additional attributes to use when creating a token + # @option token_attributes [Integer] :expires_in + # token lifetime in seconds + # @option token_attributes [Boolean] :use_refresh_token + # whether to use the refresh token + # + # @return [Doorkeeper::AccessToken] new access token + # + def create_for(application:, resource_owner:, scopes:, **token_attributes) + token_attributes[:application_id] = application&.id + token_attributes[:scopes] = scopes.to_s if Doorkeeper.configuration.try(:polymorphic_resource_owner?) - attributes[:resource_owner] = resource_owner + token_attributes[:resource_owner] = resource_owner else - attributes[:resource_owner_id] = resource_owner_id_for(resource_owner) + token_attributes[:resource_owner_id] = resource_owner_id_for(resource_owner) end - create!(**attributes) + create!(token_attributes) end # Looking for not revoked Access Token records that belongs to specific