Class: Adyen::Utils::HmacValidator
- Inherits:
-
Object
- Object
- Adyen::Utils::HmacValidator
- Defined in:
- lib/adyen/utils/hmac_validator.rb
Constant Summary collapse
- HMAC_ALGORITHM =
'sha256'.freeze
- DATA_SEPARATOR =
':'.freeze
- WEBHOOK_VALIDATION_KEYS =
%w[ pspReference originalReference merchantAccountCode merchantReference amount.value amount.currency eventCode success ].freeze
Instance Method Summary collapse
- #calculate_notification_hmac(notification_request_item, hmac_key) ⇒ Object deprecated Deprecated.
- #calculate_webhook_hmac(webhook_request_item, hmac_key) ⇒ Object
- #calculate_webhook_payload_hmac(data, hmac_key) ⇒ Object
- #data_to_sign(webhook_request_item) ⇒ Object
- #valid_notification_hmac?(notification_request_item, hmac_key) ⇒ Boolean deprecated Deprecated.
-
#valid_webhook_hmac?(webhook_request_item, hmac_key) ⇒ Boolean
Returns true if the HMAC signature is valid, otherwise false.
-
#valid_webhook_payload_hmac?(hmac_signature, hmac_key, payload) ⇒ Boolean
Returns true if the HMAC signature is valid, otherwise false.
Instance Method Details
#calculate_notification_hmac(notification_request_item, hmac_key) ⇒ Object
Deprecated.
DEPRECATED: Please use calculate_webhook_hmac() instead.
50 51 52 53 |
# File 'lib/adyen/utils/hmac_validator.rb', line 50 def calculate_notification_hmac(notification_request_item, hmac_key) Adyen::Deprecation.warn(:calculate_notification_hmac, message: "Use calculate_webhook_hmac() instead.") calculate_webhook_hmac(notification_request_item, hmac_key) end |
#calculate_webhook_hmac(webhook_request_item, hmac_key) ⇒ Object
60 61 62 63 64 65 |
# File 'lib/adyen/utils/hmac_validator.rb', line 60 def calculate_webhook_hmac(webhook_request_item, hmac_key) data = data_to_sign(webhook_request_item) # Base64 strict encoding [OpenSSL::HMAC.digest(HMAC_ALGORITHM, [hmac_key].pack('H*'), data)].pack('m0') end |
#calculate_webhook_payload_hmac(data, hmac_key) ⇒ Object
55 56 57 58 |
# File 'lib/adyen/utils/hmac_validator.rb', line 55 def calculate_webhook_payload_hmac(data, hmac_key) # Base64 strict encoding [OpenSSL::HMAC.digest(HMAC_ALGORITHM, [hmac_key].pack('H*'), data)].pack('m0') end |
#data_to_sign(webhook_request_item) ⇒ Object
67 68 69 70 71 72 |
# File 'lib/adyen/utils/hmac_validator.rb', line 67 def data_to_sign(webhook_request_item) WEBHOOK_VALIDATION_KEYS .map { webhook_request_item.dig(*_1.split('.')).to_s } .compact .join(DATA_SEPARATOR) end |
#valid_notification_hmac?(notification_request_item, hmac_key) ⇒ Boolean
Deprecated.
DEPRECATED: Please use valid_webhook_hmac?() instead.
15 16 17 18 |
# File 'lib/adyen/utils/hmac_validator.rb', line 15 def valid_notification_hmac?(notification_request_item, hmac_key) Adyen::Deprecation.warn(:valid_notification_hmac?, message: "Use valid_webhook_hmac?() instead.") valid_webhook_hmac?(notification_request_item, hmac_key) end |
#valid_webhook_hmac?(webhook_request_item, hmac_key) ⇒ Boolean
Returns true if the HMAC signature is valid, otherwise false.
27 28 29 30 31 32 33 |
# File 'lib/adyen/utils/hmac_validator.rb', line 27 def valid_webhook_hmac?(webhook_request_item, hmac_key) expected_sign = calculate_webhook_hmac(webhook_request_item, hmac_key) merchant_sign = webhook_request_item.dig('additionalData', 'hmacSignature') merchant_sign.is_a?(String) && OpenSSL.secure_compare(expected_sign, merchant_sign) end |
#valid_webhook_payload_hmac?(hmac_signature, hmac_key, payload) ⇒ Boolean
Returns true if the HMAC signature is valid, otherwise false.
43 44 45 46 |
# File 'lib/adyen/utils/hmac_validator.rb', line 43 def valid_webhook_payload_hmac?(hmac_signature, hmac_key, payload) expected_sign = calculate_webhook_payload_hmac(payload, hmac_key) hmac_signature.is_a?(String) && OpenSSL.secure_compare(expected_sign, hmac_signature) end |