Skip to content

Commit 61ff178

Browse files
author
Luis Miguel Cabral
committed
Provide a leeway time in the verification of timestamps to account for clock skew
1 parent 0cb1d5a commit 61ff178

File tree

2 files changed

+18
-6
lines changed

2 files changed

+18
-6
lines changed

Authentication/JWT.php

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,12 @@
1515
*/
1616
class JWT
1717
{
18+
19+
/**
20+
* When cheking nbf, iat or expiration times, we want to provide some extra leeway time to account for clock skew.
21+
*/
22+
const LEEWAYTIME = 60;
23+
1824
public static $supported_algs = array(
1925
'HS256' => array('hash_hmac', 'SHA256'),
2026
'HS512' => array('hash_hmac', 'SHA512'),
@@ -80,7 +86,7 @@ public static function decode($jwt, $key = null, $allowed_algs = array())
8086

8187
// Check if the nbf if it is defined. This is the time that the
8288
// token can actually be used. If it's not yet that time, abort.
83-
if (isset($payload->nbf) && $payload->nbf > time()) {
89+
if (isset($payload->nbf) && $payload->nbf > (time() + self::LEEWAYTIME)) {
8490
throw new BeforeValidException(
8591
'Cannot handle token prior to ' . date(DateTime::ISO8601, $payload->nbf)
8692
);
@@ -89,14 +95,14 @@ public static function decode($jwt, $key = null, $allowed_algs = array())
8995
// Check that this token has been created before 'now'. This prevents
9096
// using tokens that have been created for later use (and haven't
9197
// correctly used the nbf claim).
92-
if (isset($payload->iat) && $payload->iat > time()) {
98+
if (isset($payload->iat) && $payload->iat > (time() + self::LEEWAYTIME)) {
9399
throw new BeforeValidException(
94100
'Cannot handle token prior to ' . date(DateTime::ISO8601, $payload->iat)
95101
);
96102
}
97103

98104
// Check if this token has expired.
99-
if (isset($payload->exp) && time() >= $payload->exp) {
105+
if (isset($payload->exp) && (time() - self::LEEWAYTIME) >= $payload->exp) {
100106
throw new ExpiredException('Expired token');
101107
}
102108
}

tests/JWTTest.php

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,29 +38,35 @@ public function testMalformedJsonThrowsException()
3838
public function testExpiredToken()
3939
{
4040
$this->setExpectedException('ExpiredException');
41+
$timeInPast = time() - JWT::LEEWAYTIME - 20;
4142
$payload = array(
4243
"message" => "abc",
43-
"exp" => time() - 20); // time in the past
44+
"exp" => $timeInPast // time in the past
45+
);
4446
$encoded = JWT::encode($payload, 'my_key');
4547
JWT::decode($encoded, 'my_key', array('HS256'));
4648
}
4749

4850
public function testBeforeValidTokenWithNbf()
4951
{
5052
$this->setExpectedException('BeforeValidException');
53+
$timeInFuture = time() + JWT::LEEWAYTIME + 20;
5154
$payload = array(
5255
"message" => "abc",
53-
"nbf" => time() + 20); // time in the future
56+
"nbf" => $timeInFuture // time in the future
57+
);
5458
$encoded = JWT::encode($payload, 'my_key');
5559
JWT::decode($encoded, 'my_key', array('HS256'));
5660
}
5761

5862
public function testBeforeValidTokenWithIat()
5963
{
6064
$this->setExpectedException('BeforeValidException');
65+
$timeInFuture = time() + JWT::LEEWAYTIME + 20;
6166
$payload = array(
6267
"message" => "abc",
63-
"iat" => time() + 20); // time in the future
68+
"iat" => $timeInFuture // time in the future
69+
);
6470
$encoded = JWT::encode($payload, 'my_key');
6571
JWT::decode($encoded, 'my_key', array('HS256'));
6672
}

0 commit comments

Comments
 (0)