Skip to content

Commit 452f7c9

Browse files
author
Luis Miguel Cabral
committed
Added tests with leeway value in nbf and iat
1 parent 3d9bd0a commit 452f7c9

File tree

1 file changed

+44
-0
lines changed

1 file changed

+44
-0
lines changed

tests/JWTTest.php

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,50 @@ public function testValidTokenWithNbf()
120120
$this->assertEquals($decoded->message, 'abc');
121121
}
122122

123+
public function testValidTokenWithNbfLeeway()
124+
{
125+
JWT::$leeway = 60;
126+
$payload = array(
127+
"message" => "abc",
128+
"nbf" => time() + 20); // not before in near (leeway) future
129+
$encoded = JWT::encode($payload, 'my_key');
130+
$decoded = JWT::decode($encoded, 'my_key', array('HS256'));
131+
$this->assertEquals($decoded->message, 'abc');
132+
}
133+
134+
public function testInvalidTokenWithNbfLeeway()
135+
{
136+
JWT::$leeway = 60;
137+
$payload = array(
138+
"message" => "abc",
139+
"nbf" => time() + 65); // not before too far in future
140+
$encoded = JWT::encode($payload, 'my_key');
141+
$this->setExpectedException('BeforeValidException');
142+
$decoded = JWT::decode($encoded, 'my_key', array('HS256'));
143+
}
144+
145+
public function testValidTokenWithIatLeeway()
146+
{
147+
JWT::$leeway = 60;
148+
$payload = array(
149+
"message" => "abc",
150+
"iat" => time() + 20); // issued in near (leeway) future
151+
$encoded = JWT::encode($payload, 'my_key');
152+
$decoded = JWT::decode($encoded, 'my_key', array('HS256'));
153+
$this->assertEquals($decoded->message, 'abc');
154+
}
155+
156+
public function testInvalidTokenWithIatLeeway()
157+
{
158+
JWT::$leeway = 60;
159+
$payload = array(
160+
"message" => "abc",
161+
"iat" => time() + 65); // issued too far in future
162+
$encoded = JWT::encode($payload, 'my_key');
163+
$this->setExpectedException('BeforeValidException');
164+
$decoded = JWT::decode($encoded, 'my_key', array('HS256'));
165+
}
166+
123167
public function testInvalidToken()
124168
{
125169
$payload = array(

0 commit comments

Comments
 (0)