Skip to content
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
12 changes: 9 additions & 3 deletions Authentication/JWT.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,12 @@
*/
class JWT
{

/**
* When cheking nbf, iat or expiration times, we want to provide some extra leeway time to account for clock skew.
*/
const LEEWAYTIME = 60;

public static $supported_algs = array(
'HS256' => array('hash_hmac', 'SHA256'),
'HS512' => array('hash_hmac', 'SHA512'),
Expand Down Expand Up @@ -80,7 +86,7 @@ public static function decode($jwt, $key = null, $allowed_algs = array())

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

// Check if this token has expired.
if (isset($payload->exp) && time() >= $payload->exp) {
if (isset($payload->exp) && (time() - self::LEEWAYTIME) >= $payload->exp) {
throw new ExpiredException('Expired token');
}
}
Expand Down
12 changes: 9 additions & 3 deletions tests/JWTTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,29 +38,35 @@ public function testMalformedJsonThrowsException()
public function testExpiredToken()
{
$this->setExpectedException('ExpiredException');
$timeInPast = time() - JWT::LEEWAYTIME - 20;
$payload = array(
"message" => "abc",
"exp" => time() - 20); // time in the past
"exp" => $timeInPast // time in the past
);
$encoded = JWT::encode($payload, 'my_key');
JWT::decode($encoded, 'my_key', array('HS256'));
}

public function testBeforeValidTokenWithNbf()
{
$this->setExpectedException('BeforeValidException');
$timeInFuture = time() + JWT::LEEWAYTIME + 20;
$payload = array(
"message" => "abc",
"nbf" => time() + 20); // time in the future
"nbf" => $timeInFuture // time in the future
);
$encoded = JWT::encode($payload, 'my_key');
JWT::decode($encoded, 'my_key', array('HS256'));
}

public function testBeforeValidTokenWithIat()
{
$this->setExpectedException('BeforeValidException');
$timeInFuture = time() + JWT::LEEWAYTIME + 20;
$payload = array(
"message" => "abc",
"iat" => time() + 20); // time in the future
"iat" => $timeInFuture // time in the future
);
$encoded = JWT::encode($payload, 'my_key');
JWT::decode($encoded, 'my_key', array('HS256'));
}
Expand Down