diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index be1382ad0..e3c34a257 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -53,6 +53,7 @@ jobs: - name: Install dependencies run: | + sudo apt-get update sudo apt-get install libcurl4-openssl-dev - name: Build diff --git a/lib/faraday/request/authorization.rb b/lib/faraday/request/authorization.rb index 8af00d7f7..45bb21c50 100644 --- a/lib/faraday/request/authorization.rb +++ b/lib/faraday/request/authorization.rb @@ -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) @@ -34,6 +35,7 @@ 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}" @@ -41,14 +43,11 @@ def self.build_hash(type, hash) # @param app [#call] # @param type [String, Symbol] Type of Authorization - # @param params [Array] 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 @@ -56,32 +55,7 @@ def initialize(app, type, *params) 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