-
-
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
Add custom expiration time based on scopes #1102
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
module Doorkeeper | ||
module OAuth | ||
module Authorization | ||
class Context | ||
attr_reader :client, :grant_type, :scopes | ||
|
||
def initialize(client, grant_type, scopes) | ||
@client = client | ||
@grant_type = grant_type | ||
@scopes = scopes | ||
end | ||
end | ||
end | ||
end | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,8 +5,8 @@ class Token | |
attr_accessor :pre_auth, :resource_owner, :token | ||
|
||
class << self | ||
def access_token_expires_in(server, pre_auth_or_oauth_client, grant_type) | ||
if (expiration = custom_expiration(server, pre_auth_or_oauth_client, grant_type)) | ||
def access_token_expires_in(server, pre_auth_or_oauth_client, grant_type, scopes) | ||
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. Metrics/LineLength: Line is too long. [91/80] 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. Metrics/LineLength: Line is too long. [91/80] |
||
if (expiration = custom_expiration(server, pre_auth_or_oauth_client, grant_type, scopes)) | ||
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. Metrics/LineLength: Line is too long. [101/80] 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. Metrics/LineLength: Line is too long. [101/80] |
||
expiration | ||
else | ||
server.access_token_expires_in | ||
|
@@ -15,14 +15,19 @@ def access_token_expires_in(server, pre_auth_or_oauth_client, grant_type) | |
|
||
private | ||
|
||
def custom_expiration(server, pre_auth_or_oauth_client, grant_type) | ||
def custom_expiration(server, pre_auth_or_oauth_client, grant_type, scopes) | ||
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. Metrics/LineLength: Line is too long. [85/80] 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. Metrics/LineLength: Line is too long. [85/80] |
||
oauth_client = if pre_auth_or_oauth_client.respond_to?(:client) | ||
pre_auth_or_oauth_client.client | ||
else | ||
pre_auth_or_oauth_client | ||
end | ||
context = Doorkeeper::OAuth::Authorization::Context.new( | ||
oauth_client, | ||
grant_type, | ||
scopes | ||
) | ||
|
||
server.custom_access_token_expires_in.call(oauth_client, grant_type) | ||
server.custom_access_token_expires_in.call(context) | ||
end | ||
end | ||
|
||
|
@@ -39,7 +44,8 @@ def issue_token | |
self.class.access_token_expires_in( | ||
configuration, | ||
pre_auth, | ||
Doorkeeper::OAuth::IMPLICIT | ||
Doorkeeper::OAuth::IMPLICIT, | ||
pre_auth.scopes | ||
), | ||
false | ||
) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -35,7 +35,7 @@ def find_or_create_access_token(client, resource_owner_id, scopes, server) | |
client, | ||
resource_owner_id, | ||
scopes, | ||
Authorization::Token.access_token_expires_in(server, client, grant_type), | ||
Authorization::Token.access_token_expires_in(server, client, grant_type, scopes), | ||
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. Metrics/LineLength: Line is too long. [91/80] 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. Metrics/LineLength: Line is too long. [91/80] |
||
server.refresh_token_enabled? | ||
) | ||
end | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,7 +7,7 @@ class Doorkeeper::OAuth::ClientCredentialsRequest | |
double( | ||
:server, | ||
access_token_expires_in: 100, | ||
custom_access_token_expires_in: ->(_app, _grant) { nil } | ||
custom_access_token_expires_in: ->(_context) { nil } | ||
) | ||
end | ||
let(:validation) { double :validation, valid?: true } | ||
|
@@ -61,23 +61,44 @@ class Doorkeeper::OAuth::ClientCredentialsRequest | |
end | ||
|
||
context 'with custom expirations' do | ||
let(:custom_ttl) { 1234 } | ||
let(:custom_ttl_grant) { 1234 } | ||
let(:custom_ttl_scope) { 1235 } | ||
let(:custom_scope) { 'special' } | ||
let(:server) do | ||
double( | ||
:server, | ||
custom_access_token_expires_in: ->(_app, grant) { grant == Doorkeeper::OAuth::CLIENT_CREDENTIALS ? custom_ttl : nil } | ||
custom_access_token_expires_in: lambda { |context| | ||
# scopes is normally an object but is a string in this test | ||
if context.scopes == custom_scope | ||
custom_ttl_scope | ||
elsif context.grant_type == Doorkeeper::OAuth::CLIENT_CREDENTIALS | ||
custom_ttl_grant | ||
else | ||
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. Style/EmptyElse: Redundant else-clause. |
||
nil | ||
end | ||
} | ||
) | ||
end | ||
|
||
it 'creates with correct token parameters' do | ||
it 'respects grant based rules' do | ||
expect(creator).to receive(:call).with( | ||
client, | ||
scopes, | ||
expires_in: custom_ttl, | ||
expires_in: custom_ttl_grant, | ||
use_refresh_token: false | ||
) | ||
subject.create client, scopes, creator | ||
end | ||
|
||
it 'respects scope based rules' do | ||
expect(creator).to receive(:call).with( | ||
client, | ||
custom_scope, | ||
expires_in: custom_ttl_scope, | ||
use_refresh_token: false | ||
) | ||
subject.create client, custom_scope, creator | ||
end | ||
end | ||
end | ||
end | ||
|
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.
Style/FrozenStringLiteralComment: Missing magic comment # frozen_string_literal: true.