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

This commit is contained in:
Joe Swatosh 2022-10-03 05:20:07 -07:00 committed by GitHub
parent 7926e3aa89
commit e1dbdf065e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 51 additions and 3 deletions

View File

@ -30,6 +30,14 @@ Faraday.new(...) do |conn|
end 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 ### Basic Authentication
The middleware will automatically Base64 encode your Basic username and password: The middleware will automatically Base64 encode your Basic username and password:

View File

@ -23,22 +23,27 @@ module Faraday
def on_request(env) def on_request(env)
return if env.request_headers[KEY] return if env.request_headers[KEY]
env.request_headers[KEY] = header_from(@type, *@params) env.request_headers[KEY] = header_from(@type, env, *@params)
end end
private private
# @param type [String, Symbol] # @param type [String, Symbol]
# @param env [Faraday::Env]
# @param params [Array] # @param params [Array]
# @return [String] a header value # @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 if type.to_s.casecmp('basic').zero? && params.size == 2
Utils.basic_header_from(*params) Utils.basic_header_from(*params)
elsif params.size != 1 elsif params.size != 1
raise ArgumentError, "Unexpected params received (got #{params.size} instead of 1)" raise ArgumentError, "Unexpected params received (got #{params.size} instead of 1)"
else else
value = params.first 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}" "#{type} #{value}"
end end
end end

View File

@ -72,6 +72,41 @@ RSpec.describe Faraday::Request::Authorization do
include_examples 'does not interfere with existing authentication' include_examples 'does not interfere with existing authentication'
end 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 context 'when passed too many arguments' do
let(:auth_config) { %w[baz foo] } let(:auth_config) { %w[baz foo] }