Skip to content

Commit

Permalink
Add support for Doorkeeper 5.4 #create_for method
Browse files Browse the repository at this point in the history
  • Loading branch information
nbulaj committed Apr 1, 2020
1 parent de27d3c commit d9426a5
Show file tree
Hide file tree
Showing 4 changed files with 139 additions and 9 deletions.
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
source "https://rubygems.org"

gem "doorkeeper", path: "./doorkeeper"
gemspec
gem "mongoid"
gem "rake"
gem "rspec-core"
Expand Down
86 changes: 86 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
PATH
remote: .
specs:
doorkeeper-mongodb (5.2.1)
doorkeeper (>= 5.2, < 6.0)

PATH
remote: doorkeeper
specs:
Expand Down Expand Up @@ -27,28 +33,88 @@ 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)
bson (>= 4.4.2, < 5.0.0)
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)
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion doorkeeper
Submodule doorkeeper updated 48 files
+1 −1 .hound.yml
+15 −2 .rubocop.yml
+0 −14 Appraisals
+2 −1 CHANGELOG.md
+9 −9 Gemfile
+24 −9 app/controllers/doorkeeper/authorizations_controller.rb
+10 −5 app/controllers/doorkeeper/tokens_controller.rb
+8 −7 gemfiles/rails_5_0.gemfile
+8 −7 gemfiles/rails_5_1.gemfile
+8 −7 gemfiles/rails_5_2.gemfile
+8 −7 gemfiles/rails_6_0.gemfile
+8 −7 gemfiles/rails_master.gemfile
+2 −2 lib/doorkeeper.rb
+2 −2 lib/doorkeeper/config.rb
+1 −1 lib/doorkeeper/grape/helpers.rb
+2 −6 lib/doorkeeper/models/access_grant_mixin.rb
+39 −14 lib/doorkeeper/models/access_token_mixin.rb
+1 −1 lib/doorkeeper/models/concerns/revocable.rb
+4 −2 lib/doorkeeper/oauth/authorization/code.rb
+7 −11 lib/doorkeeper/oauth/authorization/token.rb
+4 −4 lib/doorkeeper/oauth/authorization/uri_builder.rb
+8 −5 lib/doorkeeper/oauth/authorization_code_request.rb
+10 −12 lib/doorkeeper/oauth/base_request.rb
+1 −1 lib/doorkeeper/oauth/client.rb
+4 −2 lib/doorkeeper/oauth/client_credentials/creator.rb
+2 −1 lib/doorkeeper/oauth/client_credentials/issuer.rb
+5 −7 lib/doorkeeper/oauth/client_credentials_request.rb
+1 −1 lib/doorkeeper/oauth/code_request.rb
+6 −2 lib/doorkeeper/oauth/code_response.rb
+1 −5 lib/doorkeeper/oauth/helpers/scope_checker.rb
+21 −0 lib/doorkeeper/oauth/hooks/context.rb
+2 −2 lib/doorkeeper/oauth/invalid_token_response.rb
+1 −2 lib/doorkeeper/oauth/password_access_token_request.rb
+19 −21 lib/doorkeeper/oauth/refresh_token_request.rb
+1 −1 lib/doorkeeper/oauth/token_request.rb
+1 −1 lib/doorkeeper/oauth/token_response.rb
+2 −2 lib/doorkeeper/request/strategy.rb
+3 −3 lib/doorkeeper/server.rb
+11 −3 lib/generators/doorkeeper/templates/initializer.rb
+8 −4 spec/controllers/authorizations_controller_spec.rb
+4 −3 spec/controllers/tokens_controller_spec.rb
+1 −1 spec/dummy/app/helpers/application_helper.rb
+2 −3 spec/lib/models/revocable_spec.rb
+9 −8 spec/lib/oauth/authorization_code_request_spec.rb
+5 −4 spec/lib/oauth/client_credentials_request_spec.rb
+5 −9 spec/lib/oauth/password_access_token_request_spec.rb
+16 −13 spec/lib/oauth/refresh_token_request_spec.rb
+10 −10 spec/requests/flows/revoke_token_spec.rb
59 changes: 51 additions & 8 deletions lib/doorkeeper-mongodb/mixins/mongoid/access_token_mixin.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit d9426a5

Please sign in to comment.