-
Notifications
You must be signed in to change notification settings - Fork 13
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
3830ae2
commit 5db6578
Showing
3 changed files
with
110 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
<?php | ||
|
||
namespace Voucherify; | ||
|
||
class Utils | ||
{ | ||
/** | ||
* @param array|stdClass $params Params object | ||
* | ||
* Verify webhook signature | ||
* | ||
* @throws \Voucherify\ClientException | ||
*/ | ||
public static function verifyWebhookSignature($signature, $message, $secretKey) | ||
{ | ||
$data = ""; | ||
|
||
if (is_string($message)) { | ||
$data = $message; | ||
} else { | ||
$data = json_encode($message); | ||
} | ||
return hash_hmac("sha256", $data, $secretKey) == $signature; | ||
} | ||
} | ||
|
||
|
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,63 @@ | ||
<?php | ||
|
||
use Voucherify\Utils; | ||
|
||
class UtilsTest extends PHPUnit_Framework_TestCase | ||
{ | ||
public function testVerifyWebhookSignature() | ||
{ | ||
// /////////////////////////////////////// HAPPY PATH (STRING) | ||
|
||
$signature = "776247e1a5ed607c7d1ad00d2b0f5760d4a8d5ab680b86acbcea2d3d56e55fa3"; | ||
$data = "Example test message"; | ||
$secretKey = "secret_01234567890_secret"; | ||
|
||
$result = Utils::verifyWebhookSignature($signature, $data, $secretKey); | ||
$this->assertEquals($result, true); | ||
|
||
// /////////////////////////////////////// HAPPY PATH (OBJECT) | ||
|
||
$signature = "e87e23da6caa7407a6177cc84ba1b26f58e1a68e24f08eba6ee023c8932b111a"; | ||
$data = (object)[ | ||
"item1" => 123, | ||
"item2" => "message", | ||
"item3" => (object)[ | ||
"a" => 1, | ||
"b" => 2 | ||
], | ||
"item4" => [ "a", "b", "c" ] | ||
]; | ||
$secretKey = "secret_01234567890_secret"; | ||
|
||
$result = Utils::verifyWebhookSignature($signature, $data, $secretKey); | ||
$this->assertEquals($result, true); | ||
|
||
// /////////////////////////////////////// HAPPY PATH (ARRAY) | ||
|
||
$signature = "62cd3e5dcbce78cbfadc2b8f67cde4d6853b6cea467bbbf37328193662ee040e"; | ||
$data = ["item_a", "item_b", "item_c"]; | ||
$secretKey = "secret_01234567890_secret"; | ||
|
||
$result = Utils::verifyWebhookSignature($signature, $data, $secretKey); | ||
$this->assertEquals($result, true); | ||
|
||
// /////////////////////////////////////// HAPPY PATH (NULL) | ||
|
||
$signature = "6a660d319bc87b480a48b68cd089d3c3f5e2c787a6feeb637cf0d62562560fba"; | ||
$data = NULL; | ||
$secretKey = "secret_01234567890_secret"; | ||
|
||
$result = Utils::verifyWebhookSignature($signature, $data, $secretKey); | ||
$this->assertEquals($result, true); | ||
|
||
// /////////////////////////////////////// UNHAPPY PATH (INVALID SIGNATURE) | ||
|
||
$signature = "invalid_signature"; | ||
$data = "Example test message"; | ||
$secretKey = "secret_01234567890_secret"; | ||
|
||
$result = Utils::verifyWebhookSignature($signature, $message, $secretKey); | ||
|
||
$this->assertEquals($result, false); | ||
} | ||
} |