Sha256: 4e394a781b686bda873a430d4bdec0c2b1de867db7c1d9886d5d16322d6a3c77

Contents?: true

Size: 761 Bytes

Versions: 5

Compression:

Stored size: 761 Bytes

Contents

require 'openssl'
require 'active_support/security_utils'
# Module for Authenticating the Kopo Kopo Signature via HMAC
module K2Authenticator
  # Compares HMAC signature with the key.
  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
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
k2-connect-ruby-2.0.0 lib/k2-connect-ruby/k2_utilities/k2_authorize.rb
k2-connect-ruby-1.0.0 lib/k2-connect-ruby/k2_utilities/k2_authorize.rb
k2-connect-ruby-0.0.3 lib/k2-connect-ruby/k2_utilities/k2_authorize.rb
k2-connect-ruby-0.0.2 lib/k2-connect-ruby/k2_utilities/k2_authorize.rb
k2-connect-ruby-0.0.1 lib/k2-connect-ruby/k2_utilities/k2_authorize.rb