Skip to content

feat: adds payload to exceptions #292

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

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 13 additions & 1 deletion src/ExpiredException.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,18 @@
<?php
namespace Firebase\JWT;

class ExpiredException extends \UnexpectedValueException
class ExpiredException extends \UnexpectedValueException implements IJWTException
{
private $payload;

public function __construct($payload, $message = "", $code = 0, $previous = null)
{
parent::__construct($message, $code, $previous);
$this->payload = $payload;
}

public function getPayload()
{
return $this->payload;
}
}
12 changes: 12 additions & 0 deletions src/IJWTException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php
namespace Firebase\JWT;

interface IJWTException
{
/**
* Get the payload that caused this exception.
*
* @return object
*/
public function getPayload();
}
4 changes: 2 additions & 2 deletions src/JWT.php
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ public static function decode($jwt, $key, array $allowed_algs = array())

// Check the signature
if (!static::verify("$headb64.$bodyb64", $sig, $key, $header->alg)) {
throw new SignatureInvalidException('Signature verification failed');
throw new SignatureInvalidException($payload, 'Signature verification failed');
}

// Check the nbf if it is defined. This is the time that the
Expand All @@ -141,7 +141,7 @@ public static function decode($jwt, $key, array $allowed_algs = array())

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

return $payload;
Expand Down
12 changes: 12 additions & 0 deletions src/SignatureInvalidException.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,16 @@

class SignatureInvalidException extends \UnexpectedValueException
{
private $payload;

public function __construct($payload, $message = "", $code = 0, $previous = null)
{
parent::__construct($message, $code, $previous);
$this->payload = $payload;
}

public function getPayload()
{
return $this->payload;
}
}
25 changes: 22 additions & 3 deletions tests/JWTTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,13 @@ public function testExpiredToken()
"message" => "abc",
"exp" => time() - 20); // time in the past
$encoded = JWT::encode($payload, 'my_key');
JWT::decode($encoded, 'my_key', array('HS256'));
try {
JWT::decode($encoded, 'my_key', array('HS256'));
} catch (\Exception $e) {
$exceptionPayload = (array)($e->getPayload());
$this->assertEquals($exceptionPayload, $payload);
throw $e;
}
}

public function testBeforeValidTokenWithNbf()
Expand Down Expand Up @@ -111,7 +117,13 @@ public function testExpiredTokenWithLeeway()
"exp" => time() - 70); // time far in the past
$this->setExpectedException('Firebase\JWT\ExpiredException');
$encoded = JWT::encode($payload, 'my_key');
$decoded = JWT::decode($encoded, 'my_key', array('HS256'));
try {
$decoded = JWT::decode($encoded, 'my_key', array('HS256'));
} catch (\Exception $e) {
$exceptionPayload = (array)($e->getPayload());
$this->assertEquals($exceptionPayload, $payload);
throw $e;
}
$this->assertEquals($decoded->message, 'abc');
JWT::$leeway = 0;
}
Expand Down Expand Up @@ -193,7 +205,14 @@ public function testInvalidToken()
"exp" => time() + 20); // time in the future
$encoded = JWT::encode($payload, 'my_key');
$this->setExpectedException('Firebase\JWT\SignatureInvalidException');
JWT::decode($encoded, 'my_key2', array('HS256'));

try {
JWT::decode($encoded, 'my_key2', array('HS256'));
} catch (\Exception $e) {
$exceptionPayload = (array)($e->getPayload());
$this->assertEquals($exceptionPayload, $payload);
throw $e;
}
}

public function testNullKeyFails()
Expand Down