Skip to content

Commit

Permalink
Proc auth value backport cleanup (#1326)
Browse files Browse the repository at this point in the history
  • Loading branch information
iMacTia committed Sep 18, 2021
1 parent 2a5702a commit 8cd1bf1
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 33 deletions.
1 change: 1 addition & 0 deletions .github/workflows/ci.yml
Expand Up @@ -53,6 +53,7 @@ jobs:

- name: Install dependencies
run: |
sudo apt-get update
sudo apt-get install libcurl4-openssl-dev
- name: Build
Expand Down
40 changes: 7 additions & 33 deletions lib/faraday/request/authorization.rb
Expand Up @@ -15,7 +15,8 @@ class Authorization < Faraday::Middleware
# @return [String] a header value
def self.header(type, token)
case token
when String, Symbol
when String, Symbol, Proc
token = token.call if token.is_a?(Proc)
"#{type} #{token}"
when Hash
build_hash(type.to_s, token)
Expand All @@ -34,54 +35,27 @@ def self.build_hash(type, hash)
comma = ', '
values = []
hash.each do |key, value|
value = value.call if value.is_a?(Proc)
values << "#{key}=#{value.to_s.inspect}"
end
"#{type} #{values * comma}"
end

# @param app [#call]
# @param type [String, Symbol] Type of Authorization
# @param params [Array<String, Proc>] 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.
# @param param [String, Symbol, Hash, Proc] parameter to build the Authorization header.
# This value can be a proc, in which case it will be invoked on each request.
def initialize(app, type, *params)
def initialize(app, type, param)
@type = type
@params = params
@header_value = self.class.header(type, params[0]) unless params[0].is_a? Proc
@param = param
super(app)
end

# @param env [Faraday::Env]
def on_request(env)
return if env.request_headers[KEY]

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

private

# @param type [String, Symbol]
# @param params [Array]
# @return [String] a header value
def header_from(type, *params)
return @header_value if @header_value

if type.to_s.casecmp('basic').zero? && params.size == 2
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)
"#{type} #{value}"
end
end

def basic_header_from(login, pass)
value = Base64.encode64("#{login}:#{pass}")
value.delete!("\n")
"Basic #{value}"
env.request_headers[KEY] = self.class.header(@type, @param)
end
end
end
Expand Down

0 comments on commit 8cd1bf1

Please sign in to comment.