Skip to content

Commit

Permalink
Improve code style
Browse files Browse the repository at this point in the history
  • Loading branch information
nbulaj committed Jan 29, 2020
1 parent cb2b56d commit 8fec40d
Show file tree
Hide file tree
Showing 43 changed files with 135 additions and 92 deletions.
1 change: 1 addition & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
inherit_from: .rubocop_todo.yml
require: rubocop-performance
AllCops:
TargetRubyVersion: 2.4
Expand Down
40 changes: 40 additions & 0 deletions .rubocop_todo.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
# This configuration was generated by
# `rubocop --auto-gen-config`
# on 2020-01-29 11:31:56 +0300 using RuboCop version 0.78.0.
# The point is for the user to remove these configuration records
# one by one as the offenses are removed from the code base.
# Note that changes in the inspected code, or installation of new
# versions of RuboCop, may require this file to be generated again.

# Offense count: 1
# Configuration parameters: CountComments, ExcludedMethods.
# ExcludedMethods: refine
Metrics/BlockLength:
Max: 58

# Offense count: 2
# Configuration parameters: CountComments.
Metrics/ClassLength:
Max: 214

# Offense count: 4
# Configuration parameters: CountComments.
Metrics/ModuleLength:
Max: 470

# Offense count: 1
# Configuration parameters: EnforcedStyleForLeadingUnderscores.
# SupportedStylesForLeadingUnderscores: disallowed, required, optional
Naming/MemoizedInstanceVariableName:
Exclude:
- 'lib/doorkeeper/oauth/authorization/code.rb'

# Offense count: 5
# Configuration parameters: MinBodyLength.
Style/GuardClause:
Exclude:
- 'lib/doorkeeper/config.rb'
- 'lib/doorkeeper/helpers/controller.rb'
- 'lib/doorkeeper/oauth/client/credentials.rb'
- 'lib/doorkeeper/oauth/helpers/scope_checker.rb'
- 'lib/doorkeeper/oauth/token.rb'
4 changes: 2 additions & 2 deletions app/controllers/doorkeeper/application_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@

module Doorkeeper
class ApplicationController <
Doorkeeper.configuration.resolve_controller(:base)
Doorkeeper.config.resolve_controller(:base)
include Helpers::Controller

unless Doorkeeper.configuration.api_only
unless Doorkeeper.config.api_only
protect_from_forgery with: :exception
helper "doorkeeper/dashboard"
end
Expand Down
4 changes: 2 additions & 2 deletions app/controllers/doorkeeper/application_metal_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@

module Doorkeeper
class ApplicationMetalController <
Doorkeeper.configuration.resolve_controller(:base_metal)
Doorkeeper.config.resolve_controller(:base_metal)
include Helpers::Controller

before_action :enforce_content_type,
if: -> { Doorkeeper.configuration.enforce_content_type }
if: -> { Doorkeeper.config.enforce_content_type }

ActiveSupport.run_load_hooks(:doorkeeper_metal_controller, self)
end
Expand Down
2 changes: 1 addition & 1 deletion lib/doorkeeper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@
# Main Doorkeeper namespace.
#
module Doorkeeper
def self.authenticate(request, methods = Doorkeeper.configuration.access_token_methods)
def self.authenticate(request, methods = Doorkeeper.config.access_token_methods)
OAuth::Token.authenticate(request, *methods)
end
end
2 changes: 1 addition & 1 deletion lib/doorkeeper/grape/helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ def endpoint
def doorkeeper_token
@doorkeeper_token ||= OAuth::Token.authenticate(
decorated_request,
*Doorkeeper.configuration.access_token_methods,
*Doorkeeper.config.access_token_methods,
)
end

Expand Down
10 changes: 5 additions & 5 deletions lib/doorkeeper/helpers/controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,17 @@ def authenticate_resource_owner!
# :doc:
def current_resource_owner
@current_resource_owner ||= begin
instance_eval(&Doorkeeper.configuration.authenticate_resource_owner)
instance_eval(&Doorkeeper.config.authenticate_resource_owner)
end
end

def resource_owner_from_credentials
instance_eval(&Doorkeeper.configuration.resource_owner_from_credentials)
instance_eval(&Doorkeeper.config.resource_owner_from_credentials)
end

# :doc:
def authenticate_admin!
instance_eval(&Doorkeeper.configuration.authenticate_admin)
instance_eval(&Doorkeeper.config.authenticate_admin)
end

def server
Expand All @@ -40,7 +40,7 @@ def doorkeeper_token
end

def config_methods
@config_methods ||= Doorkeeper.configuration.access_token_methods
@config_methods ||= Doorkeeper.config.access_token_methods
end

def get_error_response_from_exception(exception)
Expand All @@ -67,7 +67,7 @@ def handle_token_exception(exception)
def skip_authorization?
!!instance_exec(
[server.current_resource_owner, @pre_auth.client],
&Doorkeeper.configuration.skip_authorization
&Doorkeeper.config.skip_authorization
)
end

