Module: K2ConnectRuby::K2Utilities::K2Authenticator

Extended by:
K2Authenticator
Included in:
K2Authenticator
Defined in:
lib/k2-connect-ruby/k2_utilities/k2_authenticator.rb

Instance Method Summary collapse

Instance Method Details

#authenticate(body, api_secret_key, signature) ⇒ Object

Raises:

  • (ArgumentError)


9
10
11
12
13
14
15
16
# File 'lib/k2-connect-ruby/k2_utilities/k2_authenticator.rb', line 9

def authenticate(body, api_secret_key, signature)
  raise ArgumentError, "Nil Authentication Argument!\n Check whether your Input is Empty" if body.blank? || api_secret_key.blank? || signature.blank?

  digest = OpenSSL::Digest.new('sha256')
  hmac = OpenSSL::HMAC.hexdigest(digest, api_secret_key, body.to_json)
  raise ArgumentError, "Invalid Details Given!\n Ensure that your the Arguments Given are correct, namely:\n\t- The Response Body\n\t- Secret Key\n\t- Signature" unless ActiveSupport::SecurityUtils.secure_compare(hmac, signature)
  true
end