forked from luciferous/jwt
/
JWKTest.php
163 lines (132 loc) · 4.46 KB
/
JWKTest.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
<?php
namespace Firebase\JWT;
use PHPUnit\Framework\TestCase;
class JWKTest extends TestCase
{
private static $keys;
private static $privKey1;
private static $privKey2;
public function testMissingKty()
{
$this->setExpectedException(
'UnexpectedValueException',
'JWK must contain a "kty" parameter'
);
$badJwk = array('kid' => 'foo');
$keys = JWK::parseKeySet(array('keys' => array($badJwk)));
}
public function testInvalidAlgorithm()
{
$this->setExpectedException(
'UnexpectedValueException',
'No supported algorithms found in JWK Set'
);
$badJwk = array('kty' => 'BADALG', 'alg' => 'RSA256');
$keys = JWK::parseKeySet(array('keys' => array($badJwk)));
}
public function testParsePrivateKey()
{
$this->setExpectedException(
'UnexpectedValueException',
'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->setExpectedException(
'UnexpectedValueException',
'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->setExpectedException('InvalidArgumentException', 'JWK must not be empty');
JWK::parseKeySet(array('keys' => array(array())));
}
public function testParseJwkKeySet_empty()
{
$this->setExpectedException('InvalidArgumentException', 'JWK Set did not contain any keys');
JWK::parseKeySet(array('keys' => array()));
}
/**
* @depends testParseJwkKeySet
*/
public function testDecodeByJwkKeySetTokenExpired()
{
$privKey1 = file_get_contents(__DIR__ . '/data/rsa1-private.pem');
$payload = array('exp' => strtotime('-1 hour'));
$msg = JWT::encode($payload, $privKey1, 'RS256', 'jwk1');
$this->setExpectedException('Firebase\JWT\ExpiredException');
JWT::decode($msg, self::$keys, array('RS256'));
}
/**
* @depends testParseJwkKeySet
*/
public function testDecodeByJwkKeySet()
{
$privKey1 = file_get_contents(__DIR__ . '/data/rsa1-private.pem');
$payload = array('sub' => 'foo', 'exp' => strtotime('+10 seconds'));
$msg = JWT::encode($payload, $privKey1, 'RS256', 'jwk1');
$result = JWT::decode($msg, self::$keys, array('RS256'));
$this->assertEquals("foo", $result->sub);
}
/**
* @depends testParseJwkKeySet
*/
public function testDecodeByMultiJwkKeySet()
{
$privKey2 = file_get_contents(__DIR__ . '/data/rsa2-private.pem');
$payload = array('sub' => 'bar', 'exp' => strtotime('+10 seconds'));
$msg = JWT::encode($payload, $privKey2, 'RS256', 'jwk2');
$result = JWT::decode($msg, self::$keys, array('RS256'));
$this->assertEquals("bar", $result->sub);
}
/*
* For compatibility with PHPUnit 4.8 and PHP < 5.6
*/
public function setExpectedException($exceptionName, $message = '', $code = null)
{
if (method_exists($this, 'expectException')) {
$this->expectException($exceptionName);
if ($message) {
$this->expectExceptionMessage($message);
}
} else {
parent::setExpectedException($exceptionName, $message, $code);
}
}
}