Class: Mach::Faraday::HmacAuthentication

Inherits:
Faraday::Middleware
  • Object
show all
Includes:
HMAC
Defined in:
lib/mach/faraday/request/hmac_authentication.rb

Constant Summary collapse

KEY =
"Authorization".freeze

Instance Method Summary collapse

Methods included from HMAC

#mac_ext, #mac_host, #mac_path, #mac_port, #mac_request_method

Constructor Details

#initialize(app, id, key) ⇒ HmacAuthentication

Returns a new instance of HmacAuthentication.



11
12
13
14
15
# File 'lib/mach/faraday/request/hmac_authentication.rb', line 11

def initialize(app, id, key)
  @mac_id = id
  @mac_key = key
  super(app)
end

Instance Method Details

#call(env) ⇒ Object



17
18
19
20
21
22
# File 'lib/mach/faraday/request/hmac_authentication.rb', line 17

def call(env)
  unless env[:request_headers][KEY]
    env[:request_headers][KEY] = self.header(env)
  end
  @app.call(env)
end

#header(env) ⇒ Object



24
25
26
27
28
29
30
31
# File 'lib/mach/faraday/request/hmac_authentication.rb', line 24

def header(env)
  AuthorizationHeader.new(@mac_id, @mac_key,
                          :request_method => mac_request_method(request_method(env)),
                          :path => mac_path(path(env), query_string(env)),
                          :host => mac_host(host(env)),
                          :port => mac_port(port(env), scheme(env)),
                          :ext => mac_ext(ext(env))).to_s
end