Expand Down
4 changes: 2 additions & 2 deletions lib/doorkeeper/models/access_grant_mixin.rb
Original file line number Diff line number Diff line change
Expand Up @@ -103,14 +103,14 @@ def pkce_supported?
# Determines the secret storing transformer
# Unless configured otherwise, uses the plain secret strategy
def secret_strategy
::Doorkeeper.configuration.token_secret_strategy
::Doorkeeper.config.token_secret_strategy
end

##
# Determine the fallback storing strategy
# Unless configured, there will be no fallback
def fallback_secret_strategy
::Doorkeeper.configuration.token_secret_fallback_strategy
::Doorkeeper.config.token_secret_fallback_strategy
end
end
end
Expand Down
12 changes: 6 additions & 6 deletions lib/doorkeeper/models/access_token_mixin.rb
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ def scopes_match?(token_scopes, param_scopes, app_scopes)
(token_scopes.sort == param_scopes.sort) &&
Doorkeeper::OAuth::Helpers::ScopeChecker.valid?(
scope_str: param_scopes.to_s,
server_scopes: Doorkeeper.configuration.scopes,
server_scopes: Doorkeeper.config.scopes,
app_scopes: app_scopes,
)
end
Expand All @@ -181,7 +181,7 @@ def scopes_match?(token_scopes, param_scopes, app_scopes)
# @return [Doorkeeper::AccessToken] existing record or a new one
#
def find_or_create_for(application, resource_owner_id, scopes, expires_in, use_refresh_token)
if Doorkeeper.configuration.reuse_access_token
if Doorkeeper.config.reuse_access_token
access_token = matching_token_for(application, resource_owner_id, scopes)

return access_token if access_token&.reusable?
Expand Down Expand Up @@ -234,14 +234,14 @@ def last_authorized_token_for(application_id, resource_owner_id)
# Determines the secret storing transformer
# Unless configured otherwise, uses the plain secret strategy
def secret_strategy
::Doorkeeper.configuration.token_secret_strategy
::Doorkeeper.config.token_secret_strategy
end

##
# Determine the fallback storing strategy
# Unless configured, there will be no fallback
def fallback_secret_strategy
::Doorkeeper.configuration.token_secret_fallback_strategy
::Doorkeeper.config.token_secret_fallback_strategy
end
end

Expand Down Expand Up @@ -350,7 +350,7 @@ def generate_refresh_token
end

# Generates and sets the token value with the
# configured Generator class (see Doorkeeper.configuration).
# configured Generator class (see Doorkeeper.config).
#
# @return [String] generated token value
#
Expand All @@ -375,7 +375,7 @@ def generate_token
end

def token_generator
generator_name = Doorkeeper.configuration.access_token_generator
generator_name = Doorkeeper.config.access_token_generator
generator = generator_name.constantize

return generator if generator.respond_to?(:generate)
Expand Down
4 changes: 2 additions & 2 deletions lib/doorkeeper/models/application_mixin.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,14 +47,14 @@ def by_uid(uid)
# Determines the secret storing transformer
# Unless configured otherwise, uses the plain secret strategy
def secret_strategy
::Doorkeeper.configuration.application_secret_strategy
::Doorkeeper.config.application_secret_strategy
end

##
# Determine the fallback storing strategy
# Unless configured, there will be no fallback
def fallback_secret_strategy
::Doorkeeper.configuration.application_secret_fallback_strategy
::Doorkeeper.config.application_secret_fallback_strategy
end
end

Expand Down
2 changes: 1 addition & 1 deletion lib/doorkeeper/models/concerns/ownership.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ module Ownership
end

def validate_owner?
Doorkeeper.configuration.confirm_application_owner?
Doorkeeper.config.confirm_application_owner?
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/doorkeeper/models/concerns/reusable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ def reusable?
return false if expired?
return true unless expires_in

threshold_limit = 100 - Doorkeeper.configuration.token_reuse_limit
threshold_limit = 100 - Doorkeeper.config.token_reuse_limit
expires_in_seconds >= threshold_limit * expires_in / 100
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/doorkeeper/oauth/authorization/code.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ def oob_redirect
private

def authorization_code_expires_in
Doorkeeper.configuration.authorization_code_expires_in
Doorkeeper.config.authorization_code_expires_in
end

def access_grant_attributes
Expand Down
4 changes: 2 additions & 2 deletions lib/doorkeeper/oauth/authorization/token.rb
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ def issue_token
pre_auth.scopes,
)

