-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
Allow for expired refresh tokens to be revoked #1744
Open
ransombriggs
wants to merge
6
commits into
doorkeeper-gem:main
Choose a base branch
from
ransombriggs:allow-refresh-tokens-to-be-revoked-after-expire
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
3df99ca
Allow for expired refresh tokens to be revoked
ransombriggs 07861c6
Updating CHANGELOG.md
ransombriggs a009e88
Try to fix revoke_token cognitive complexity
ransombriggs 9f3b7d1
Merge branch 'main' into allow-refresh-tokens-to-be-revoked-after-expire
ransombriggs 5dd6454
Move revoke logic out into classes
ransombriggs 1503e70
Merge branch 'main' into allow-refresh-tokens-to-be-revoked-after-expire
ransombriggs File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
# frozen_string_literal: true | ||
|
||
module Doorkeeper | ||
module RevocableTokens | ||
class RevocableAccessToken | ||
attr_reader :token | ||
|
||
def initialize(token) | ||
@token = token | ||
end | ||
|
||
def revocable? | ||
token.accessible? | ||
end | ||
|
||
def revoke | ||
token.revoke | ||
end | ||
end | ||
end | ||
end |
21 changes: 21 additions & 0 deletions
21
lib/doorkeeper/revocable_tokens/revocable_refresh_token.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
# frozen_string_literal: true | ||
|
||
module Doorkeeper | ||
module RevocableTokens | ||
class RevocableRefreshToken | ||
attr_reader :token | ||
|
||
def initialize(token) | ||
@token = token | ||
end | ||
|
||
def revocable? | ||
!token.revoked? | ||
end | ||
|
||
def revoke | ||
token.revoke | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -172,7 +172,15 @@ | |
# https://datatracker.ietf.org/doc/html/rfc7009#section-2.2 | ||
describe "POST #revoke" do | ||
let(:client) { FactoryBot.create(:application) } | ||
let(:access_token) { FactoryBot.create(:access_token, application: client) } | ||
let(:revoked_at) { nil } | ||
let(:access_token) do | ||
FactoryBot.create( | ||
:access_token, | ||
application: client, | ||
revoked_at: revoked_at, | ||
use_refresh_token: true | ||
) | ||
end | ||
|
||
context "when associated app is public" do | ||
let(:client) { FactoryBot.create(:application, confidential: false) } | ||
|
@@ -183,8 +191,89 @@ | |
expect(response.status).to eq 200 | ||
end | ||
|
||
it "revokes the access token" do | ||
post :revoke, params: { client_id: client.uid, token: access_token.token } | ||
it "does not revoke the access token when token_type_hint == refresh_token" do | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The |
||
post :revoke, params: { | ||
client_id: client.uid, | ||
token: access_token.token, | ||
token_type_hint: "refresh_token", | ||
} | ||
|
||
expect(response.status).to eq 200 | ||
|
||
expect(access_token.reload).to have_attributes(revoked?: false) | ||
end | ||
|
||
it "revokes the refresh token when token_type_hint == refresh_token" do | ||
post :revoke, params: { | ||
client_id: client.uid, | ||
token: access_token.refresh_token, | ||
token_type_hint: "refresh_token", | ||
} | ||
|
||
expect(response.status).to eq 200 | ||
|
||
expect(access_token.reload).to have_attributes(revoked?: true) | ||
end | ||
|
||
it "revokes the refresh token when token_type_hint not passed" do | ||
post :revoke, params: { | ||
client_id: client.uid, | ||
token: access_token.refresh_token, | ||
} | ||
|
||
expect(response.status).to eq 200 | ||
|
||
expect(access_token.reload).to have_attributes(revoked?: true) | ||
end | ||
|
||
context "when access_token has already been revoked" do | ||
let(:revoked_at) { 1.day.ago.floor } | ||
|
||
it "does not update the revoked_at when the access token has already been revoked" do | ||
post :revoke, params: { | ||
client_id: client.uid, | ||
token: access_token.token, | ||
} | ||
|
||
expect(response.status).to eq 200 | ||
|
||
expect(access_token.reload).to have_attributes(revoked_at: revoked_at) | ||
end | ||
|
||
it "does not update the revoked_at when the refresh token has already been revoked" do | ||
post :revoke, params: { | ||
client_id: client.uid, | ||
token: access_token.refresh_token, | ||
} | ||
|
||
expect(response.status).to eq 200 | ||
|
||
expect(access_token.reload).to have_attributes(revoked_at: revoked_at) | ||
end | ||
end | ||
|
||
it "does not revoke when the access token has expired" do | ||
access_token.update!(created_at: access_token.created_at - access_token.expires_in - 1) | ||
|
||
post :revoke, params: { | ||
client_id: client.uid, | ||
token: access_token.token, | ||
} | ||
|
||
expect(response.status).to eq 200 | ||
|
||
expect(access_token.reload).to have_attributes(revoked?: false) | ||
end | ||
|
||
it "revokes the refresh token after the access token has expired" do | ||
access_token.update!(created_at: access_token.created_at - access_token.expires_in - 1) | ||
|
||
post :revoke, params: { | ||
client_id: client.uid, | ||
token: access_token.refresh_token, | ||
} | ||
|
||
expect(response.status).to eq 200 | ||
|
||
expect(access_token.reload).to have_attributes(revoked?: true) | ||
end | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We have already checked
token.blank?
by the time we call this so the&.
felt unnecessary, but I can bring it back.