Skip to content

Commit 95fa9ae

Browse files
author
Luis Miguel Cabral
committed
Changed the leeway to be a static variable
1 parent 61ff178 commit 95fa9ae

File tree

3 files changed

+44
-9
lines changed

3 files changed

+44
-9
lines changed

Authentication/JWT.php

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,11 @@ class JWT
1717
{
1818

1919
/**
20-
* When cheking nbf, iat or expiration times, we want to provide some extra leeway time to account for clock skew.
20+
* When checking nbf, iat or expiration times,
21+
* we want to provide some extra leeway time to
22+
* account for clock skew.
2123
*/
22-
const LEEWAYTIME = 60;
24+
public static $leeway = 0;
2325

2426
public static $supported_algs = array(
2527
'HS256' => array('hash_hmac', 'SHA256'),
@@ -86,7 +88,7 @@ public static function decode($jwt, $key = null, $allowed_algs = array())
8688

8789
// Check if the nbf if it is defined. This is the time that the
8890
// token can actually be used. If it's not yet that time, abort.
89-
if (isset($payload->nbf) && $payload->nbf > (time() + self::LEEWAYTIME)) {
91+
if (isset($payload->nbf) && $payload->nbf > (time() + self::$leeway)) {
9092
throw new BeforeValidException(
9193
'Cannot handle token prior to ' . date(DateTime::ISO8601, $payload->nbf)
9294
);
@@ -95,14 +97,14 @@ public static function decode($jwt, $key = null, $allowed_algs = array())
9597
// Check that this token has been created before 'now'. This prevents
9698
// using tokens that have been created for later use (and haven't
9799
// correctly used the nbf claim).
98-
if (isset($payload->iat) && $payload->iat > (time() + self::LEEWAYTIME)) {
100+
if (isset($payload->iat) && $payload->iat > (time() + self::$leeway)) {
99101
throw new BeforeValidException(
100102
'Cannot handle token prior to ' . date(DateTime::ISO8601, $payload->iat)
101103
);
102104
}
103105

104106
// Check if this token has expired.
105-
if (isset($payload->exp) && (time() - self::LEEWAYTIME) >= $payload->exp) {
107+
if (isset($payload->exp) && (time() - self::$leeway) >= $payload->exp) {
106108
throw new ExpiredException('Expired token');
107109
}
108110
}

README.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,16 @@ print_r($decoded);
4545

4646
$decoded_array = (array) $decoded;
4747

48+
/**
49+
* You can add a leeway to account for when there is a clock skew times between
50+
* the signing and verifying servers. It is recomended this leeway should not
51+
* be bigger than a few minutes.
52+
* Source: http://self-issued.info/docs/draft-ietf-oauth-json-web-token.html#nbfDef
53+
*/
54+
55+
JWT::$leeway = 60;
56+
$decoded = JWT::decode($jwt, $key, array('HS256'));
57+
4858
?>
4959
```
5060

tests/JWTTest.php

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ public function testMalformedJsonThrowsException()
3838
public function testExpiredToken()
3939
{
4040
$this->setExpectedException('ExpiredException');
41-
$timeInPast = time() - JWT::LEEWAYTIME - 20;
41+
$timeInPast = time() - 20;
4242
$payload = array(
4343
"message" => "abc",
4444
"exp" => $timeInPast // time in the past
@@ -50,7 +50,7 @@ public function testExpiredToken()
5050
public function testBeforeValidTokenWithNbf()
5151
{
5252
$this->setExpectedException('BeforeValidException');
53-
$timeInFuture = time() + JWT::LEEWAYTIME + 20;
53+
$timeInFuture = time() + 20;
5454
$payload = array(
5555
"message" => "abc",
5656
"nbf" => $timeInFuture // time in the future
@@ -62,7 +62,7 @@ public function testBeforeValidTokenWithNbf()
6262
public function testBeforeValidTokenWithIat()
6363
{
6464
$this->setExpectedException('BeforeValidException');
65-
$timeInFuture = time() + JWT::LEEWAYTIME + 20;
65+
$timeInFuture = time() + 20;
6666
$payload = array(
6767
"message" => "abc",
6868
"iat" => $timeInFuture // time in the future
@@ -75,7 +75,30 @@ public function testValidToken()
7575
{
7676
$payload = array(
7777
"message" => "abc",
78-
"exp" => time() + 20); // time in the future
78+
"exp" => time() + JWT::$leeway + 20); // time in the future
79+
$encoded = JWT::encode($payload, 'my_key');
80+
$decoded = JWT::decode($encoded, 'my_key', array('HS256'));
81+
$this->assertEquals($decoded->message, 'abc');
82+
}
83+
84+
public function testValidTokenWithLeeway()
85+
{
86+
JWT::$leeway = 60;
87+
$payload = array(
88+
"message" => "abc",
89+
"exp" => time() - 20); // time in the past
90+
$encoded = JWT::encode($payload, 'my_key');
91+
$decoded = JWT::decode($encoded, 'my_key', array('HS256'));
92+
$this->assertEquals($decoded->message, 'abc');
93+
}
94+
95+
public function testExpiredTokenWithLeeway()
96+
{
97+
JWT::$leeway = 60;
98+
$payload = array(
99+
"message" => "abc",
100+
"exp" => time() - 70); // time far in the past
101+
$this->setExpectedException('ExpiredException');
79102
$encoded = JWT::encode($payload, 'my_key');
80103
$decoded = JWT::decode($encoded, 'my_key', array('HS256'));
81104
$this->assertEquals($decoded->message, 'abc');

0 commit comments

Comments
 (0)