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

Adds custom expiration time based on grant type #1049

Merged
merged 1 commit into from
Mar 12, 2018

Conversation

rishabhsairawat
Copy link
Contributor

Summary

This PR aims to solve #996 Expiration Time Base On Grant Type

@nbulaj
Copy link
Member

nbulaj commented Mar 12, 2018

Hi @rishabhsairawat . Thank you for th PR!

I think that we can extract grant type names (like 'client_credentials' and other) in some constant 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

@nbulaj nbulaj self-requested a review March 12, 2018 09:18
@nbulaj nbulaj self-assigned this Mar 12, 2018
@nbulaj nbulaj added this to the 5.0 milestone Mar 12, 2018
@@ -2,6 +2,8 @@ module Doorkeeper
module OAuth
class BaseRequest
include Validations

attr_accessor :grant_type
Copy link
Member

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'),
Copy link
Member

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.

Copy link
Contributor Author

@rishabhsairawat rishabhsairawat Mar 12, 2018

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?

Copy link
Member

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

Copy link
Member

@nbulaj nbulaj Mar 12, 2018

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'
# ...

Copy link
Contributor Author

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
Copy link
Member

@nbulaj nbulaj Mar 12, 2018

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.

@@ -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|

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 }

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 }

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 }

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 }

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),

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]

@@ -2,6 +2,8 @@ module Doorkeeper
module OAuth
class BaseRequest
include Validations

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),

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))

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)

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]

REFRESH_TOKEN = 'refresh_token'.freeze
].freeze
end
end

Choose a reason for hiding this comment

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

Final newline missing.

PASSWORD = 'password'.freeze,
CLIENT_CREDENTIALS = 'client_credentials'.freeze,
REFRESH_TOKEN = 'refresh_token'.freeze
].freeze

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.

module Doorkeeper
module OAuth
GRANT_TYPES = [
AUTHORIZATION_CODE = 'authorization_code'.freeze,

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.

@@ -0,0 +1,11 @@
module Doorkeeper
module OAuth
GRANT_TYPES = [

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.

@@ -0,0 +1,11 @@
module Doorkeeper
module OAuth

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

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.

@rishabhsairawat rishabhsairawat force-pushed the master branch 2 times, most recently from 5ccc413 to 0cd20d7 Compare March 12, 2018 16:49
@@ -0,0 +1,12 @@
# frozen_string_literal: true
module Doorkeeper

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.

@nbulaj nbulaj merged commit 55439ee into doorkeeper-gem:master Mar 12, 2018
@nbulaj
Copy link
Member

nbulaj commented Mar 12, 2018

Thank you @rishabhsairawat 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants