Sha256: d86bf7d3d1252ccae0f98ab980c990c81ac2092eec85433e75c9f3fef50ffaac

Contents?: true

Size: 1.77 KB

Versions: 25

Compression:

Stored size: 1.77 KB

Contents

# frozen_string_literal: true

require 'base64'

module Faraday
  class Request
    # Request middleware for the Authorization HTTP header
    class Authorization < Faraday::Middleware
      unless defined?(::Faraday::Request::Authorization::KEY)
        KEY = 'Authorization'
      end

      # @param type [String, Symbol]
      # @param token [String, Symbol, Hash]
      # @return [String] a header value
      def self.header(type, token)
        case token
        when String, Symbol, Proc
          token = token.call if token.is_a?(Proc)
          "#{type} #{token}"
        when Hash
          build_hash(type.to_s, token)
        else
          raise ArgumentError,
                "Can't build an Authorization #{type}" \
                  "header from #{token.inspect}"
        end
      end

      # @param type [String]
      # @param hash [Hash]
      # @return [String] type followed by comma-separated key=value pairs
      # @api private
      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 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, param)
        @type = type
        @param = param
        super(app)
      end

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

        env.request_headers[KEY] = self.class.header(@type, @param)
      end
    end
  end
end

Version data entries

25 entries across 24 versions & 6 rubygems

Version Path
avalara_sdk-24.12.2 vendor/bundle/ruby/2.7.0/gems/faraday-1.10.4/lib/faraday/request/authorization.rb
avalara_sdk-24.12.1 vendor/bundle/ruby/2.7.0/gems/faraday-1.10.4/lib/faraday/request/authorization.rb
avalara_sdk-24.12.0 vendor/bundle/ruby/2.7.0/gems/faraday-1.10.4/lib/faraday/request/authorization.rb
minato_ruby_api_client-0.2.2 vendor/bundle/ruby/3.2.0/gems/faraday-1.10.3/lib/faraday/request/authorization.rb
faraday-1.10.4 lib/faraday/request/authorization.rb
avalara_sdk-24.2.29 vendor/bundle/ruby/2.7.0/gems/faraday-1.10.3/lib/faraday/request/authorization.rb
faraday-1.10.3 lib/faraday/request/authorization.rb
faraday-1.10.2 lib/faraday/request/authorization.rb
faraday-1.10.1 lib/faraday/request/authorization.rb
op_connect-0.1.2 vendor/bundle/ruby/3.1.0/gems/faraday-1.10.0/lib/faraday/request/authorization.rb
plaid-14.13.0 vendor/bundle/ruby/3.0.0/gems/faraday-1.10.0/lib/faraday/request/authorization.rb
tdiary-5.2.1 vendor/bundle/ruby/3.1.0/gems/faraday-1.10.0/lib/faraday/request/authorization.rb
faraday-1.10.0 lib/faraday/request/authorization.rb
plaid-14.12.1 vendor/bundle/ruby/3.0.0/gems/faraday-1.9.3/lib/faraday/request/authorization.rb
plaid-14.12.0 vendor/bundle/ruby/2.6.0/gems/faraday-1.8.0/lib/faraday/request/authorization.rb
plaid-14.12.0 vendor/bundle/ruby/2.6.0/gems/faraday-1.9.3/lib/faraday/request/authorization.rb
plaid-14.11.1 vendor/bundle/ruby/2.6.0/gems/faraday-1.9.3/lib/faraday/request/authorization.rb
faraday-1.9.3 lib/faraday/request/authorization.rb
faraday-1.9.2 lib/faraday/request/authorization.rb
faraday-1.9.1 lib/faraday/request/authorization.rb