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

Callable authorizers #1345

Merged
merged 4 commits into from
Dec 15, 2021
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
7 changes: 4 additions & 3 deletions lib/faraday/request/authorization.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,11 @@ class Authorization < Faraday::Middleware

# @param app [#call]
# @param type [String, Symbol] Type of Authorization
# @param params [Array<String, Proc>] parameters to build the Authorization header.
# @param params [Array<String, Proc, #call>] parameters to build the Authorization header.
# If the type is `:basic`, then these can be a login and password pair.
# Otherwise, a single value is expected that will be appended after the type.
# This value can be a proc, in which case it will be invoked on each request.
# This value can be a proc or an object responding to `.call`, in which case
# it will be invoked on each request.
def initialize(app, type, *params)
@type = type
@params = params
Expand All @@ -37,7 +38,7 @@ def header_from(type, *params)
raise ArgumentError, "Unexpected params received (got #{params.size} instead of 1)"
else
value = params.first
value = value.call if value.is_a?(Proc)
value = value.call if value.is_a?(Proc) || value.respond_to?(:call)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The || is on purpose, because checking the ancestors of an object should be faster than looking up a method. This way, there should be no performance penalty on existing code bases which use a proc already.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Love this attention to performance, thanks for caring and explaining!

"#{type} #{value}"
end
end
Expand Down
9 changes: 9 additions & 0 deletions spec/faraday/request/authorization_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,15 @@
include_examples 'does not interfere with existing authentication'
end

context 'when passed a callable' do
let(:callable) { double('Callable Authorizer', call: 'custom_from_callable') }
let(:auth_config) { [callable] }

it { expect(response.body).to eq('Bearer custom_from_callable') }

include_examples 'does not interfere with existing authentication'
end

context 'when passed too many arguments' do
let(:auth_config) { %w[baz foo] }

Expand Down