|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types = 1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Sop\JWX\JWK; |
|
6
|
|
|
|
|
7
|
|
|
use Sop\JWX\JWK\Parameter\JWKParameter; |
|
8
|
|
|
use Sop\JWX\JWK\Parameter\KeyIDParameter; |
|
9
|
|
|
|
|
10
|
|
|
/** |
|
11
|
|
|
* Class to represent JWK structure. |
|
12
|
|
|
* |
|
13
|
|
|
* @see https://tools.ietf.org/html/rfc7517#section-4 |
|
14
|
|
|
*/ |
|
15
|
|
|
class JWK implements \Countable, \IteratorAggregate |
|
16
|
|
|
{ |
|
17
|
|
|
use TypedJWK; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* Parameters. |
|
21
|
|
|
* |
|
22
|
|
|
* @var JWKParameter[] |
|
23
|
|
|
*/ |
|
24
|
|
|
protected $_parameters; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* Constructor. |
|
28
|
|
|
* |
|
29
|
|
|
* @param JWKParameter ...$params |
|
30
|
|
|
*/ |
|
31
|
153 |
|
public function __construct(JWKParameter ...$params) |
|
32
|
|
|
{ |
|
33
|
153 |
|
$this->_parameters = []; |
|
34
|
153 |
|
foreach ($params as $param) { |
|
35
|
142 |
|
$this->_parameters[$param->name()] = $param; |
|
36
|
|
|
} |
|
37
|
153 |
|
} |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* Initialize from an array representing a JSON object. |
|
41
|
|
|
* |
|
42
|
|
|
* @param array $members |
|
43
|
|
|
* |
|
44
|
|
|
* @return self |
|
45
|
|
|
*/ |
|
46
|
35 |
|
public static function fromArray(array $members): self |
|
47
|
|
|
{ |
|
48
|
35 |
|
$params = []; |
|
49
|
35 |
|
foreach ($members as $name => $value) { |
|
50
|
33 |
|
$params[] = JWKParameter::fromNameAndValue($name, $value); |
|
51
|
|
|
} |
|
52
|
35 |
|
return new static(...$params); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* Initialize from a JSON string. |
|
57
|
|
|
* |
|
58
|
|
|
* @param string $json |
|
59
|
|
|
* |
|
60
|
|
|
* @throws \UnexpectedValueException |
|
61
|
|
|
* |
|
62
|
|
|
* @return self |
|
63
|
|
|
*/ |
|
64
|
11 |
|
public static function fromJSON(string $json): self |
|
65
|
|
|
{ |
|
66
|
11 |
|
$members = json_decode($json, true, 32, JSON_BIGINT_AS_STRING); |
|
67
|
11 |
|
if (!is_array($members)) { |
|
68
|
1 |
|
throw new \UnexpectedValueException('Invalid JSON.'); |
|
69
|
|
|
} |
|
70
|
10 |
|
return static::fromArray($members); |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* Initialize from another JWK. |
|
75
|
|
|
* |
|
76
|
|
|
* Allows casting to subclass by late static binding. |
|
77
|
|
|
* |
|
78
|
|
|
* @param JWK $jwk |
|
79
|
|
|
* |
|
80
|
|
|
* @return self |
|
81
|
|
|
*/ |
|
82
|
58 |
|
public static function fromJWK(JWK $jwk): self |
|
83
|
|
|
{ |
|
84
|
58 |
|
return new static(...array_values($jwk->_parameters)); |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
/** |
|
88
|
|
|
* Get self with parameters added. |
|
89
|
|
|
* |
|
90
|
|
|
* @param JWKParameter ...$params |
|
91
|
|
|
* |
|
92
|
|
|
* @return self |
|
93
|
|
|
*/ |
|
94
|
10 |
|
public function withParameters(JWKParameter ...$params): self |
|
95
|
|
|
{ |
|
96
|
10 |
|
$obj = clone $this; |
|
97
|
10 |
|
foreach ($params as $param) { |
|
98
|
10 |
|
$obj->_parameters[$param->name()] = $param; |
|
99
|
|
|
} |
|
100
|
10 |
|
return $obj; |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
/** |
|
104
|
|
|
* Get all parameters. |
|
105
|
|
|
* |
|
106
|
|
|
* @return JWKParameter[] |
|
107
|
|
|
*/ |
|
108
|
3 |
|
public function parameters(): array |
|
109
|
|
|
{ |
|
110
|
3 |
|
return array_values($this->_parameters); |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
/** |
|
114
|
|
|
* Get self with given key ID added to parameters. |
|
115
|
|
|
* |
|
116
|
|
|
* @param string $id Key ID as a string |
|
117
|
|
|
* |
|
118
|
|
|
* @return self |
|
119
|
|
|
*/ |
|
120
|
4 |
|
public function withKeyID(string $id): self |
|
121
|
|
|
{ |
|
122
|
4 |
|
return $this->withParameters(new KeyIDParameter($id)); |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
/** |
|
126
|
|
|
* Whether parameters are present. |
|
127
|
|
|
* |
|
128
|
|
|
* Returns false if any of the given parameters is not set. |
|
129
|
|
|
* |
|
130
|
|
|
* @param string ...$names Parameter names |
|
131
|
|
|
* |
|
132
|
|
|
* @return bool |
|
133
|
|
|
*/ |
|
134
|
233 |
|
public function has(string ...$names): bool |
|
135
|
|
|
{ |
|
136
|
233 |
|
foreach ($names as $name) { |
|
137
|
233 |
|
if (!isset($this->_parameters[$name])) { |
|
138
|
233 |
|
return false; |
|
139
|
|
|
} |
|
140
|
|
|
} |
|
141
|
221 |
|
return true; |
|
142
|
|
|
} |
|
143
|
|
|
|
|
144
|
|
|
/** |
|
145
|
|
|
* Get a parameter. |
|
146
|
|
|
* |
|
147
|
|
|
* @param string $name Parameter name |
|
148
|
|
|
* |
|
149
|
|
|
* @throws \LogicException |
|
150
|
|
|
* |
|
151
|
|
|
* @return JWKParameter |
|
152
|
|
|
*/ |
|
153
|
196 |
|
public function get(string $name): JWKParameter |
|
154
|
|
|
{ |
|
155
|
196 |
|
if (!$this->has($name)) { |
|
156
|
1 |
|
throw new \LogicException("Parameter {$name} doesn't exists."); |
|
157
|
|
|
} |
|
158
|
195 |
|
return $this->_parameters[$name]; |
|
159
|
|
|
} |
|
160
|
|
|
|
|
161
|
|
|
/** |
|
162
|
|
|
* Convert to array. |
|
163
|
|
|
* |
|
164
|
|
|
* @return array Parameter values keyed by parameter names |
|
165
|
|
|
*/ |
|
166
|
5 |
|
public function toArray(): array |
|
167
|
|
|
{ |
|
168
|
5 |
|
$a = []; |
|
169
|
5 |
|
foreach ($this->_parameters as $param) { |
|
170
|
2 |
|
$a[$param->name()] = $param->value(); |
|
171
|
|
|
} |
|
172
|
5 |
|
return $a; |
|
173
|
|
|
} |
|
174
|
|
|
|
|
175
|
|
|
/** |
|
176
|
|
|
* Convert to JSON. |
|
177
|
|
|
* |
|
178
|
|
|
* @return string |
|
179
|
|
|
*/ |
|
180
|
2 |
|
public function toJSON(): string |
|
181
|
|
|
{ |
|
182
|
2 |
|
$data = $this->toArray(); |
|
183
|
2 |
|
if (empty($data)) { |
|
184
|
1 |
|
return ''; |
|
185
|
|
|
} |
|
186
|
1 |
|
return json_encode((object) $data, JSON_UNESCAPED_SLASHES); |
|
187
|
|
|
} |
|
188
|
|
|
|
|
189
|
|
|
/** |
|
190
|
|
|
* Get the number of parameters. |
|
191
|
|
|
* |
|
192
|
|
|
* @see \Countable::count() |
|
193
|
|
|
*/ |
|
194
|
1 |
|
public function count(): int |
|
195
|
|
|
{ |
|
196
|
1 |
|
return count($this->_parameters); |
|
197
|
|
|
} |
|
198
|
|
|
|
|
199
|
|
|
/** |
|
200
|
|
|
* Get iterator for the parameters. |
|
201
|
|
|
* |
|
202
|
|
|
* @see \IteratorAggregate::getIterator() |
|
203
|
|
|
* |
|
204
|
|
|
* @return \ArrayIterator |
|
205
|
|
|
*/ |
|
206
|
1 |
|
public function getIterator(): \ArrayIterator |
|
207
|
|
|
{ |
|
208
|
1 |
|
return new \ArrayIterator($this->_parameters); |
|
209
|
|
|
} |
|
210
|
|
|
} |
|
211
|
|
|
|