3
3
namespace Firebase \JWT ;
4
4
5
5
use ArrayObject ;
6
- use PHPUnit \Framework \TestCase ;
7
6
use DomainException ;
8
7
use InvalidArgumentException ;
8
+ use PHPUnit \Framework \TestCase ;
9
+ use stdClass ;
9
10
use TypeError ;
10
11
use UnexpectedValueException ;
11
- use stdClass ;
12
12
13
13
class JWTTest extends TestCase
14
14
{
@@ -37,7 +37,8 @@ public function testExpiredToken()
37
37
$ this ->expectException (ExpiredException::class);
38
38
$ payload = [
39
39
'message ' => 'abc ' ,
40
- 'exp ' => time () - 20 ]; // time in the past
40
+ 'exp ' => time () - 20 , // time in the past
41
+ ];
41
42
$ encoded = JWT ::encode ($ payload , 'my_key ' , 'HS256 ' );
42
43
JWT ::decode ($ encoded , new Key ('my_key ' , 'HS256 ' ));
43
44
}
@@ -47,7 +48,8 @@ public function testBeforeValidTokenWithNbf()
47
48
$ this ->expectException (BeforeValidException::class);
48
49
$ payload = [
49
50
'message ' => 'abc ' ,
50
- "nbf " => time () + 20 ]; // time in the future
51
+ 'nbf ' => time () + 20 , // time in the future
52
+ ];
51
53
$ encoded = JWT ::encode ($ payload , 'my_key ' , 'HS256 ' );
52
54
JWT ::decode ($ encoded , new Key ('my_key ' , 'HS256 ' ));
53
55
}
@@ -57,7 +59,8 @@ public function testBeforeValidTokenWithIat()
57
59
$ this ->expectException (BeforeValidException::class);
58
60
$ payload = [
59
61
'message ' => 'abc ' ,
60
- "iat " => time () + 20 ]; // time in the future
62
+ 'iat ' => time () + 20 , // time in the future
63
+ ];
61
64
$ encoded = JWT ::encode ($ payload , 'my_key ' , 'HS256 ' );
62
65
JWT ::decode ($ encoded , new Key ('my_key ' , 'HS256 ' ));
63
66
}
@@ -66,7 +69,8 @@ public function testValidToken()
66
69
{
67
70
$ payload = [
68
71
'message ' => 'abc ' ,
69
- 'exp ' => time () + JWT ::$ leeway + 20 ]; // time in the future
72
+ 'exp ' => time () + JWT ::$ leeway + 20 , // time in the future
73
+ ];
70
74
$ encoded = JWT ::encode ($ payload , 'my_key ' , 'HS256 ' );
71
75
$ decoded = JWT ::decode ($ encoded , new Key ('my_key ' , 'HS256 ' ));
72
76
$ this ->assertEquals ($ decoded ->message , 'abc ' );
@@ -77,7 +81,8 @@ public function testValidTokenWithLeeway()
77
81
JWT ::$ leeway = 60 ;
78
82
$ payload = [
79
83
'message ' => 'abc ' ,
80
- 'exp ' => time () - 20 ]; // time in the past
84
+ 'exp ' => time () - 20 , // time in the past
85
+ ];
81
86
$ encoded = JWT ::encode ($ payload , 'my_key ' , 'HS256 ' );
82
87
$ decoded = JWT ::decode ($ encoded , new Key ('my_key ' , 'HS256 ' ));
83
88
$ this ->assertEquals ($ decoded ->message , 'abc ' );
@@ -89,7 +94,8 @@ public function testExpiredTokenWithLeeway()
89
94
JWT ::$ leeway = 60 ;
90
95
$ payload = [
91
96
'message ' => 'abc ' ,
92
- 'exp ' => time () - 70 ]; // time far in the past
97
+ 'exp ' => time () - 70 , // time far in the past
98
+ ];
93
99
$ this ->expectException (ExpiredException::class);
94
100
$ encoded = JWT ::encode ($ payload , 'my_key ' , 'HS256 ' );
95
101
$ decoded = JWT ::decode ($ encoded , new Key ('my_key ' , 'HS256 ' ));
@@ -101,9 +107,10 @@ public function testValidTokenWithNbf()
101
107
{
102
108
$ payload = [
103
109
'message ' => 'abc ' ,
104
- " iat " => time (),
110
+ ' iat ' => time (),
105
111
'exp ' => time () + 20 , // time in the future
106
- "nbf " => time () - 20 ];
112
+ 'nbf ' => time () - 20
113
+ ];
107
114
$ encoded = JWT ::encode ($ payload , 'my_key ' , 'HS256 ' );
108
115
$ decoded = JWT ::decode ($ encoded , new Key ('my_key ' , 'HS256 ' ));
109
116
$ this ->assertEquals ($ decoded ->message , 'abc ' );
@@ -114,7 +121,8 @@ public function testValidTokenWithNbfLeeway()
114
121
JWT ::$ leeway = 60 ;
115
122
$ payload = [
116
123
'message ' => 'abc ' ,
117
- "nbf " => time () + 20 ]; // not before in near (leeway) future
124
+ 'nbf ' => time () + 20 , // not before in near (leeway) future
125
+ ];
118
126
$ encoded = JWT ::encode ($ payload , 'my_key ' , 'HS256 ' );
119
127
$ decoded = JWT ::decode ($ encoded , new Key ('my_key ' , 'HS256 ' ));
120
128
$ this ->assertEquals ($ decoded ->message , 'abc ' );
@@ -126,7 +134,8 @@ public function testInvalidTokenWithNbfLeeway()
126
134
JWT ::$ leeway = 60 ;
127
135
$ payload = [
128
136
'message ' => 'abc ' ,
129
- "nbf " => time () + 65 ]; // not before too far in future
137
+ 'nbf ' => time () + 65 , // not before too far in future
138
+ ];
130
139
$ encoded = JWT ::encode ($ payload , 'my_key ' , 'HS256 ' );
131
140
$ this ->expectException (BeforeValidException::class);
132
141
JWT ::decode ($ encoded , new Key ('my_key ' , 'HS256 ' ));
@@ -138,7 +147,8 @@ public function testValidTokenWithIatLeeway()
138
147
JWT ::$ leeway = 60 ;
139
148
$ payload = [
140
149
'message ' => 'abc ' ,
141
- "iat " => time () + 20 ]; // issued in near (leeway) future
150
+ 'iat ' => time () + 20 , // issued in near (leeway) future
151
+ ];
142
152
$ encoded = JWT ::encode ($ payload , 'my_key ' , 'HS256 ' );
143
153
$ decoded = JWT ::decode ($ encoded , new Key ('my_key ' , 'HS256 ' ));
144
154
$ this ->assertEquals ($ decoded ->message , 'abc ' );
@@ -150,7 +160,8 @@ public function testInvalidTokenWithIatLeeway()
150
160
JWT ::$ leeway = 60 ;
151
161
$ payload = [
152
162
'message ' => 'abc ' ,
153
- "iat " => time () + 65 ]; // issued too far in future
163
+ 'iat ' => time () + 65 , // issued too far in future
164
+ ];
154
165
$ encoded = JWT ::encode ($ payload , 'my_key ' , 'HS256 ' );
155
166
$ this ->expectException (BeforeValidException::class);
156
167
JWT ::decode ($ encoded , new Key ('my_key ' , 'HS256 ' ));
@@ -161,7 +172,8 @@ public function testInvalidToken()
161
172
{
162
173
$ payload = [
163
174
'message ' => 'abc ' ,
164
- 'exp ' => time () + 20 ]; // time in the future
175
+ 'exp ' => time () + 20 , // time in the future
176
+ ];
165
177
$ encoded = JWT ::encode ($ payload , 'my_key ' , 'HS256 ' );
166
178
$ this ->expectException (SignatureInvalidException::class);
167
179
JWT ::decode ($ encoded , new Key ('my_key2 ' , 'HS256 ' ));
@@ -171,7 +183,8 @@ public function testNullKeyFails()
171
183
{
172
184
$ payload = [
173
185
'message ' => 'abc ' ,
174
- 'exp ' => time () + JWT ::$ leeway + 20 ]; // time in the future
186
+ 'exp ' => time () + JWT ::$ leeway + 20 , // time in the future
187
+ ];
175
188
$ encoded = JWT ::encode ($ payload , 'my_key ' , 'HS256 ' );
176
189
$ this ->expectException (TypeError::class);
177
190
JWT ::decode ($ encoded , new Key (null , 'HS256 ' ));
@@ -181,7 +194,8 @@ public function testEmptyKeyFails()
181
194
{
182
195
$ payload = [
183
196
'message ' => 'abc ' ,
184
- 'exp ' => time () + JWT ::$ leeway + 20 ]; // time in the future
197
+ 'exp ' => time () + JWT ::$ leeway + 20 , // time in the future
198
+ ];
185
199
$ encoded = JWT ::encode ($ payload , 'my_key ' , 'HS256 ' );
186
200
$ this ->expectException (InvalidArgumentException::class);
187
201
JWT ::decode ($ encoded , new Key ('' , 'HS256 ' ));
@@ -250,7 +264,7 @@ public function testInvalidSegmentCount()
250
264
251
265
public function testInvalidSignatureEncoding ()
252
266
{
253
- $ msg = " eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpZCI6MSwibmFtZSI6ImZvbyJ9.Q4Kee9E8o0Xfo4ADXvYA8t7dN_X_bU9K5w6tXuiSjlUxx " ;
267
+ $ msg = ' eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpZCI6MSwibmFtZSI6ImZvbyJ9.Q4Kee9E8o0Xfo4ADXvYA8t7dN_X_bU9K5w6tXuiSjlUxx ' ;
254
268
$ this ->expectException (UnexpectedValueException::class);
255
269
JWT ::decode ($ msg , new Key ('secret ' , 'HS256 ' ));
256
270
}
@@ -333,7 +347,7 @@ public function testDecodesEmptyArrayAsObject()
333
347
public function testDecodesArraysInJWTAsArray ()
334
348
{
335
349
$ key = 'yma6Hq4XQegCVND8ef23OYgxSrC3IKqk ' ;
336
- $ payload = ['foo ' => [1 ,2 , 3 ]];
350
+ $ payload = ['foo ' => [1 , 2 , 3 ]];
337
351
$ jwt = JWT ::encode ($ payload , $ key , 'HS256 ' );
338
352
$ decoded = JWT ::decode ($ jwt , new Key ($ key , 'HS256 ' ));
339
353
$ this ->assertEquals ($ payload ['foo ' ], $ decoded ->foo );
0 commit comments