Class: Faraday::Request::HMACAuthentication

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

Overview

#self.sign_with = access_id

#CrashLog::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.



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

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

Instance Attribute Details

#authObject (readonly)

Returns the value of attribute auth.



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

def auth
  @auth
end

#secretObject (readonly)

Returns the value of attribute secret.



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

def secret
  @secret
end

#tokenObject (readonly)

Returns the value of attribute token.



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

def token
  @token
end

Instance Method Details

#call(env) ⇒ Object

Public



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

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



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

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

#sign_request?Boolean

Returns:

  • (Boolean)


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

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