-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #147 from alma/feature/ecom-2116-php-client-create…
…-endpoint-that-returns-data feat: replace payment validator hmac verification by request hmac val…
- Loading branch information
Showing
3 changed files
with
144 additions
and
3 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,22 @@ | ||
<?php | ||
|
||
namespace Alma\API\Lib; | ||
|
||
class RequestUtils | ||
{ | ||
/** | ||
* Validate the HMAC signature of the request | ||
* | ||
* @param string $data | ||
* @param string $apiKey | ||
* @param string $signature | ||
* @return bool | ||
*/ | ||
public static function isHmacValidated($data, $apiKey, $signature) | ||
{ | ||
return is_string($data) && | ||
is_string($apiKey) && | ||
hash_hmac('sha256', $data, $apiKey) === $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,118 @@ | ||
<?php | ||
|
||
namespace Alma\API\Tests\Unit\Lib; | ||
|
||
use Alma\API\Lib\RequestUtils; | ||
use PHPUnit\Framework\TestCase; | ||
use stdClass; | ||
|
||
class RequestUtilsTest extends TestCase | ||
{ | ||
public function testValidateRequestSignature() | ||
{ | ||
$data = 'merchant_id_test'; | ||
$apiKey = 'api_key_test'; | ||
$signature = '0dd3cb4632c074ead0d0f346c75015c76ad4e1e115f01c7e0850dd5accb7b4b0'; | ||
|
||
$this->assertTrue(RequestUtils::isHmacValidated($data, $apiKey, $signature)); | ||
} | ||
/** | ||
* @dataProvider checkHmacInvalidDataProvider | ||
* @param $data | ||
* @param $apiKey | ||
* @param $signature | ||
* @return void | ||
*/ | ||
public function testHmacDataDifferentFromSignature($data, $apiKey, $signature) | ||
{ | ||
$this->assertFalse(RequestUtils::isHmacValidated($data, $apiKey, $signature)); | ||
} | ||
|
||
public static function checkHmacInvalidDataProvider() | ||
{ | ||
return [ | ||
'String data' => [ | ||
'data' => 'payment_id_test', | ||
'apiKey' => 'api_key_test', | ||
'signature' => 'wrong_signature' | ||
], | ||
'Empty array data' => [ | ||
'data' => [], | ||
'apiKey' => 'api_key_test', | ||
'signature' => 'wrong_signature' | ||
], | ||
'Empty array apiKey' => [ | ||
'data' => 'payment_id_test', | ||
'apiKey' => [], | ||
'signature' => 'wrong_signature' | ||
], | ||
'Empty array signature' => [ | ||
'data' => 'payment_id_test', | ||
'apiKey' => 'api_key_test', | ||
'signature' => [] | ||
], | ||
'Empty string data' => [ | ||
'data' => '', | ||
'apiKey' => 'api_key_test', | ||
'signature' => 'wrong_signature' | ||
], | ||
'Empty string apiKey' => [ | ||
'data' => 'payment_id_test', | ||
'apiKey' => '', | ||
'signature' => 'wrong_signature' | ||
], | ||
'Empty string signature' => [ | ||
'data' => 'payment_id_test', | ||
'apiKey' => 'api_key_test', | ||
'signature' => '' | ||
], | ||
'Object data' => [ | ||
'data' => new stdClass(), | ||
'apiKey' => 'api_key_test', | ||
'signature' => 'wrong_signature' | ||
], | ||
'Object apiKey' => [ | ||
'data' => 'payment_id_test', | ||
'apiKey' => new stdClass(), | ||
'signature' => 'wrong_signature' | ||
], | ||
'Object signature' => [ | ||
'data' => 'payment_id_test', | ||
'apiKey' => 'api_key_test', | ||
'signature' => new stdClass() | ||
], | ||
'Boolean data' => [ | ||
'data' => false, | ||
'apiKey' => 'api_key_test', | ||
'signature' => 'wrong_signature' | ||
], | ||
'Boolean apiKey' => [ | ||
'data' => 'payment_id_test', | ||
'apiKey' => true, | ||
'signature' => 'wrong_signature' | ||
], | ||
'Boolean signature' => [ | ||
'data' => 'payment_id_test', | ||
'apiKey' => 'api_key_test', | ||
'signature' => true | ||
], | ||
'Int data' => [ | ||
'data' => 1, | ||
'apiKey' => 'api_key_test', | ||
'signature' => 'wrong_signature' | ||
], | ||
'Int apiKey' => [ | ||
'data' => 'payment_id_test', | ||
'apiKey' => 2, | ||
'signature' => 'wrong_signature' | ||
], | ||
'Int signature' => [ | ||
'data' => 'payment_id_test', | ||
'apiKey' => 'api_key_test', | ||
'signature' => 3 | ||
] | ||
|
||
]; | ||
} | ||
|
||
} |