Class: Bitflyer::HTTP::Authentication

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

Instance Method Summary collapse

Constructor Details

#initialize(app, key, secret) ⇒ Authentication

Returns a new instance of Authentication.



28
29
30
31
32
# File 'lib/bitflyer/http.rb', line 28

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

Instance Method Details

#call(env) ⇒ Object



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

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] || ''
  signature = OpenSSL::HMAC.hexdigest(OpenSSL::Digest.new('sha256'), @secret, timestamp + method + path + body)
  env[:request_headers]['ACCESS-KEY'] = @key if @key
  env[:request_headers]['ACCESS-TIMESTAMP'] = timestamp
  env[:request_headers]['ACCESS-SIGN'] = signature
  @app.call env
end