Skip to content

Commit

Permalink
harden tests, move errors to E_USER_WARNING for backward compatibilit…
Browse files Browse the repository at this point in the history
…y until 5.0
  • Loading branch information
SecondeJK committed Oct 9, 2023
1 parent aa19853 commit 7439793
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 5 deletions.
7 changes: 5 additions & 2 deletions src/Client/Credentials/Keypair.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,10 @@ public function generateJwt(array $claims = []): Token
$generator = new TokenGenerator($this->application, $this->getKeyRaw());

if (isset($claims['exp'])) {
// This will change to an Exception in 5.0
trigger_error('Expiry date is automatically generated from now and TTL, so cannot be passed in
as an argument in claims', E_USER_WARNING);
unset($claims['exp']);
unset($claims['nbf']);
}

if (isset($claims['ttl'])) {
Expand All @@ -79,7 +80,9 @@ public function generateJwt(array $claims = []): Token
if (isset($claims['nbf'])) {
// Due to older versions of lcobucci/jwt, this claim has
// historic fraction conversation issues. For now, nbf is not supported.
throw new Validation('NotBefore Claim is not supported in Vonage JWT');
// This will change to an Exception in 5.0
trigger_error('NotBefore Claim is not supported in Vonage JWT', E_USER_WARNING);
unset($claims['nbf']);
}

if (isset($claims['sub'])) {
Expand Down
53 changes: 50 additions & 3 deletions test/Client/Credentials/KeypairTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

namespace VonageTest\Client\Credentials;

use Vonage\Client\Exception\Validation;
use VonageTest\VonageTestCase;
use Vonage\Client\Credentials\Keypair;

Expand All @@ -19,8 +20,8 @@

class KeypairTest extends VonageTestCase
{
protected $key;
protected $application = 'c90ddd99-9a5d-455f-8ade-dde4859e590e';
protected string $key;
protected string $application = 'c90ddd99-9a5d-455f-8ade-dde4859e590e';

public function setUp(): void
{
Expand Down Expand Up @@ -48,7 +49,6 @@ public function testDefaultJWT(): void
{
$credentials = new Keypair($this->key, $this->application);

//could use the JWT object, but hope to remove as a dependency
$jwt = (string)$credentials->generateJwt()->toString();

[$header, $payload] = $this->decodeJWT($jwt);
Expand Down Expand Up @@ -81,6 +81,53 @@ public function testAdditionalClaims(): void
$this->assertEquals($claims['arbitrary'], $payload['arbitrary']);
}

public function testNbfNotSupported(): void
{
set_error_handler(static function (int $errno, string $errstr) {
throw new \Exception($errstr, $errno);
}, E_USER_WARNING);

$this->expectExceptionMessage('NotBefore Claim is not supported in Vonage JWT');

$credentials = new Keypair($this->key, $this->application);

$claims = [
'nbf' => time() + 900
];

$jwt = $credentials->generateJwt($claims);
[, $payload] = $this->decodeJWT($jwt->toString());

$this->assertArrayHasKey('arbitrary', $payload);
$this->assertEquals($claims['arbitrary'], $payload['arbitrary']);

restore_error_handler();
}

public function testExpNotSupported(): void
{
set_error_handler(static function (int $errno, string $errstr) {
throw new \Exception($errstr, $errno);
}, E_USER_WARNING);

$this->expectExceptionMessage('Expiry date is automatically generated from now and TTL, so cannot be passed in
as an argument in claims');

$credentials = new Keypair($this->key, $this->application);

$claims = [
'exp' => time() + 900
];

$jwt = $credentials->generateJwt($claims);
[, $payload] = $this->decodeJWT($jwt->toString());

$this->assertArrayHasKey('arbitrary', $payload);
$this->assertEquals($claims['arbitrary'], $payload['arbitrary']);

restore_error_handler();
}

/**
* @link https://github.com/Vonage/vonage-php-sdk-core/issues/276
*/
Expand Down

0 comments on commit 7439793

Please sign in to comment.