Class: Request::Hmac

Inherits:
Faraday::Middleware
  • Object
show all
Defined in:
lib/faraday/request/hmac.rb

Instance Method Summary collapse

Constructor Details

#initialize(app, secret, options = {}) ⇒ Hmac

create a new Hmac middleware instance

Parameters:

  • app (Object)

    The url of the request

  • secret (String)

    The shared secret for the signature

  • options (Hash) (defaults to: {})

    Options for the signature generation

Options Hash (options):

  • :nonce (String) — default: ''

    The nonce to use in the signature

  • :date (String, #strftime) — default: Time.now

    The date to use in the signature

  • :headers (Hash) — default: {}

    A list of optional headers to include in the signature

  • :auth_scheme (String) — default: 'HMAC'

    The name of the authorization scheme used in the Authorization header and to construct various header-names

  • :auth_param (String) — default: 'auth'

    The name of the authentication param to use for query based authentication

  • :extra_auth_params (Hash) — default: {}

    Additional parameters to inject in the auth parameter. This parameter is ignored unless :query_based evaluates to true.

  • :auth_header (String) — default: 'Authorization'

    The name of the authorization header to use

  • :auth_header_format (String) — default: '%{auth_scheme} %{signature}'

    The format of the authorization header. Will be interpolated with the given options and the signature.

  • :nonce_header (String) — default: 'X-#{auth_scheme}-Nonce'

    The header name for the request nonce

  • :alternate_date_header (String) — default: 'X-#{auth_scheme}-Date'

    The header name for the alternate date header

  • :query_based (Bool) — default: false

    Whether to use query based authentication

  • :use_alternate_date_header (Bool) — default: false

    Use the alternate date header instead of ‘Date`



27
28
29
# File 'lib/faraday/request/hmac.rb', line 27

def initialize(app, secret, options = {})
  @app, @secret, @options, @query_values = app, secret, options
end

Instance Method Details

#call(env) ⇒ Object



31
32
33
34
# File 'lib/faraday/request/hmac.rb', line 31

def call(env)
  sign(env)
  @app.call(env)
end

#sign(env) ⇒ Object



36
37
38
39
40
41
42
43
44
45
# File 'lib/faraday/request/hmac.rb', line 36

def sign(env)
  signer = HMAC::Signer.new
  url = env[:url]
  method = env[:method]
  headers, url = *signer.sign_request(url, @secret, @options.merge(:method => env[:method]))

  env[:request_headers] = (env[:request_headers] || {}).merge(headers)
  env[:url] = URI.parse(url)
  env
end