From 034d87eff801ef6576fa1af31ba995eefa435924 Mon Sep 17 00:00:00 2001 From: Nikita Bulai Date: Fri, 8 May 2020 15:17:34 +0300 Subject: [PATCH] Code refactoring --- lib/doorkeeper/engine.rb | 2 +- lib/doorkeeper/oauth/authorization/code.rb | 2 +- lib/doorkeeper/oauth/authorization/token.rb | 2 +- lib/doorkeeper/oauth/code_request.rb | 4 ++-- lib/doorkeeper/oauth/token.rb | 6 +++--- lib/doorkeeper/oauth/token_introspection.rb | 6 +----- lib/doorkeeper/oauth/token_request.rb | 2 +- lib/doorkeeper/server.rb | 2 +- lib/doorkeeper/stale_records_cleaner.rb | 8 ++++---- spec/lib/oauth/code_response_spec.rb | 2 +- spec/lib/server_spec.rb | 3 ++- 11 files changed, 18 insertions(+), 21 deletions(-) diff --git a/lib/doorkeeper/engine.rb b/lib/doorkeeper/engine.rb index ba66b645a..c49b93e6c 100644 --- a/lib/doorkeeper/engine.rb +++ b/lib/doorkeeper/engine.rb @@ -4,7 +4,7 @@ module Doorkeeper class Engine < Rails::Engine initializer "doorkeeper.params.filter" do |app| parameters = %w[client_secret code authentication_token access_token refresh_token] - app.config.filter_parameters << /^(#{Regexp.union parameters})$/ + app.config.filter_parameters << /^(#{Regexp.union(parameters)})$/ end initializer "doorkeeper.routes" do diff --git a/lib/doorkeeper/oauth/authorization/code.rb b/lib/doorkeeper/oauth/authorization/code.rb index 45873e4e7..94f7d0ab0 100644 --- a/lib/doorkeeper/oauth/authorization/code.rb +++ b/lib/doorkeeper/oauth/authorization/code.rb @@ -11,7 +11,7 @@ def initialize(pre_auth, resource_owner) @resource_owner = resource_owner end - def issue_token + def issue_token! return @token if defined?(@token) @token = Doorkeeper.config.access_grant_model.create!(access_grant_attributes) diff --git a/lib/doorkeeper/oauth/authorization/token.rb b/lib/doorkeeper/oauth/authorization/token.rb index 424ceb22a..a41c17621 100644 --- a/lib/doorkeeper/oauth/authorization/token.rb +++ b/lib/doorkeeper/oauth/authorization/token.rb @@ -48,7 +48,7 @@ def initialize(pre_auth, resource_owner) @resource_owner = resource_owner end - def issue_token + def issue_token! return @token if defined?(@token) context = self.class.build_context( diff --git a/lib/doorkeeper/oauth/code_request.rb b/lib/doorkeeper/oauth/code_request.rb index e6caa66cc..a2d0eafa6 100644 --- a/lib/doorkeeper/oauth/code_request.rb +++ b/lib/doorkeeper/oauth/code_request.rb @@ -6,13 +6,13 @@ class CodeRequest attr_reader :pre_auth, :resource_owner def initialize(pre_auth, resource_owner) - @pre_auth = pre_auth + @pre_auth = pre_auth @resource_owner = resource_owner end def authorize auth = Authorization::Code.new(pre_auth, resource_owner) - auth.issue_token + auth.issue_token! CodeResponse.new(pre_auth, auth) end diff --git a/lib/doorkeeper/oauth/token.rb b/lib/doorkeeper/oauth/token.rb index e6bff44f9..a56c7b3b5 100644 --- a/lib/doorkeeper/oauth/token.rb +++ b/lib/doorkeeper/oauth/token.rb @@ -32,13 +32,13 @@ def from_bearer_param(request) def from_bearer_authorization(request) pattern = /^Bearer /i - header = request.authorization + header = request.authorization token_from_header(header, pattern) if match?(header, pattern) end def from_basic_authorization(request) pattern = /^Basic /i - header = request.authorization + header = request.authorization token_from_basic_header(header, pattern) if match?(header, pattern) end @@ -54,7 +54,7 @@ def decode_basic_credentials_token(encoded_header) end def token_from_header(header, pattern) - header.gsub pattern, "" + header.gsub(pattern, "") end def match?(header, pattern) diff --git a/lib/doorkeeper/oauth/token_introspection.rb b/lib/doorkeeper/oauth/token_introspection.rb index 514b2be02..dd753e2b4 100644 --- a/lib/doorkeeper/oauth/token_introspection.rb +++ b/lib/doorkeeper/oauth/token_introspection.rb @@ -179,11 +179,7 @@ def token_introspection_allowed?(auth_client: nil, auth_token: nil) allow_introspection = Doorkeeper.config.allow_token_introspection return allow_introspection unless allow_introspection.respond_to?(:call) - allow_introspection.call( - @token, - auth_client, - auth_token, - ) + allow_introspection.call(@token, auth_client, auth_token) end # Allows to customize introspection response. diff --git a/lib/doorkeeper/oauth/token_request.rb b/lib/doorkeeper/oauth/token_request.rb index 1b3ee95cb..f19c372b5 100644 --- a/lib/doorkeeper/oauth/token_request.rb +++ b/lib/doorkeeper/oauth/token_request.rb @@ -12,7 +12,7 @@ def initialize(pre_auth, resource_owner) def authorize auth = Authorization::Token.new(pre_auth, resource_owner) - auth.issue_token + auth.issue_token! CodeResponse.new(pre_auth, auth, response_on_fragment: true) end diff --git a/lib/doorkeeper/server.rb b/lib/doorkeeper/server.rb index 248819d48..4ba0e3665 100644 --- a/lib/doorkeeper/server.rb +++ b/lib/doorkeeper/server.rb @@ -4,7 +4,7 @@ module Doorkeeper class Server attr_reader :context - def initialize(context = nil) + def initialize(context) @context = context end diff --git a/lib/doorkeeper/stale_records_cleaner.rb b/lib/doorkeeper/stale_records_cleaner.rb index 4214f4c2b..443d1a852 100644 --- a/lib/doorkeeper/stale_records_cleaner.rb +++ b/lib/doorkeeper/stale_records_cleaner.rb @@ -13,12 +13,12 @@ def self.for(base_scope) raise Doorkeeper::Errors::NoOrmCleaner, "'#{configured_orm}' ORM has no cleaner!" end - def self.configured_orm - Doorkeeper.config.orm - end - def self.new(base_scope) self.for(base_scope) end + + def self.configured_orm + Doorkeeper.config.orm + end end end diff --git a/spec/lib/oauth/code_response_spec.rb b/spec/lib/oauth/code_response_spec.rb index 93725b4b8..f94e32816 100644 --- a/spec/lib/oauth/code_response_spec.rb +++ b/spec/lib/oauth/code_response_spec.rb @@ -21,7 +21,7 @@ let :auth do Doorkeeper::OAuth::Authorization::Token.new(pre_auth, owner).tap do |c| - c.issue_token + c.issue_token! allow(c.token).to receive(:expires_in_seconds).and_return(3600) end end diff --git a/spec/lib/server_spec.rb b/spec/lib/server_spec.rb index aefc6afbf..9397d071c 100644 --- a/spec/lib/server_spec.rb +++ b/spec/lib/server_spec.rb @@ -4,9 +4,10 @@ describe Doorkeeper::Server do let(:fake_class) { double :fake_class } + let(:context) { double :context } subject do - described_class.new + described_class.new(context) end describe ".authorization_request" do