Skip to content

Commit

Permalink
Added config option revoke_previous_authorization_code_token.
Browse files Browse the repository at this point in the history
  • Loading branch information
JeremyC-za committed May 19, 2023
1 parent e2bbbfc commit e29b063
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 0 deletions.
11 changes: 11 additions & 0 deletions lib/doorkeeper/config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,13 @@ def revoke_previous_client_credentials_token
@config.instance_variable_set(:@revoke_previous_client_credentials_token, true)
end

# Only allow one valid access token obtained via authorization code
# per client. If a new access token is obtained before the old one
# expired, the old one gets revoked (disabled by default)
def revoke_previous_authorization_code_token
@config.instance_variable_set(:@revoke_previous_authorization_code_token, true)
end

# Use an API mode for applications generated with --api argument
# It will skip applications controller, disable forgery protection
def api_only
Expand Down Expand Up @@ -481,6 +488,10 @@ def revoke_previous_client_credentials_token?
option_set? :revoke_previous_client_credentials_token
end

def revoke_previous_authorization_code_token?
option_set? :revoke_previous_authorization_code_token
end

def enforce_configured_scopes?
option_set? :enforce_configured_scopes
end
Expand Down
8 changes: 8 additions & 0 deletions lib/doorkeeper/oauth/authorization_code_request.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ def before_successful_response
grant.lock!
raise Errors::InvalidGrantReuse if grant.revoked?

if Doorkeeper.config.revoke_previous_authorization_code_token?
revoke_previous_tokens(grant.application, resource_owner)
end

grant.revoke

find_or_create_access_token(
Expand Down Expand Up @@ -109,6 +113,10 @@ def custom_token_attributes_with_data
.slice(*Doorkeeper.config.custom_access_token_attributes)
.symbolize_keys
end

def revoke_previous_tokens(application, resource_owner)
Doorkeeper.config.access_token_model.revoke_all_for(application.id, resource_owner)
end
end
end
end
6 changes: 6 additions & 0 deletions lib/generators/doorkeeper/templates/initializer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,12 @@
#
# revoke_previous_client_credentials_token

# Only allow one valid access token obtained via authorization code
# per client. If a new access token is obtained before the old one
# expired, the old one gets revoked (disabled by default)
#
# revoke_previous_authorization_code_token

# Hash access and refresh tokens before persisting them.
# This will disable the possibility to use +reuse_access_token+
# since plain values can no longer be retrieved.
Expand Down
36 changes: 36 additions & 0 deletions spec/lib/oauth/authorization_code_request_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -220,4 +220,40 @@
end
end
end

context "when revoke_previous_authorization_code_token is false" do
before do
allow(Doorkeeper.config).to receive(:revoke_previous_authorization_code_token?).and_return(false)
end

it "does not revoke the previous token" do
previous_token = FactoryBot.create(
:access_token,
application_id: client.id,
resource_owner_id: grant.resource_owner_id,
resource_owner_type: grant.resource_owner_type,
scopes: grant.scopes.to_s,
)

expect { request.authorize }.not_to(change { previous_token.reload.revoked_at })
end
end

context "when revoke_previous_authorization_code_token is true" do
before do
allow(Doorkeeper.config).to receive(:revoke_previous_authorization_code_token?).and_return(true)
end

it "revokes the previous token" do
previous_token = FactoryBot.create(
:access_token,
application_id: client.id,
resource_owner_id: grant.resource_owner_id,
resource_owner_type: grant.resource_owner_type,
scopes: grant.scopes.to_s,
)

expect { request.authorize }.to(change { previous_token.reload.revoked_at })
end
end
end

0 comments on commit e29b063

Please sign in to comment.