Class: Deribit::Authentication

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

Overview

Deribit authentication implemented as Faraday middleware

Instance Method Summary collapse

Constructor Details

#initialize(app, key, secret) ⇒ Authentication

Returns a new instance of Authentication.



9
10
11
12
13
# File 'lib/deribit/authentication.rb', line 9

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

Instance Method Details

#call(env) ⇒ Object



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

def call(env)
  return @app.call(env) if env['url'].path.include? 'public'
  return @app.call(env) if @key.nil? || @secret.nil?

  timestamp = Time.now.utc.to_i * 1000
  nonce = rand(999999)
  env.request_headers['Authorization'] = header env, timestamp, nonce

  @app.call env
end

#header(env, timestamp, nonce) ⇒ Object

Returns the HTTP header: “Authorization: deri-hmac-sha256 id=ClientId,ts=Timestamp,sig=Signature,nonce=Nonce”.

Returns:

  • the HTTP header: “Authorization: deri-hmac-sha256 id=ClientId,ts=Timestamp,sig=Signature,nonce=Nonce”



27
28
29
30
# File 'lib/deribit/authentication.rb', line 27

def header(env, timestamp, nonce)
  signature = Deribit.http_signature env, timestamp, nonce, @secret
  "deri-hmac-sha256 id=#{@key},ts=#{timestamp},sig=#{signature},nonce=#{nonce}"
end