Skip to content

Commit

Permalink
Merge pull request #1599 from saiqulhaq/refactor-mixin
Browse files Browse the repository at this point in the history
fix: remove unnecessarily refetches application object
  • Loading branch information
nbulaj authored Nov 28, 2022
2 parents 934ac41 + ecec9b6 commit cb70561
Show file tree
Hide file tree
Showing 6 changed files with 10 additions and 6 deletions.
2 changes: 1 addition & 1 deletion lib/doorkeeper/models/access_token_mixin.rb
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ def find_or_create_for(application:, resource_owner:, scopes:, **token_attribute
# @return [Doorkeeper::AccessToken] new access token
#
def create_for(application:, resource_owner:, scopes:, **token_attributes)
token_attributes[:application_id] = application&.id
token_attributes[:application] = application
token_attributes[:scopes] = scopes.to_s

if Doorkeeper.config.polymorphic_resource_owner?
Expand Down
3 changes: 2 additions & 1 deletion lib/doorkeeper/oauth/authorization/token.rb
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,9 @@ def issue_token!
resource_owner,
)

application = pre_auth.client.is_a?(Doorkeeper::Application) ? pre_auth.client : pre_auth.client.application if pre_auth.client
@token = Doorkeeper.config.access_token_model.find_or_create_for(
application: pre_auth.client,
application: application,
resource_owner: resource_owner,
scopes: pre_auth.scopes,
expires_in: self.class.access_token_expires_in(Doorkeeper.config, context),
Expand Down
3 changes: 2 additions & 1 deletion lib/doorkeeper/oauth/base_request.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,9 @@ def scopes

def find_or_create_access_token(client, resource_owner, scopes, server)
context = Authorization::Token.build_context(client, grant_type, scopes, resource_owner)
application = client.is_a?(Doorkeeper::Application) ? client : client.application if client
@access_token = server_config.access_token_model.find_or_create_for(
application: client,
application: application,
resource_owner: resource_owner,
scopes: scopes,
expires_in: Authorization::Token.access_token_expires_in(server, context),
Expand Down
3 changes: 2 additions & 1 deletion lib/doorkeeper/oauth/client_credentials/creator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,9 @@ def call(client, scopes, attributes = {})
end

with_revocation(existing_token: existing_token) do
application = client.is_a?(Doorkeeper::Application) ? client : client.application if client
server_config.access_token_model.create_for(
application: client,
application: application,
resource_owner: nil,
scopes: scopes,
**attributes,
Expand Down
2 changes: 1 addition & 1 deletion spec/lib/oauth/base_request_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
created_at: 0
end

let(:client) { double :client, id: "1" }
let(:client) { Doorkeeper::Application.new(id: "1") }

let(:scopes_array) { %w[public write] }

Expand Down
3 changes: 2 additions & 1 deletion spec/lib/oauth/code_response_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@

RSpec.describe Doorkeeper::OAuth::CodeResponse do
let(:pre_auth) do
application = FactoryBot.create(:application, scopes: "")
double(
:pre_auth,
client: double(:application, id: 1),
client: application,
redirect_uri: "http://tst.com/cb",
state: "state",
scopes: Doorkeeper::OAuth::Scopes.from_string("public"),
Expand Down

0 comments on commit cb70561

Please sign in to comment.