Class: Modulr::Auth::Signature

Inherits:
Object
  • Object
show all
Defined in:
lib/modulr/auth/signature.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(apikey:, nonce:, signature:, timestamp:) ⇒ Signature

Returns a new instance of Signature.



14
15
16
17
18
19
20
21
22
23
24
# File 'lib/modulr/auth/signature.rb', line 14

def initialize(apikey:, nonce:, signature:, timestamp:)
  @nonce = nonce
  @signature = signature
  @timestamp = timestamp
  @authorization = [
    "Signature keyId=\"#{apikey}\"",
    'algorithm="hmac-sha512"',
    'headers="date x-mod-nonce"',
    "signature=\"#{signature}\"",
  ].join(",")
end

Instance Attribute Details

#authorizationObject (readonly)

Returns the value of attribute authorization.



12
13
14
# File 'lib/modulr/auth/signature.rb', line 12

def authorization
  @authorization
end

#nonceObject (readonly)

Returns the value of attribute nonce.



12
13
14
# File 'lib/modulr/auth/signature.rb', line 12

def nonce
  @nonce
end

#signatureObject (readonly)

Returns the value of attribute signature.



12
13
14
# File 'lib/modulr/auth/signature.rb', line 12

def signature
  @signature
end

#timestampObject (readonly)

Returns the value of attribute timestamp.



12
13
14
# File 'lib/modulr/auth/signature.rb', line 12

def timestamp
  @timestamp
end

Class Method Details

.calculate(apikey:, apisecret:, nonce: SecureRandom.base64(30), timestamp: DateTime.now.httpdate) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/modulr/auth/signature.rb', line 26

def self.calculate(apikey:, apisecret:, nonce: SecureRandom.base64(30), timestamp: DateTime.now.httpdate)
  signature_string = "date: #{timestamp}\nx-mod-nonce: #{nonce}"
  digest = OpenSSL::HMAC.digest(
    "SHA512",
    apisecret.dup.force_encoding("UTF-8"),
    signature_string.dup.force_encoding("UTF-8")
  )
  b64 = Base64.strict_encode64(digest)
  url_safe_code = ERB::Util.url_encode(b64.strip)

  new(apikey: apikey, nonce: nonce, signature: url_safe_code, timestamp: timestamp)
end