Skip to content

Commit

Permalink
Forward the env to Request::Authorization#header_from (#1450)
Browse files Browse the repository at this point in the history
  • Loading branch information
swatosh authored Oct 3, 2022
1 parent 7926e3a commit e1dbdf0
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 3 deletions.
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

0 comments on commit e1dbdf0

Please sign in to comment.