Sha256: 2e85709f5742e191b43c0769c2133730c6008d193eafcb739899658aaa71dbdb

Contents?: true

Size: 1.52 KB

Versions: 3

Compression:

Stored size: 1.52 KB

Contents

# frozen_string_literal: true

module Faraday
  class Request
    # Request middleware for the Authorization HTTP header
    class Authorization < Faraday::Middleware
      KEY = 'Authorization'

      # @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.
      #   This value can be a proc, in which case it will be invoked on each request.
      def initialize(app, type, *params)
        @type = type
        @params = params
        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)
        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)
          "#{type} #{value}"
        end
      end
    end
  end
end

Faraday::Request.register_middleware(authorization: Faraday::Request::Authorization)

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
faraday-2.0.0.alpha.pre.3 lib/faraday/request/authorization.rb
faraday-2.0.0.alpha.pre.2 lib/faraday/request/authorization.rb
faraday-2.0.0.alpha.pre.1 lib/faraday/request/authorization.rb