Class: CoinRail::HTTP::Authentication

Inherits:
Faraday::Middleware
  • Object
show all
Defined in:
lib/coinrail/http.rb

Instance Method Summary collapse

Constructor Details

#initialize(app, key, secret) ⇒ Authentication

Returns a new instance of Authentication.



27
28
29
30
31
# File 'lib/coinrail/http.rb', line 27

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

Instance Method Details

#call(env) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/coinrail/http.rb', line 33

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

  timestamp = Time.now.to_i.to_s
  method = env[:method].to_s.upcase
  path = env[:url].path + (env[:url].query ? '?' + env[:url].query : '')
  body = env[:body] || ''
  encoded_payload = Base64.strict_encode64(body)
  signature = OpenSSL::HMAC.hexdigest(OpenSSL::Digest.new('sha512'), @secret, encoded_payload)
  env[:request_headers]['X-COINRAIL-PAYLOAD'] = encoded_payload
  env[:request_headers]['X-COINRAIL-SIGNATURE'] = signature
  @app.call env
end