From 763abb7bef8e5f400a56661141fd908e8b7659ab Mon Sep 17 00:00:00 2001 From: Justin Bull Date: Sun, 11 Feb 2018 09:54:51 -0500 Subject: [PATCH 1/2] Refactor #1001 into configurable lambdas --- lib/doorkeeper/config.rb | 3 ++- lib/doorkeeper/oauth/base_request.rb | 8 ++++++-- lib/generators/doorkeeper/templates/initializer.rb | 11 +++++++++++ spec/dummy/config/initializers/doorkeeper.rb | 11 +++++++++++ spec/lib/oauth/authorization_code_request_spec.rb | 6 +++--- spec/lib/oauth/password_access_token_request_spec.rb | 6 +++--- spec/lib/oauth/refresh_token_request_spec.rb | 6 +++--- 7 files changed, 39 insertions(+), 12 deletions(-) diff --git a/lib/doorkeeper/config.rb b/lib/doorkeeper/config.rb index a41239bcd..9f0d5045a 100644 --- a/lib/doorkeeper/config.rb +++ b/lib/doorkeeper/config.rb @@ -228,7 +228,8 @@ def option(name, options = {}) warn(I18n.translate('doorkeeper.errors.messages.credential_flow_not_configured')) nil end) - + option :before_successful_strategy_response, default: ->(_request) {} + option :after_successful_strategy_response, default: ->(_request, _response) {} option :skip_authorization, default: ->(_routes) {} option :access_token_expires_in, default: 7200 option :custom_access_token_expires_in, default: ->(_app) { nil } diff --git a/lib/doorkeeper/oauth/base_request.rb b/lib/doorkeeper/oauth/base_request.rb index 563b9e98a..cb8c580c2 100644 --- a/lib/doorkeeper/oauth/base_request.rb +++ b/lib/doorkeeper/oauth/base_request.rb @@ -42,9 +42,13 @@ def find_or_create_access_token(client, resource_owner_id, scopes, server) ) end - def before_successful_response; end + def before_successful_response + Doorkeeper.configuration.before_successful_strategy_response.call(self) + end - def after_successful_response; end + def after_successful_response + Doorkeeper.configuration.after_successful_strategy_response.call(self, @response) + end end end end diff --git a/lib/generators/doorkeeper/templates/initializer.rb b/lib/generators/doorkeeper/templates/initializer.rb index 7406f8661..8dc09c414 100644 --- a/lib/generators/doorkeeper/templates/initializer.rb +++ b/lib/generators/doorkeeper/templates/initializer.rb @@ -102,6 +102,17 @@ # # grant_flows %w[authorization_code client_credentials] + # Hook into the strategies' request & response life-cycle in case your + # application needs advanced customization or logging: + # + # before_successful_strategy_response do |request| + # puts "BEFORE HOOK FIRED! #{request}" + # end + # + # after_successful_strategy_response do |request, response| + # puts "AFTER HOOK FIRED! #{request}, #{response}" + # end + # Under some circumstances you might want to have applications auto-approved, # so that the user skips the authorization step. # For example if dealing with a trusted application. diff --git a/spec/dummy/config/initializers/doorkeeper.rb b/spec/dummy/config/initializers/doorkeeper.rb index 60cb60e9a..7fdad5365 100644 --- a/spec/dummy/config/initializers/doorkeeper.rb +++ b/spec/dummy/config/initializers/doorkeeper.rb @@ -84,6 +84,17 @@ # # grant_flows %w[authorization_code client_credentials] + # Hook into the strategies' request & response life-cycle in case your + # application needs advanced customization or logging: + # + # before_successful_strategy_response do |request| + # puts "BEFORE HOOK FIRED! #{request}" + # end + # + # after_successful_strategy_response do |request, response| + # puts "AFTER HOOK FIRED! #{request}, #{response}" + # end + # Under some circumstances you might want to have applications auto-approved, # so that the user skips the authorization step. # For example if dealing with a trusted application. diff --git a/spec/lib/oauth/authorization_code_request_spec.rb b/spec/lib/oauth/authorization_code_request_spec.rb index 0a31c8b48..6883ee448 100644 --- a/spec/lib/oauth/authorization_code_request_spec.rb +++ b/spec/lib/oauth/authorization_code_request_spec.rb @@ -80,9 +80,9 @@ module Doorkeeper::OAuth end.to_not change { Doorkeeper::AccessToken.count } end - it "calls BaseRequest callback methods" do - expect_any_instance_of(BaseRequest).to receive(:before_successful_response).once - expect_any_instance_of(BaseRequest).to receive(:after_successful_response).once + it "calls configured request callback methods" do + expect(Doorkeeper.configuration.before_successful_strategy_response).to receive(:call).with(subject).once + expect(Doorkeeper.configuration.after_successful_strategy_response).to receive(:call).with(subject, instance_of(Doorkeeper::OAuth::TokenResponse)).once subject.authorize end diff --git a/spec/lib/oauth/password_access_token_request_spec.rb b/spec/lib/oauth/password_access_token_request_spec.rb index 4612160dc..3f8bdb82a 100644 --- a/spec/lib/oauth/password_access_token_request_spec.rb +++ b/spec/lib/oauth/password_access_token_request_spec.rb @@ -67,9 +67,9 @@ module Doorkeeper::OAuth end.to_not change { Doorkeeper::AccessToken.count } end - it "calls BaseRequest callback methods" do - expect_any_instance_of(BaseRequest).to receive(:before_successful_response).once - expect_any_instance_of(BaseRequest).to receive(:after_successful_response).once + it "calls configured request callback methods" do + expect(Doorkeeper.configuration.before_successful_strategy_response).to receive(:call).with(subject).once + expect(Doorkeeper.configuration.after_successful_strategy_response).to receive(:call).with(subject, instance_of(Doorkeeper::OAuth::TokenResponse)).once subject.authorize end diff --git a/spec/lib/oauth/refresh_token_request_spec.rb b/spec/lib/oauth/refresh_token_request_spec.rb index a36b0dde6..610515af1 100644 --- a/spec/lib/oauth/refresh_token_request_spec.rb +++ b/spec/lib/oauth/refresh_token_request_spec.rb @@ -44,9 +44,9 @@ module Doorkeeper::OAuth expect { subject.authorize }.to change { refresh_token.revoked? }.from(false).to(true) end - it "calls BaseRequest callback methods" do - expect_any_instance_of(BaseRequest).to receive(:before_successful_response).once - expect_any_instance_of(BaseRequest).to receive(:after_successful_response).once + it "calls configured request callback methods" do + expect(Doorkeeper.configuration.before_successful_strategy_response).to receive(:call).with(subject).once + expect(Doorkeeper.configuration.after_successful_strategy_response).to receive(:call).with(subject, instance_of(Doorkeeper::OAuth::TokenResponse)).once subject.authorize end From f2eff53f1857ffaa3e454b7ed84dc2d9e72f176a Mon Sep 17 00:00:00 2001 From: Justin Bull Date: Sun, 11 Feb 2018 10:00:16 -0500 Subject: [PATCH 2/2] [Lint] Lines too long --- lib/doorkeeper/config.rb | 3 ++- lib/doorkeeper/oauth/base_request.rb | 3 ++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/lib/doorkeeper/config.rb b/lib/doorkeeper/config.rb index 9f0d5045a..b2d92b0a8 100644 --- a/lib/doorkeeper/config.rb +++ b/lib/doorkeeper/config.rb @@ -229,7 +229,8 @@ def option(name, options = {}) nil end) option :before_successful_strategy_response, default: ->(_request) {} - option :after_successful_strategy_response, default: ->(_request, _response) {} + option :after_successful_strategy_response, + default: ->(_request, _response) {} option :skip_authorization, default: ->(_routes) {} option :access_token_expires_in, default: 7200 option :custom_access_token_expires_in, default: ->(_app) { nil } diff --git a/lib/doorkeeper/oauth/base_request.rb b/lib/doorkeeper/oauth/base_request.rb index cb8c580c2..cd85a8ec2 100644 --- a/lib/doorkeeper/oauth/base_request.rb +++ b/lib/doorkeeper/oauth/base_request.rb @@ -47,7 +47,8 @@ def before_successful_response end def after_successful_response - Doorkeeper.configuration.after_successful_strategy_response.call(self, @response) + Doorkeeper.configuration.after_successful_strategy_response. + call(self, @response) end end end