Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

deprecated old HmacSignature method, adjusted unittest #685

Merged
merged 6 commits into from
Aug 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
jillingk marked this conversation as resolved.
Show resolved Hide resolved
{
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(
acampos1916 marked this conversation as resolved.
Show resolved Hide resolved
"D7DD5BA6146493707BF0BE7496F6404EC7A63616B7158EC927B9F54BB436765F",
"9Qz9S/0xpar1klkniKdshxpAhRKbiSAewPpWoxKefQA=",
$params
Expand Down