Class: Cryptopay::Authentication

Inherits:
Object
  • Object
show all
Defined in:
lib/cryptopay/authentication.rb

Instance Method Summary collapse

Constructor Details

#initialize(api_key:, api_secret:) ⇒ Authentication

Returns a new instance of Authentication.



5
6
7
8
# File 'lib/cryptopay/authentication.rb', line 5

def initialize(api_key:, api_secret:)
  @api_key = api_key
  @api_secret = api_secret
end

Instance Method Details

#sign(request) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/cryptopay/authentication.rb', line 10

def sign(request)
  signature_string = [
    request.method.to_s.upcase,
    request.body.nil? ? '' : Digest::MD5.hexdigest(request.body),
    request.content_type,
    request.date,
    request.path
  ].join("\n")

  signature = Base64.strict_encode64(OpenSSL::HMAC.digest('sha1', api_secret, signature_string))

  request.add_header('Authorization', "HMAC #{api_key}:#{signature}")

  request
end