Skip to content

Commit 83b8899

Browse files
author
Chris Raynor
committed
Merge pull request firebase#24 from brendo/iat-claim-check
Add check for iat claim with some minor documentation updates
2 parents c20a3cb + 8b6d4f0 commit 83b8899

File tree

2 files changed

+35
-9
lines changed

2 files changed

+35
-9
lines changed

Authentication/JWT.php

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,13 @@ 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
34-
* @throws DomainException Algorithm was not provided
33+
*
34+
* @throws DomainException Algorithm was not provided
35+
* @throws UnexpectedValueException Provided JWT was invalid
36+
* @throws SignatureInvalidException Provided JWT was invalid because the signature verification failed
37+
* @throws BeforeValidException Provided JWT is trying to be used before it's eligible as defined by 'nbf'
38+
* @throws BeforeValidException Provided JWT is trying to be used before it's been created as defined by 'iat'
39+
* @throws ExpiredException Provided JWT has since expired, as defined by the 'exp' claim
3540
*
3641
* @uses jsonDecode
3742
* @uses urlsafeB64Decode
@@ -67,17 +72,27 @@ public static function decode($jwt, $key = null, $verify = true)
6772
throw new SignatureInvalidException('Signature verification failed');
6873
}
6974

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.
75+
// Check if the nbf if it is defined. This is the time that the
76+
// token can actually be used. If it's not yet that time, abort.
7677
if (isset($payload->nbf) && $payload->nbf > time()) {
7778
throw new BeforeValidException(
7879
'Cannot handle token prior to ' . date(DateTime::ISO8601, $payload->nbf)
7980
);
8081
}
82+
83+
// Check that this token has been created before 'now'. This prevents
84+
// using tokens that have been created for later use (and haven't
85+
// correctly used the nbf claim).
86+
if (isset($payload->iat) && $payload->iat > time()) {
87+
throw new BeforeValidException(
88+
'Cannot handle token prior to ' . date(DateTime::ISO8601, $payload->iat)
89+
);
90+
}
91+
92+
// Check if this token has expired.
93+
if (isset($payload->exp) && time() >= $payload->exp) {
94+
throw new ExpiredException('Expired token');
95+
}
8196
}
8297

8398
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)