-
-
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
Adds custom expiration time based on grant type #1049
Conversation
Hi @rishabhsairawat . Thank you for th PR! I think that we can extract grant type names (like module Doorkeeper
module OAuth
GRANT_TYPES = [
CLIENT_CREDENTIALS = 'client_credentials'.freeze,
REFRESH_TOKEN = 'refresh_token'.freeze,
# ...
].freeze
# ...
end
end And use it on generation custom expiration time |
lib/doorkeeper/oauth/base_request.rb
Outdated
@@ -2,6 +2,8 @@ module Doorkeeper | |||
module OAuth | |||
class BaseRequest | |||
include Validations | |||
|
|||
attr_accessor :grant_type |
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.
Do we really need accessor ? I think attr_reader
would be enough
@@ -36,7 +36,7 @@ def issue_token | |||
pre_auth.client, | |||
resource_owner.id, | |||
pre_auth.scopes, | |||
self.class.access_token_expires_in(configuration, pre_auth), | |||
self.class.access_token_expires_in(configuration, pre_auth, 'implicit'), |
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.
Use constants for such things as I described in the topic comments.
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.
Hi @nbulaj Where to define these constants for grant flows as there is no class for grants in Doorkeeper::OAuth module. Should I define them in BaseRequest class?
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.
Let's place them into Doorkeeper::Request
module as GRANT_TYPES
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.
No, let's introduce lib/doorkeeper/oauth.rb
with explicit OAuth module:
module Doorkeeper
module OAuth
GRANT_TYPES = [
# ...
]. freeze
end
end
Don't forget to require it in main doorkeeper.rb:
require 'doorkeeper/oauth'
require 'doorkeeper/oauth/authorization/code'
require 'doorkeeper/oauth/authorization/token'
# ...
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.
I was thinking the same as It would be better to place them in separate file.
before do | ||
allow(Doorkeeper.configuration).to receive(:grant_flows).and_return(["implicit"]) | ||
allow(controller).to receive(:current_resource_owner).and_return(user) | ||
allow(Doorkeeper.configuration).to receive(:custom_access_token_expires_in).and_return(proc {|app, grant| | ||
grant == 'implicit' ? 1234 : nil |
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.
Use flows constants (mentioned above) in specs too.
spec/lib/oauth/token_request_spec.rb
Outdated
@@ -51,8 +51,8 @@ module Doorkeeper::OAuth | |||
before do | |||
Doorkeeper.configure do | |||
orm DOORKEEPER_ORM | |||
custom_access_token_expires_in do |_oauth_client| | |||
1234 | |||
custom_access_token_expires_in do |_oauth_client, _grant_type| |
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.
Do not use prefix _ for a variable that is used.
@@ -30,7 +30,7 @@ module Doorkeeper::OAuth | |||
it 'issues a new token for the client with custom expires_in' do | |||
server = double :server, | |||
access_token_expires_in: 2.minutes, | |||
custom_access_token_expires_in: ->(_oauth_client) { 1234 } | |||
custom_access_token_expires_in: ->(_app, _grant) { _grant == Doorkeeper::OAuth::REFRESH_TOKEN ? 1234 : nil } |
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.
Do not use prefix _ for a variable that is used.
@@ -9,7 +9,7 @@ module Doorkeeper::OAuth | |||
let(:server) do | |||
double :server, | |||
access_token_expires_in: 2.minutes, | |||
custom_access_token_expires_in: -> (_oauth_client) { nil } | |||
custom_access_token_expires_in: -> (_oauth_client, _grant) { nil } |
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.
Do not use spaces between -> and opening brace in lambda literals
@@ -8,7 +8,7 @@ module Doorkeeper::OAuth | |||
default_scopes: Doorkeeper::OAuth::Scopes.new, | |||
access_token_expires_in: 2.hours, | |||
refresh_token_enabled?: false, | |||
custom_access_token_expires_in: ->(_app) { nil } | |||
custom_access_token_expires_in: ->(_app, _grant) { _grant == Doorkeeper::OAuth::PASSWORD ? 1234 : nil } |
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.
Do not use prefix _ for a variable that is used.
let(:server) do | ||
double( | ||
:server, | ||
custom_access_token_expires_in: ->(_app) { custom_ttl } | ||
custom_access_token_expires_in: ->(_app, _grant) { _grant == Doorkeeper::OAuth::CLIENT_CREDENTIALS ? custom_ttl : nil } |
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.
Do not use prefix _ for a variable that is used.
@@ -37,7 +39,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), | |||
Authorization::Token.access_token_expires_in(server, client, grant_type), |
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.
Line is too long. [83/80]
lib/doorkeeper/oauth/base_request.rb
Outdated
@@ -2,6 +2,8 @@ module Doorkeeper | |||
module OAuth | |||
class BaseRequest | |||
include Validations | |||
|
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.
Trailing whitespace detected.
@@ -36,7 +36,7 @@ def issue_token | |||
pre_auth.client, | |||
resource_owner.id, | |||
pre_auth.scopes, | |||
self.class.access_token_expires_in(configuration, pre_auth), | |||
self.class.access_token_expires_in(configuration, pre_auth, Doorkeeper::OAuth::IMPLICIT), |
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.
Line is too long. [101/80]
def access_token_expires_in(server, pre_auth_or_oauth_client) | ||
if (expiration = custom_expiration(server, pre_auth_or_oauth_client)) | ||
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)) |
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.
Line is too long. [93/80]
@@ -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) | |||
if (expiration = custom_expiration(server, pre_auth_or_oauth_client)) | |||
def access_token_expires_in(server, pre_auth_or_oauth_client, grant_type) |
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.
Line is too long. [83/80]
lib/doorkeeper/oauth.rb
Outdated
REFRESH_TOKEN = 'refresh_token'.freeze | ||
].freeze | ||
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.
Final newline missing.
lib/doorkeeper/oauth.rb
Outdated
PASSWORD = 'password'.freeze, | ||
CLIENT_CREDENTIALS = 'client_credentials'.freeze, | ||
REFRESH_TOKEN = 'refresh_token'.freeze | ||
].freeze |
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.
Indent the right bracket the same as the start of the line where the left bracket is.
lib/doorkeeper/oauth.rb
Outdated
module Doorkeeper | ||
module OAuth | ||
GRANT_TYPES = [ | ||
AUTHORIZATION_CODE = 'authorization_code'.freeze, |
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.
Use 2 spaces for indentation in an array, relative to the start of the line where the left square bracket is.
lib/doorkeeper/oauth.rb
Outdated
@@ -0,0 +1,11 @@ | |||
module Doorkeeper | |||
module OAuth | |||
GRANT_TYPES = [ |
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.
Use 2 (not 0) spaces for indentation.
lib/doorkeeper/oauth.rb
Outdated
@@ -0,0 +1,11 @@ | |||
module Doorkeeper | |||
module OAuth |
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.
Use 2 (not 4) spaces for indentation.
@@ -0,0 +1,11 @@ | |||
module Doorkeeper |
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.
Missing magic comment # frozen_string_literal: true.
5ccc413
to
0cd20d7
Compare
lib/doorkeeper/oauth.rb
Outdated
@@ -0,0 +1,12 @@ | |||
# frozen_string_literal: true | |||
module Doorkeeper |
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.
Add an empty line after magic comments.
Thank you @rishabhsairawat 👍 |
Summary
This PR aims to solve #996 Expiration Time Base On Grant Type