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

Forward the env to Request::Authorization#header_from #1450

Merged
merged 1 commit into from
Oct 3, 2022
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
8 changes: 8 additions & 0 deletions docs/middleware/request/authentication.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,14 @@ Faraday.new(...) do |conn|
end
```

If the proc takes an argument, it will receive the forwarded `env`

```ruby
Faraday.new(...) do |conn|
conn.request :authorization, 'Bearer', ->(env) { MyAuthStorage.get_auth_token(env) }
end
```

### Basic Authentication

The middleware will automatically Base64 encode your Basic username and password:
Expand Down
11 changes: 8 additions & 3 deletions lib/faraday/request/authorization.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,22 +23,27 @@ def initialize(app, type, *params)
def on_request(env)
return if env.request_headers[KEY]

env.request_headers[KEY] = header_from(@type, *@params)
env.request_headers[KEY] = header_from(@type, env, *@params)
end

private

# @param type [String, Symbol]
# @param env [Faraday::Env]
# @param params [Array]
# @return [String] a header value
def header_from(type, *params)
def header_from(type, env, *params)
if type.to_s.casecmp('basic').zero? && params.size == 2
Utils.basic_header_from(*params)
elsif params.size != 1
raise ArgumentError, "Unexpected params received (got #{params.size} instead of 1)"
else
value = params.first
value = value.call if value.is_a?(Proc) || value.respond_to?(:call)
if (value.is_a?(Proc) && value.arity == 1) || (value.respond_to?(:call) && value.method(:call).arity == 1)
value = value.call(env)
elsif value.is_a?(Proc) || value.respond_to?(:call)
value = value.call
end
"#{type} #{value}"
end
end
Expand Down
35 changes: 35 additions & 0 deletions spec/faraday/request/authorization_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,41 @@
include_examples 'does not interfere with existing authentication'
end

context 'with an argument' do
let(:response) { conn.get('/auth-echo', nil, 'middle' => 'crunchy surprise') }

context 'when passed a proc' do
let(:auth_config) { [proc { |env| "proc #{env.request_headers['middle']}" }] }

it { expect(response.body).to eq('Bearer proc crunchy surprise') }

include_examples 'does not interfere with existing authentication'
end

context 'when passed a lambda' do
let(:auth_config) { [->(env) { "lambda #{env.request_headers['middle']}" }] }

it { expect(response.body).to eq('Bearer lambda crunchy surprise') }

include_examples 'does not interfere with existing authentication'
end

context 'when passed a callable with an argument' do
let(:callable) do
Class.new do
def call(env)
"callable #{env.request_headers['middle']}"
end
end.new
end
let(:auth_config) { [callable] }

it { expect(response.body).to eq('Bearer callable crunchy surprise') }

include_examples 'does not interfere with existing authentication'
end
end

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

Expand Down