Class: SolidusPaybright::SigningHelper

Inherits:
Object
  • Object
show all
Defined in:
lib/solidus_paybright/signing_helper.rb

Constant Summary collapse

SIGNATURE_PARAM_KEY =
"x_signature"

Instance Method Summary collapse

Constructor Details

#initialize(hmac_key) ⇒ SigningHelper

Returns a new instance of SigningHelper.

Parameters:

  • hmac_key (String)

    The HMAC secret key used for signing



8
9
10
# File 'lib/solidus_paybright/signing_helper.rb', line 8

def initialize(hmac_key)
  @hmac_key = hmac_key
end

Instance Method Details

#params_signature(params) ⇒ String

Returns The signature of the parameters.

Parameters:

  • params (Hash)

    The parameters to be signed

Returns:

  • (String)

    The signature of the parameters



14
15
16
17
18
19
20
# File 'lib/solidus_paybright/signing_helper.rb', line 14

def params_signature(params)
  message = params.select do |key, _|
    key != SIGNATURE_PARAM_KEY && key.start_with?("x_")
  end.sort.join

  OpenSSL::HMAC.hexdigest(OpenSSL::Digest.new("sha256"), @hmac_key, message)
end

#valid_params?(params) ⇒ Boolean

Returns The validation result.

Parameters:

  • params (Hash)

    The parameters to be validated, including the “x_signature” key

Returns:

  • (Boolean)

    The validation result



24
25
26
27
28
29
# File 'lib/solidus_paybright/signing_helper.rb', line 24

def valid_params?(params)
  expected_signature = params[SIGNATURE_PARAM_KEY].to_s
  signature = params_signature(params)

  expected_signature.casecmp(signature.downcase).zero?
end