Skip to content

Commit

Permalink
deprecated old HmacSignature method, adjusted unittest (#685)
Browse files Browse the repository at this point in the history
* deprecated old HmacSignature method, adjusted unittest

* Update src/Adyen/Util/HmacSignature.php

Co-authored-by: jillingk <[email protected]>

* updated HmacSignature

* Update HmacSignatureTest.php

Included tests for deprecated and corrected  HMAC validation methods

* updated unit test and deprecated hmacvalidation method

---------

Co-authored-by: jillingk <[email protected]>
  • Loading branch information
DjoykeAbyah and jillingk authored Aug 1, 2024
1 parent 883a33e commit 433c784
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 1 deletion.
22 changes: 22 additions & 0 deletions src/Adyen/Util/HmacSignature.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,35 @@ class HmacSignature
const EVENT_CODE = "eventCode";

/**
* @deprecated use Use validateHMACSignature with correct parameter order instead
* @param string $hmacKey Can be found in Customer Area
* @param string $hmacSign Can be found in the Webhook headers
* @param string $webhook The response from Adyen
* @return bool
* @throws AdyenException
*/
public function validateHMAC(string $hmacKey, string $hmacSign, string $webhook): bool
{
if (!ctype_xdigit($hmacSign)) {
throw new AdyenException("Invalid HMAC key: $hmacKey");
}
$expectedSign = base64_encode(hash_hmac(
'sha256',
$webhook,
pack("H*", $hmacSign),
true
));
return hash_equals($expectedSign, $hmacKey);
}

/**
* @param string $hmacKey Can be found in Customer Area
* @param string $hmacSign Can be found in the Webhook headers
* @param string $webhook The response from Adyen
* @return bool
* @throws AdyenException
*/
public function validateHMACSignature(string $hmacKey, string $hmacSign, string $webhook): bool
{
if (!ctype_xdigit($hmacKey)) {
throw new AdyenException("Invalid HMAC key: $hmacKey");
Expand Down
22 changes: 21 additions & 1 deletion tests/Unit/Util/HmacSignatureTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -151,8 +151,8 @@ public function testIsHmacSupportedEventCode()
$this->fail('Unexpected exception');
}
}

/**
* @deprecated
* @throws AdyenException
*/
public function testBankingWebhookHmacValidation()
Expand All @@ -165,6 +165,26 @@ public function testBankingWebhookHmacValidation()
. "\"test\",\"type\":\"balancePlatform.balanceAccountSweep.updated\"}";
$hmac = new HmacSignature();
$result = $hmac->validateHMAC(
"9Qz9S/0xpar1klkniKdshxpAhRKbiSAewPpWoxKefQA=",
"D7DD5BA6146493707BF0BE7496F6404EC7A63616B7158EC927B9F54BB436765F",
$params
);
self::assertTrue($result);
}

/**
* @throws AdyenException
*/
public function testBankingWebhookHmacSignature()
{
$params = "{\"data\":{\"balancePlatform\":\"Integration_tools_test\","
. "\"accountId\":\"BA32272223222H5HVKTBK4MLB\",\"sweep\":{\"id\":\"SWPC42272223222H5HVKV6H8C64DP5\","
. "\"schedule\":{\"type\":\"balance\"},\"status\":\"active\",\"targetAmount\":{\"currency\":\"EUR\""
. ",\"value\":0},\"triggerAmount\":{\"currency\":\"EUR\",\"value\":0},\"type\":\"pull\",\"counterparty\":"
. "{\"balanceAccountId\":\"BA3227C223222H5HVKT3H9WLC\"},\"currency\":\"EUR\"}},\"environment\":"
. "\"test\",\"type\":\"balancePlatform.balanceAccountSweep.updated\"}";
$hmac = new HmacSignature();
$result = $hmac->validateHMACSignature(
"D7DD5BA6146493707BF0BE7496F6404EC7A63616B7158EC927B9F54BB436765F",
"9Qz9S/0xpar1klkniKdshxpAhRKbiSAewPpWoxKefQA=",
$params
Expand Down

0 comments on commit 433c784

Please sign in to comment.