Skip to content

Commit ec3a4a8

Browse files
author
Brendan Abbott
committed
Add check for iat claim with some minor documentation updates
1 parent c20a3cb commit ec3a4a8

File tree

2 files changed

+32
-8
lines changed

2 files changed

+32
-8
lines changed

Authentication/JWT.php

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,11 @@ class JWT
3030
* @param bool $verify Don't skip verification process
3131
*
3232
* @return object The JWT's payload as a PHP object
33-
* @throws UnexpectedValueException Provided JWT was invalid
3433
* @throws DomainException Algorithm was not provided
34+
* @throws UnexpectedValueException Provided JWT was invalid
35+
* @throws BeforeValidException Provided JWT is trying to be used before it's eligible as defined by 'nbf'
36+
* @throws BeforeValidException Provided JWT is trying to be used before it's been created as defined by 'iat'
37+
* @throws ExpiredException Provided JWT has since expired, as defined by the 'exp' claim
3538
*
3639
* @uses jsonDecode
3740
* @uses urlsafeB64Decode
@@ -67,17 +70,27 @@ public static function decode($jwt, $key = null, $verify = true)
6770
throw new SignatureInvalidException('Signature verification failed');
6871
}
6972

70-
// Check token expiry time if defined.
71-
if (isset($payload->exp) && time() >= $payload->exp) {
72-
throw new ExpiredException('Expired token');
73-
}
74-
75-
// Check if the nbf if it is defined.
73+
// Check if the nbf if it is defined. This is the time that the
74+
// token can actually be used. If it's not yet that time, abort.
7675
if (isset($payload->nbf) && $payload->nbf > time()) {
7776
throw new BeforeValidException(
7877
'Cannot handle token prior to ' . date(DateTime::ISO8601, $payload->nbf)
7978
);
8079
}
80+
81+
// Check that this token has been created before 'now'. This prevents
82+
// using tokens that have been created for later use (and haven't
83+
// correctly used the nbf claim).
84+
if (isset($payload->iat) && $payload->iat > time()) {
85+
throw new BeforeValidException(
86+
'Cannot handle token prior to ' . date(DateTime::ISO8601, $payload->iat)
87+
);
88+
}
89+
90+
// Check if this token has expired.
91+
if (isset($payload->exp) && time() >= $payload->exp) {
92+
throw new ExpiredException('Expired token');
93+
}
8194
}
8295

8396
return $payload;

tests/JWTTest.php

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ public function testExpiredToken()
4545
JWT::decode($encoded, 'my_key');
4646
}
4747

48-
public function testBeforeValidToken()
48+
public function testBeforeValidTokenWithNbf()
4949
{
5050
$this->setExpectedException('BeforeValidException');
5151
$payload = array(
@@ -55,6 +55,16 @@ public function testBeforeValidToken()
5555
JWT::decode($encoded, 'my_key');
5656
}
5757

58+
public function testBeforeValidTokenWithIat()
59+
{
60+
$this->setExpectedException('BeforeValidException');
61+
$payload = array(
62+
"message" => "abc",
63+
"iat" => time() + 20); // time in the future
64+
$encoded = JWT::encode($payload, 'my_key');
65+
JWT::decode($encoded, 'my_key');
66+
}
67+
5868
public function testValidToken()
5969
{
6070
$payload = array(
@@ -69,6 +79,7 @@ public function testValidTokenWithNbf()
6979
{
7080
$payload = array(
7181
"message" => "abc",
82+
"iat" => time(),
7283
"exp" => time() + 20, // time in the future
7384
"nbf" => time() - 20);
7485
$encoded = JWT::encode($payload, 'my_key');

0 commit comments

Comments
 (0)