From 8fec40d5b873dc62e8bc29b8ebfe71a003b09dab Mon Sep 17 00:00:00 2001 From: Nikita Bulai Date: Wed, 29 Jan 2020 11:38:13 +0300 Subject: [PATCH] Improve code style --- .rubocop.yml | 1 + .rubocop_todo.yml | 40 +++++++++++++++++++ .../doorkeeper/application_controller.rb | 4 +- .../application_metal_controller.rb | 4 +- lib/doorkeeper.rb | 2 +- lib/doorkeeper/grape/helpers.rb | 2 +- lib/doorkeeper/helpers/controller.rb | 10 ++--- lib/doorkeeper/models/access_grant_mixin.rb | 4 +- lib/doorkeeper/models/access_token_mixin.rb | 12 +++--- lib/doorkeeper/models/application_mixin.rb | 4 +- lib/doorkeeper/models/concerns/ownership.rb | 2 +- lib/doorkeeper/models/concerns/reusable.rb | 2 +- lib/doorkeeper/oauth/authorization/code.rb | 2 +- lib/doorkeeper/oauth/authorization/token.rb | 4 +- lib/doorkeeper/oauth/base_request.rb | 4 +- .../oauth/client_credentials/creator.rb | 6 +-- .../oauth/client_credentials/validation.rb | 2 +- lib/doorkeeper/oauth/error_response.rb | 6 +-- lib/doorkeeper/oauth/helpers/scope_checker.rb | 2 +- lib/doorkeeper/oauth/helpers/unique_token.rb | 13 +++--- .../oauth/password_access_token_request.rb | 2 +- lib/doorkeeper/oauth/pre_authorization.rb | 2 +- lib/doorkeeper/oauth/token.rb | 2 +- lib/doorkeeper/oauth/token_introspection.rb | 6 +-- lib/doorkeeper/orm/active_record.rb | 4 +- .../orm/active_record/mixins/access_grant.rb | 5 ++- .../orm/active_record/mixins/access_token.rb | 5 ++- .../orm/active_record/mixins/application.rb | 2 +- .../active_record/redirect_uri_validator.rb | 6 +-- lib/doorkeeper/rails/helpers.rb | 6 +-- lib/doorkeeper/rails/routes.rb | 4 +- lib/doorkeeper/rake/db.rake | 4 +- lib/doorkeeper/request.rb | 2 +- lib/doorkeeper/request/authorization_code.rb | 2 +- lib/doorkeeper/request/client_credentials.rb | 2 +- lib/doorkeeper/request/password.rb | 2 +- lib/doorkeeper/request/refresh_token.rb | 2 +- lib/doorkeeper/server.rb | 2 +- lib/doorkeeper/stale_records_cleaner.rb | 2 +- .../application_metal_controller_spec.rb | 2 +- .../applications_controller_spec.rb | 5 ++- .../authorizations_controller_spec.rb | 30 +++++++------- spec/lib/doorkeeper_spec.rb | 2 +- 43 files changed, 135 insertions(+), 92 deletions(-) create mode 100644 .rubocop_todo.yml diff --git a/.rubocop.yml b/.rubocop.yml index 2145b4cfe..2f4b917cd 100644 --- a/.rubocop.yml +++ b/.rubocop.yml @@ -1,3 +1,4 @@ +inherit_from: .rubocop_todo.yml require: rubocop-performance AllCops: TargetRubyVersion: 2.4 diff --git a/.rubocop_todo.yml b/.rubocop_todo.yml new file mode 100644 index 000000000..26f113cdf --- /dev/null +++ b/.rubocop_todo.yml @@ -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' diff --git a/app/controllers/doorkeeper/application_controller.rb b/app/controllers/doorkeeper/application_controller.rb index 80be8a165..beb1888ac 100644 --- a/app/controllers/doorkeeper/application_controller.rb +++ b/app/controllers/doorkeeper/application_controller.rb @@ -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 diff --git a/app/controllers/doorkeeper/application_metal_controller.rb b/app/controllers/doorkeeper/application_metal_controller.rb index 3072cf696..caea55878 100644 --- a/app/controllers/doorkeeper/application_metal_controller.rb +++ b/app/controllers/doorkeeper/application_metal_controller.rb @@ -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 diff --git a/lib/doorkeeper.rb b/lib/doorkeeper.rb index edd5c863c..86550bffc 100644 --- a/lib/doorkeeper.rb +++ b/lib/doorkeeper.rb @@ -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 diff --git a/lib/doorkeeper/grape/helpers.rb b/lib/doorkeeper/grape/helpers.rb index 6862fbdc4..6b6743e6f 100644 --- a/lib/doorkeeper/grape/helpers.rb +++ b/lib/doorkeeper/grape/helpers.rb @@ -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 diff --git a/lib/doorkeeper/helpers/controller.rb b/lib/doorkeeper/helpers/controller.rb index 48f7a37d5..b4a3264d7 100644 --- a/lib/doorkeeper/helpers/controller.rb +++ b/lib/doorkeeper/helpers/controller.rb @@ -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 @@ -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) @@ -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 diff --git a/lib/doorkeeper/models/access_grant_mixin.rb b/lib/doorkeeper/models/access_grant_mixin.rb index 31e9835de..6fba84104 100644 --- a/lib/doorkeeper/models/access_grant_mixin.rb +++ b/lib/doorkeeper/models/access_grant_mixin.rb @@ -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 diff --git a/lib/doorkeeper/models/access_token_mixin.rb b/lib/doorkeeper/models/access_token_mixin.rb index a1c7917a7..565dd21a3 100644 --- a/lib/doorkeeper/models/access_token_mixin.rb +++ b/lib/doorkeeper/models/access_token_mixin.rb @@ -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 @@ -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? @@ -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 @@ -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 # @@ -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) diff --git a/lib/doorkeeper/models/application_mixin.rb b/lib/doorkeeper/models/application_mixin.rb index 95ce55b65..bcc36e350 100644 --- a/lib/doorkeeper/models/application_mixin.rb +++ b/lib/doorkeeper/models/application_mixin.rb @@ -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 diff --git a/lib/doorkeeper/models/concerns/ownership.rb b/lib/doorkeeper/models/concerns/ownership.rb index 714723645..634c023c6 100644 --- a/lib/doorkeeper/models/concerns/ownership.rb +++ b/lib/doorkeeper/models/concerns/ownership.rb @@ -11,7 +11,7 @@ module Ownership end def validate_owner? - Doorkeeper.configuration.confirm_application_owner? + Doorkeeper.config.confirm_application_owner? end end end diff --git a/lib/doorkeeper/models/concerns/reusable.rb b/lib/doorkeeper/models/concerns/reusable.rb index 4084ba67d..70b335741 100644 --- a/lib/doorkeeper/models/concerns/reusable.rb +++ b/lib/doorkeeper/models/concerns/reusable.rb @@ -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 diff --git a/lib/doorkeeper/oauth/authorization/code.rb b/lib/doorkeeper/oauth/authorization/code.rb index 8ee48c872..38a46b09d 100644 --- a/lib/doorkeeper/oauth/authorization/code.rb +++ b/lib/doorkeeper/oauth/authorization/code.rb @@ -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 diff --git a/lib/doorkeeper/oauth/authorization/token.rb b/lib/doorkeeper/oauth/authorization/token.rb index c18d797b4..6b8af3ae3 100644 --- a/lib/doorkeeper/oauth/authorization/token.rb +++ b/lib/doorkeeper/oauth/authorization/token.rb @@ -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, @@ -77,7 +77,7 @@ def oob_redirect private def configuration - Doorkeeper.configuration + Doorkeeper.config end def controller diff --git a/lib/doorkeeper/oauth/base_request.rb b/lib/doorkeeper/oauth/base_request.rb index 176395b0f..23e2b0fd0 100644 --- a/lib/doorkeeper/oauth/base_request.rb +++ b/lib/doorkeeper/oauth/base_request.rb @@ -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 diff --git a/lib/doorkeeper/oauth/client_credentials/creator.rb b/lib/doorkeeper/oauth/client_credentials/creator.rb index 1d5514a61..cdf3b63aa 100644 --- a/lib/doorkeeper/oauth/client_credentials/creator.rb +++ b/lib/doorkeeper/oauth/client_credentials/creator.rb @@ -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 @@ -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 diff --git a/lib/doorkeeper/oauth/client_credentials/validation.rb b/lib/doorkeeper/oauth/client_credentials/validation.rb index f154d8fc9..2d99434fe 100644 --- a/lib/doorkeeper/oauth/client_credentials/validation.rb +++ b/lib/doorkeeper/oauth/client_credentials/validation.rb @@ -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, ) diff --git a/lib/doorkeeper/oauth/error_response.rb b/lib/doorkeeper/oauth/error_response.rb index c7ec4a77c..30d8104c3 100644 --- a/lib/doorkeeper/oauth/error_response.rb +++ b/lib/doorkeeper/oauth/error_response.rb @@ -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 @@ -70,7 +70,7 @@ def raise_exception! delegate :realm, to: :configuration def configuration - Doorkeeper.configuration + Doorkeeper.config end def exception_class diff --git a/lib/doorkeeper/oauth/helpers/scope_checker.rb b/lib/doorkeeper/oauth/helpers/scope_checker.rb index 213fdbce5..22ecec4be 100644 --- a/lib/doorkeeper/oauth/helpers/scope_checker.rb +++ b/lib/doorkeeper/oauth/helpers/scope_checker.rb @@ -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 diff --git a/lib/doorkeeper/oauth/helpers/unique_token.rb b/lib/doorkeeper/oauth/helpers/unique_token.rb index 4fbf52750..6e40f0ccb 100644 --- a/lib/doorkeeper/oauth/helpers/unique_token.rb +++ b/lib/doorkeeper/oauth/helpers/unique_token.rb @@ -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 @@ -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 diff --git a/lib/doorkeeper/oauth/password_access_token_request.rb b/lib/doorkeeper/oauth/password_access_token_request.rb index 530576e25..187909c03 100644 --- a/lib/doorkeeper/oauth/password_access_token_request.rb +++ b/lib/doorkeeper/oauth/password_access_token_request.rb @@ -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 diff --git a/lib/doorkeeper/oauth/pre_authorization.rb b/lib/doorkeeper/oauth/pre_authorization.rb index f1e39fa4e..5d0e4599d 100644 --- a/lib/doorkeeper/oauth/pre_authorization.rb +++ b/lib/doorkeeper/oauth/pre_authorization.rb @@ -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 diff --git a/lib/doorkeeper/oauth/token.rb b/lib/doorkeeper/oauth/token.rb index 555eb2f3d..88d6e63d9 100644 --- a/lib/doorkeeper/oauth/token.rb +++ b/lib/doorkeeper/oauth/token.rb @@ -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 diff --git a/lib/doorkeeper/oauth/token_introspection.rb b/lib/doorkeeper/oauth/token_introspection.rb index afadbbb1a..fe385284e 100644 --- a/lib/doorkeeper/oauth/token_introspection.rb +++ b/lib/doorkeeper/oauth/token_introspection.rb @@ -174,9 +174,9 @@ def authorized_token_matches_introspected? authorized_token.token == @token&.token end - # config constraints for introspection in Doorkeeper.configuration.allow_token_introspection + # Config constraints for introspection in Doorkeeper.config.allow_token_introspection def token_introspection_allowed?(auth_client: nil, auth_token: nil) - allow_introspection = Doorkeeper.configuration.allow_token_introspection + allow_introspection = Doorkeeper.config.allow_token_introspection return allow_introspection unless allow_introspection.respond_to?(:call) allow_introspection.call( @@ -193,7 +193,7 @@ def token_introspection_allowed?(auth_client: nil, auth_token: nil) # @see https://tools.ietf.org/html/rfc7662#section-2.2 # def customize_response(response) - customized_response = Doorkeeper.configuration.custom_introspection_response.call( + customized_response = Doorkeeper.config.custom_introspection_response.call( token, server.context, ) diff --git a/lib/doorkeeper/orm/active_record.rb b/lib/doorkeeper/orm/active_record.rb index fee5e977e..d250d0e51 100644 --- a/lib/doorkeeper/orm/active_record.rb +++ b/lib/doorkeeper/orm/active_record.rb @@ -20,9 +20,9 @@ def self.initialize_models! require "doorkeeper/orm/active_record/access_token" require "doorkeeper/orm/active_record/application" - if Doorkeeper.configuration.active_record_options[:establish_connection] + if Doorkeeper.config.active_record_options[:establish_connection] Doorkeeper::Orm::ActiveRecord.models.each do |model| - options = Doorkeeper.configuration.active_record_options[:establish_connection] + options = Doorkeeper.config.active_record_options[:establish_connection] model.establish_connection(options) end end diff --git a/lib/doorkeeper/orm/active_record/mixins/access_grant.rb b/lib/doorkeeper/orm/active_record/mixins/access_grant.rb index 3c03a9cce..e68eca4d1 100644 --- a/lib/doorkeeper/orm/active_record/mixins/access_grant.rb +++ b/lib/doorkeeper/orm/active_record/mixins/access_grant.rb @@ -9,8 +9,9 @@ module AccessGrant include ::Doorkeeper::AccessGrantMixin - belongs_to :application, class_name: Doorkeeper.configuration.application_class, - optional: true, inverse_of: :access_grants + belongs_to :application, class_name: Doorkeeper.config.application_class, + optional: true, + inverse_of: :access_grants validates :resource_owner_id, :application_id, diff --git a/lib/doorkeeper/orm/active_record/mixins/access_token.rb b/lib/doorkeeper/orm/active_record/mixins/access_token.rb index e11021632..6edd60f6b 100644 --- a/lib/doorkeeper/orm/active_record/mixins/access_token.rb +++ b/lib/doorkeeper/orm/active_record/mixins/access_token.rb @@ -9,8 +9,9 @@ module AccessToken include ::Doorkeeper::AccessTokenMixin - belongs_to :application, class_name: Doorkeeper.configuration.application_class, - inverse_of: :access_tokens, optional: true + belongs_to :application, class_name: Doorkeeper.config.application_class, + inverse_of: :access_tokens, + optional: true validates :token, presence: true, uniqueness: { case_sensitive: true } validates :refresh_token, uniqueness: { case_sensitive: true }, if: :use_refresh_token? diff --git a/lib/doorkeeper/orm/active_record/mixins/application.rb b/lib/doorkeeper/orm/active_record/mixins/application.rb index e20aeafce..cf34134c2 100644 --- a/lib/doorkeeper/orm/active_record/mixins/application.rb +++ b/lib/doorkeeper/orm/active_record/mixins/application.rb @@ -87,7 +87,7 @@ def generate_secret def scopes_match_configured if scopes.present? && !Doorkeeper::OAuth::Helpers::ScopeChecker.valid?( scope_str: scopes.to_s, - server_scopes: Doorkeeper.configuration.scopes, + server_scopes: Doorkeeper.config.scopes, ) errors.add(:scopes, :not_match_configured) end diff --git a/lib/doorkeeper/orm/active_record/redirect_uri_validator.rb b/lib/doorkeeper/orm/active_record/redirect_uri_validator.rb index 8fb8414d2..5c3683500 100644 --- a/lib/doorkeeper/orm/active_record/redirect_uri_validator.rb +++ b/lib/doorkeeper/orm/active_record/redirect_uri_validator.rb @@ -8,7 +8,7 @@ module Doorkeeper class RedirectUriValidator < ActiveModel::EachValidator def validate_each(record, attribute, value) if value.blank? - return if Doorkeeper.configuration.allow_blank_redirect_uri?(record) + return if Doorkeeper.config.allow_blank_redirect_uri?(record) record.errors.add(attribute, :blank) else @@ -34,7 +34,7 @@ def oob_redirect_uri?(uri) end def forbidden_uri?(uri) - Doorkeeper.configuration.forbid_redirect_uri.call(uri) + Doorkeeper.config.forbid_redirect_uri.call(uri) end def unspecified_scheme?(uri) @@ -48,7 +48,7 @@ def relative_uri?(uri) end def invalid_ssl_uri?(uri) - forces_ssl = Doorkeeper.configuration.force_ssl_in_redirect_uri + forces_ssl = Doorkeeper.config.force_ssl_in_redirect_uri non_https = uri.try(:scheme) == "http" if forces_ssl.respond_to?(:call) diff --git a/lib/doorkeeper/rails/helpers.rb b/lib/doorkeeper/rails/helpers.rb index 3ab7ef225..f1607c4b1 100644 --- a/lib/doorkeeper/rails/helpers.rb +++ b/lib/doorkeeper/rails/helpers.rb @@ -4,7 +4,7 @@ module Doorkeeper module Rails module Helpers def doorkeeper_authorize!(*scopes) - @_doorkeeper_scopes = scopes.presence || Doorkeeper.configuration.default_scopes + @_doorkeeper_scopes = scopes.presence || Doorkeeper.config.default_scopes doorkeeper_render_error unless valid_doorkeeper_token? end @@ -21,7 +21,7 @@ def valid_doorkeeper_token? def doorkeeper_render_error error = doorkeeper_error - error.raise_exception! if Doorkeeper.configuration.raise_on_errors? + error.raise_exception! if Doorkeeper.config.raise_on_errors? headers.merge!(error.headers.reject { |k| k == "Content-Type" }) doorkeeper_render_error_with(error) @@ -72,7 +72,7 @@ def doorkeeper_invalid_token_response? def doorkeeper_token @doorkeeper_token ||= OAuth::Token.authenticate( request, - *Doorkeeper.configuration.access_token_methods, + *Doorkeeper.config.access_token_methods, ) end end diff --git a/lib/doorkeeper/rails/routes.rb b/lib/doorkeeper/rails/routes.rb index b6a26920d..1b4d405ee 100644 --- a/lib/doorkeeper/rails/routes.rb +++ b/lib/doorkeeper/rails/routes.rb @@ -26,9 +26,7 @@ def initialize(routes, &block) @routes = routes @mapping = Mapper.new.map(&block) - if Doorkeeper.configuration.api_only - @mapping.skips.push(:applications, :authorized_applications) - end + @mapping.skips.push(:applications, :authorized_applications) if Doorkeeper.config.api_only end def generate_routes!(options) diff --git a/lib/doorkeeper/rake/db.rake b/lib/doorkeeper/rake/db.rake index 5cfac3996..044075476 100644 --- a/lib/doorkeeper/rake/db.rake +++ b/lib/doorkeeper/rake/db.rake @@ -21,7 +21,7 @@ namespace :doorkeeper do task expired_tokens: "doorkeeper:setup" do expirable_tokens = Doorkeeper.config.access_token_model.where(refresh_token: nil) cleaner = Doorkeeper::StaleRecordsCleaner.new(expirable_tokens) - cleaner.clean_expired(Doorkeeper.configuration.access_token_expires_in) + cleaner.clean_expired(Doorkeeper.config.access_token_expires_in) end desc "Removes stale access grants" @@ -33,7 +33,7 @@ namespace :doorkeeper do desc "Removes expired (TTL passed) access grants" task expired_grants: "doorkeeper:setup" do cleaner = Doorkeeper::StaleRecordsCleaner.new(Doorkeeper::AccessGrant) - cleaner.clean_expired(Doorkeeper.configuration.authorization_code_expires_in) + cleaner.clean_expired(Doorkeeper.config.authorization_code_expires_in) end end end diff --git a/lib/doorkeeper/request.rb b/lib/doorkeeper/request.rb index a60830ff8..62606920c 100644 --- a/lib/doorkeeper/request.rb +++ b/lib/doorkeeper/request.rb @@ -24,7 +24,7 @@ def get_strategy(grant_type, available) private def token_grant_types - Doorkeeper.configuration.token_grant_types + Doorkeeper.config.token_grant_types end def build_strategy_class(grant_or_request_type) diff --git a/lib/doorkeeper/request/authorization_code.rb b/lib/doorkeeper/request/authorization_code.rb index 84f22719e..6f69a5650 100644 --- a/lib/doorkeeper/request/authorization_code.rb +++ b/lib/doorkeeper/request/authorization_code.rb @@ -7,7 +7,7 @@ class AuthorizationCode < Strategy def request @request ||= OAuth::AuthorizationCodeRequest.new( - Doorkeeper.configuration, + Doorkeeper.config, grant, client, parameters, diff --git a/lib/doorkeeper/request/client_credentials.rb b/lib/doorkeeper/request/client_credentials.rb index fe336a7b8..a86736d91 100644 --- a/lib/doorkeeper/request/client_credentials.rb +++ b/lib/doorkeeper/request/client_credentials.rb @@ -7,7 +7,7 @@ class ClientCredentials < Strategy def request @request ||= OAuth::ClientCredentialsRequest.new( - Doorkeeper.configuration, + Doorkeeper.config, client, parameters, ) diff --git a/lib/doorkeeper/request/password.rb b/lib/doorkeeper/request/password.rb index 02eaccae8..16c2aba4c 100644 --- a/lib/doorkeeper/request/password.rb +++ b/lib/doorkeeper/request/password.rb @@ -7,7 +7,7 @@ class Password < Strategy def request @request ||= OAuth::PasswordAccessTokenRequest.new( - Doorkeeper.configuration, + Doorkeeper.config, client, resource_owner, parameters, diff --git a/lib/doorkeeper/request/refresh_token.rb b/lib/doorkeeper/request/refresh_token.rb index 35c6ab6d0..1c5edfc29 100644 --- a/lib/doorkeeper/request/refresh_token.rb +++ b/lib/doorkeeper/request/refresh_token.rb @@ -11,7 +11,7 @@ def refresh_token def request @request ||= OAuth::RefreshTokenRequest.new( - Doorkeeper.configuration, + Doorkeeper.config, refresh_token, credentials, parameters, ) diff --git a/lib/doorkeeper/server.rb b/lib/doorkeeper/server.rb index 68eb1b2ee..5e0fd059e 100644 --- a/lib/doorkeeper/server.rb +++ b/lib/doorkeeper/server.rb @@ -37,7 +37,7 @@ def resource_owner end def credentials - methods = Doorkeeper.configuration.client_credentials_methods + methods = Doorkeeper.config.client_credentials_methods @credentials ||= OAuth::Client::Credentials.from_request(context.request, *methods) end end diff --git a/lib/doorkeeper/stale_records_cleaner.rb b/lib/doorkeeper/stale_records_cleaner.rb index c2d10c071..4214f4c2b 100644 --- a/lib/doorkeeper/stale_records_cleaner.rb +++ b/lib/doorkeeper/stale_records_cleaner.rb @@ -14,7 +14,7 @@ def self.for(base_scope) end def self.configured_orm - Doorkeeper.configuration.orm + Doorkeeper.config.orm end def self.new(base_scope) diff --git a/spec/controllers/application_metal_controller_spec.rb b/spec/controllers/application_metal_controller_spec.rb index a15b9a654..829173f04 100644 --- a/spec/controllers/application_metal_controller_spec.rb +++ b/spec/controllers/application_metal_controller_spec.rb @@ -21,7 +21,7 @@ def create end describe "enforce_content_type" do - before { allow(Doorkeeper.configuration).to receive(:enforce_content_type).and_return(flag) } + before { allow(Doorkeeper.config).to receive(:enforce_content_type).and_return(flag) } context "enabled" do let(:flag) { true } diff --git a/spec/controllers/applications_controller_spec.rb b/spec/controllers/applications_controller_spec.rb index 23d247f5b..90c72061b 100644 --- a/spec/controllers/applications_controller_spec.rb +++ b/spec/controllers/applications_controller_spec.rb @@ -117,7 +117,7 @@ module Doorkeeper context "when admin is not authenticated" do before do - allow(Doorkeeper.configuration).to receive(:authenticate_admin).and_return(proc do + allow(Doorkeeper.config).to receive(:authenticate_admin).and_return(proc do redirect_to main_app.root_url end) end @@ -142,7 +142,8 @@ module Doorkeeper context "when admin is authenticated" do context "when application secrets are hashed" do before do - allow(Doorkeeper.configuration).to receive(:application_secret_strategy).and_return(Doorkeeper::SecretStoring::Sha256Hash) + allow(Doorkeeper.configuration) + .to receive(:application_secret_strategy).and_return(Doorkeeper::SecretStoring::Sha256Hash) end it "shows the application secret after creating a new application" do diff --git a/spec/controllers/authorizations_controller_spec.rb b/spec/controllers/authorizations_controller_spec.rb index ea03b9e65..d3939c232 100644 --- a/spec/controllers/authorizations_controller_spec.rb +++ b/spec/controllers/authorizations_controller_spec.rb @@ -27,8 +27,8 @@ def query_params end) end - allow(Doorkeeper.configuration).to receive(:grant_flows).and_return(["implicit"]) - allow(Doorkeeper.configuration).to receive(:authenticate_resource_owner).and_return(->(_) { authenticator_method }) + allow(Doorkeeper.config).to receive(:grant_flows).and_return(["implicit"]) + allow(Doorkeeper.config).to receive(:authenticate_resource_owner).and_return(->(_) { authenticator_method }) allow(controller).to receive(:authenticator_method).and_return(user) expect(controller).to receive(:authenticator_method).at_most(:once) end @@ -69,7 +69,7 @@ def query_params describe "POST #create in API mode" do before do - allow(Doorkeeper.configuration).to receive(:api_only).and_return(true) + allow(Doorkeeper.config).to receive(:api_only).and_return(true) post :create, params: { client_id: client.uid, response_type: "token", redirect_uri: client.redirect_uri } end @@ -177,7 +177,7 @@ def query_params describe "POST #create in API mode with errors" do context "when missing client_id" do before do - allow(Doorkeeper.configuration).to receive(:api_only).and_return(true) + allow(Doorkeeper.config).to receive(:api_only).and_return(true) post :create, params: { client_id: "", @@ -209,7 +209,7 @@ def query_params context "when other error happens" do before do - allow(Doorkeeper.configuration).to receive(:api_only).and_return(true) + allow(Doorkeeper.config).to receive(:api_only).and_return(true) default_scopes_exist :public post :create, params: { @@ -251,7 +251,7 @@ def query_params describe "POST #create with application already authorized" do before do - allow(Doorkeeper.configuration).to receive(:reuse_access_token).and_return(true) + allow(Doorkeeper.config).to receive(:reuse_access_token).and_return(true) access_token.save! @@ -286,12 +286,12 @@ def query_params end it "should call :before_successful_authorization callback" do - expect(Doorkeeper.configuration) + expect(Doorkeeper.config) .to receive_message_chain(:before_successful_authorization, :call).with(instance_of(described_class)) end it "should call :after_successful_authorization callback" do - expect(Doorkeeper.configuration) + expect(Doorkeeper.config) .to receive_message_chain(:after_successful_authorization, :call).with(instance_of(described_class)) end end @@ -302,18 +302,18 @@ def query_params end it "should not call :before_successful_authorization callback" do - expect(Doorkeeper.configuration).not_to receive(:before_successful_authorization) + expect(Doorkeeper.config).not_to receive(:before_successful_authorization) end it "should not call :after_successful_authorization callback" do - expect(Doorkeeper.configuration).not_to receive(:after_successful_authorization) + expect(Doorkeeper.config).not_to receive(:after_successful_authorization) end end end describe "GET #new token request with native url and skip_authorization true" do before do - allow(Doorkeeper.configuration).to receive(:skip_authorization).and_return(proc do + allow(Doorkeeper.config).to receive(:skip_authorization).and_return(proc do true end) @@ -342,8 +342,8 @@ def query_params describe "GET #new code request with native url and skip_authorization true" do before do - allow(Doorkeeper.configuration).to receive(:grant_flows).and_return(%w[authorization_code]) - allow(Doorkeeper.configuration).to receive(:skip_authorization).and_return(proc do + allow(Doorkeeper.config).to receive(:grant_flows).and_return(%w[authorization_code]) + allow(Doorkeeper.config).to receive(:skip_authorization).and_return(proc do true end) @@ -373,7 +373,7 @@ def query_params describe "GET #new with skip_authorization true" do before do - allow(Doorkeeper.configuration).to receive(:skip_authorization).and_return(proc do + allow(Doorkeeper.config).to receive(:skip_authorization).and_return(proc do true end) @@ -412,7 +412,7 @@ def query_params describe "GET #new in API mode" do before do - allow(Doorkeeper.configuration).to receive(:api_only).and_return(true) + allow(Doorkeeper.config).to receive(:api_only).and_return(true) get :new, params: { client_id: client.uid, diff --git a/spec/lib/doorkeeper_spec.rb b/spec/lib/doorkeeper_spec.rb index 062b3cd4c..04d44c77e 100644 --- a/spec/lib/doorkeeper_spec.rb +++ b/spec/lib/doorkeeper_spec.rb @@ -7,7 +7,7 @@ let(:request) { double } it "calls OAuth::Token#authenticate" do - token_strategies = Doorkeeper.configuration.access_token_methods + token_strategies = Doorkeeper.config.access_token_methods expect(Doorkeeper::OAuth::Token).to receive(:authenticate) .with(request, *token_strategies)