forked from luciferous/jwt
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
Copy pathJWTTest.php
576 lines (511 loc) · 19.3 KB
/
JWTTest.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
<?php
namespace Firebase\JWT;
use ArrayObject;
use DomainException;
use InvalidArgumentException;
use PHPUnit\Framework\TestCase;
use stdClass;
use TypeError;
use UnexpectedValueException;
class JWTTest extends TestCase
{
public function testUrlSafeCharacters()
{
$encoded = JWT::encode(['message' => 'f?'], 'a', 'HS256');
$expected = new stdClass();
$expected->message = 'f?';
$this->assertEquals($expected, JWT::decode($encoded, new Key('a', 'HS256')));
}
public function testMalformedUtf8StringsFail()
{
$this->expectException(DomainException::class);
JWT::encode(['message' => pack('c', 128)], 'a', 'HS256');
}
public function testInvalidKeyOpensslSignFail()
{
$this->expectException(DomainException::class);
JWT::sign('message', 'invalid key', 'openssl');
}
public function testMalformedJsonThrowsException()
{
$this->expectException(DomainException::class);
JWT::jsonDecode('this is not valid JSON string');
}
public function testExpiredToken()
{
$this->expectException(ExpiredException::class);
$payload = [
'message' => 'abc',
'exp' => time() - 20, // time in the past
];
$encoded = JWT::encode($payload, 'my_key', 'HS256');
JWT::decode($encoded, new Key('my_key', 'HS256'));
}
public function testBeforeValidTokenWithNbf()
{
$this->expectException(BeforeValidException::class);
$payload = [
'message' => 'abc',
'nbf' => time() + 20, // time in the future
];
$encoded = JWT::encode($payload, 'my_key', 'HS256');
JWT::decode($encoded, new Key('my_key', 'HS256'));
}
public function testBeforeValidTokenWithIat()
{
$this->expectException(BeforeValidException::class);
$payload = [
'message' => 'abc',
'iat' => time() + 20, // time in the future
];
$encoded = JWT::encode($payload, 'my_key', 'HS256');
JWT::decode($encoded, new Key('my_key', 'HS256'));
}
public function testValidToken()
{
$payload = [
'message' => 'abc',
'exp' => time() + JWT::$leeway + 20, // time in the future
];
$encoded = JWT::encode($payload, 'my_key', 'HS256');
$decoded = JWT::decode($encoded, new Key('my_key', 'HS256'));
$this->assertSame($decoded->message, 'abc');
}
/**
* @runInSeparateProcess
*/
public function testValidTokenWithLeeway()
{
JWT::$leeway = 60;
$payload = [
'message' => 'abc',
'exp' => time() - 20, // time in the past
];
$encoded = JWT::encode($payload, 'my_key', 'HS256');
$decoded = JWT::decode($encoded, new Key('my_key', 'HS256'));
$this->assertSame($decoded->message, 'abc');
}
/**
* @runInSeparateProcess
*/
public function testExpiredTokenWithLeeway()
{
JWT::$leeway = 60;
$payload = [
'message' => 'abc',
'exp' => time() - 70, // time far in the past
];
$this->expectException(ExpiredException::class);
$encoded = JWT::encode($payload, 'my_key', 'HS256');
$decoded = JWT::decode($encoded, new Key('my_key', 'HS256'));
$this->assertSame($decoded->message, 'abc');
}
public function testExpiredExceptionPayload()
{
$this->expectException(ExpiredException::class);
$payload = [
'message' => 'abc',
'exp' => time() - 100, // time in the past
];
$encoded = JWT::encode($payload, 'my_key', 'HS256');
try {
JWT::decode($encoded, new Key('my_key', 'HS256'));
} catch (ExpiredException $e) {
$exceptionPayload = (array) $e->getPayload();
$this->assertEquals($exceptionPayload, $payload);
throw $e;
}
}
public function testBeforeValidExceptionPayload()
{
$this->expectException(BeforeValidException::class);
$payload = [
'message' => 'abc',
'iat' => time() + 100, // time in the future
];
$encoded = JWT::encode($payload, 'my_key', 'HS256');
try {
JWT::decode($encoded, new Key('my_key', 'HS256'));
} catch (BeforeValidException $e) {
$exceptionPayload = (array) $e->getPayload();
$this->assertEquals($exceptionPayload, $payload);
throw $e;
}
}
public function testValidTokenWithNbf()
{
$payload = [
'message' => 'abc',
'iat' => time(),
'exp' => time() + 20, // time in the future
'nbf' => time() - 20
];
$encoded = JWT::encode($payload, 'my_key', 'HS256');
$decoded = JWT::decode($encoded, new Key('my_key', 'HS256'));
$this->assertSame($decoded->message, 'abc');
}
/**
* @runInSeparateProcess
*/
public function testValidTokenWithNbfLeeway()
{
JWT::$leeway = 60;
$payload = [
'message' => 'abc',
'nbf' => time() + 20, // not before in near (leeway) future
];
$encoded = JWT::encode($payload, 'my_key', 'HS256');
$decoded = JWT::decode($encoded, new Key('my_key', 'HS256'));
$this->assertSame($decoded->message, 'abc');
}
/**
* @runInSeparateProcess
*/
public function testInvalidTokenWithNbfLeeway()
{
JWT::$leeway = 60;
$payload = [
'message' => 'abc',
'nbf' => time() + 65, // not before too far in future
];
$encoded = JWT::encode($payload, 'my_key', 'HS256');
$this->expectException(BeforeValidException::class);
$this->expectExceptionMessage('Cannot handle token with nbf prior to');
JWT::decode($encoded, new Key('my_key', 'HS256'));
}
public function testValidTokenWithNbfIgnoresIat()
{
$payload = [
'message' => 'abc',
'nbf' => time() - 20, // time in the future
'iat' => time() + 20, // time in the past
];
$encoded = JWT::encode($payload, 'my_key', 'HS256');
$decoded = JWT::decode($encoded, new Key('my_key', 'HS256'));
$this->assertEquals('abc', $decoded->message);
}
public function testValidTokenWithNbfMicrotime()
{
$payload = [
'message' => 'abc',
'nbf' => microtime(true), // use microtime
];
$encoded = JWT::encode($payload, 'my_key', 'HS256');
$decoded = JWT::decode($encoded, new Key('my_key', 'HS256'));
$this->assertEquals('abc', $decoded->message);
}
public function testInvalidTokenWithNbfMicrotime()
{
$this->expectException(BeforeValidException::class);
$this->expectExceptionMessage('Cannot handle token with nbf prior to');
$payload = [
'message' => 'abc',
'nbf' => microtime(true) + 20, // use microtime in the future
];
$encoded = JWT::encode($payload, 'my_key', 'HS256');
JWT::decode($encoded, new Key('my_key', 'HS256'));
}
/**
* @runInSeparateProcess
*/
public function testValidTokenWithIatLeeway()
{
JWT::$leeway = 60;
$payload = [
'message' => 'abc',
'iat' => time() + 20, // issued in near (leeway) future
];
$encoded = JWT::encode($payload, 'my_key', 'HS256');
$decoded = JWT::decode($encoded, new Key('my_key', 'HS256'));
$this->assertSame($decoded->message, 'abc');
}
/**
* @runInSeparateProcess
*/
public function testInvalidTokenWithIatLeeway()
{
JWT::$leeway = 60;
$payload = [
'message' => 'abc',
'iat' => time() + 65, // issued too far in future
];
$encoded = JWT::encode($payload, 'my_key', 'HS256');
$this->expectException(BeforeValidException::class);
$this->expectExceptionMessage('Cannot handle token with iat prior to');
JWT::decode($encoded, new Key('my_key', 'HS256'));
}
public function testValidTokenWithIatMicrotime()
{
$payload = [
'message' => 'abc',
'iat' => microtime(true), // use microtime
];
$encoded = JWT::encode($payload, 'my_key', 'HS256');
$decoded = JWT::decode($encoded, new Key('my_key', 'HS256'));
$this->assertEquals('abc', $decoded->message);
}
public function testInvalidTokenWithIatMicrotime()
{
$this->expectException(BeforeValidException::class);
$this->expectExceptionMessage('Cannot handle token with iat prior to');
$payload = [
'message' => 'abc',
'iat' => microtime(true) + 20, // use microtime in the future
];
$encoded = JWT::encode($payload, 'my_key', 'HS256');
JWT::decode($encoded, new Key('my_key', 'HS256'));
}
public function testInvalidToken()
{
$payload = [
'message' => 'abc',
'exp' => time() + 20, // time in the future
];
$encoded = JWT::encode($payload, 'my_key', 'HS256');
$this->expectException(SignatureInvalidException::class);
JWT::decode($encoded, new Key('my_key2', 'HS256'));
}
public function testNullKeyFails()
{
$payload = [
'message' => 'abc',
'exp' => time() + JWT::$leeway + 20, // time in the future
];
$encoded = JWT::encode($payload, 'my_key', 'HS256');
$this->expectException(TypeError::class);
JWT::decode($encoded, new Key(null, 'HS256'));
}
public function testEmptyKeyFails()
{
$payload = [
'message' => 'abc',
'exp' => time() + JWT::$leeway + 20, // time in the future
];
$encoded = JWT::encode($payload, 'my_key', 'HS256');
$this->expectException(InvalidArgumentException::class);
JWT::decode($encoded, new Key('', 'HS256'));
}
public function testKIDChooser()
{
$keys = [
'0' => new Key('my_key0', 'HS256'),
'1' => new Key('my_key1', 'HS256'),
'2' => new Key('my_key2', 'HS256')
];
$msg = JWT::encode(['message' => 'abc'], $keys['0']->getKeyMaterial(), 'HS256', '0');
$decoded = JWT::decode($msg, $keys);
$expected = new stdClass();
$expected->message = 'abc';
$this->assertEquals($decoded, $expected);
}
public function testArrayAccessKIDChooser()
{
$keys = new ArrayObject([
'0' => new Key('my_key0', 'HS256'),
'1' => new Key('my_key1', 'HS256'),
'2' => new Key('my_key2', 'HS256'),
]);
$msg = JWT::encode(['message' => 'abc'], $keys['0']->getKeyMaterial(), 'HS256', '0');
$decoded = JWT::decode($msg, $keys);
$expected = new stdClass();
$expected->message = 'abc';
$this->assertEquals($decoded, $expected);
}
public function testNoneAlgorithm()
{
$msg = JWT::encode(['message' => 'abc'], 'my_key', 'HS256');
$this->expectException(UnexpectedValueException::class);
JWT::decode($msg, new Key('my_key', 'none'));
}
public function testIncorrectAlgorithm()
{
$msg = JWT::encode(['message' => 'abc'], 'my_key', 'HS256');
$this->expectException(UnexpectedValueException::class);
JWT::decode($msg, new Key('my_key', 'RS256'));
}
public function testEmptyAlgorithm()
{
$msg = JWT::encode(['message' => 'abc'], 'my_key', 'HS256');
$this->expectException(InvalidArgumentException::class);
JWT::decode($msg, new Key('my_key', ''));
}
public function testAdditionalHeaders()
{
$msg = JWT::encode(['message' => 'abc'], 'my_key', 'HS256', null, ['cty' => 'test-eit;v=1']);
$expected = new stdClass();
$expected->message = 'abc';
$this->assertEquals(JWT::decode($msg, new Key('my_key', 'HS256')), $expected);
}
public function testInvalidSegmentCount()
{
$this->expectException(UnexpectedValueException::class);
JWT::decode('brokenheader.brokenbody', new Key('my_key', 'HS256'));
}
public function testInvalidSignatureEncoding()
{
$msg = 'eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpZCI6MSwibmFtZSI6ImZvbyJ9.Q4Kee9E8o0Xfo4ADXvYA8t7dN_X_bU9K5w6tXuiSjlUxx';
$this->expectException(UnexpectedValueException::class);
JWT::decode($msg, new Key('secret', 'HS256'));
}
public function testHSEncodeDecode()
{
$msg = JWT::encode(['message' => 'abc'], 'my_key', 'HS256');
$expected = new stdClass();
$expected->message = 'abc';
$this->assertEquals(JWT::decode($msg, new Key('my_key', 'HS256')), $expected);
}
public function testRSEncodeDecode()
{
$privKey = openssl_pkey_new(['digest_alg' => 'sha256',
'private_key_bits' => 1024,
'private_key_type' => OPENSSL_KEYTYPE_RSA]);
$msg = JWT::encode(['message' => 'abc'], $privKey, 'RS256');
$pubKey = openssl_pkey_get_details($privKey);
$pubKey = $pubKey['key'];
$decoded = JWT::decode($msg, new Key($pubKey, 'RS256'));
$expected = new stdClass();
$expected->message = 'abc';
$this->assertEquals($decoded, $expected);
}
public function testEdDsaEncodeDecode()
{
$keyPair = sodium_crypto_sign_keypair();
$privKey = base64_encode(sodium_crypto_sign_secretkey($keyPair));
$payload = ['foo' => 'bar'];
$msg = JWT::encode($payload, $privKey, 'EdDSA');
$pubKey = base64_encode(sodium_crypto_sign_publickey($keyPair));
$decoded = JWT::decode($msg, new Key($pubKey, 'EdDSA'));
$this->assertSame('bar', $decoded->foo);
}
public function testInvalidEdDsaEncodeDecode()
{
$keyPair = sodium_crypto_sign_keypair();
$privKey = base64_encode(sodium_crypto_sign_secretkey($keyPair));
$payload = ['foo' => 'bar'];
$msg = JWT::encode($payload, $privKey, 'EdDSA');
// Generate a different key.
$keyPair = sodium_crypto_sign_keypair();
$pubKey = base64_encode(sodium_crypto_sign_publickey($keyPair));
$this->expectException(SignatureInvalidException::class);
JWT::decode($msg, new Key($pubKey, 'EdDSA'));
}
public function testRSEncodeDecodeWithPassphrase()
{
$privateKey = openssl_pkey_get_private(
file_get_contents(__DIR__ . '/data/rsa-with-passphrase.pem'),
'passphrase'
);
$jwt = JWT::encode(['message' => 'abc'], $privateKey, 'RS256');
$keyDetails = openssl_pkey_get_details($privateKey);
$pubKey = $keyDetails['key'];
$decoded = JWT::decode($jwt, new Key($pubKey, 'RS256'));
$expected = new stdClass();
$expected->message = 'abc';
$this->assertEquals($decoded, $expected);
}
public function testDecodesEmptyArrayAsObject()
{
$key = 'yma6Hq4XQegCVND8ef23OYgxSrC3IKqk';
$payload = [];
$jwt = JWT::encode($payload, $key, 'HS256');
$decoded = JWT::decode($jwt, new Key($key, 'HS256'));
$this->assertEquals((object) $payload, $decoded);
}
public function testDecodesArraysInJWTAsArray()
{
$key = 'yma6Hq4XQegCVND8ef23OYgxSrC3IKqk';
$payload = ['foo' => [1, 2, 3]];
$jwt = JWT::encode($payload, $key, 'HS256');
$decoded = JWT::decode($jwt, new Key($key, 'HS256'));
$this->assertSame($payload['foo'], $decoded->foo);
}
/**
* @runInSeparateProcess
* @dataProvider provideEncodeDecode
*/
public function testEncodeDecode($privateKeyFile, $publicKeyFile, $alg)
{
$privateKey = file_get_contents($privateKeyFile);
$payload = ['foo' => 'bar'];
$encoded = JWT::encode($payload, $privateKey, $alg);
// Verify decoding succeeds
$publicKey = file_get_contents($publicKeyFile);
$decoded = JWT::decode($encoded, new Key($publicKey, $alg));
$this->assertSame('bar', $decoded->foo);
}
public function provideEncodeDecode()
{
return [
[__DIR__ . '/data/ecdsa-private.pem', __DIR__ . '/data/ecdsa-public.pem', 'ES256'],
[__DIR__ . '/data/ecdsa384-private.pem', __DIR__ . '/data/ecdsa384-public.pem', 'ES384'],
[__DIR__ . '/data/rsa1-private.pem', __DIR__ . '/data/rsa1-public.pub', 'RS512'],
[__DIR__ . '/data/ed25519-1.sec', __DIR__ . '/data/ed25519-1.pub', 'EdDSA'],
[__DIR__ . '/data/secp256k1-private.pem', __DIR__ . '/data/secp256k1-public.pem', 'ES256K'],
];
}
public function testEncodeDecodeWithResource()
{
$pem = file_get_contents(__DIR__ . '/data/rsa1-public.pub');
$resource = openssl_pkey_get_public($pem);
$privateKey = file_get_contents(__DIR__ . '/data/rsa1-private.pem');
$payload = ['foo' => 'bar'];
$encoded = JWT::encode($payload, $privateKey, 'RS512');
// Verify decoding succeeds
$decoded = JWT::decode($encoded, new Key($resource, 'RS512'));
$this->assertSame('bar', $decoded->foo);
}
public function testGetHeaders()
{
$payload = [
'message' => 'abc',
'exp' => time() + JWT::$leeway + 20, // time in the future
];
$headers = new stdClass();
$encoded = JWT::encode($payload, 'my_key', 'HS256');
JWT::decode($encoded, new Key('my_key', 'HS256'), $headers);
$this->assertEquals($headers->typ, 'JWT');
$this->assertEquals($headers->alg, 'HS256');
}
public function testAdditionalHeaderOverrides()
{
$msg = JWT::encode(
['message' => 'abc'],
'my_key',
'HS256',
'my_key_id',
[
'cty' => 'test-eit;v=1',
'typ' => 'JOSE', // override type header
'kid' => 'not_my_key_id', // should not override $key param
'alg' => 'BAD', // should not override $alg param
]
);
$headers = new stdClass();
JWT::decode($msg, new Key('my_key', 'HS256'), $headers);
$this->assertEquals('test-eit;v=1', $headers->cty, 'additional field works');
$this->assertEquals('JOSE', $headers->typ, 'typ override works');
$this->assertEquals('my_key_id', $headers->kid, 'key param not overridden');
$this->assertEquals('HS256', $headers->alg, 'alg param not overridden');
}
public function testDecodeExpectsIntegerIat()
{
$this->expectException(UnexpectedValueException::class);
$this->expectExceptionMessage('Payload iat must be a number');
$payload = JWT::encode(['iat' => 'not-an-int'], 'secret', 'HS256');
JWT::decode($payload, new Key('secret', 'HS256'));
}
public function testDecodeExpectsIntegerNbf()
{
$this->expectException(UnexpectedValueException::class);
$this->expectExceptionMessage('Payload nbf must be a number');
$payload = JWT::encode(['nbf' => 'not-an-int'], 'secret', 'HS256');
JWT::decode($payload, new Key('secret', 'HS256'));
}
public function testDecodeExpectsIntegerExp()
{
$this->expectException(UnexpectedValueException::class);
$this->expectExceptionMessage('Payload exp must be a number');
$payload = JWT::encode(['exp' => 'not-an-int'], 'secret', 'HS256');
JWT::decode($payload, new Key('secret', 'HS256'));
}
}