summaryrefslogtreecommitdiffstats
path: root/tests/JWTTest.php
diff options
context:
space:
mode:
authorRob DiMarco <rob@firebase.com>2015-05-18 14:27:55 -0700
committerRob DiMarco <rob@firebase.com>2015-05-18 14:27:55 -0700
commitf68efb87855220b9c13bdcd56a90840be0b8ba9e (patch)
treef3efd651ef19f9d342e484eefeaf5195888f9d84 /tests/JWTTest.php
parent652f3c62094b2447a0c4cad6b11541bef19ebfaa (diff)
parent452f7c92cd6beaa272eb822e87a73b4b51d7a29e (diff)
downloadphp-jwt-f68efb87855220b9c13bdcd56a90840be0b8ba9e.zip
php-jwt-f68efb87855220b9c13bdcd56a90840be0b8ba9e.tar.gz
php-jwt-f68efb87855220b9c13bdcd56a90840be0b8ba9e.tar.bz2
Merge pull request #46 from lcabral37/skew
Provide a leeway in verification of times to account for clock skew
Diffstat (limited to 'tests/JWTTest.php')
-rw-r--r--tests/JWTTest.php69
1 files changed, 68 insertions, 1 deletions
diff --git a/tests/JWTTest.php b/tests/JWTTest.php
index c48427d..da9975c 100644
--- a/tests/JWTTest.php
+++ b/tests/JWTTest.php
@@ -69,7 +69,30 @@ class JWTTest extends PHPUnit_Framework_TestCase
{
$payload = array(
"message" => "abc",
- "exp" => time() + 20); // time in the future
+ "exp" => time() + JWT::$leeway + 20); // time in the future
+ $encoded = JWT::encode($payload, 'my_key');
+ $decoded = JWT::decode($encoded, 'my_key', array('HS256'));
+ $this->assertEquals($decoded->message, 'abc');
+ }
+
+ public function testValidTokenWithLeeway()
+ {
+ JWT::$leeway = 60;
+ $payload = array(
+ "message" => "abc",
+ "exp" => time() - 20); // time in the past
+ $encoded = JWT::encode($payload, 'my_key');
+ $decoded = JWT::decode($encoded, 'my_key', array('HS256'));
+ $this->assertEquals($decoded->message, 'abc');
+ }
+
+ public function testExpiredTokenWithLeeway()
+ {
+ JWT::$leeway = 60;
+ $payload = array(
+ "message" => "abc",
+ "exp" => time() - 70); // time far in the past
+ $this->setExpectedException('ExpiredException');
$encoded = JWT::encode($payload, 'my_key');
$decoded = JWT::decode($encoded, 'my_key', array('HS256'));
$this->assertEquals($decoded->message, 'abc');
@@ -97,6 +120,50 @@ class JWTTest extends PHPUnit_Framework_TestCase
$this->assertEquals($decoded->message, 'abc');
}
+ public function testValidTokenWithNbfLeeway()
+ {
+ JWT::$leeway = 60;
+ $payload = array(
+ "message" => "abc",
+ "nbf" => time() + 20); // not before in near (leeway) future
+ $encoded = JWT::encode($payload, 'my_key');
+ $decoded = JWT::decode($encoded, 'my_key', array('HS256'));
+ $this->assertEquals($decoded->message, 'abc');
+ }
+
+ public function testInvalidTokenWithNbfLeeway()
+ {
+ JWT::$leeway = 60;
+ $payload = array(
+ "message" => "abc",
+ "nbf" => time() + 65); // not before too far in future
+ $encoded = JWT::encode($payload, 'my_key');
+ $this->setExpectedException('BeforeValidException');
+ $decoded = JWT::decode($encoded, 'my_key', array('HS256'));
+ }
+
+ public function testValidTokenWithIatLeeway()
+ {
+ JWT::$leeway = 60;
+ $payload = array(
+ "message" => "abc",
+ "iat" => time() + 20); // issued in near (leeway) future
+ $encoded = JWT::encode($payload, 'my_key');
+ $decoded = JWT::decode($encoded, 'my_key', array('HS256'));
+ $this->assertEquals($decoded->message, 'abc');
+ }
+
+ public function testInvalidTokenWithIatLeeway()
+ {
+ JWT::$leeway = 60;
+ $payload = array(
+ "message" => "abc",
+ "iat" => time() + 65); // issued too far in future
+ $encoded = JWT::encode($payload, 'my_key');
+ $this->setExpectedException('BeforeValidException');
+ $decoded = JWT::decode($encoded, 'my_key', array('HS256'));
+ }
+
public function testInvalidToken()
{
$payload = array(