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

Refactor BaseRequest callbacks into configurable lambdas #1032

Merged
merged 2 commits into from
Feb 26, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion lib/doorkeeper/config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,9 @@ 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 }
Expand Down
9 changes: 7 additions & 2 deletions lib/doorkeeper/oauth/base_request.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,14 @@ 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
11 changes: 11 additions & 0 deletions lib/generators/doorkeeper/templates/initializer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
11 changes: 11 additions & 0 deletions spec/dummy/config/initializers/doorkeeper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
6 changes: 3 additions & 3 deletions spec/lib/oauth/authorization_code_request_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
6 changes: 3 additions & 3 deletions spec/lib/oauth/password_access_token_request_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
6 changes: 3 additions & 3 deletions spec/lib/oauth/refresh_token_request_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down