Class: Modulr::Auth::Signature
- Inherits:
-
Object
- Object
- Modulr::Auth::Signature
- Defined in:
- lib/modulr/auth/signature.rb
Instance Attribute Summary collapse
-
#authorization ⇒ Object
readonly
Returns the value of attribute authorization.
-
#nonce ⇒ Object
readonly
Returns the value of attribute nonce.
-
#signature ⇒ Object
readonly
Returns the value of attribute signature.
-
#timestamp ⇒ Object
readonly
Returns the value of attribute timestamp.
Class Method Summary collapse
Instance Method Summary collapse
-
#initialize(apikey:, nonce:, signature:, timestamp:) ⇒ Signature
constructor
A new instance of Signature.
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 = @authorization = [ "Signature keyId=\"#{apikey}\"", 'algorithm="hmac-sha512"', 'headers="date x-mod-nonce"', "signature=\"#{signature}\"", ].join(",") end |
Instance Attribute Details
#authorization ⇒ Object (readonly)
Returns the value of attribute authorization.
12 13 14 |
# File 'lib/modulr/auth/signature.rb', line 12 def @authorization end |
#nonce ⇒ Object (readonly)
Returns the value of attribute nonce.
12 13 14 |
# File 'lib/modulr/auth/signature.rb', line 12 def nonce @nonce end |
#signature ⇒ Object (readonly)
Returns the value of attribute signature.
12 13 14 |
# File 'lib/modulr/auth/signature.rb', line 12 def signature @signature end |
#timestamp ⇒ Object (readonly)
Returns the value of attribute timestamp.
12 13 14 |
# File 'lib/modulr/auth/signature.rb', line 12 def @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: #{}\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: ) end |