Class: Seekmodo::Sdk::HmacSigner

Inherits:
Object
  • Object
show all
Defined in:
lib/seekmodo/sdk/hmac_signer.rb

Constant Summary collapse

HEADER_TENANT =
"X-Seekmodo-Tenant"
HEADER_SIGNATURE =
"X-Seekmodo-Signature"
HEADER_TIMESTAMP =
"X-Seekmodo-Timestamp"
HEADER_SESSION =
"X-Seekmodo-Session"
HEADER_STOREFRONT_HOST =
"X-Seekmodo-Storefront-Host"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(tenant_id, shared_secret) ⇒ HmacSigner

Returns a new instance of HmacSigner.



16
17
18
19
# File 'lib/seekmodo/sdk/hmac_signer.rb', line 16

def initialize(tenant_id, shared_secret)
  @tenant_id = tenant_id
  @shared_secret = shared_secret
end

Instance Attribute Details

#tenant_idObject (readonly)

Returns the value of attribute tenant_id.



14
15
16
# File 'lib/seekmodo/sdk/hmac_signer.rb', line 14

def tenant_id
  @tenant_id
end

Instance Method Details

#configured?Boolean

Returns:

  • (Boolean)


38
39
40
# File 'lib/seekmodo/sdk/hmac_signer.rb', line 38

def configured?
  !@tenant_id.to_s.empty? && !@shared_secret.to_s.empty?
end

#headers(raw_body, timestamp = nil) ⇒ Object



21
22
23
24
25
26
27
28
29
# File 'lib/seekmodo/sdk/hmac_signer.rb', line 21

def headers(raw_body, timestamp = nil)
  ts = (timestamp || Time.now.to_i).to_s
  signature = OpenSSL::HMAC.hexdigest("SHA256", @shared_secret, raw_body)
  {
    HEADER_TENANT => @tenant_id,
    HEADER_SIGNATURE => signature,
    HEADER_TIMESTAMP => ts
  }
end

#verify(raw_body, signature) ⇒ Object



31
32
33
34
35
36
# File 'lib/seekmodo/sdk/hmac_signer.rb', line 31

def verify(raw_body, signature)
  expected = OpenSSL::HMAC.hexdigest("SHA256", @shared_secret, raw_body)
  return false if signature.nil? || signature.empty?

  OpenSSL.secure_compare(expected, signature)
end