@@ -69,7 +69,30 @@ public function testValidToken()
69
69
{
70
70
$ payload = array (
71
71
"message " => "abc " ,
72
- "exp " => time () + 20 ); // time in the future
72
+ "exp " => time () + JWT ::$ leeway + 20 ); // time in the future
73
+ $ encoded = JWT ::encode ($ payload , 'my_key ' );
74
+ $ decoded = JWT ::decode ($ encoded , 'my_key ' , array ('HS256 ' ));
75
+ $ this ->assertEquals ($ decoded ->message , 'abc ' );
76
+ }
77
+
78
+ public function testValidTokenWithLeeway ()
79
+ {
80
+ JWT ::$ leeway = 60 ;
81
+ $ payload = array (
82
+ "message " => "abc " ,
83
+ "exp " => time () - 20 ); // time in the past
84
+ $ encoded = JWT ::encode ($ payload , 'my_key ' );
85
+ $ decoded = JWT ::decode ($ encoded , 'my_key ' , array ('HS256 ' ));
86
+ $ this ->assertEquals ($ decoded ->message , 'abc ' );
87
+ }
88
+
89
+ public function testExpiredTokenWithLeeway ()
90
+ {
91
+ JWT ::$ leeway = 60 ;
92
+ $ payload = array (
93
+ "message " => "abc " ,
94
+ "exp " => time () - 70 ); // time far in the past
95
+ $ this ->setExpectedException ('ExpiredException ' );
73
96
$ encoded = JWT ::encode ($ payload , 'my_key ' );
74
97
$ decoded = JWT ::decode ($ encoded , 'my_key ' , array ('HS256 ' ));
75
98
$ this ->assertEquals ($ decoded ->message , 'abc ' );
@@ -97,6 +120,50 @@ public function testValidTokenWithNbf()
97
120
$ this ->assertEquals ($ decoded ->message , 'abc ' );
98
121
}
99
122
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
+
100
167
public function testInvalidToken ()
101
168
{
102
169
$ payload = array (
0 commit comments