|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types = 1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Sop\JWX\JWK; |
|
6
|
|
|
|
|
7
|
|
|
use Sop\JWX\JWK\Parameter\JWKParameter; |
|
8
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
* Represents a JWK set structure. |
|
11
|
|
|
* |
|
12
|
|
|
* @see https://tools.ietf.org/html/rfc7517#section-5 |
|
13
|
|
|
*/ |
|
14
|
|
|
class JWKSet implements \Countable, \IteratorAggregate |
|
15
|
|
|
{ |
|
16
|
|
|
/** |
|
17
|
|
|
* JWK objects. |
|
18
|
|
|
* |
|
19
|
|
|
* @var JWK[] |
|
20
|
|
|
*/ |
|
21
|
|
|
protected $_jwks; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* Additional members. |
|
25
|
|
|
* |
|
26
|
|
|
* @var array |
|
27
|
|
|
*/ |
|
28
|
|
|
protected $_additional; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* JWK mappings. |
|
32
|
|
|
* |
|
33
|
|
|
* @var array |
|
34
|
|
|
*/ |
|
35
|
|
|
private $_mappings = []; |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* Constructor. |
|
39
|
|
|
* |
|
40
|
|
|
* @param JWK ...$jwks |
|
41
|
|
|
*/ |
|
42
|
55 |
|
public function __construct(JWK ...$jwks) |
|
43
|
|
|
{ |
|
44
|
55 |
|
$this->_jwks = $jwks; |
|
45
|
55 |
|
$this->_additional = []; |
|
46
|
55 |
|
} |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* Reset internal cache variables on clone. |
|
50
|
|
|
*/ |
|
51
|
2 |
|
public function __clone() |
|
52
|
|
|
{ |
|
53
|
2 |
|
$this->_mappings = []; |
|
54
|
2 |
|
} |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* Initialize from an array representing a JSON object. |
|
58
|
|
|
* |
|
59
|
|
|
* @param array $members |
|
60
|
|
|
* |
|
61
|
|
|
* @throws \UnexpectedValueException |
|
62
|
|
|
* |
|
63
|
|
|
* @return self |
|
64
|
|
|
*/ |
|
65
|
3 |
|
public static function fromArray(array $members): self |
|
66
|
|
|
{ |
|
67
|
3 |
|
if (!isset($members['keys']) || !is_array($members['keys'])) { |
|
68
|
1 |
|
throw new \UnexpectedValueException( |
|
69
|
1 |
|
"JWK Set must have a 'keys' member."); |
|
70
|
|
|
} |
|
71
|
2 |
|
$jwks = array_map( |
|
72
|
|
|
function ($jwkdata) { |
|
73
|
2 |
|
return JWK::fromArray($jwkdata); |
|
74
|
2 |
|
}, $members['keys']); |
|
75
|
2 |
|
unset($members['keys']); |
|
76
|
2 |
|
$obj = new self(...$jwks); |
|
77
|
2 |
|
$obj->_additional = $members; |
|
78
|
2 |
|
return $obj; |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
/** |
|
82
|
|
|
* Initialize from a JSON string. |
|
83
|
|
|
* |
|
84
|
|
|
* @param string $json |
|
85
|
|
|
* |
|
86
|
|
|
* @throws \UnexpectedValueException |
|
87
|
|
|
* |
|
88
|
|
|
* @return self |
|
89
|
|
|
*/ |
|
90
|
3 |
|
public static function fromJSON(string $json): self |
|
91
|
|
|
{ |
|
92
|
3 |
|
$members = json_decode($json, true, 32, JSON_BIGINT_AS_STRING); |
|
93
|
3 |
|
if (!is_array($members)) { |
|
94
|
1 |
|
throw new \UnexpectedValueException('Invalid JSON.'); |
|
95
|
|
|
} |
|
96
|
2 |
|
return self::fromArray($members); |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
/** |
|
100
|
|
|
* Get self with keys added. |
|
101
|
|
|
* |
|
102
|
|
|
* @param JWK ...$keys JWK objects |
|
103
|
|
|
* |
|
104
|
|
|
* @return self |
|
105
|
|
|
*/ |
|
106
|
1 |
|
public function withKeys(JWK ...$keys): self |
|
107
|
|
|
{ |
|
108
|
1 |
|
$obj = clone $this; |
|
109
|
1 |
|
$obj->_jwks = array_merge($obj->_jwks, $keys); |
|
110
|
1 |
|
return $obj; |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
/** |
|
114
|
|
|
* Get all JWK's in a set. |
|
115
|
|
|
* |
|
116
|
|
|
* @return JWK[] |
|
117
|
|
|
*/ |
|
118
|
1 |
|
public function keys(): array |
|
119
|
|
|
{ |
|
120
|
1 |
|
return $this->_jwks; |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
/** |
|
124
|
|
|
* Get the first JWK in the set. |
|
125
|
|
|
* |
|
126
|
|
|
* @throws \LogicException |
|
127
|
|
|
* |
|
128
|
|
|
* @return JWK |
|
129
|
|
|
*/ |
|
130
|
5 |
|
public function first(): JWK |
|
131
|
|
|
{ |
|
132
|
5 |
|
if (!count($this->_jwks)) { |
|
133
|
1 |
|
throw new \LogicException('No keys.'); |
|
134
|
|
|
} |
|
135
|
4 |
|
return $this->_jwks[0]; |
|
136
|
|
|
} |
|
137
|
|
|
|
|
138
|
|
|
/** |
|
139
|
|
|
* Check whether set has a JWK with a given key ID. |
|
140
|
|
|
* |
|
141
|
|
|
* @param string $id |
|
142
|
|
|
* |
|
143
|
|
|
* @return bool |
|
144
|
|
|
*/ |
|
145
|
13 |
|
public function hasKeyID(string $id): bool |
|
146
|
|
|
{ |
|
147
|
13 |
|
return null !== $this->_getKeyByID($id); |
|
148
|
|
|
} |
|
149
|
|
|
|
|
150
|
|
|
/** |
|
151
|
|
|
* Get a JWK by a key ID. |
|
152
|
|
|
* |
|
153
|
|
|
* @param string $id |
|
154
|
|
|
* |
|
155
|
|
|
* @throws \LogicException |
|
156
|
|
|
* |
|
157
|
|
|
* @return JWK |
|
158
|
|
|
*/ |
|
159
|
12 |
|
public function keyByID(string $id): JWK |
|
160
|
|
|
{ |
|
161
|
12 |
|
$jwk = $this->_getKeyByID($id); |
|
162
|
12 |
|
if (!$jwk) { |
|
163
|
1 |
|
throw new \LogicException("No key ID {$id}."); |
|
164
|
|
|
} |
|
165
|
11 |
|
return $jwk; |
|
166
|
|
|
} |
|
167
|
|
|
|
|
168
|
|
|
/** |
|
169
|
|
|
* Convert to array. |
|
170
|
|
|
* |
|
171
|
|
|
* @return array |
|
172
|
|
|
*/ |
|
173
|
1 |
|
public function toArray(): array |
|
174
|
|
|
{ |
|
175
|
1 |
|
$data = $this->_additional; |
|
176
|
1 |
|
$data['keys'] = array_map( |
|
177
|
|
|
function (JWK $jwk) { |
|
178
|
1 |
|
return $jwk->toArray(); |
|
179
|
1 |
|
}, $this->_jwks); |
|
180
|
1 |
|
return $data; |
|
181
|
|
|
} |
|
182
|
|
|
|
|
183
|
|
|
/** |
|
184
|
|
|
* Convert to JSON. |
|
185
|
|
|
* |
|
186
|
|
|
* @return string |
|
187
|
|
|
*/ |
|
188
|
1 |
|
public function toJSON(): string |
|
189
|
|
|
{ |
|
190
|
1 |
|
return json_encode((object) $this->toArray(), JSON_UNESCAPED_SLASHES); |
|
191
|
|
|
} |
|
192
|
|
|
|
|
193
|
|
|
/** |
|
194
|
|
|
* Get the number of keys. |
|
195
|
|
|
* |
|
196
|
|
|
* @see \Countable::count() |
|
197
|
|
|
*/ |
|
198
|
14 |
|
public function count(): int |
|
199
|
|
|
{ |
|
200
|
14 |
|
return count($this->_jwks); |
|
201
|
|
|
} |
|
202
|
|
|
|
|
203
|
|
|
/** |
|
204
|
|
|
* Get iterator for JWK objects. |
|
205
|
|
|
* |
|
206
|
|
|
* @see \IteratorAggregate::getIterator() |
|
207
|
|
|
* |
|
208
|
|
|
* @return \ArrayIterator |
|
209
|
|
|
*/ |
|
210
|
1 |
|
public function getIterator(): \ArrayIterator |
|
211
|
|
|
{ |
|
212
|
1 |
|
return new \ArrayIterator($this->_jwks); |
|
213
|
|
|
} |
|
214
|
|
|
|
|
215
|
|
|
/** |
|
216
|
|
|
* Get JWK by key ID. |
|
217
|
|
|
* |
|
218
|
|
|
* @param string $id |
|
219
|
|
|
* |
|
220
|
|
|
* @return null|JWK Null if not found |
|
221
|
|
|
*/ |
|
222
|
18 |
|
protected function _getKeyByID(string $id): ?JWK |
|
223
|
|
|
{ |
|
224
|
18 |
|
$map = $this->_getMapping(JWKParameter::PARAM_KEY_ID); |
|
225
|
18 |
|
return isset($map[$id]) ? $map[$id] : null; |
|
226
|
|
|
} |
|
227
|
|
|
|
|
228
|
|
|
/** |
|
229
|
|
|
* Get mapping from parameter values of given parameter name to JWK. |
|
230
|
|
|
* |
|
231
|
|
|
* Later duplicate value shall override earlier JWK. |
|
232
|
|
|
* |
|
233
|
|
|
* @param string $name Parameter name |
|
234
|
|
|
* |
|
235
|
|
|
* @return array |
|
236
|
|
|
*/ |
|
237
|
18 |
|
protected function _getMapping(string $name): array |
|
238
|
|
|
{ |
|
239
|
18 |
|
if (!isset($this->_mappings[$name])) { |
|
240
|
14 |
|
$mapping = []; |
|
241
|
14 |
|
foreach ($this->_jwks as $jwk) { |
|
242
|
14 |
|
if ($jwk->has($name)) { |
|
243
|
11 |
|
$key = (string) $jwk->get($name)->value(); |
|
244
|
14 |
|
$mapping[$key] = $jwk; |
|
245
|
|
|
} |
|
246
|
|
|
} |
|
247
|
14 |
|
$this->_mappings[$name] = $mapping; |
|
248
|
|
|
} |
|
249
|
18 |
|
return $this->_mappings[$name]; |
|
250
|
|
|
} |
|
251
|
|
|
} |
|
252
|
|
|
|