@token = Doorkeeper.config.access_token_model.find_or_create_for(
@token = configuration.access_token_model.find_or_create_for(
pre_auth.client,
resource_owner.id,
pre_auth.scopes,
Expand All @@ -77,7 +77,7 @@ def oob_redirect
private

def configuration
Doorkeeper.configuration
Doorkeeper.config
end

def controller
Expand Down
4 changes: 2 additions & 2 deletions lib/doorkeeper/oauth/base_request.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,11 @@ def find_or_create_access_token(client, resource_owner_id, scopes, server)
end

def before_successful_response
Doorkeeper.configuration.before_successful_strategy_response.call(self)
Doorkeeper.config.before_successful_strategy_response.call(self)
end

def after_successful_response
Doorkeeper.configuration.after_successful_strategy_response.call(self, @response)
Doorkeeper.config.after_successful_strategy_response.call(self, @response)
end

private
Expand Down
6 changes: 2 additions & 4 deletions lib/doorkeeper/oauth/client_credentials/creator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,7 @@ class Creator
def call(client, scopes, attributes = {})
existing_token = existing_token_for(client, scopes)

if Doorkeeper.configuration.reuse_access_token && existing_token&.reusable?
return existing_token
end
return existing_token if Doorkeeper.config.reuse_access_token && existing_token&.reusable?

existing_token&.revoke

Expand All @@ -22,7 +20,7 @@ def call(client, scopes, attributes = {})
private

def existing_token_for(client, scopes)
Doorkeeper.config.access_token_model.matching_token_for client, nil, scopes
Doorkeeper.config.access_token_model.matching_token_for(client, nil, scopes)
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/doorkeeper/oauth/client_credentials/validation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ def validate_client
end

def validate_client_supports_grant_flow
Doorkeeper.configuration.allow_grant_flow_for_client?(
Doorkeeper.config.allow_grant_flow_for_client?(
Doorkeeper::OAuth::CLIENT_CREDENTIALS,
@client,
)
Expand Down
6 changes: 3 additions & 3 deletions lib/doorkeeper/oauth/error_response.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,9 @@ def redirectable?

def redirect_uri
if @response_on_fragment
Authorization::URIBuilder.uri_with_fragment @redirect_uri, body
Authorization::URIBuilder.uri_with_fragment(@redirect_uri, body)
else
Authorization::URIBuilder.uri_with_query @redirect_uri, body
Authorization::URIBuilder.uri_with_query(@redirect_uri, body)
end
end

Expand All @@ -70,7 +70,7 @@ def raise_exception!
delegate :realm, to: :configuration

def configuration
Doorkeeper.configuration
Doorkeeper.config
end

def exception_class
Expand Down
2 changes: 1 addition & 1 deletion lib/doorkeeper/oauth/helpers/scope_checker.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ def initialize(scope_str, server_scopes, app_scopes, grant_type)
@valid_scopes = valid_scopes(server_scopes, app_scopes)

if grant_type
@scopes_by_grant_type = Doorkeeper.configuration.scopes_by_grant_type[grant_type.to_sym]
@scopes_by_grant_type = Doorkeeper.config.scopes_by_grant_type[grant_type.to_sym]
end
end

Expand Down
13 changes: 8 additions & 5 deletions lib/doorkeeper/oauth/helpers/unique_token.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
module Doorkeeper
module OAuth
module Helpers
# Default Doorkeeper token generator. Follows OAuth RFC and
# could be customized using `default_generator_method` in
# configuration.
module UniqueToken
def self.generate(options = {})
# Access Token value must be 1*VSCHAR or
Expand All @@ -11,15 +14,15 @@ def self.generate(options = {})
# @see https://tools.ietf.org/html/rfc6749#appendix-A.12
# @see https://tools.ietf.org/html/rfc6750#section-2.1
#
generator_method = options.delete(:generator) || SecureRandom.method(self.generator_method)
token_size = options.delete(:size) || 32
generator_method.call(token_size)
generator = options.delete(:generator) || SecureRandom.method(default_generator_method)
token_size = options.delete(:size) || 32
generator.call(token_size)
end

# Generator method for default generator class (SecureRandom)
#
def self.generator_method
Doorkeeper.configuration.default_generator_method
def self.default_generator_method
Doorkeeper.config.default_generator_method
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/doorkeeper/oauth/password_access_token_request.rb
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ def validate_client
end

def validate_client_supports_grant_flow
Doorkeeper.configuration.allow_grant_flow_for_client?(grant_type, client)
Doorkeeper.config.allow_grant_flow_for_client?(grant_type, client)
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/doorkeeper/oauth/pre_authorization.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ def authorizable?
end

def validate_client_supports_grant_flow
Doorkeeper.configuration.allow_grant_flow_for_client?(grant_type, client.application)
Doorkeeper.config.allow_grant_flow_for_client?(grant_type, client.application)
end

def scopes
Expand Down
2 changes: 1 addition & 1 deletion lib/doorkeeper/oauth/token.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ def from_request(request, *methods)
def authenticate(request, *methods)
if (token = from_request(request, *methods))
access_token = Doorkeeper.config.access_token_model.by_token(token)
refresh_token_enabled = Doorkeeper.configuration.refresh_token_enabled?
refresh_token_enabled = Doorkeeper.config.refresh_token_enabled?
if access_token.present? && refresh_token_enabled
access_token.revoke_previous_refresh_token!
end
Expand Down
Loading

0 comments on commit 8fec40d

Please sign in to comment.