This repository has been archived by the owner on Mar 19, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
959c911
commit ef1ac4e
Showing
412 changed files
with
563 additions
and
471 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
<?php | ||
namespace Ingenico\Connect\Sdk\Webhooks; | ||
|
||
/** | ||
* Class SignatureValidator | ||
* | ||
* @package Ingenico\Connect\Sdk\Webhooks | ||
*/ | ||
class SignatureValidator | ||
{ | ||
/** @var SecretKeyStore */ | ||
private $secretKeyStore; | ||
|
||
/** | ||
* @param SecretKeyStore $secretKeyStore | ||
*/ | ||
public function __construct(SecretKeyStore $secretKeyStore) | ||
{ | ||
$this->secretKeyStore = $secretKeyStore; | ||
} | ||
|
||
/** | ||
* Validates the given body using the given request headers. | ||
* @param string $body | ||
* @param array $requestHeaders | ||
* @throws SignatureValidationException | ||
*/ | ||
public function validate($body, $requestHeaders) | ||
{ | ||
$this->validateBody($body, $requestHeaders); | ||
} | ||
|
||
// utility methods | ||
|
||
private function validateBody($body, $requestHeaders) | ||
{ | ||
$signature = $this->getHeaderValue($requestHeaders, 'X-GCS-Signature'); | ||
$keyId = $this->getHeaderValue($requestHeaders, 'X-GCS-KeyId'); | ||
$secretKey = $this->secretKeyStore->getSecretKey($keyId); | ||
|
||
$expectedSignature = base64_encode(hash_hmac("sha256", $body, $secretKey, true)); | ||
|
||
$isValid = $this->areEqualSignatures($signature, $expectedSignature); | ||
if (!$isValid) { | ||
throw new SignatureValidationException("failed to validate signature '$signature'"); | ||
} | ||
} | ||
|
||
private function areEqualSignatures($signature, $expectedSignature) { | ||
if (function_exists('hash_equals')) { | ||
return hash_equals($expectedSignature, $signature); | ||
} else { | ||
// copied from http://php.net/manual/en/function.hash-equals.php#115635 | ||
if(strlen($expectedSignature) != strlen($signature)) { | ||
return false; | ||
} else { | ||
$res = $expectedSignature ^ $signature; | ||
$ret = 0; | ||
for($i = strlen($res) - 1; $i >= 0; $i--) $ret |= ord($res[$i]); | ||
return !$ret; | ||
} | ||
} | ||
} | ||
|
||
// general utility methods | ||
|
||
private function getHeaderValue($requestHeaders, $headerName) { | ||
$lowerCaseHeaderName = strtolower($headerName); | ||
foreach ($requestHeaders as $name => $value) { | ||
if ($lowerCaseHeaderName === strtolower($name)) { | ||
return $value; | ||
} | ||
} | ||
throw new SignatureValidationException("could not find header '$headerName'"); | ||
} | ||
} |
Oops, something went wrong.