Module: K2Authenticator

Defined in:
lib/k2-connect-ruby/k2_utilities/k2_authorize.rb

Overview

Module for Authenticating the Kopo Kopo Signature via HMAC

Class Method Summary collapse

Class Method Details

.authenticate(body, api_secret_key, signature) ⇒ Object

Compares HMAC signature with the key.

Raises:

  • (ArgumentError)


6
7
8
9
10
11
12
13
# File 'lib/k2-connect-ruby/k2_utilities/k2_authorize.rb', line 6

def self.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