Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add payload to jwt exception #521

Merged
merged 13 commits into from
Oct 4, 2023
13 changes: 12 additions & 1 deletion src/BeforeValidException.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,17 @@

namespace Firebase\JWT;

class BeforeValidException extends \UnexpectedValueException
class BeforeValidException extends \UnexpectedValueException implements JWTExceptionWithPayloadInterface
{
private object $payload;

public function setPayload(object $payload): void
{
$this->payload = $payload;
}

public function getPayload(): object
{
return $this->payload;
}
}
13 changes: 12 additions & 1 deletion src/ExpiredException.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,17 @@

namespace Firebase\JWT;

class ExpiredException extends \UnexpectedValueException
class ExpiredException extends \UnexpectedValueException implements JWTExceptionWithPayloadInterface
{
private object $payload;

public function setPayload(object $payload): void
{
$this->payload = $payload;
}

public function getPayload(): object
{
return $this->payload;
}
}
12 changes: 9 additions & 3 deletions src/JWT.php
Original file line number Diff line number Diff line change
Expand Up @@ -153,23 +153,29 @@ public static function decode(
// Check the nbf if it is defined. This is the time that the
// token can actually be used. If it's not yet that time, abort.
if (isset($payload->nbf) && floor($payload->nbf) > ($timestamp + static::$leeway)) {
throw new BeforeValidException(
$ex = new BeforeValidException(
'Cannot handle token with nbf prior to ' . \date(DateTime::ISO8601, (int) $payload->nbf)
);
$ex->setPayload($payload);
throw $ex;
}

// Check that this token has been created before 'now'. This prevents
// using tokens that have been created for later use (and haven't
// correctly used the nbf claim).
if (!isset($payload->nbf) && isset($payload->iat) && floor($payload->iat) > ($timestamp + static::$leeway)) {
throw new BeforeValidException(
$ex = new BeforeValidException(
'Cannot handle token with iat prior to ' . \date(DateTime::ISO8601, (int) $payload->iat)
);
$ex->setPayload($payload);
throw $ex;
}

// Check if this token has expired.
if (isset($payload->exp) && ($timestamp - static::$leeway) >= $payload->exp) {
throw new ExpiredException('Expired token');
$ex = new ExpiredException('Expired token');
$ex->setPayload($payload);
throw $ex;
}

return $payload;
Expand Down
20 changes: 20 additions & 0 deletions src/JWTExceptionWithPayloadInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php
namespace Firebase\JWT;

interface JWTExceptionWithPayloadInterface
{
/**
* Get the payload that caused this exception.
*
* @return object
*/
public function getPayload(): object;

/**
* Get the payload that caused this exception.
*
* @param object $payload
* @return void
*/
public function setPayload(object $payload): void;
}
34 changes: 34 additions & 0 deletions tests/JWTTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,40 @@ public function testExpiredTokenWithLeeway()
$this->assertSame($decoded->message, 'abc');
}

public function testExpiredExceptionPayload()
{
$this->expectException(ExpiredException::class);
$payload = [
'message' => 'abc',
'exp' => time() - 100, // time in the past
];
$encoded = JWT::encode($payload, 'my_key', 'HS256');
try {
JWT::decode($encoded, new Key('my_key', 'HS256'));
} catch (ExpiredException $e) {
$exceptionPayload = (array) $e->getPayload();
$this->assertEquals($exceptionPayload, $payload);
throw $e;
}
}

public function testBeforeValidExceptionPayload()
{
$this->expectException(BeforeValidException::class);
$payload = [
'message' => 'abc',
'iat' => time() + 100, // time in the future
];
$encoded = JWT::encode($payload, 'my_key', 'HS256');
try {
JWT::decode($encoded, new Key('my_key', 'HS256'));
} catch (BeforeValidException $e) {
$exceptionPayload = (array) $e->getPayload();
$this->assertEquals($exceptionPayload, $payload);
throw $e;
}
}

public function testValidTokenWithNbf()
{
$payload = [
Expand Down
Loading