Class: FaradayMiddleware::Authentication

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

Instance Method Summary collapse

Constructor Details

#initialize(app, key, secret) ⇒ Authentication

Returns a new instance of Authentication.



7
8
9
10
11
# File 'lib/faraday/authentication.rb', line 7

def initialize(app, key, secret)
  super(app)
  @key = key
  @secret = secret
end

Instance Method Details

#call(env) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/faraday/authentication.rb', line 13

def call(env)
  return @app.call(env) if @key.nil? || @secret.nil?

  timestamp = (Time.now - 600).to_i.to_s
  method = env[:method].to_s.upcase
  path = env[:url].path + (env[:url].query ? '?' + env[:url].query : '')
  body = env[:body] || ''
  signature = OpenSSL::HMAC.hexdigest(OpenSSL::Digest.new('sha256'), @secret, method + path + timestamp + body)
  env[:request_headers]['api-expires'] = timestamp
  env[:request_headers]['api-key'] = @key if @key
  env[:request_headers]['api-signature'] = signature
  @app.call env
end