forked from luciferous/jwt
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Expand file tree
/
Copy pathJWKTest.php
More file actions
144 lines (114 loc) · 4.02 KB
/
JWKTest.php
File metadata and controls
144 lines (114 loc) · 4.02 KB
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
<?php
namespace Firebase\JWT;
use InvalidArgumentException;
use PHPUnit\Framework\TestCase;
use UnexpectedValueException;
class JWKTest extends TestCase
{
private static $keys;
private static $privKey1;
private static $privKey2;
public function testMissingKty()
{
$this->expectException(UnexpectedValueException::class);
$this->expectExceptionMessage('JWK must contain a "kty" parameter');
$badJwk = ['kid' => 'foo'];
$keys = JWK::parseKeySet(['keys' => [$badJwk]]);
}
public function testInvalidAlgorithm()
{
$this->expectException(UnexpectedValueException::class);
$this->expectExceptionMessage('No supported algorithms found in JWK Set');
$badJwk = ['kty' => 'BADTYPE', 'alg' => 'RSA256'];
$keys = JWK::parseKeySet(['keys' => [$badJwk]]);
}
public function testParsePrivateKey()
{
$this->expectException(UnexpectedValueException::class);
$this->expectExceptionMessage('RSA private keys are not supported');
$jwkSet = json_decode(
file_get_contents(__DIR__ . '/data/rsa-jwkset.json'),
true
);
$jwkSet['keys'][0]['d'] = 'privatekeyvalue';
JWK::parseKeySet($jwkSet);
}
public function testParsePrivateKeyWithoutAlg()
{
$this->expectException(UnexpectedValueException::class);
$this->expectExceptionMessage('JWK must contain an "alg" parameter');
$jwkSet = json_decode(
file_get_contents(__DIR__ . '/data/rsa-jwkset.json'),
true
);
unset($jwkSet['keys'][0]['alg']);
JWK::parseKeySet($jwkSet);
}
public function testParseKeyWithEmptyDValue()
{
$jwkSet = json_decode(
file_get_contents(__DIR__ . '/data/rsa-jwkset.json'),
true
);
// empty or null values are ok
$jwkSet['keys'][0]['d'] = null;
$keys = JWK::parseKeySet($jwkSet);
$this->assertTrue(\is_array($keys));
}
public function testParseJwkKeySet()
{
$jwkSet = json_decode(
file_get_contents(__DIR__ . '/data/rsa-jwkset.json'),
true
);
$keys = JWK::parseKeySet($jwkSet);
$this->assertTrue(\is_array($keys));
$this->assertArrayHasKey('jwk1', $keys);
self::$keys = $keys;
}
public function testParseJwkKey_empty()
{
$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage('JWK must not be empty');
JWK::parseKeySet(['keys' => [[]]]);
}
public function testParseJwkKeySet_empty()
{
$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage('JWK Set did not contain any keys');
JWK::parseKeySet(['keys' => []]);
}
/**
* @depends testParseJwkKeySet
*/
public function testDecodeByJwkKeySetTokenExpired()
{
$privKey1 = file_get_contents(__DIR__ . '/data/rsa1-private.pem');
$payload = ['exp' => strtotime('-1 hour')];
$msg = JWT::encode($payload, $privKey1, 'RS256', 'jwk1');
$this->expectException(ExpiredException::class);
JWT::decode($msg, self::$keys);
}
/**
* @depends testParseJwkKeySet
*/
public function testDecodeByJwkKeySet()
{
$privKey1 = file_get_contents(__DIR__ . '/data/rsa1-private.pem');
$payload = ['sub' => 'foo', 'exp' => strtotime('+10 seconds')];
$msg = JWT::encode($payload, $privKey1, 'RS256', 'jwk1');
$result = JWT::decode($msg, self::$keys);
$this->assertEquals('foo', $result->sub);
}
/**
* @depends testParseJwkKeySet
*/
public function testDecodeByMultiJwkKeySet()
{
$privKey2 = file_get_contents(__DIR__ . '/data/rsa2-private.pem');
$payload = ['sub' => 'bar', 'exp' => strtotime('+10 seconds')];
$msg = JWT::encode($payload, $privKey2, 'RS256', 'jwk2');
$result = JWT::decode($msg, self::$keys);
$this->assertEquals('bar', $result->sub);
}
}