Class: Faraday::Request::HMACAuthentication

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

Overview

#self.sign_with = access_id

#Captivus::AuthHMAC.keys[access_id] = secret

end

Defined Under Namespace

Classes: CanonicalString

Constant Summary collapse

KEY =
"Authorization".freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of HMACAuthentication.



43
44
45
46
47
48
49
# File 'lib/faraday/request/hmac_authentication.rb', line 43

def initialize(app, token, secret, options = {})
  options.merge!(:signature => HMACAuthentication::CanonicalString)
  keys = {token => secret}
  @token, @secret = token, secret
  @auth = Captivus::AuthHMAC.new(keys, options)
  super(app)
end

Instance Attribute Details

#authObject (readonly)

Returns the value of attribute auth.



41
42
43
# File 'lib/faraday/request/hmac_authentication.rb', line 41

def auth
  @auth
end

#secretObject (readonly)

Returns the value of attribute secret.



41
42
43
# File 'lib/faraday/request/hmac_authentication.rb', line 41

def secret
  @secret
end

#tokenObject (readonly)

Returns the value of attribute token.



41
42
43
# File 'lib/faraday/request/hmac_authentication.rb', line 41

def token
  @token
end

Instance Method Details

#call(env) ⇒ Object

Public



52
53
54
55
# File 'lib/faraday/request/hmac_authentication.rb', line 52

def call(env)
  env[:request_headers][KEY] ||= hmac_auth_header(env).to_s if sign_request?
  @app.call(env)
end

#hmac_auth_header(env) ⇒ Object



57
58
59
# File 'lib/faraday/request/hmac_authentication.rb', line 57

def hmac_auth_header(env)
  auth.authorization(env, token, secret)
end

#sign_request?Boolean

Returns:

  • (Boolean)


61
62
63
# File 'lib/faraday/request/hmac_authentication.rb', line 61

def sign_request?
  !!@token && !!@secret
end