Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: remove unnecessarily refetches application object #1599

Merged
merged 1 commit into from
Nov 28, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Copy link

@sealabcore sealabcore Nov 28, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Doing this breaks apps that are using Custom application classes as defined in doorkeeper.rb

application_class "MyCustomApplicationClass"

And defined like so

# frozen_string_literal: true

class MyCustomApplicationClass < ApplicationRecord
  include ::Doorkeeper::Orm::ActiveRecord::Mixins::Application
end

@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