forked from bitpay/php-kiosk-demo-laravel
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
SP-1024 Implementing IPN HMAC Verification
- Loading branch information
1 parent
73875bb
commit 6fc1382
Showing
13 changed files
with
435 additions
and
98 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
58 changes: 58 additions & 0 deletions
58
app/Features/Invoice/UpdateInvoice/BitPaySignatureValidator.php
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,58 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace App\Features\Invoice\UpdateInvoice; | ||
|
||
use App\Shared\Exceptions\SignatureVerificationFailed; | ||
use App\Features\Shared\Configuration\BitPayConfigurationInterface; | ||
use Psr\Log\LoggerInterface; | ||
use RuntimeException; | ||
|
||
final class BitPaySignatureValidator | ||
{ | ||
public const MISSING_SIGNATURE_MESSAGE = 'Missing signature header'; | ||
public const INVALID_SIGNATURE_MESSAGE = 'Invalid signature'; | ||
public const MISSING_TOKEN_MESSAGE = 'Invalid BitPay configuration - missing token'; | ||
|
||
private BitPayConfigurationInterface $bitpayConfiguration; | ||
private LoggerInterface $logger; | ||
public function __construct( | ||
BitPayConfigurationInterface $bitpayConfiguration, | ||
LoggerInterface $logger | ||
) { | ||
$this->bitpayConfiguration = $bitpayConfiguration; | ||
$this->logger = $logger; | ||
} | ||
|
||
public function execute(array $data, array $headers): void | ||
{ | ||
$token = $this->bitpayConfiguration->getToken(); | ||
|
||
if (!$token) { | ||
throw new RuntimeException(self::MISSING_TOKEN_MESSAGE); | ||
} | ||
|
||
$sigHeader = $headers['x-signature'][0] ?? null; | ||
|
||
if (!$sigHeader) { | ||
throw new SignatureVerificationFailed(self::MISSING_SIGNATURE_MESSAGE); | ||
} | ||
|
||
$hmac = base64_encode(hash_hmac( | ||
'sha256', | ||
json_encode($data), | ||
$token, | ||
true | ||
)); | ||
|
||
$this->logger->info('HMAC', [ | ||
'hmac' => $hmac, | ||
'sigHeader' => $sigHeader, | ||
]); | ||
|
||
if ($sigHeader !== $hmac) { | ||
throw new SignatureVerificationFailed(self::INVALID_SIGNATURE_MESSAGE); | ||
} | ||
} | ||
} |
19 changes: 19 additions & 0 deletions
19
app/Features/Invoice/UpdateInvoice/BitPaySignatureValidatorInterface.php
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,19 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace App\Features\Invoice\UpdateInvoice; | ||
|
||
interface BitPaySignatureValidatorInterface | ||
{ | ||
/** | ||
* Validates the BitPay signature against the provided data and headers | ||
* | ||
* @param array $data The payload data to validate | ||
* @param array $headers The headers containing the signature - x-signature | ||
* | ||
* @throws \App\Shared\Exceptions\ValidationFailed When signature is missing or invalid | ||
* @throws \RuntimeException When BitPay token is missing | ||
*/ | ||
public function execute(array $data, array $headers): void; | ||
} |
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
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,13 @@ | ||
<?php | ||
|
||
/** | ||
* Copyright (c) 2019 BitPay | ||
**/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace App\Shared\Exceptions; | ||
|
||
class SignatureVerificationFailed extends \RuntimeException | ||
{ | ||
} |
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
Oops, something went wrong.