@@ -13,117 +13,117 @@ |
||
13 | 13 | */ |
14 | 14 | class CurveParameter extends JWKParameter |
15 | 15 | { |
16 | - use StringParameterValue; |
|
16 | + use StringParameterValue; |
|
17 | 17 | |
18 | - /** |
|
19 | - * P-256 Curve. |
|
20 | - * |
|
21 | - * @var string |
|
22 | - */ |
|
23 | - const CURVE_P256 = 'P-256'; |
|
18 | + /** |
|
19 | + * P-256 Curve. |
|
20 | + * |
|
21 | + * @var string |
|
22 | + */ |
|
23 | + const CURVE_P256 = 'P-256'; |
|
24 | 24 | |
25 | - /** |
|
26 | - * P-384 Curve. |
|
27 | - * |
|
28 | - * @var string |
|
29 | - */ |
|
30 | - const CURVE_P384 = 'P-384'; |
|
25 | + /** |
|
26 | + * P-384 Curve. |
|
27 | + * |
|
28 | + * @var string |
|
29 | + */ |
|
30 | + const CURVE_P384 = 'P-384'; |
|
31 | 31 | |
32 | - /** |
|
33 | - * P-521 Curve. |
|
34 | - * |
|
35 | - * @var string |
|
36 | - */ |
|
37 | - const CURVE_P521 = 'P-521'; |
|
32 | + /** |
|
33 | + * P-521 Curve. |
|
34 | + * |
|
35 | + * @var string |
|
36 | + */ |
|
37 | + const CURVE_P521 = 'P-521'; |
|
38 | 38 | |
39 | - /** |
|
40 | - * Mapping from curve OID to curve name. |
|
41 | - * |
|
42 | - * @internal |
|
43 | - * |
|
44 | - * @var array |
|
45 | - */ |
|
46 | - const MAP_OID_TO_CURVE = [ |
|
47 | - '1.2.840.10045.3.1.7' => self::CURVE_P256, |
|
48 | - '1.3.132.0.34' => self::CURVE_P384, |
|
49 | - '1.3.132.0.35' => self::CURVE_P521, |
|
50 | - ]; |
|
39 | + /** |
|
40 | + * Mapping from curve OID to curve name. |
|
41 | + * |
|
42 | + * @internal |
|
43 | + * |
|
44 | + * @var array |
|
45 | + */ |
|
46 | + const MAP_OID_TO_CURVE = [ |
|
47 | + '1.2.840.10045.3.1.7' => self::CURVE_P256, |
|
48 | + '1.3.132.0.34' => self::CURVE_P384, |
|
49 | + '1.3.132.0.35' => self::CURVE_P521, |
|
50 | + ]; |
|
51 | 51 | |
52 | - /** |
|
53 | - * Mapping from curve name to bit size. |
|
54 | - * |
|
55 | - * @internal |
|
56 | - * |
|
57 | - * @var array |
|
58 | - */ |
|
59 | - const MAP_CURVE_TO_SIZE = [ |
|
60 | - self::CURVE_P256 => 256, |
|
61 | - self::CURVE_P384 => 384, |
|
62 | - self::CURVE_P521 => 521, |
|
63 | - ]; |
|
52 | + /** |
|
53 | + * Mapping from curve name to bit size. |
|
54 | + * |
|
55 | + * @internal |
|
56 | + * |
|
57 | + * @var array |
|
58 | + */ |
|
59 | + const MAP_CURVE_TO_SIZE = [ |
|
60 | + self::CURVE_P256 => 256, |
|
61 | + self::CURVE_P384 => 384, |
|
62 | + self::CURVE_P521 => 521, |
|
63 | + ]; |
|
64 | 64 | |
65 | - /** |
|
66 | - * Constructor. |
|
67 | - * |
|
68 | - * @param string $curve Curve name |
|
69 | - */ |
|
70 | - public function __construct(string $curve) |
|
71 | - { |
|
72 | - parent::__construct(self::PARAM_CURVE, $curve); |
|
73 | - } |
|
65 | + /** |
|
66 | + * Constructor. |
|
67 | + * |
|
68 | + * @param string $curve Curve name |
|
69 | + */ |
|
70 | + public function __construct(string $curve) |
|
71 | + { |
|
72 | + parent::__construct(self::PARAM_CURVE, $curve); |
|
73 | + } |
|
74 | 74 | |
75 | - /** |
|
76 | - * Initialize from curve OID. |
|
77 | - * |
|
78 | - * @param string $oid Object identifier in dotted format |
|
79 | - * |
|
80 | - * @throws \UnexpectedValueException If the curve is not supported |
|
81 | - * |
|
82 | - * @return self |
|
83 | - */ |
|
84 | - public static function fromOID(string $oid): self |
|
85 | - { |
|
86 | - if (!array_key_exists($oid, self::MAP_OID_TO_CURVE)) { |
|
87 | - throw new \UnexpectedValueException("OID {$oid} not supported."); |
|
88 | - } |
|
89 | - $curve = self::MAP_OID_TO_CURVE[$oid]; |
|
90 | - return new self($curve); |
|
91 | - } |
|
75 | + /** |
|
76 | + * Initialize from curve OID. |
|
77 | + * |
|
78 | + * @param string $oid Object identifier in dotted format |
|
79 | + * |
|
80 | + * @throws \UnexpectedValueException If the curve is not supported |
|
81 | + * |
|
82 | + * @return self |
|
83 | + */ |
|
84 | + public static function fromOID(string $oid): self |
|
85 | + { |
|
86 | + if (!array_key_exists($oid, self::MAP_OID_TO_CURVE)) { |
|
87 | + throw new \UnexpectedValueException("OID {$oid} not supported."); |
|
88 | + } |
|
89 | + $curve = self::MAP_OID_TO_CURVE[$oid]; |
|
90 | + return new self($curve); |
|
91 | + } |
|
92 | 92 | |
93 | - /** |
|
94 | - * Get key size in bits for the curve. |
|
95 | - * |
|
96 | - * @throws \UnexpectedValueException |
|
97 | - * |
|
98 | - * @return int |
|
99 | - */ |
|
100 | - public function keySizeBits(): int |
|
101 | - { |
|
102 | - if (!array_key_exists($this->_value, self::MAP_CURVE_TO_SIZE)) { |
|
103 | - throw new \UnexpectedValueException( |
|
104 | - 'Curve ' . $this->_value . ' not supported.'); |
|
105 | - } |
|
106 | - return self::MAP_CURVE_TO_SIZE[$this->_value]; |
|
107 | - } |
|
93 | + /** |
|
94 | + * Get key size in bits for the curve. |
|
95 | + * |
|
96 | + * @throws \UnexpectedValueException |
|
97 | + * |
|
98 | + * @return int |
|
99 | + */ |
|
100 | + public function keySizeBits(): int |
|
101 | + { |
|
102 | + if (!array_key_exists($this->_value, self::MAP_CURVE_TO_SIZE)) { |
|
103 | + throw new \UnexpectedValueException( |
|
104 | + 'Curve ' . $this->_value . ' not supported.'); |
|
105 | + } |
|
106 | + return self::MAP_CURVE_TO_SIZE[$this->_value]; |
|
107 | + } |
|
108 | 108 | |
109 | - /** |
|
110 | - * Get the curve OID by curve name. |
|
111 | - * |
|
112 | - * @param string $name Curve parameter name |
|
113 | - * |
|
114 | - * @throws \UnexpectedValueException If the curve is not supported |
|
115 | - * |
|
116 | - * @return string OID in dotted format |
|
117 | - */ |
|
118 | - public static function nameToOID(string $name): string |
|
119 | - { |
|
120 | - static $reverseMap; |
|
121 | - if (!isset($reverseMap)) { |
|
122 | - $reverseMap = array_flip(self::MAP_OID_TO_CURVE); |
|
123 | - } |
|
124 | - if (!isset($reverseMap[$name])) { |
|
125 | - throw new \UnexpectedValueException("Curve {$name} not supported."); |
|
126 | - } |
|
127 | - return $reverseMap[$name]; |
|
128 | - } |
|
109 | + /** |
|
110 | + * Get the curve OID by curve name. |
|
111 | + * |
|
112 | + * @param string $name Curve parameter name |
|
113 | + * |
|
114 | + * @throws \UnexpectedValueException If the curve is not supported |
|
115 | + * |
|
116 | + * @return string OID in dotted format |
|
117 | + */ |
|
118 | + public static function nameToOID(string $name): string |
|
119 | + { |
|
120 | + static $reverseMap; |
|
121 | + if (!isset($reverseMap)) { |
|
122 | + $reverseMap = array_flip(self::MAP_OID_TO_CURVE); |
|
123 | + } |
|
124 | + if (!isset($reverseMap[$name])) { |
|
125 | + throw new \UnexpectedValueException("Curve {$name} not supported."); |
|
126 | + } |
|
127 | + return $reverseMap[$name]; |
|
128 | + } |
|
129 | 129 | } |
@@ -1,6 +1,6 @@ |
||
1 | 1 | <?php |
2 | 2 | |
3 | -declare(strict_types = 1); |
|
3 | +declare(strict_types=1); |
|
4 | 4 | |
5 | 5 | namespace Sop\JWX\JWK\Parameter; |
6 | 6 |
@@ -11,594 +11,594 @@ |
||
11 | 11 | */ |
12 | 12 | trait TypedJWK |
13 | 13 | { |
14 | - /** |
|
15 | - * Whether parameters are present. |
|
16 | - * |
|
17 | - * @param string ...$names Parameter names |
|
18 | - * |
|
19 | - * @return bool |
|
20 | - */ |
|
21 | - abstract public function has(string ...$names): bool; |
|
22 | - |
|
23 | - /** |
|
24 | - * Get a parameter. |
|
25 | - * |
|
26 | - * @param string $name Parameter name |
|
27 | - * |
|
28 | - * @return JWKParameter |
|
29 | - */ |
|
30 | - abstract public function get(string $name): JWKParameter; |
|
31 | - |
|
32 | - /** |
|
33 | - * Check whether the algorithm parameter is present. |
|
34 | - * |
|
35 | - * @return bool |
|
36 | - */ |
|
37 | - public function hasAlgorithmParameter(): bool |
|
38 | - { |
|
39 | - return $this->has(Parameter\JWKParameter::P_ALG); |
|
40 | - } |
|
41 | - |
|
42 | - /** |
|
43 | - * Get the algorithm parameter. |
|
44 | - * |
|
45 | - * @throws \UnexpectedValueException If the parameter has a wrong class |
|
46 | - * @throws \LogicException If the parameter is not present |
|
47 | - * |
|
48 | - * @return Parameter\AlgorithmParameter |
|
49 | - */ |
|
50 | - public function algorithmParameter(): Parameter\AlgorithmParameter |
|
51 | - { |
|
52 | - return self::_checkType($this->get(Parameter\JWKParameter::P_ALG), |
|
53 | - Parameter\AlgorithmParameter::class); |
|
54 | - } |
|
55 | - |
|
56 | - /** |
|
57 | - * Check whether the curve parameter is present. |
|
58 | - * |
|
59 | - * @return bool |
|
60 | - */ |
|
61 | - public function hasCurveParameter(): bool |
|
62 | - { |
|
63 | - return $this->has(Parameter\JWKParameter::P_CRV); |
|
64 | - } |
|
65 | - |
|
66 | - /** |
|
67 | - * Get the curve parameter. |
|
68 | - * |
|
69 | - * @throws \UnexpectedValueException If the parameter has a wrong class |
|
70 | - * @throws \LogicException If the parameter is not present |
|
71 | - * |
|
72 | - * @return Parameter\CurveParameter |
|
73 | - */ |
|
74 | - public function curveParameter(): Parameter\CurveParameter |
|
75 | - { |
|
76 | - return self::_checkType($this->get(Parameter\JWKParameter::P_CRV), |
|
77 | - Parameter\CurveParameter::class); |
|
78 | - } |
|
79 | - |
|
80 | - /** |
|
81 | - * Check whether the ECC private key parameter is present. |
|
82 | - * |
|
83 | - * @return bool |
|
84 | - */ |
|
85 | - public function hasECCPrivateKeyParameter(): bool |
|
86 | - { |
|
87 | - return $this->has(Parameter\JWKParameter::P_ECC_D); |
|
88 | - } |
|
89 | - |
|
90 | - /** |
|
91 | - * Get the ECC private key parameter. |
|
92 | - * |
|
93 | - * @throws \UnexpectedValueException If the parameter has a wrong class |
|
94 | - * @throws \LogicException If the parameter is not present |
|
95 | - * |
|
96 | - * @return Parameter\ECCPrivateKeyParameter |
|
97 | - */ |
|
98 | - public function ECCPrivateKeyParameter(): Parameter\ECCPrivateKeyParameter |
|
99 | - { |
|
100 | - return self::_checkType($this->get(Parameter\JWKParameter::P_ECC_D), |
|
101 | - Parameter\ECCPrivateKeyParameter::class); |
|
102 | - } |
|
103 | - |
|
104 | - /** |
|
105 | - * Check whether the exponent parameter is present. |
|
106 | - * |
|
107 | - * @return bool |
|
108 | - */ |
|
109 | - public function hasExponentParameter(): bool |
|
110 | - { |
|
111 | - return $this->has(Parameter\JWKParameter::P_E); |
|
112 | - } |
|
113 | - |
|
114 | - /** |
|
115 | - * Get the exponent parameter. |
|
116 | - * |
|
117 | - * @throws \UnexpectedValueException If the parameter has a wrong class |
|
118 | - * @throws \LogicException If the parameter is not present |
|
119 | - * |
|
120 | - * @return Parameter\ExponentParameter |
|
121 | - */ |
|
122 | - public function exponentParameter(): Parameter\ExponentParameter |
|
123 | - { |
|
124 | - return self::_checkType($this->get(Parameter\JWKParameter::P_E), |
|
125 | - Parameter\ExponentParameter::class); |
|
126 | - } |
|
127 | - |
|
128 | - /** |
|
129 | - * Check whether the first CRT coefficient parameter is present. |
|
130 | - * |
|
131 | - * @return bool |
|
132 | - */ |
|
133 | - public function hasFirstCRTCoefficientParameter(): bool |
|
134 | - { |
|
135 | - return $this->has(Parameter\JWKParameter::P_QI); |
|
136 | - } |
|
137 | - |
|
138 | - /** |
|
139 | - * Get the first CRT coefficient parameter. |
|
140 | - * |
|
141 | - * @throws \UnexpectedValueException If the parameter has a wrong class |
|
142 | - * @throws \LogicException If the parameter is not present |
|
143 | - * |
|
144 | - * @return Parameter\FirstCRTCoefficientParameter |
|
145 | - */ |
|
146 | - public function firstCRTCoefficientParameter(): Parameter\FirstCRTCoefficientParameter |
|
147 | - { |
|
148 | - return self::_checkType($this->get(Parameter\JWKParameter::P_QI), |
|
149 | - Parameter\FirstCRTCoefficientParameter::class); |
|
150 | - } |
|
151 | - |
|
152 | - /** |
|
153 | - * Check whether the first factor CRT exponent parameter is present. |
|
154 | - * |
|
155 | - * @return bool |
|
156 | - */ |
|
157 | - public function hasFirstFactorCRTExponentParameter(): bool |
|
158 | - { |
|
159 | - return $this->has(Parameter\JWKParameter::P_DP); |
|
160 | - } |
|
161 | - |
|
162 | - /** |
|
163 | - * Get the first factor CRT exponent parameter. |
|
164 | - * |
|
165 | - * @throws \UnexpectedValueException If the parameter has a wrong class |
|
166 | - * @throws \LogicException If the parameter is not present |
|
167 | - * |
|
168 | - * @return Parameter\FirstFactorCRTExponentParameter |
|
169 | - */ |
|
170 | - public function firstFactorCRTExponentParameter(): Parameter\FirstFactorCRTExponentParameter |
|
171 | - { |
|
172 | - return self::_checkType($this->get(Parameter\JWKParameter::P_DP), |
|
173 | - Parameter\FirstFactorCRTExponentParameter::class); |
|
174 | - } |
|
175 | - |
|
176 | - /** |
|
177 | - * Check whether the first prime factor parameter is present. |
|
178 | - * |
|
179 | - * @return bool |
|
180 | - */ |
|
181 | - public function hasFirstPrimeFactorParameter(): bool |
|
182 | - { |
|
183 | - return $this->has(Parameter\JWKParameter::P_P); |
|
184 | - } |
|
185 | - |
|
186 | - /** |
|
187 | - * Get the first prime factor parameter. |
|
188 | - * |
|
189 | - * @throws \UnexpectedValueException If the parameter has a wrong class |
|
190 | - * @throws \LogicException If the parameter is not present |
|
191 | - * |
|
192 | - * @return Parameter\FirstPrimeFactorParameter |
|
193 | - */ |
|
194 | - public function firstPrimeFactorParameter(): Parameter\FirstPrimeFactorParameter |
|
195 | - { |
|
196 | - return self::_checkType($this->get(Parameter\JWKParameter::P_P), |
|
197 | - Parameter\FirstPrimeFactorParameter::class); |
|
198 | - } |
|
199 | - |
|
200 | - /** |
|
201 | - * Check whether the key ID parameter is present. |
|
202 | - * |
|
203 | - * @return bool |
|
204 | - */ |
|
205 | - public function hasKeyIDParameter(): bool |
|
206 | - { |
|
207 | - return $this->has(Parameter\JWKParameter::P_KID); |
|
208 | - } |
|
209 | - |
|
210 | - /** |
|
211 | - * Get the key ID parameter. |
|
212 | - * |
|
213 | - * @throws \UnexpectedValueException If the parameter has a wrong class |
|
214 | - * @throws \LogicException If the parameter is not present |
|
215 | - * |
|
216 | - * @return Parameter\KeyIDParameter |
|
217 | - */ |
|
218 | - public function keyIDParameter(): Parameter\KeyIDParameter |
|
219 | - { |
|
220 | - return self::_checkType($this->get(Parameter\JWKParameter::P_KID), |
|
221 | - Parameter\KeyIDParameter::class); |
|
222 | - } |
|
223 | - |
|
224 | - /** |
|
225 | - * Check whether the key operations parameter is present. |
|
226 | - * |
|
227 | - * @return bool |
|
228 | - */ |
|
229 | - public function hasKeyOperationsParameter(): bool |
|
230 | - { |
|
231 | - return $this->has(Parameter\JWKParameter::P_KEY_OPS); |
|
232 | - } |
|
233 | - |
|
234 | - /** |
|
235 | - * Get the key operations parameter. |
|
236 | - * |
|
237 | - * @throws \UnexpectedValueException If the parameter has a wrong class |
|
238 | - * @throws \LogicException If the parameter is not present |
|
239 | - * |
|
240 | - * @return Parameter\KeyOperationsParameter |
|
241 | - */ |
|
242 | - public function keyOperationsParameter(): Parameter\KeyOperationsParameter |
|
243 | - { |
|
244 | - return self::_checkType($this->get(Parameter\JWKParameter::P_KEY_OPS), |
|
245 | - Parameter\KeyOperationsParameter::class); |
|
246 | - } |
|
247 | - |
|
248 | - /** |
|
249 | - * Check whether the key type parameter is present. |
|
250 | - * |
|
251 | - * @return bool |
|
252 | - */ |
|
253 | - public function hasKeyTypeParameter(): bool |
|
254 | - { |
|
255 | - return $this->has(Parameter\JWKParameter::P_KTY); |
|
256 | - } |
|
257 | - |
|
258 | - /** |
|
259 | - * Get the key type parameter. |
|
260 | - * |
|
261 | - * @throws \UnexpectedValueException If the parameter has a wrong class |
|
262 | - * @throws \LogicException If the parameter is not present |
|
263 | - * |
|
264 | - * @return Parameter\KeyTypeParameter |
|
265 | - */ |
|
266 | - public function keyTypeParameter(): Parameter\KeyTypeParameter |
|
267 | - { |
|
268 | - return self::_checkType($this->get(Parameter\JWKParameter::P_KTY), |
|
269 | - Parameter\KeyTypeParameter::class); |
|
270 | - } |
|
271 | - |
|
272 | - /** |
|
273 | - * Check whether the key value parameter is present. |
|
274 | - * |
|
275 | - * @return bool |
|
276 | - */ |
|
277 | - public function hasKeyValueParameter(): bool |
|
278 | - { |
|
279 | - return $this->has(Parameter\JWKParameter::P_K); |
|
280 | - } |
|
281 | - |
|
282 | - /** |
|
283 | - * Get the key value parameter. |
|
284 | - * |
|
285 | - * @throws \UnexpectedValueException If the parameter has a wrong class |
|
286 | - * @throws \LogicException If the parameter is not present |
|
287 | - * |
|
288 | - * @return Parameter\KeyValueParameter |
|
289 | - */ |
|
290 | - public function keyValueParameter(): Parameter\KeyValueParameter |
|
291 | - { |
|
292 | - return self::_checkType($this->get(Parameter\JWKParameter::P_K), |
|
293 | - Parameter\KeyValueParameter::class); |
|
294 | - } |
|
295 | - |
|
296 | - /** |
|
297 | - * Check whether the modulus parameter is present. |
|
298 | - * |
|
299 | - * @return bool |
|
300 | - */ |
|
301 | - public function hasModulusParameter(): bool |
|
302 | - { |
|
303 | - return $this->has(Parameter\JWKParameter::P_N); |
|
304 | - } |
|
305 | - |
|
306 | - /** |
|
307 | - * Get the modulus parameter. |
|
308 | - * |
|
309 | - * @throws \UnexpectedValueException If the parameter has a wrong class |
|
310 | - * @throws \LogicException If the parameter is not present |
|
311 | - * |
|
312 | - * @return Parameter\ModulusParameter |
|
313 | - */ |
|
314 | - public function modulusParameter(): Parameter\ModulusParameter |
|
315 | - { |
|
316 | - return self::_checkType($this->get(Parameter\JWKParameter::P_N), |
|
317 | - Parameter\ModulusParameter::class); |
|
318 | - } |
|
319 | - |
|
320 | - /** |
|
321 | - * Check whether the other primes info parameter is present. |
|
322 | - * |
|
323 | - * @return bool |
|
324 | - */ |
|
325 | - public function hasOtherPrimesInfoParameter(): bool |
|
326 | - { |
|
327 | - return $this->has(Parameter\JWKParameter::P_OTH); |
|
328 | - } |
|
329 | - |
|
330 | - /** |
|
331 | - * Get the other primes info parameter. |
|
332 | - * |
|
333 | - * @throws \UnexpectedValueException If the parameter has a wrong class |
|
334 | - * @throws \LogicException If the parameter is not present |
|
335 | - * |
|
336 | - * @return Parameter\OtherPrimesInfoParameter |
|
337 | - */ |
|
338 | - public function otherPrimesInfoParameter(): Parameter\OtherPrimesInfoParameter |
|
339 | - { |
|
340 | - return self::_checkType($this->get(Parameter\JWKParameter::P_OTH), |
|
341 | - Parameter\OtherPrimesInfoParameter::class); |
|
342 | - } |
|
343 | - |
|
344 | - /** |
|
345 | - * Check whether the private exponent parameter is present. |
|
346 | - * |
|
347 | - * @return bool |
|
348 | - */ |
|
349 | - public function hasPrivateExponentParameter(): bool |
|
350 | - { |
|
351 | - return $this->has(Parameter\JWKParameter::P_RSA_D); |
|
352 | - } |
|
353 | - |
|
354 | - /** |
|
355 | - * Get the private exponent parameter. |
|
356 | - * |
|
357 | - * @throws \UnexpectedValueException If the parameter has a wrong class |
|
358 | - * @throws \LogicException If the parameter is not present |
|
359 | - * |
|
360 | - * @return Parameter\PrivateExponentParameter |
|
361 | - */ |
|
362 | - public function privateExponentParameter(): Parameter\PrivateExponentParameter |
|
363 | - { |
|
364 | - return self::_checkType($this->get(Parameter\JWKParameter::P_RSA_D), |
|
365 | - Parameter\PrivateExponentParameter::class); |
|
366 | - } |
|
367 | - |
|
368 | - /** |
|
369 | - * Check whether the public key use parameter is present. |
|
370 | - * |
|
371 | - * @return bool |
|
372 | - */ |
|
373 | - public function hasPublicKeyUseParameter(): bool |
|
374 | - { |
|
375 | - return $this->has(Parameter\JWKParameter::P_USE); |
|
376 | - } |
|
377 | - |
|
378 | - /** |
|
379 | - * Get the public key use parameter. |
|
380 | - * |
|
381 | - * @throws \UnexpectedValueException If the parameter has a wrong class |
|
382 | - * @throws \LogicException If the parameter is not present |
|
383 | - * |
|
384 | - * @return Parameter\PublicKeyUseParameter |
|
385 | - */ |
|
386 | - public function publicKeyUseParameter(): Parameter\PublicKeyUseParameter |
|
387 | - { |
|
388 | - return self::_checkType($this->get(Parameter\JWKParameter::P_USE), |
|
389 | - Parameter\PublicKeyUseParameter::class); |
|
390 | - } |
|
391 | - |
|
392 | - /** |
|
393 | - * Check whether the second factor CRT exponent parameter is present. |
|
394 | - * |
|
395 | - * @return bool |
|
396 | - */ |
|
397 | - public function hasSecondFactorCRTExponentParameter(): bool |
|
398 | - { |
|
399 | - return $this->has(Parameter\JWKParameter::P_DQ); |
|
400 | - } |
|
401 | - |
|
402 | - /** |
|
403 | - * Get the second factor CRT exponent parameter. |
|
404 | - * |
|
405 | - * @throws \UnexpectedValueException If the parameter has a wrong class |
|
406 | - * @throws \LogicException If the parameter is not present |
|
407 | - * |
|
408 | - * @return Parameter\SecondFactorCRTExponentParameter |
|
409 | - */ |
|
410 | - public function secondFactorCRTExponentParameter(): Parameter\SecondFactorCRTExponentParameter |
|
411 | - { |
|
412 | - return self::_checkType($this->get(Parameter\JWKParameter::P_DQ), |
|
413 | - Parameter\SecondFactorCRTExponentParameter::class); |
|
414 | - } |
|
415 | - |
|
416 | - /** |
|
417 | - * Check whether the second prime factor parameter is present. |
|
418 | - * |
|
419 | - * @return bool |
|
420 | - */ |
|
421 | - public function hasSecondPrimeFactorParameter(): bool |
|
422 | - { |
|
423 | - return $this->has(Parameter\JWKParameter::P_Q); |
|
424 | - } |
|
425 | - |
|
426 | - /** |
|
427 | - * Get the second prime factor parameter. |
|
428 | - * |
|
429 | - * @throws \UnexpectedValueException If the parameter has a wrong class |
|
430 | - * @throws \LogicException If the parameter is not present |
|
431 | - * |
|
432 | - * @return Parameter\SecondPrimeFactorParameter |
|
433 | - */ |
|
434 | - public function secondPrimeFactorParameter(): Parameter\SecondPrimeFactorParameter |
|
435 | - { |
|
436 | - return self::_checkType($this->get(Parameter\JWKParameter::P_Q), |
|
437 | - Parameter\SecondPrimeFactorParameter::class); |
|
438 | - } |
|
439 | - |
|
440 | - /** |
|
441 | - * Check whether the X.509 certificate chain parameter is present. |
|
442 | - * |
|
443 | - * @return bool |
|
444 | - */ |
|
445 | - public function hasX509CertificateChainParameter(): bool |
|
446 | - { |
|
447 | - return $this->has(Parameter\JWKParameter::P_X5C); |
|
448 | - } |
|
449 | - |
|
450 | - /** |
|
451 | - * Get the X.509 certificate chain parameter. |
|
452 | - * |
|
453 | - * @throws \UnexpectedValueException If the parameter has a wrong class |
|
454 | - * @throws \LogicException If the parameter is not present |
|
455 | - * |
|
456 | - * @return Parameter\X509CertificateChainParameter |
|
457 | - */ |
|
458 | - public function X509CertificateChainParameter(): Parameter\X509CertificateChainParameter |
|
459 | - { |
|
460 | - return self::_checkType($this->get(Parameter\JWKParameter::P_X5C), |
|
461 | - Parameter\X509CertificateChainParameter::class); |
|
462 | - } |
|
463 | - |
|
464 | - /** |
|
465 | - * Check whether the X.509 certificate SHA-1 thumbprint parameter is |
|
466 | - * present. |
|
467 | - * |
|
468 | - * @return bool |
|
469 | - */ |
|
470 | - public function hasX509CertificateSHA1ThumbprintParameter(): bool |
|
471 | - { |
|
472 | - return $this->has(Parameter\JWKParameter::P_X5T); |
|
473 | - } |
|
474 | - |
|
475 | - /** |
|
476 | - * Get the X.509 certificate SHA-1 thumbprint parameter. |
|
477 | - * |
|
478 | - * @throws \UnexpectedValueException If the parameter has a wrong class |
|
479 | - * @throws \LogicException If the parameter is not present |
|
480 | - * |
|
481 | - * @return Parameter\X509CertificateSHA1ThumbprintParameter |
|
482 | - */ |
|
483 | - public function X509CertificateSHA1ThumbprintParameter(): Parameter\X509CertificateSHA1ThumbprintParameter |
|
484 | - { |
|
485 | - return self::_checkType($this->get(Parameter\JWKParameter::P_X5T), |
|
486 | - Parameter\X509CertificateSHA1ThumbprintParameter::class); |
|
487 | - } |
|
488 | - |
|
489 | - /** |
|
490 | - * Check whether the X.509 certificate SHA-256 thumbprint parameter is |
|
491 | - * present. |
|
492 | - * |
|
493 | - * @return bool |
|
494 | - */ |
|
495 | - public function hasX509CertificateSHA256ThumbprintParameter(): bool |
|
496 | - { |
|
497 | - return $this->has(Parameter\JWKParameter::P_X5TS256); |
|
498 | - } |
|
499 | - |
|
500 | - /** |
|
501 | - * Get the X.509 certificate SHA-256 thumbprint parameter. |
|
502 | - * |
|
503 | - * @throws \UnexpectedValueException If the parameter has a wrong class |
|
504 | - * @throws \LogicException If the parameter is not present |
|
505 | - * |
|
506 | - * @return Parameter\X509CertificateSHA256ThumbprintParameter |
|
507 | - */ |
|
508 | - public function X509CertificateSHA256ThumbprintParameter(): Parameter\X509CertificateSHA256ThumbprintParameter |
|
509 | - { |
|
510 | - return self::_checkType($this->get(Parameter\JWKParameter::P_X5TS256), |
|
511 | - Parameter\X509CertificateSHA256ThumbprintParameter::class); |
|
512 | - } |
|
513 | - |
|
514 | - /** |
|
515 | - * Check whether the X.509 URL parameter is present. |
|
516 | - * |
|
517 | - * @return bool |
|
518 | - */ |
|
519 | - public function hasX509URLParameter(): bool |
|
520 | - { |
|
521 | - return $this->has(Parameter\JWKParameter::P_X5U); |
|
522 | - } |
|
523 | - |
|
524 | - /** |
|
525 | - * Get the X.509 URL parameter. |
|
526 | - * |
|
527 | - * @throws \UnexpectedValueException If the parameter has a wrong class |
|
528 | - * @throws \LogicException If the parameter is not present |
|
529 | - * |
|
530 | - * @return Parameter\X509URLParameter |
|
531 | - */ |
|
532 | - public function X509URLParameter(): Parameter\X509URLParameter |
|
533 | - { |
|
534 | - return self::_checkType($this->get(Parameter\JWKParameter::P_X5U), |
|
535 | - Parameter\X509URLParameter::class); |
|
536 | - } |
|
537 | - |
|
538 | - /** |
|
539 | - * Check whether the X coordinate parameter is present. |
|
540 | - * |
|
541 | - * @return bool |
|
542 | - */ |
|
543 | - public function hasXCoordinateParameter(): bool |
|
544 | - { |
|
545 | - return $this->has(Parameter\JWKParameter::P_X); |
|
546 | - } |
|
547 | - |
|
548 | - /** |
|
549 | - * Get the X coordinate parameter. |
|
550 | - * |
|
551 | - * @throws \UnexpectedValueException If the parameter has a wrong class |
|
552 | - * @throws \LogicException If the parameter is not present |
|
553 | - * |
|
554 | - * @return Parameter\XCoordinateParameter |
|
555 | - */ |
|
556 | - public function XCoordinateParameter(): Parameter\XCoordinateParameter |
|
557 | - { |
|
558 | - return self::_checkType($this->get(Parameter\JWKParameter::P_X), |
|
559 | - Parameter\XCoordinateParameter::class); |
|
560 | - } |
|
561 | - |
|
562 | - /** |
|
563 | - * Check whether the Y coordinate parameter is present. |
|
564 | - * |
|
565 | - * @return bool |
|
566 | - */ |
|
567 | - public function hasYCoordinateParameter(): bool |
|
568 | - { |
|
569 | - return $this->has(Parameter\JWKParameter::P_Y); |
|
570 | - } |
|
571 | - |
|
572 | - /** |
|
573 | - * Get the Y coordinate parameter. |
|
574 | - * |
|
575 | - * @throws \UnexpectedValueException If the parameter has a wrong class |
|
576 | - * @throws \LogicException If the parameter is not present |
|
577 | - * |
|
578 | - * @return Parameter\YCoordinateParameter |
|
579 | - */ |
|
580 | - public function YCoordinateParameter(): Parameter\YCoordinateParameter |
|
581 | - { |
|
582 | - return self::_checkType($this->get(Parameter\JWKParameter::P_Y), |
|
583 | - Parameter\YCoordinateParameter::class); |
|
584 | - } |
|
585 | - |
|
586 | - /** |
|
587 | - * Check that the parameter is an instance of the given class. |
|
588 | - * |
|
589 | - * @param Parameter\JWKParameter $param Parameter |
|
590 | - * @param string $cls Class name |
|
591 | - * |
|
592 | - * @throws \UnexpectedValueException |
|
593 | - * |
|
594 | - * @return Parameter\JWKParameter |
|
595 | - */ |
|
596 | - private static function _checkType(Parameter\JWKParameter $param, string $cls): Parameter\JWKParameter |
|
597 | - { |
|
598 | - if (!$param instanceof $cls) { |
|
599 | - throw new \UnexpectedValueException( |
|
600 | - "{$cls} expected, got " . get_class($param)); |
|
601 | - } |
|
602 | - return $param; |
|
603 | - } |
|
14 | + /** |
|
15 | + * Whether parameters are present. |
|
16 | + * |
|
17 | + * @param string ...$names Parameter names |
|
18 | + * |
|
19 | + * @return bool |
|
20 | + */ |
|
21 | + abstract public function has(string ...$names): bool; |
|
22 | + |
|
23 | + /** |
|
24 | + * Get a parameter. |
|
25 | + * |
|
26 | + * @param string $name Parameter name |
|
27 | + * |
|
28 | + * @return JWKParameter |
|
29 | + */ |
|
30 | + abstract public function get(string $name): JWKParameter; |
|
31 | + |
|
32 | + /** |
|
33 | + * Check whether the algorithm parameter is present. |
|
34 | + * |
|
35 | + * @return bool |
|
36 | + */ |
|
37 | + public function hasAlgorithmParameter(): bool |
|
38 | + { |
|
39 | + return $this->has(Parameter\JWKParameter::P_ALG); |
|
40 | + } |
|
41 | + |
|
42 | + /** |
|
43 | + * Get the algorithm parameter. |
|
44 | + * |
|
45 | + * @throws \UnexpectedValueException If the parameter has a wrong class |
|
46 | + * @throws \LogicException If the parameter is not present |
|
47 | + * |
|
48 | + * @return Parameter\AlgorithmParameter |
|
49 | + */ |
|
50 | + public function algorithmParameter(): Parameter\AlgorithmParameter |
|
51 | + { |
|
52 | + return self::_checkType($this->get(Parameter\JWKParameter::P_ALG), |
|
53 | + Parameter\AlgorithmParameter::class); |
|
54 | + } |
|
55 | + |
|
56 | + /** |
|
57 | + * Check whether the curve parameter is present. |
|
58 | + * |
|
59 | + * @return bool |
|
60 | + */ |
|
61 | + public function hasCurveParameter(): bool |
|
62 | + { |
|
63 | + return $this->has(Parameter\JWKParameter::P_CRV); |
|
64 | + } |
|
65 | + |
|
66 | + /** |
|
67 | + * Get the curve parameter. |
|
68 | + * |
|
69 | + * @throws \UnexpectedValueException If the parameter has a wrong class |
|
70 | + * @throws \LogicException If the parameter is not present |
|
71 | + * |
|
72 | + * @return Parameter\CurveParameter |
|
73 | + */ |
|
74 | + public function curveParameter(): Parameter\CurveParameter |
|
75 | + { |
|
76 | + return self::_checkType($this->get(Parameter\JWKParameter::P_CRV), |
|
77 | + Parameter\CurveParameter::class); |
|
78 | + } |
|
79 | + |
|
80 | + /** |
|
81 | + * Check whether the ECC private key parameter is present. |
|
82 | + * |
|
83 | + * @return bool |
|
84 | + */ |
|
85 | + public function hasECCPrivateKeyParameter(): bool |
|
86 | + { |
|
87 | + return $this->has(Parameter\JWKParameter::P_ECC_D); |
|
88 | + } |
|
89 | + |
|
90 | + /** |
|
91 | + * Get the ECC private key parameter. |
|
92 | + * |
|
93 | + * @throws \UnexpectedValueException If the parameter has a wrong class |
|
94 | + * @throws \LogicException If the parameter is not present |
|
95 | + * |
|
96 | + * @return Parameter\ECCPrivateKeyParameter |
|
97 | + */ |
|
98 | + public function ECCPrivateKeyParameter(): Parameter\ECCPrivateKeyParameter |
|
99 | + { |
|
100 | + return self::_checkType($this->get(Parameter\JWKParameter::P_ECC_D), |
|
101 | + Parameter\ECCPrivateKeyParameter::class); |
|
102 | + } |
|
103 | + |
|
104 | + /** |
|
105 | + * Check whether the exponent parameter is present. |
|
106 | + * |
|
107 | + * @return bool |
|
108 | + */ |
|
109 | + public function hasExponentParameter(): bool |
|
110 | + { |
|
111 | + return $this->has(Parameter\JWKParameter::P_E); |
|
112 | + } |
|
113 | + |
|
114 | + /** |
|
115 | + * Get the exponent parameter. |
|
116 | + * |
|
117 | + * @throws \UnexpectedValueException If the parameter has a wrong class |
|
118 | + * @throws \LogicException If the parameter is not present |
|
119 | + * |
|
120 | + * @return Parameter\ExponentParameter |
|
121 | + */ |
|
122 | + public function exponentParameter(): Parameter\ExponentParameter |
|
123 | + { |
|
124 | + return self::_checkType($this->get(Parameter\JWKParameter::P_E), |
|
125 | + Parameter\ExponentParameter::class); |
|
126 | + } |
|
127 | + |
|
128 | + /** |
|
129 | + * Check whether the first CRT coefficient parameter is present. |
|
130 | + * |
|
131 | + * @return bool |
|
132 | + */ |
|
133 | + public function hasFirstCRTCoefficientParameter(): bool |
|
134 | + { |
|
135 | + return $this->has(Parameter\JWKParameter::P_QI); |
|
136 | + } |
|
137 | + |
|
138 | + /** |
|
139 | + * Get the first CRT coefficient parameter. |
|
140 | + * |
|
141 | + * @throws \UnexpectedValueException If the parameter has a wrong class |
|
142 | + * @throws \LogicException If the parameter is not present |
|
143 | + * |
|
144 | + * @return Parameter\FirstCRTCoefficientParameter |
|
145 | + */ |
|
146 | + public function firstCRTCoefficientParameter(): Parameter\FirstCRTCoefficientParameter |
|
147 | + { |
|
148 | + return self::_checkType($this->get(Parameter\JWKParameter::P_QI), |
|
149 | + Parameter\FirstCRTCoefficientParameter::class); |
|
150 | + } |
|
151 | + |
|
152 | + /** |
|
153 | + * Check whether the first factor CRT exponent parameter is present. |
|
154 | + * |
|
155 | + * @return bool |
|
156 | + */ |
|
157 | + public function hasFirstFactorCRTExponentParameter(): bool |
|
158 | + { |
|
159 | + return $this->has(Parameter\JWKParameter::P_DP); |
|
160 | + } |
|
161 | + |
|
162 | + /** |
|
163 | + * Get the first factor CRT exponent parameter. |
|
164 | + * |
|
165 | + * @throws \UnexpectedValueException If the parameter has a wrong class |
|
166 | + * @throws \LogicException If the parameter is not present |
|
167 | + * |
|
168 | + * @return Parameter\FirstFactorCRTExponentParameter |
|
169 | + */ |
|
170 | + public function firstFactorCRTExponentParameter(): Parameter\FirstFactorCRTExponentParameter |
|
171 | + { |
|
172 | + return self::_checkType($this->get(Parameter\JWKParameter::P_DP), |
|
173 | + Parameter\FirstFactorCRTExponentParameter::class); |
|
174 | + } |
|
175 | + |
|
176 | + /** |
|
177 | + * Check whether the first prime factor parameter is present. |
|
178 | + * |
|
179 | + * @return bool |
|
180 | + */ |
|
181 | + public function hasFirstPrimeFactorParameter(): bool |
|
182 | + { |
|
183 | + return $this->has(Parameter\JWKParameter::P_P); |
|
184 | + } |
|
185 | + |
|
186 | + /** |
|
187 | + * Get the first prime factor parameter. |
|
188 | + * |
|
189 | + * @throws \UnexpectedValueException If the parameter has a wrong class |
|
190 | + * @throws \LogicException If the parameter is not present |
|
191 | + * |
|
192 | + * @return Parameter\FirstPrimeFactorParameter |
|
193 | + */ |
|
194 | + public function firstPrimeFactorParameter(): Parameter\FirstPrimeFactorParameter |
|
195 | + { |
|
196 | + return self::_checkType($this->get(Parameter\JWKParameter::P_P), |
|
197 | + Parameter\FirstPrimeFactorParameter::class); |
|
198 | + } |
|
199 | + |
|
200 | + /** |
|
201 | + * Check whether the key ID parameter is present. |
|
202 | + * |
|
203 | + * @return bool |
|
204 | + */ |
|
205 | + public function hasKeyIDParameter(): bool |
|
206 | + { |
|
207 | + return $this->has(Parameter\JWKParameter::P_KID); |
|
208 | + } |
|
209 | + |
|
210 | + /** |
|
211 | + * Get the key ID parameter. |
|
212 | + * |
|
213 | + * @throws \UnexpectedValueException If the parameter has a wrong class |
|
214 | + * @throws \LogicException If the parameter is not present |
|
215 | + * |
|
216 | + * @return Parameter\KeyIDParameter |
|
217 | + */ |
|
218 | + public function keyIDParameter(): Parameter\KeyIDParameter |
|
219 | + { |
|
220 | + return self::_checkType($this->get(Parameter\JWKParameter::P_KID), |
|
221 | + Parameter\KeyIDParameter::class); |
|
222 | + } |
|
223 | + |
|
224 | + /** |
|
225 | + * Check whether the key operations parameter is present. |
|
226 | + * |
|
227 | + * @return bool |
|
228 | + */ |
|
229 | + public function hasKeyOperationsParameter(): bool |
|
230 | + { |
|
231 | + return $this->has(Parameter\JWKParameter::P_KEY_OPS); |
|
232 | + } |
|
233 | + |
|
234 | + /** |
|
235 | + * Get the key operations parameter. |
|
236 | + * |
|
237 | + * @throws \UnexpectedValueException If the parameter has a wrong class |
|
238 | + * @throws \LogicException If the parameter is not present |
|
239 | + * |
|
240 | + * @return Parameter\KeyOperationsParameter |
|
241 | + */ |
|
242 | + public function keyOperationsParameter(): Parameter\KeyOperationsParameter |
|
243 | + { |
|
244 | + return self::_checkType($this->get(Parameter\JWKParameter::P_KEY_OPS), |
|
245 | + Parameter\KeyOperationsParameter::class); |
|
246 | + } |
|
247 | + |
|
248 | + /** |
|
249 | + * Check whether the key type parameter is present. |
|
250 | + * |
|
251 | + * @return bool |
|
252 | + */ |
|
253 | + public function hasKeyTypeParameter(): bool |
|
254 | + { |
|
255 | + return $this->has(Parameter\JWKParameter::P_KTY); |
|
256 | + } |
|
257 | + |
|
258 | + /** |
|
259 | + * Get the key type parameter. |
|
260 | + * |
|
261 | + * @throws \UnexpectedValueException If the parameter has a wrong class |
|
262 | + * @throws \LogicException If the parameter is not present |
|
263 | + * |
|
264 | + * @return Parameter\KeyTypeParameter |
|
265 | + */ |
|
266 | + public function keyTypeParameter(): Parameter\KeyTypeParameter |
|
267 | + { |
|
268 | + return self::_checkType($this->get(Parameter\JWKParameter::P_KTY), |
|
269 | + Parameter\KeyTypeParameter::class); |
|
270 | + } |
|
271 | + |
|
272 | + /** |
|
273 | + * Check whether the key value parameter is present. |
|
274 | + * |
|
275 | + * @return bool |
|
276 | + */ |
|
277 | + public function hasKeyValueParameter(): bool |
|
278 | + { |
|
279 | + return $this->has(Parameter\JWKParameter::P_K); |
|
280 | + } |
|
281 | + |
|
282 | + /** |
|
283 | + * Get the key value parameter. |
|
284 | + * |
|
285 | + * @throws \UnexpectedValueException If the parameter has a wrong class |
|
286 | + * @throws \LogicException If the parameter is not present |
|
287 | + * |
|
288 | + * @return Parameter\KeyValueParameter |
|
289 | + */ |
|
290 | + public function keyValueParameter(): Parameter\KeyValueParameter |
|
291 | + { |
|
292 | + return self::_checkType($this->get(Parameter\JWKParameter::P_K), |
|
293 | + Parameter\KeyValueParameter::class); |
|
294 | + } |
|
295 | + |
|
296 | + /** |
|
297 | + * Check whether the modulus parameter is present. |
|
298 | + * |
|
299 | + * @return bool |
|
300 | + */ |
|
301 | + public function hasModulusParameter(): bool |
|
302 | + { |
|
303 | + return $this->has(Parameter\JWKParameter::P_N); |
|
304 | + } |
|
305 | + |
|
306 | + /** |
|
307 | + * Get the modulus parameter. |
|
308 | + * |
|
309 | + * @throws \UnexpectedValueException If the parameter has a wrong class |
|
310 | + * @throws \LogicException If the parameter is not present |
|
311 | + * |
|
312 | + * @return Parameter\ModulusParameter |
|
313 | + */ |
|
314 | + public function modulusParameter(): Parameter\ModulusParameter |
|
315 | + { |
|
316 | + return self::_checkType($this->get(Parameter\JWKParameter::P_N), |
|
317 | + Parameter\ModulusParameter::class); |
|
318 | + } |
|
319 | + |
|
320 | + /** |
|
321 | + * Check whether the other primes info parameter is present. |
|
322 | + * |
|
323 | + * @return bool |
|
324 | + */ |
|
325 | + public function hasOtherPrimesInfoParameter(): bool |
|
326 | + { |
|
327 | + return $this->has(Parameter\JWKParameter::P_OTH); |
|
328 | + } |
|
329 | + |
|
330 | + /** |
|
331 | + * Get the other primes info parameter. |
|
332 | + * |
|
333 | + * @throws \UnexpectedValueException If the parameter has a wrong class |
|
334 | + * @throws \LogicException If the parameter is not present |
|
335 | + * |
|
336 | + * @return Parameter\OtherPrimesInfoParameter |
|
337 | + */ |
|
338 | + public function otherPrimesInfoParameter(): Parameter\OtherPrimesInfoParameter |
|
339 | + { |
|
340 | + return self::_checkType($this->get(Parameter\JWKParameter::P_OTH), |
|
341 | + Parameter\OtherPrimesInfoParameter::class); |
|
342 | + } |
|
343 | + |
|
344 | + /** |
|
345 | + * Check whether the private exponent parameter is present. |
|
346 | + * |
|
347 | + * @return bool |
|
348 | + */ |
|
349 | + public function hasPrivateExponentParameter(): bool |
|
350 | + { |
|
351 | + return $this->has(Parameter\JWKParameter::P_RSA_D); |
|
352 | + } |
|
353 | + |
|
354 | + /** |
|
355 | + * Get the private exponent parameter. |
|
356 | + * |
|
357 | + * @throws \UnexpectedValueException If the parameter has a wrong class |
|
358 | + * @throws \LogicException If the parameter is not present |
|
359 | + * |
|
360 | + * @return Parameter\PrivateExponentParameter |
|
361 | + */ |
|
362 | + public function privateExponentParameter(): Parameter\PrivateExponentParameter |
|
363 | + { |
|
364 | + return self::_checkType($this->get(Parameter\JWKParameter::P_RSA_D), |
|
365 | + Parameter\PrivateExponentParameter::class); |
|
366 | + } |
|
367 | + |
|
368 | + /** |
|
369 | + * Check whether the public key use parameter is present. |
|
370 | + * |
|
371 | + * @return bool |
|
372 | + */ |
|
373 | + public function hasPublicKeyUseParameter(): bool |
|
374 | + { |
|
375 | + return $this->has(Parameter\JWKParameter::P_USE); |
|
376 | + } |
|
377 | + |
|
378 | + /** |
|
379 | + * Get the public key use parameter. |
|
380 | + * |
|
381 | + * @throws \UnexpectedValueException If the parameter has a wrong class |
|
382 | + * @throws \LogicException If the parameter is not present |
|
383 | + * |
|
384 | + * @return Parameter\PublicKeyUseParameter |
|
385 | + */ |
|
386 | + public function publicKeyUseParameter(): Parameter\PublicKeyUseParameter |
|
387 | + { |
|
388 | + return self::_checkType($this->get(Parameter\JWKParameter::P_USE), |
|
389 | + Parameter\PublicKeyUseParameter::class); |
|
390 | + } |
|
391 | + |
|
392 | + /** |
|
393 | + * Check whether the second factor CRT exponent parameter is present. |
|
394 | + * |
|
395 | + * @return bool |
|
396 | + */ |
|
397 | + public function hasSecondFactorCRTExponentParameter(): bool |
|
398 | + { |
|
399 | + return $this->has(Parameter\JWKParameter::P_DQ); |
|
400 | + } |
|
401 | + |
|
402 | + /** |
|
403 | + * Get the second factor CRT exponent parameter. |
|
404 | + * |
|
405 | + * @throws \UnexpectedValueException If the parameter has a wrong class |
|
406 | + * @throws \LogicException If the parameter is not present |
|
407 | + * |
|
408 | + * @return Parameter\SecondFactorCRTExponentParameter |
|
409 | + */ |
|
410 | + public function secondFactorCRTExponentParameter(): Parameter\SecondFactorCRTExponentParameter |
|
411 | + { |
|
412 | + return self::_checkType($this->get(Parameter\JWKParameter::P_DQ), |
|
413 | + Parameter\SecondFactorCRTExponentParameter::class); |
|
414 | + } |
|
415 | + |
|
416 | + /** |
|
417 | + * Check whether the second prime factor parameter is present. |
|
418 | + * |
|
419 | + * @return bool |
|
420 | + */ |
|
421 | + public function hasSecondPrimeFactorParameter(): bool |
|
422 | + { |
|
423 | + return $this->has(Parameter\JWKParameter::P_Q); |
|
424 | + } |
|
425 | + |
|
426 | + /** |
|
427 | + * Get the second prime factor parameter. |
|
428 | + * |
|
429 | + * @throws \UnexpectedValueException If the parameter has a wrong class |
|
430 | + * @throws \LogicException If the parameter is not present |
|
431 | + * |
|
432 | + * @return Parameter\SecondPrimeFactorParameter |
|
433 | + */ |
|
434 | + public function secondPrimeFactorParameter(): Parameter\SecondPrimeFactorParameter |
|
435 | + { |
|
436 | + return self::_checkType($this->get(Parameter\JWKParameter::P_Q), |
|
437 | + Parameter\SecondPrimeFactorParameter::class); |
|
438 | + } |
|
439 | + |
|
440 | + /** |
|
441 | + * Check whether the X.509 certificate chain parameter is present. |
|
442 | + * |
|
443 | + * @return bool |
|
444 | + */ |
|
445 | + public function hasX509CertificateChainParameter(): bool |
|
446 | + { |
|
447 | + return $this->has(Parameter\JWKParameter::P_X5C); |
|
448 | + } |
|
449 | + |
|
450 | + /** |
|
451 | + * Get the X.509 certificate chain parameter. |
|
452 | + * |
|
453 | + * @throws \UnexpectedValueException If the parameter has a wrong class |
|
454 | + * @throws \LogicException If the parameter is not present |
|
455 | + * |
|
456 | + * @return Parameter\X509CertificateChainParameter |
|
457 | + */ |
|
458 | + public function X509CertificateChainParameter(): Parameter\X509CertificateChainParameter |
|
459 | + { |
|
460 | + return self::_checkType($this->get(Parameter\JWKParameter::P_X5C), |
|
461 | + Parameter\X509CertificateChainParameter::class); |
|
462 | + } |
|
463 | + |
|
464 | + /** |
|
465 | + * Check whether the X.509 certificate SHA-1 thumbprint parameter is |
|
466 | + * present. |
|
467 | + * |
|
468 | + * @return bool |
|
469 | + */ |
|
470 | + public function hasX509CertificateSHA1ThumbprintParameter(): bool |
|
471 | + { |
|
472 | + return $this->has(Parameter\JWKParameter::P_X5T); |
|
473 | + } |
|
474 | + |
|
475 | + /** |
|
476 | + * Get the X.509 certificate SHA-1 thumbprint parameter. |
|
477 | + * |
|
478 | + * @throws \UnexpectedValueException If the parameter has a wrong class |
|
479 | + * @throws \LogicException If the parameter is not present |
|
480 | + * |
|
481 | + * @return Parameter\X509CertificateSHA1ThumbprintParameter |
|
482 | + */ |
|
483 | + public function X509CertificateSHA1ThumbprintParameter(): Parameter\X509CertificateSHA1ThumbprintParameter |
|
484 | + { |
|
485 | + return self::_checkType($this->get(Parameter\JWKParameter::P_X5T), |
|
486 | + Parameter\X509CertificateSHA1ThumbprintParameter::class); |
|
487 | + } |
|
488 | + |
|
489 | + /** |
|
490 | + * Check whether the X.509 certificate SHA-256 thumbprint parameter is |
|
491 | + * present. |
|
492 | + * |
|
493 | + * @return bool |
|
494 | + */ |
|
495 | + public function hasX509CertificateSHA256ThumbprintParameter(): bool |
|
496 | + { |
|
497 | + return $this->has(Parameter\JWKParameter::P_X5TS256); |
|
498 | + } |
|
499 | + |
|
500 | + /** |
|
501 | + * Get the X.509 certificate SHA-256 thumbprint parameter. |
|
502 | + * |
|
503 | + * @throws \UnexpectedValueException If the parameter has a wrong class |
|
504 | + * @throws \LogicException If the parameter is not present |
|
505 | + * |
|
506 | + * @return Parameter\X509CertificateSHA256ThumbprintParameter |
|
507 | + */ |
|
508 | + public function X509CertificateSHA256ThumbprintParameter(): Parameter\X509CertificateSHA256ThumbprintParameter |
|
509 | + { |
|
510 | + return self::_checkType($this->get(Parameter\JWKParameter::P_X5TS256), |
|
511 | + Parameter\X509CertificateSHA256ThumbprintParameter::class); |
|
512 | + } |
|
513 | + |
|
514 | + /** |
|
515 | + * Check whether the X.509 URL parameter is present. |
|
516 | + * |
|
517 | + * @return bool |
|
518 | + */ |
|
519 | + public function hasX509URLParameter(): bool |
|
520 | + { |
|
521 | + return $this->has(Parameter\JWKParameter::P_X5U); |
|
522 | + } |
|
523 | + |
|
524 | + /** |
|
525 | + * Get the X.509 URL parameter. |
|
526 | + * |
|
527 | + * @throws \UnexpectedValueException If the parameter has a wrong class |
|
528 | + * @throws \LogicException If the parameter is not present |
|
529 | + * |
|
530 | + * @return Parameter\X509URLParameter |
|
531 | + */ |
|
532 | + public function X509URLParameter(): Parameter\X509URLParameter |
|
533 | + { |
|
534 | + return self::_checkType($this->get(Parameter\JWKParameter::P_X5U), |
|
535 | + Parameter\X509URLParameter::class); |
|
536 | + } |
|
537 | + |
|
538 | + /** |
|
539 | + * Check whether the X coordinate parameter is present. |
|
540 | + * |
|
541 | + * @return bool |
|
542 | + */ |
|
543 | + public function hasXCoordinateParameter(): bool |
|
544 | + { |
|
545 | + return $this->has(Parameter\JWKParameter::P_X); |
|
546 | + } |
|
547 | + |
|
548 | + /** |
|
549 | + * Get the X coordinate parameter. |
|
550 | + * |
|
551 | + * @throws \UnexpectedValueException If the parameter has a wrong class |
|
552 | + * @throws \LogicException If the parameter is not present |
|
553 | + * |
|
554 | + * @return Parameter\XCoordinateParameter |
|
555 | + */ |
|
556 | + public function XCoordinateParameter(): Parameter\XCoordinateParameter |
|
557 | + { |
|
558 | + return self::_checkType($this->get(Parameter\JWKParameter::P_X), |
|
559 | + Parameter\XCoordinateParameter::class); |
|
560 | + } |
|
561 | + |
|
562 | + /** |
|
563 | + * Check whether the Y coordinate parameter is present. |
|
564 | + * |
|
565 | + * @return bool |
|
566 | + */ |
|
567 | + public function hasYCoordinateParameter(): bool |
|
568 | + { |
|
569 | + return $this->has(Parameter\JWKParameter::P_Y); |
|
570 | + } |
|
571 | + |
|
572 | + /** |
|
573 | + * Get the Y coordinate parameter. |
|
574 | + * |
|
575 | + * @throws \UnexpectedValueException If the parameter has a wrong class |
|
576 | + * @throws \LogicException If the parameter is not present |
|
577 | + * |
|
578 | + * @return Parameter\YCoordinateParameter |
|
579 | + */ |
|
580 | + public function YCoordinateParameter(): Parameter\YCoordinateParameter |
|
581 | + { |
|
582 | + return self::_checkType($this->get(Parameter\JWKParameter::P_Y), |
|
583 | + Parameter\YCoordinateParameter::class); |
|
584 | + } |
|
585 | + |
|
586 | + /** |
|
587 | + * Check that the parameter is an instance of the given class. |
|
588 | + * |
|
589 | + * @param Parameter\JWKParameter $param Parameter |
|
590 | + * @param string $cls Class name |
|
591 | + * |
|
592 | + * @throws \UnexpectedValueException |
|
593 | + * |
|
594 | + * @return Parameter\JWKParameter |
|
595 | + */ |
|
596 | + private static function _checkType(Parameter\JWKParameter $param, string $cls): Parameter\JWKParameter |
|
597 | + { |
|
598 | + if (!$param instanceof $cls) { |
|
599 | + throw new \UnexpectedValueException( |
|
600 | + "{$cls} expected, got " . get_class($param)); |
|
601 | + } |
|
602 | + return $param; |
|
603 | + } |
|
604 | 604 | } |
@@ -1,6 +1,6 @@ |
||
1 | 1 | <?php |
2 | 2 | |
3 | -declare(strict_types = 1); |
|
3 | +declare(strict_types=1); |
|
4 | 4 | |
5 | 5 | namespace Sop\JWX\JWK; |
6 | 6 |
@@ -14,197 +14,197 @@ |
||
14 | 14 | */ |
15 | 15 | class JWK implements \Countable, \IteratorAggregate |
16 | 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 | - public function __construct(JWKParameter ...$params) |
|
32 | - { |
|
33 | - $this->_parameters = []; |
|
34 | - foreach ($params as $param) { |
|
35 | - $this->_parameters[$param->name()] = $param; |
|
36 | - } |
|
37 | - } |
|
38 | - |
|
39 | - /** |
|
40 | - * Initialize from an array representing a JSON object. |
|
41 | - * |
|
42 | - * @param array $members |
|
43 | - * |
|
44 | - * @return self |
|
45 | - */ |
|
46 | - public static function fromArray(array $members): self |
|
47 | - { |
|
48 | - $params = []; |
|
49 | - foreach ($members as $name => $value) { |
|
50 | - $params[] = JWKParameter::fromNameAndValue($name, $value); |
|
51 | - } |
|
52 | - 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 | - public static function fromJSON(string $json): self |
|
65 | - { |
|
66 | - $members = json_decode($json, true, 32, JSON_BIGINT_AS_STRING); |
|
67 | - if (!is_array($members)) { |
|
68 | - throw new \UnexpectedValueException('Invalid JSON.'); |
|
69 | - } |
|
70 | - 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 | - public static function fromJWK(JWK $jwk): self |
|
83 | - { |
|
84 | - 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 | - public function withParameters(JWKParameter ...$params): self |
|
95 | - { |
|
96 | - $obj = clone $this; |
|
97 | - foreach ($params as $param) { |
|
98 | - $obj->_parameters[$param->name()] = $param; |
|
99 | - } |
|
100 | - return $obj; |
|
101 | - } |
|
102 | - |
|
103 | - /** |
|
104 | - * Get all parameters. |
|
105 | - * |
|
106 | - * @return JWKParameter[] |
|
107 | - */ |
|
108 | - public function parameters(): array |
|
109 | - { |
|
110 | - 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 | - public function withKeyID(string $id): self |
|
121 | - { |
|
122 | - 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 | - public function has(string ...$names): bool |
|
135 | - { |
|
136 | - foreach ($names as $name) { |
|
137 | - if (!isset($this->_parameters[$name])) { |
|
138 | - return false; |
|
139 | - } |
|
140 | - } |
|
141 | - 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 | - public function get(string $name): JWKParameter |
|
154 | - { |
|
155 | - if (!$this->has($name)) { |
|
156 | - throw new \LogicException("Parameter {$name} doesn't exists."); |
|
157 | - } |
|
158 | - return $this->_parameters[$name]; |
|
159 | - } |
|
160 | - |
|
161 | - /** |
|
162 | - * Convert to array. |
|
163 | - * |
|
164 | - * @return array Parameter values keyed by parameter names |
|
165 | - */ |
|
166 | - public function toArray(): array |
|
167 | - { |
|
168 | - $a = []; |
|
169 | - foreach ($this->_parameters as $param) { |
|
170 | - $a[$param->name()] = $param->value(); |
|
171 | - } |
|
172 | - return $a; |
|
173 | - } |
|
174 | - |
|
175 | - /** |
|
176 | - * Convert to JSON. |
|
177 | - * |
|
178 | - * @return string |
|
179 | - */ |
|
180 | - public function toJSON(): string |
|
181 | - { |
|
182 | - $data = $this->toArray(); |
|
183 | - if (empty($data)) { |
|
184 | - return ''; |
|
185 | - } |
|
186 | - return json_encode((object) $data, JSON_UNESCAPED_SLASHES); |
|
187 | - } |
|
188 | - |
|
189 | - /** |
|
190 | - * Get the number of parameters. |
|
191 | - * |
|
192 | - * @see \Countable::count() |
|
193 | - */ |
|
194 | - public function count(): int |
|
195 | - { |
|
196 | - return count($this->_parameters); |
|
197 | - } |
|
198 | - |
|
199 | - /** |
|
200 | - * Get iterator for the parameters. |
|
201 | - * |
|
202 | - * @see \IteratorAggregate::getIterator() |
|
203 | - * |
|
204 | - * @return \ArrayIterator |
|
205 | - */ |
|
206 | - public function getIterator(): \ArrayIterator |
|
207 | - { |
|
208 | - return new \ArrayIterator($this->_parameters); |
|
209 | - } |
|
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 | + public function __construct(JWKParameter ...$params) |
|
32 | + { |
|
33 | + $this->_parameters = []; |
|
34 | + foreach ($params as $param) { |
|
35 | + $this->_parameters[$param->name()] = $param; |
|
36 | + } |
|
37 | + } |
|
38 | + |
|
39 | + /** |
|
40 | + * Initialize from an array representing a JSON object. |
|
41 | + * |
|
42 | + * @param array $members |
|
43 | + * |
|
44 | + * @return self |
|
45 | + */ |
|
46 | + public static function fromArray(array $members): self |
|
47 | + { |
|
48 | + $params = []; |
|
49 | + foreach ($members as $name => $value) { |
|
50 | + $params[] = JWKParameter::fromNameAndValue($name, $value); |
|
51 | + } |
|
52 | + 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 | + public static function fromJSON(string $json): self |
|
65 | + { |
|
66 | + $members = json_decode($json, true, 32, JSON_BIGINT_AS_STRING); |
|
67 | + if (!is_array($members)) { |
|
68 | + throw new \UnexpectedValueException('Invalid JSON.'); |
|
69 | + } |
|
70 | + 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 | + public static function fromJWK(JWK $jwk): self |
|
83 | + { |
|
84 | + 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 | + public function withParameters(JWKParameter ...$params): self |
|
95 | + { |
|
96 | + $obj = clone $this; |
|
97 | + foreach ($params as $param) { |
|
98 | + $obj->_parameters[$param->name()] = $param; |
|
99 | + } |
|
100 | + return $obj; |
|
101 | + } |
|
102 | + |
|
103 | + /** |
|
104 | + * Get all parameters. |
|
105 | + * |
|
106 | + * @return JWKParameter[] |
|
107 | + */ |
|
108 | + public function parameters(): array |
|
109 | + { |
|
110 | + 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 | + public function withKeyID(string $id): self |
|
121 | + { |
|
122 | + 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 | + public function has(string ...$names): bool |
|
135 | + { |
|
136 | + foreach ($names as $name) { |
|
137 | + if (!isset($this->_parameters[$name])) { |
|
138 | + return false; |
|
139 | + } |
|
140 | + } |
|
141 | + 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 | + public function get(string $name): JWKParameter |
|
154 | + { |
|
155 | + if (!$this->has($name)) { |
|
156 | + throw new \LogicException("Parameter {$name} doesn't exists."); |
|
157 | + } |
|
158 | + return $this->_parameters[$name]; |
|
159 | + } |
|
160 | + |
|
161 | + /** |
|
162 | + * Convert to array. |
|
163 | + * |
|
164 | + * @return array Parameter values keyed by parameter names |
|
165 | + */ |
|
166 | + public function toArray(): array |
|
167 | + { |
|
168 | + $a = []; |
|
169 | + foreach ($this->_parameters as $param) { |
|
170 | + $a[$param->name()] = $param->value(); |
|
171 | + } |
|
172 | + return $a; |
|
173 | + } |
|
174 | + |
|
175 | + /** |
|
176 | + * Convert to JSON. |
|
177 | + * |
|
178 | + * @return string |
|
179 | + */ |
|
180 | + public function toJSON(): string |
|
181 | + { |
|
182 | + $data = $this->toArray(); |
|
183 | + if (empty($data)) { |
|
184 | + return ''; |
|
185 | + } |
|
186 | + return json_encode((object) $data, JSON_UNESCAPED_SLASHES); |
|
187 | + } |
|
188 | + |
|
189 | + /** |
|
190 | + * Get the number of parameters. |
|
191 | + * |
|
192 | + * @see \Countable::count() |
|
193 | + */ |
|
194 | + public function count(): int |
|
195 | + { |
|
196 | + return count($this->_parameters); |
|
197 | + } |
|
198 | + |
|
199 | + /** |
|
200 | + * Get iterator for the parameters. |
|
201 | + * |
|
202 | + * @see \IteratorAggregate::getIterator() |
|
203 | + * |
|
204 | + * @return \ArrayIterator |
|
205 | + */ |
|
206 | + public function getIterator(): \ArrayIterator |
|
207 | + { |
|
208 | + return new \ArrayIterator($this->_parameters); |
|
209 | + } |
|
210 | 210 | } |
@@ -1,6 +1,6 @@ |
||
1 | 1 | <?php |
2 | 2 | |
3 | -declare(strict_types = 1); |
|
3 | +declare(strict_types=1); |
|
4 | 4 | |
5 | 5 | namespace Sop\JWX\JWK; |
6 | 6 |
@@ -18,42 +18,42 @@ |
||
18 | 18 | */ |
19 | 19 | abstract class PublicKeyJWK extends JWK |
20 | 20 | { |
21 | - /** |
|
22 | - * Convert public key to PEM. |
|
23 | - * |
|
24 | - * @return PEM |
|
25 | - */ |
|
26 | - abstract public function toPEM(): PEM; |
|
21 | + /** |
|
22 | + * Convert public key to PEM. |
|
23 | + * |
|
24 | + * @return PEM |
|
25 | + */ |
|
26 | + abstract public function toPEM(): PEM; |
|
27 | 27 | |
28 | - /** |
|
29 | - * Initialize from a PublicKey object. |
|
30 | - * |
|
31 | - * @param PublicKey $pub_key Public key |
|
32 | - * |
|
33 | - * @throws \UnexpectedValueException |
|
34 | - * |
|
35 | - * @return self |
|
36 | - */ |
|
37 | - public static function fromPublicKey(PublicKey $pub_key): PublicKeyJWK |
|
38 | - { |
|
39 | - if ($pub_key instanceof RSAPublicKey) { |
|
40 | - return RSAPublicKeyJWK::fromRSAPublicKey($pub_key); |
|
41 | - } |
|
42 | - if ($pub_key instanceof ECPublicKey) { |
|
43 | - return ECPublicKeyJWK::fromECPublicKey($pub_key); |
|
44 | - } |
|
45 | - throw new \UnexpectedValueException('Unsupported public key.'); |
|
46 | - } |
|
28 | + /** |
|
29 | + * Initialize from a PublicKey object. |
|
30 | + * |
|
31 | + * @param PublicKey $pub_key Public key |
|
32 | + * |
|
33 | + * @throws \UnexpectedValueException |
|
34 | + * |
|
35 | + * @return self |
|
36 | + */ |
|
37 | + public static function fromPublicKey(PublicKey $pub_key): PublicKeyJWK |
|
38 | + { |
|
39 | + if ($pub_key instanceof RSAPublicKey) { |
|
40 | + return RSAPublicKeyJWK::fromRSAPublicKey($pub_key); |
|
41 | + } |
|
42 | + if ($pub_key instanceof ECPublicKey) { |
|
43 | + return ECPublicKeyJWK::fromECPublicKey($pub_key); |
|
44 | + } |
|
45 | + throw new \UnexpectedValueException('Unsupported public key.'); |
|
46 | + } |
|
47 | 47 | |
48 | - /** |
|
49 | - * Initialize from a PublicKeyInfo object. |
|
50 | - * |
|
51 | - * @param PublicKeyInfo $pki Public key info |
|
52 | - * |
|
53 | - * @return self |
|
54 | - */ |
|
55 | - public static function fromPublicKeyInfo(PublicKeyInfo $pki): PublicKeyJWK |
|
56 | - { |
|
57 | - return self::fromPublicKey($pki->publicKey()); |
|
58 | - } |
|
48 | + /** |
|
49 | + * Initialize from a PublicKeyInfo object. |
|
50 | + * |
|
51 | + * @param PublicKeyInfo $pki Public key info |
|
52 | + * |
|
53 | + * @return self |
|
54 | + */ |
|
55 | + public static function fromPublicKeyInfo(PublicKeyInfo $pki): PublicKeyJWK |
|
56 | + { |
|
57 | + return self::fromPublicKey($pki->publicKey()); |
|
58 | + } |
|
59 | 59 | } |
@@ -1,6 +1,6 @@ |
||
1 | 1 | <?php |
2 | 2 | |
3 | -declare(strict_types = 1); |
|
3 | +declare(strict_types=1); |
|
4 | 4 | |
5 | 5 | namespace Sop\JWX\JWK\Asymmetric; |
6 | 6 |
@@ -18,49 +18,49 @@ |
||
18 | 18 | */ |
19 | 19 | abstract class PrivateKeyJWK extends JWK |
20 | 20 | { |
21 | - /** |
|
22 | - * Get the public key component of the asymmetric key pair. |
|
23 | - * |
|
24 | - * @return PublicKeyJWK |
|
25 | - */ |
|
26 | - abstract public function publicKey(): PublicKeyJWK; |
|
21 | + /** |
|
22 | + * Get the public key component of the asymmetric key pair. |
|
23 | + * |
|
24 | + * @return PublicKeyJWK |
|
25 | + */ |
|
26 | + abstract public function publicKey(): PublicKeyJWK; |
|
27 | 27 | |
28 | - /** |
|
29 | - * Convert private key to PEM. |
|
30 | - * |
|
31 | - * @return PEM |
|
32 | - */ |
|
33 | - abstract public function toPEM(): PEM; |
|
28 | + /** |
|
29 | + * Convert private key to PEM. |
|
30 | + * |
|
31 | + * @return PEM |
|
32 | + */ |
|
33 | + abstract public function toPEM(): PEM; |
|
34 | 34 | |
35 | - /** |
|
36 | - * Initialize from a PrivateKey object. |
|
37 | - * |
|
38 | - * @param PrivateKey $priv_key Private key |
|
39 | - * |
|
40 | - * @throws \UnexpectedValueException |
|
41 | - * |
|
42 | - * @return self |
|
43 | - */ |
|
44 | - public static function fromPrivateKey(PrivateKey $priv_key): PrivateKeyJWK |
|
45 | - { |
|
46 | - if ($priv_key instanceof RSAPrivateKey) { |
|
47 | - return RSAPrivateKeyJWK::fromRSAPrivateKey($priv_key); |
|
48 | - } |
|
49 | - if ($priv_key instanceof ECPrivateKey) { |
|
50 | - return ECPrivateKeyJWK::fromECPrivateKey($priv_key); |
|
51 | - } |
|
52 | - throw new \UnexpectedValueException('Unsupported private key.'); |
|
53 | - } |
|
35 | + /** |
|
36 | + * Initialize from a PrivateKey object. |
|
37 | + * |
|
38 | + * @param PrivateKey $priv_key Private key |
|
39 | + * |
|
40 | + * @throws \UnexpectedValueException |
|
41 | + * |
|
42 | + * @return self |
|
43 | + */ |
|
44 | + public static function fromPrivateKey(PrivateKey $priv_key): PrivateKeyJWK |
|
45 | + { |
|
46 | + if ($priv_key instanceof RSAPrivateKey) { |
|
47 | + return RSAPrivateKeyJWK::fromRSAPrivateKey($priv_key); |
|
48 | + } |
|
49 | + if ($priv_key instanceof ECPrivateKey) { |
|
50 | + return ECPrivateKeyJWK::fromECPrivateKey($priv_key); |
|
51 | + } |
|
52 | + throw new \UnexpectedValueException('Unsupported private key.'); |
|
53 | + } |
|
54 | 54 | |
55 | - /** |
|
56 | - * Initialize from a PrivateKeyInfo object. |
|
57 | - * |
|
58 | - * @param PrivateKeyInfo $pki PrivateKeyInfo |
|
59 | - * |
|
60 | - * @return self |
|
61 | - */ |
|
62 | - public static function fromPrivateKeyInfo(PrivateKeyInfo $pki): PrivateKeyJWK |
|
63 | - { |
|
64 | - return self::fromPrivateKey($pki->privateKey()); |
|
65 | - } |
|
55 | + /** |
|
56 | + * Initialize from a PrivateKeyInfo object. |
|
57 | + * |
|
58 | + * @param PrivateKeyInfo $pki PrivateKeyInfo |
|
59 | + * |
|
60 | + * @return self |
|
61 | + */ |
|
62 | + public static function fromPrivateKeyInfo(PrivateKeyInfo $pki): PrivateKeyJWK |
|
63 | + { |
|
64 | + return self::fromPrivateKey($pki->privateKey()); |
|
65 | + } |
|
66 | 66 | } |
@@ -1,6 +1,6 @@ |
||
1 | 1 | <?php |
2 | 2 | |
3 | -declare(strict_types = 1); |
|
3 | +declare(strict_types=1); |
|
4 | 4 | |
5 | 5 | namespace Sop\JWX\JWK\Asymmetric; |
6 | 6 |
@@ -23,85 +23,85 @@ |
||
23 | 23 | */ |
24 | 24 | class ECPublicKeyJWK extends PublicKeyJWK |
25 | 25 | { |
26 | - /** |
|
27 | - * Parameter names managed by this class. |
|
28 | - * |
|
29 | - * @var string[] |
|
30 | - */ |
|
31 | - const MANAGED_PARAMS = [ |
|
32 | - JWKParameter::PARAM_KEY_TYPE, |
|
33 | - JWKParameter::PARAM_CURVE, |
|
34 | - JWKParameter::PARAM_X_COORDINATE, |
|
35 | - ]; |
|
26 | + /** |
|
27 | + * Parameter names managed by this class. |
|
28 | + * |
|
29 | + * @var string[] |
|
30 | + */ |
|
31 | + const MANAGED_PARAMS = [ |
|
32 | + JWKParameter::PARAM_KEY_TYPE, |
|
33 | + JWKParameter::PARAM_CURVE, |
|
34 | + JWKParameter::PARAM_X_COORDINATE, |
|
35 | + ]; |
|
36 | 36 | |
37 | - /** |
|
38 | - * Constructor. |
|
39 | - * |
|
40 | - * @param JWKParameter ...$params |
|
41 | - * |
|
42 | - * @throws \UnexpectedValueException If missing required parameter |
|
43 | - */ |
|
44 | - public function __construct(JWKParameter ...$params) |
|
45 | - { |
|
46 | - parent::__construct(...$params); |
|
47 | - foreach (self::MANAGED_PARAMS as $name) { |
|
48 | - if (!$this->has($name)) { |
|
49 | - throw new \UnexpectedValueException( |
|
50 | - "Missing '{$name}' parameter."); |
|
51 | - } |
|
52 | - } |
|
53 | - if (KeyTypeParameter::TYPE_EC !== $this->keyTypeParameter()->value()) { |
|
54 | - throw new \UnexpectedValueException('Invalid key type.'); |
|
55 | - } |
|
56 | - } |
|
37 | + /** |
|
38 | + * Constructor. |
|
39 | + * |
|
40 | + * @param JWKParameter ...$params |
|
41 | + * |
|
42 | + * @throws \UnexpectedValueException If missing required parameter |
|
43 | + */ |
|
44 | + public function __construct(JWKParameter ...$params) |
|
45 | + { |
|
46 | + parent::__construct(...$params); |
|
47 | + foreach (self::MANAGED_PARAMS as $name) { |
|
48 | + if (!$this->has($name)) { |
|
49 | + throw new \UnexpectedValueException( |
|
50 | + "Missing '{$name}' parameter."); |
|
51 | + } |
|
52 | + } |
|
53 | + if (KeyTypeParameter::TYPE_EC !== $this->keyTypeParameter()->value()) { |
|
54 | + throw new \UnexpectedValueException('Invalid key type.'); |
|
55 | + } |
|
56 | + } |
|
57 | 57 | |
58 | - /** |
|
59 | - * Initialize from ECPublicKey. |
|
60 | - * |
|
61 | - * @param ECPublicKey $pk |
|
62 | - * |
|
63 | - * @throws \UnexpectedValueException |
|
64 | - * |
|
65 | - * @return self |
|
66 | - */ |
|
67 | - public static function fromECPublicKey(ECPublicKey $pk): self |
|
68 | - { |
|
69 | - if (!$pk->hasNamedCurve()) { |
|
70 | - throw new \UnexpectedValueException('No curve name.'); |
|
71 | - } |
|
72 | - $curve = CurveParameter::fromOID($pk->namedCurve()); |
|
73 | - [$x, $y] = $pk->curvePointOctets(); |
|
74 | - $xcoord = XCoordinateParameter::fromString($x); |
|
75 | - $ycoord = YCoordinateParameter::fromString($y); |
|
76 | - $key_type = new KeyTypeParameter(KeyTypeParameter::TYPE_EC); |
|
77 | - return new self($key_type, $curve, $xcoord, $ycoord); |
|
78 | - } |
|
58 | + /** |
|
59 | + * Initialize from ECPublicKey. |
|
60 | + * |
|
61 | + * @param ECPublicKey $pk |
|
62 | + * |
|
63 | + * @throws \UnexpectedValueException |
|
64 | + * |
|
65 | + * @return self |
|
66 | + */ |
|
67 | + public static function fromECPublicKey(ECPublicKey $pk): self |
|
68 | + { |
|
69 | + if (!$pk->hasNamedCurve()) { |
|
70 | + throw new \UnexpectedValueException('No curve name.'); |
|
71 | + } |
|
72 | + $curve = CurveParameter::fromOID($pk->namedCurve()); |
|
73 | + [$x, $y] = $pk->curvePointOctets(); |
|
74 | + $xcoord = XCoordinateParameter::fromString($x); |
|
75 | + $ycoord = YCoordinateParameter::fromString($y); |
|
76 | + $key_type = new KeyTypeParameter(KeyTypeParameter::TYPE_EC); |
|
77 | + return new self($key_type, $curve, $xcoord, $ycoord); |
|
78 | + } |
|
79 | 79 | |
80 | - /** |
|
81 | - * Initialize from PEM. |
|
82 | - * |
|
83 | - * @param PEM $pem |
|
84 | - * |
|
85 | - * @return self |
|
86 | - */ |
|
87 | - public static function fromPEM(PEM $pem): self |
|
88 | - { |
|
89 | - return self::fromECPublicKey(ECPublicKey::fromPEM($pem)); |
|
90 | - } |
|
80 | + /** |
|
81 | + * Initialize from PEM. |
|
82 | + * |
|
83 | + * @param PEM $pem |
|
84 | + * |
|
85 | + * @return self |
|
86 | + */ |
|
87 | + public static function fromPEM(PEM $pem): self |
|
88 | + { |
|
89 | + return self::fromECPublicKey(ECPublicKey::fromPEM($pem)); |
|
90 | + } |
|
91 | 91 | |
92 | - /** |
|
93 | - * Convert EC public key to PEM. |
|
94 | - * |
|
95 | - * @return PEM |
|
96 | - */ |
|
97 | - public function toPEM(): PEM |
|
98 | - { |
|
99 | - $curve_oid = CurveParameter::nameToOID($this->curveParameter()->value()); |
|
100 | - $x = ECConversion::octetsToNumber( |
|
101 | - $this->XCoordinateParameter()->coordinateOctets()); |
|
102 | - $y = ECConversion::octetsToNumber( |
|
103 | - $this->YCoordinateParameter()->coordinateOctets()); |
|
104 | - $ec = ECPublicKey::fromCoordinates($x, $y, $curve_oid); |
|
105 | - return $ec->publicKeyInfo()->toPEM(); |
|
106 | - } |
|
92 | + /** |
|
93 | + * Convert EC public key to PEM. |
|
94 | + * |
|
95 | + * @return PEM |
|
96 | + */ |
|
97 | + public function toPEM(): PEM |
|
98 | + { |
|
99 | + $curve_oid = CurveParameter::nameToOID($this->curveParameter()->value()); |
|
100 | + $x = ECConversion::octetsToNumber( |
|
101 | + $this->XCoordinateParameter()->coordinateOctets()); |
|
102 | + $y = ECConversion::octetsToNumber( |
|
103 | + $this->YCoordinateParameter()->coordinateOctets()); |
|
104 | + $ec = ECPublicKey::fromCoordinates($x, $y, $curve_oid); |
|
105 | + return $ec->publicKeyInfo()->toPEM(); |
|
106 | + } |
|
107 | 107 | } |
@@ -1,6 +1,6 @@ |
||
1 | 1 | <?php |
2 | 2 | |
3 | -declare(strict_types = 1); |
|
3 | +declare(strict_types=1); |
|
4 | 4 | |
5 | 5 | namespace Sop\JWX\JWK\EC; |
6 | 6 |
@@ -26,109 +26,109 @@ |
||
26 | 26 | */ |
27 | 27 | class ECPrivateKeyJWK extends PrivateKeyJWK |
28 | 28 | { |
29 | - /** |
|
30 | - * Parameter names managed by this class. |
|
31 | - * |
|
32 | - * @internal |
|
33 | - * |
|
34 | - * @var string[] |
|
35 | - */ |
|
36 | - const MANAGED_PARAMS = [ |
|
37 | - JWKParameter::PARAM_KEY_TYPE, |
|
38 | - JWKParameter::PARAM_CURVE, |
|
39 | - JWKParameter::PARAM_X_COORDINATE, |
|
40 | - JWKParameter::PARAM_ECC_PRIVATE_KEY, |
|
41 | - ]; |
|
29 | + /** |
|
30 | + * Parameter names managed by this class. |
|
31 | + * |
|
32 | + * @internal |
|
33 | + * |
|
34 | + * @var string[] |
|
35 | + */ |
|
36 | + const MANAGED_PARAMS = [ |
|
37 | + JWKParameter::PARAM_KEY_TYPE, |
|
38 | + JWKParameter::PARAM_CURVE, |
|
39 | + JWKParameter::PARAM_X_COORDINATE, |
|
40 | + JWKParameter::PARAM_ECC_PRIVATE_KEY, |
|
41 | + ]; |
|
42 | 42 | |
43 | - /** |
|
44 | - * Constructor. |
|
45 | - * |
|
46 | - * @param JWKParameter ...$params |
|
47 | - * |
|
48 | - * @throws \UnexpectedValueException If missing required parameter |
|
49 | - */ |
|
50 | - public function __construct(JWKParameter ...$params) |
|
51 | - { |
|
52 | - parent::__construct(...$params); |
|
53 | - foreach (self::MANAGED_PARAMS as $name) { |
|
54 | - if (!$this->has($name)) { |
|
55 | - throw new \UnexpectedValueException("Missing '{$name}' parameter."); |
|
56 | - } |
|
57 | - } |
|
58 | - if (KeyTypeParameter::TYPE_EC !== $this->keyTypeParameter()->value()) { |
|
59 | - throw new \UnexpectedValueException('Invalid key type.'); |
|
60 | - } |
|
61 | - // cast ECC private key parameter to correct class |
|
62 | - $key = JWKParameter::PARAM_ECC_PRIVATE_KEY; |
|
63 | - $this->_parameters[$key] = new ECCPrivateKeyParameter( |
|
64 | - $this->_parameters[$key]->value()); |
|
65 | - } |
|
43 | + /** |
|
44 | + * Constructor. |
|
45 | + * |
|
46 | + * @param JWKParameter ...$params |
|
47 | + * |
|
48 | + * @throws \UnexpectedValueException If missing required parameter |
|
49 | + */ |
|
50 | + public function __construct(JWKParameter ...$params) |
|
51 | + { |
|
52 | + parent::__construct(...$params); |
|
53 | + foreach (self::MANAGED_PARAMS as $name) { |
|
54 | + if (!$this->has($name)) { |
|
55 | + throw new \UnexpectedValueException("Missing '{$name}' parameter."); |
|
56 | + } |
|
57 | + } |
|
58 | + if (KeyTypeParameter::TYPE_EC !== $this->keyTypeParameter()->value()) { |
|
59 | + throw new \UnexpectedValueException('Invalid key type.'); |
|
60 | + } |
|
61 | + // cast ECC private key parameter to correct class |
|
62 | + $key = JWKParameter::PARAM_ECC_PRIVATE_KEY; |
|
63 | + $this->_parameters[$key] = new ECCPrivateKeyParameter( |
|
64 | + $this->_parameters[$key]->value()); |
|
65 | + } |
|
66 | 66 | |
67 | - /** |
|
68 | - * Initialize from ECPrivateKey. |
|
69 | - * |
|
70 | - * @param ECPrivateKey $pk |
|
71 | - * |
|
72 | - * @throws \UnexpectedValueException |
|
73 | - * |
|
74 | - * @return self |
|
75 | - */ |
|
76 | - public static function fromECPrivateKey(ECPrivateKey $pk): self |
|
77 | - { |
|
78 | - if (!$pk->hasNamedCurve()) { |
|
79 | - throw new \UnexpectedValueException('No curve name.'); |
|
80 | - } |
|
81 | - $curve = CurveParameter::fromOID($pk->namedCurve()); |
|
82 | - $pubkey = $pk->publicKey(); |
|
83 | - [$x, $y] = $pubkey->curvePointOctets(); |
|
84 | - $xcoord = XCoordinateParameter::fromString($x); |
|
85 | - $ycoord = YCoordinateParameter::fromString($y); |
|
86 | - $priv = ECCPrivateKeyParameter::fromString($pk->privateKeyOctets()); |
|
87 | - $key_type = new KeyTypeParameter(KeyTypeParameter::TYPE_EC); |
|
88 | - return new self($key_type, $curve, $xcoord, $ycoord, $priv); |
|
89 | - } |
|
67 | + /** |
|
68 | + * Initialize from ECPrivateKey. |
|
69 | + * |
|
70 | + * @param ECPrivateKey $pk |
|
71 | + * |
|
72 | + * @throws \UnexpectedValueException |
|
73 | + * |
|
74 | + * @return self |
|
75 | + */ |
|
76 | + public static function fromECPrivateKey(ECPrivateKey $pk): self |
|
77 | + { |
|
78 | + if (!$pk->hasNamedCurve()) { |
|
79 | + throw new \UnexpectedValueException('No curve name.'); |
|
80 | + } |
|
81 | + $curve = CurveParameter::fromOID($pk->namedCurve()); |
|
82 | + $pubkey = $pk->publicKey(); |
|
83 | + [$x, $y] = $pubkey->curvePointOctets(); |
|
84 | + $xcoord = XCoordinateParameter::fromString($x); |
|
85 | + $ycoord = YCoordinateParameter::fromString($y); |
|
86 | + $priv = ECCPrivateKeyParameter::fromString($pk->privateKeyOctets()); |
|
87 | + $key_type = new KeyTypeParameter(KeyTypeParameter::TYPE_EC); |
|
88 | + return new self($key_type, $curve, $xcoord, $ycoord, $priv); |
|
89 | + } |
|
90 | 90 | |
91 | - /** |
|
92 | - * Initialize from PEM. |
|
93 | - * |
|
94 | - * @param PEM $pem |
|
95 | - * |
|
96 | - * @return self |
|
97 | - */ |
|
98 | - public static function fromPEM(PEM $pem): self |
|
99 | - { |
|
100 | - return self::fromECPrivateKey(ECPrivateKey::fromPEM($pem)); |
|
101 | - } |
|
91 | + /** |
|
92 | + * Initialize from PEM. |
|
93 | + * |
|
94 | + * @param PEM $pem |
|
95 | + * |
|
96 | + * @return self |
|
97 | + */ |
|
98 | + public static function fromPEM(PEM $pem): self |
|
99 | + { |
|
100 | + return self::fromECPrivateKey(ECPrivateKey::fromPEM($pem)); |
|
101 | + } |
|
102 | 102 | |
103 | - /** |
|
104 | - * Get the public key component of the EC private key. |
|
105 | - * |
|
106 | - * @return ECPublicKeyJWK |
|
107 | - */ |
|
108 | - public function publicKey(): PublicKeyJWK |
|
109 | - { |
|
110 | - $kty = $this->keyTypeParameter(); |
|
111 | - $curve = $this->curveParameter(); |
|
112 | - $xcoord = $this->XCoordinateParameter(); |
|
113 | - $ycoord = $this->YCoordinateParameter(); |
|
114 | - return new ECPublicKeyJWK($kty, $curve, $xcoord, $ycoord); |
|
115 | - } |
|
103 | + /** |
|
104 | + * Get the public key component of the EC private key. |
|
105 | + * |
|
106 | + * @return ECPublicKeyJWK |
|
107 | + */ |
|
108 | + public function publicKey(): PublicKeyJWK |
|
109 | + { |
|
110 | + $kty = $this->keyTypeParameter(); |
|
111 | + $curve = $this->curveParameter(); |
|
112 | + $xcoord = $this->XCoordinateParameter(); |
|
113 | + $ycoord = $this->YCoordinateParameter(); |
|
114 | + return new ECPublicKeyJWK($kty, $curve, $xcoord, $ycoord); |
|
115 | + } |
|
116 | 116 | |
117 | - /** |
|
118 | - * Convert EC private key to PEM. |
|
119 | - * |
|
120 | - * @return PEM |
|
121 | - */ |
|
122 | - public function toPEM(): PEM |
|
123 | - { |
|
124 | - $curve_oid = CurveParameter::nameToOID($this->curveParameter()->value()); |
|
125 | - $x = ECConversion::octetsToNumber( |
|
126 | - $this->XCoordinateParameter()->coordinateOctets()); |
|
127 | - $y = ECConversion::octetsToNumber( |
|
128 | - $this->YCoordinateParameter()->coordinateOctets()); |
|
129 | - $pubkey = ECPublicKey::fromCoordinates($x, $y, $curve_oid); |
|
130 | - $priv = $this->ECCPrivateKeyParameter()->privateKeyOctets(); |
|
131 | - $ec = new ECPrivateKey($priv, $curve_oid, $pubkey->ECPoint()); |
|
132 | - return $ec->privateKeyInfo()->toPEM(); |
|
133 | - } |
|
117 | + /** |
|
118 | + * Convert EC private key to PEM. |
|
119 | + * |
|
120 | + * @return PEM |
|
121 | + */ |
|
122 | + public function toPEM(): PEM |
|
123 | + { |
|
124 | + $curve_oid = CurveParameter::nameToOID($this->curveParameter()->value()); |
|
125 | + $x = ECConversion::octetsToNumber( |
|
126 | + $this->XCoordinateParameter()->coordinateOctets()); |
|
127 | + $y = ECConversion::octetsToNumber( |
|
128 | + $this->YCoordinateParameter()->coordinateOctets()); |
|
129 | + $pubkey = ECPublicKey::fromCoordinates($x, $y, $curve_oid); |
|
130 | + $priv = $this->ECCPrivateKeyParameter()->privateKeyOctets(); |
|
131 | + $ec = new ECPrivateKey($priv, $curve_oid, $pubkey->ECPoint()); |
|
132 | + return $ec->privateKeyInfo()->toPEM(); |
|
133 | + } |
|
134 | 134 | } |
@@ -1,6 +1,6 @@ |
||
1 | 1 | <?php |
2 | 2 | |
3 | -declare(strict_types = 1); |
|
3 | +declare(strict_types=1); |
|
4 | 4 | |
5 | 5 | namespace Sop\JWX\JWK\EC; |
6 | 6 |
@@ -16,72 +16,72 @@ |
||
16 | 16 | */ |
17 | 17 | abstract class SignatureAlgorithm implements AlgorithmParameterValue, HeaderParameters |
18 | 18 | { |
19 | - /** |
|
20 | - * ID of the key used by the algorithm. |
|
21 | - * |
|
22 | - * If set, KeyID parameter shall be automatically inserted into JWS's |
|
23 | - * header. |
|
24 | - * |
|
25 | - * @var null|string |
|
26 | - */ |
|
27 | - protected $_keyID; |
|
19 | + /** |
|
20 | + * ID of the key used by the algorithm. |
|
21 | + * |
|
22 | + * If set, KeyID parameter shall be automatically inserted into JWS's |
|
23 | + * header. |
|
24 | + * |
|
25 | + * @var null|string |
|
26 | + */ |
|
27 | + protected $_keyID; |
|
28 | 28 | |
29 | - /** |
|
30 | - * Compute signature. |
|
31 | - * |
|
32 | - * @param string $data Data for which the signature is computed |
|
33 | - * |
|
34 | - * @return string |
|
35 | - */ |
|
36 | - abstract public function computeSignature(string $data): string; |
|
29 | + /** |
|
30 | + * Compute signature. |
|
31 | + * |
|
32 | + * @param string $data Data for which the signature is computed |
|
33 | + * |
|
34 | + * @return string |
|
35 | + */ |
|
36 | + abstract public function computeSignature(string $data): string; |
|
37 | 37 | |
38 | - /** |
|
39 | - * Validate signature. |
|
40 | - * |
|
41 | - * @param string $data Data to validate |
|
42 | - * @param string $signature Signature to compare |
|
43 | - * |
|
44 | - * @return bool |
|
45 | - */ |
|
46 | - abstract public function validateSignature(string $data, string $signature): bool; |
|
38 | + /** |
|
39 | + * Validate signature. |
|
40 | + * |
|
41 | + * @param string $data Data to validate |
|
42 | + * @param string $signature Signature to compare |
|
43 | + * |
|
44 | + * @return bool |
|
45 | + */ |
|
46 | + abstract public function validateSignature(string $data, string $signature): bool; |
|
47 | 47 | |
48 | - /** |
|
49 | - * Initialize signature algorithm from a JWK and a header. |
|
50 | - * |
|
51 | - * @param JWK $jwk JSON Web Key |
|
52 | - * @param Header $header Header |
|
53 | - * |
|
54 | - * @return SignatureAlgorithm |
|
55 | - */ |
|
56 | - public static function fromJWK(JWK $jwk, Header $header): SignatureAlgorithm |
|
57 | - { |
|
58 | - $factory = new SignatureAlgorithmFactory($header); |
|
59 | - return $factory->algoByKey($jwk); |
|
60 | - } |
|
48 | + /** |
|
49 | + * Initialize signature algorithm from a JWK and a header. |
|
50 | + * |
|
51 | + * @param JWK $jwk JSON Web Key |
|
52 | + * @param Header $header Header |
|
53 | + * |
|
54 | + * @return SignatureAlgorithm |
|
55 | + */ |
|
56 | + public static function fromJWK(JWK $jwk, Header $header): SignatureAlgorithm |
|
57 | + { |
|
58 | + $factory = new SignatureAlgorithmFactory($header); |
|
59 | + return $factory->algoByKey($jwk); |
|
60 | + } |
|
61 | 61 | |
62 | - /** |
|
63 | - * Get self with key ID. |
|
64 | - * |
|
65 | - * @param null|string $id Key ID or null to remove |
|
66 | - * |
|
67 | - * @return self |
|
68 | - */ |
|
69 | - public function withKeyID(?string $id): self |
|
70 | - { |
|
71 | - $obj = clone $this; |
|
72 | - $obj->_keyID = $id; |
|
73 | - return $obj; |
|
74 | - } |
|
62 | + /** |
|
63 | + * Get self with key ID. |
|
64 | + * |
|
65 | + * @param null|string $id Key ID or null to remove |
|
66 | + * |
|
67 | + * @return self |
|
68 | + */ |
|
69 | + public function withKeyID(?string $id): self |
|
70 | + { |
|
71 | + $obj = clone $this; |
|
72 | + $obj->_keyID = $id; |
|
73 | + return $obj; |
|
74 | + } |
|
75 | 75 | |
76 | - /** |
|
77 | - * {@inheritdoc} |
|
78 | - */ |
|
79 | - public function headerParameters(): array |
|
80 | - { |
|
81 | - $params = []; |
|
82 | - if (isset($this->_keyID)) { |
|
83 | - $params[] = new KeyIDParameter($this->_keyID); |
|
84 | - } |
|
85 | - return $params; |
|
86 | - } |
|
76 | + /** |
|
77 | + * {@inheritdoc} |
|
78 | + */ |
|
79 | + public function headerParameters(): array |
|
80 | + { |
|
81 | + $params = []; |
|
82 | + if (isset($this->_keyID)) { |
|
83 | + $params[] = new KeyIDParameter($this->_keyID); |
|
84 | + } |
|
85 | + return $params; |
|
86 | + } |
|
87 | 87 | } |
@@ -1,6 +1,6 @@ |
||
1 | 1 | <?php |
2 | 2 | |
3 | -declare(strict_types = 1); |
|
3 | +declare(strict_types=1); |
|
4 | 4 | |
5 | 5 | namespace Sop\JWX\JWS; |
6 | 6 |
@@ -21,287 +21,287 @@ |
||
21 | 21 | */ |
22 | 22 | class JWS |
23 | 23 | { |
24 | - /** |
|
25 | - * Protected header. |
|
26 | - * |
|
27 | - * @var Header |
|
28 | - */ |
|
29 | - protected $_protectedHeader; |
|
24 | + /** |
|
25 | + * Protected header. |
|
26 | + * |
|
27 | + * @var Header |
|
28 | + */ |
|
29 | + protected $_protectedHeader; |
|
30 | 30 | |
31 | - /** |
|
32 | - * Payload. |
|
33 | - * |
|
34 | - * @var string |
|
35 | - */ |
|
36 | - protected $_payload; |
|
31 | + /** |
|
32 | + * Payload. |
|
33 | + * |
|
34 | + * @var string |
|
35 | + */ |
|
36 | + protected $_payload; |
|
37 | 37 | |
38 | - /** |
|
39 | - * Input value for the signature computation. |
|
40 | - * |
|
41 | - * @var string |
|
42 | - */ |
|
43 | - protected $_signatureInput; |
|
38 | + /** |
|
39 | + * Input value for the signature computation. |
|
40 | + * |
|
41 | + * @var string |
|
42 | + */ |
|
43 | + protected $_signatureInput; |
|
44 | 44 | |
45 | - /** |
|
46 | - * Signature. |
|
47 | - * |
|
48 | - * @var string |
|
49 | - */ |
|
50 | - protected $_signature; |
|
45 | + /** |
|
46 | + * Signature. |
|
47 | + * |
|
48 | + * @var string |
|
49 | + */ |
|
50 | + protected $_signature; |
|
51 | 51 | |
52 | - /** |
|
53 | - * Constructor. |
|
54 | - * |
|
55 | - * @param Header $protected_header JWS Protected Header |
|
56 | - * @param string $payload JWS Payload |
|
57 | - * @param string $signature_input Input value for the signature computation |
|
58 | - * @param string $signature JWS Signature |
|
59 | - */ |
|
60 | - protected function __construct(Header $protected_header, string $payload, |
|
61 | - string $signature_input, string $signature) |
|
62 | - { |
|
63 | - $this->_protectedHeader = $protected_header; |
|
64 | - $this->_payload = $payload; |
|
65 | - $this->_signatureInput = $signature_input; |
|
66 | - $this->_signature = $signature; |
|
67 | - } |
|
52 | + /** |
|
53 | + * Constructor. |
|
54 | + * |
|
55 | + * @param Header $protected_header JWS Protected Header |
|
56 | + * @param string $payload JWS Payload |
|
57 | + * @param string $signature_input Input value for the signature computation |
|
58 | + * @param string $signature JWS Signature |
|
59 | + */ |
|
60 | + protected function __construct(Header $protected_header, string $payload, |
|
61 | + string $signature_input, string $signature) |
|
62 | + { |
|
63 | + $this->_protectedHeader = $protected_header; |
|
64 | + $this->_payload = $payload; |
|
65 | + $this->_signatureInput = $signature_input; |
|
66 | + $this->_signature = $signature; |
|
67 | + } |
|
68 | 68 | |
69 | - /** |
|
70 | - * Convert JWS to string. |
|
71 | - * |
|
72 | - * @return string |
|
73 | - */ |
|
74 | - public function __toString(): string |
|
75 | - { |
|
76 | - return $this->toCompact(); |
|
77 | - } |
|
69 | + /** |
|
70 | + * Convert JWS to string. |
|
71 | + * |
|
72 | + * @return string |
|
73 | + */ |
|
74 | + public function __toString(): string |
|
75 | + { |
|
76 | + return $this->toCompact(); |
|
77 | + } |
|
78 | 78 | |
79 | - /** |
|
80 | - * Initialize from a compact serialization. |
|
81 | - * |
|
82 | - * @param string $data |
|
83 | - * |
|
84 | - * @return self |
|
85 | - */ |
|
86 | - public static function fromCompact(string $data): self |
|
87 | - { |
|
88 | - return self::fromParts(explode('.', $data)); |
|
89 | - } |
|
79 | + /** |
|
80 | + * Initialize from a compact serialization. |
|
81 | + * |
|
82 | + * @param string $data |
|
83 | + * |
|
84 | + * @return self |
|
85 | + */ |
|
86 | + public static function fromCompact(string $data): self |
|
87 | + { |
|
88 | + return self::fromParts(explode('.', $data)); |
|
89 | + } |
|
90 | 90 | |
91 | - /** |
|
92 | - * Initialize from the parts of a compact serialization. |
|
93 | - * |
|
94 | - * @param array $parts |
|
95 | - * |
|
96 | - * @throws \UnexpectedValueException |
|
97 | - * |
|
98 | - * @return self |
|
99 | - */ |
|
100 | - public static function fromParts(array $parts): self |
|
101 | - { |
|
102 | - if (3 !== count($parts)) { |
|
103 | - throw new \UnexpectedValueException( |
|
104 | - 'Invalid JWS compact serialization.'); |
|
105 | - } |
|
106 | - $header = Header::fromJSON(Base64::urlDecode($parts[0])); |
|
107 | - $b64 = $header->hasB64Payload() ? $header->B64Payload()->value() : true; |
|
108 | - $payload = $b64 ? Base64::urlDecode($parts[1]) : $parts[1]; |
|
109 | - $signature_input = $parts[0] . '.' . $parts[1]; |
|
110 | - $signature = Base64::urlDecode($parts[2]); |
|
111 | - return new self($header, $payload, $signature_input, $signature); |
|
112 | - } |
|
91 | + /** |
|
92 | + * Initialize from the parts of a compact serialization. |
|
93 | + * |
|
94 | + * @param array $parts |
|
95 | + * |
|
96 | + * @throws \UnexpectedValueException |
|
97 | + * |
|
98 | + * @return self |
|
99 | + */ |
|
100 | + public static function fromParts(array $parts): self |
|
101 | + { |
|
102 | + if (3 !== count($parts)) { |
|
103 | + throw new \UnexpectedValueException( |
|
104 | + 'Invalid JWS compact serialization.'); |
|
105 | + } |
|
106 | + $header = Header::fromJSON(Base64::urlDecode($parts[0])); |
|
107 | + $b64 = $header->hasB64Payload() ? $header->B64Payload()->value() : true; |
|
108 | + $payload = $b64 ? Base64::urlDecode($parts[1]) : $parts[1]; |
|
109 | + $signature_input = $parts[0] . '.' . $parts[1]; |
|
110 | + $signature = Base64::urlDecode($parts[2]); |
|
111 | + return new self($header, $payload, $signature_input, $signature); |
|
112 | + } |
|
113 | 113 | |
114 | - /** |
|
115 | - * Initialize by signing the payload with given algorithm. |
|
116 | - * |
|
117 | - * @param string $payload JWS Payload |
|
118 | - * @param SignatureAlgorithm $algo Signature algorithm |
|
119 | - * @param null|Header $header Desired header. Algorithm specific |
|
120 | - * parameters are added automatically. |
|
121 | - * |
|
122 | - * @throws \RuntimeException If signature computation fails |
|
123 | - * |
|
124 | - * @return self |
|
125 | - */ |
|
126 | - public static function sign(string $payload, SignatureAlgorithm $algo, |
|
127 | - ?Header $header = null): self |
|
128 | - { |
|
129 | - if (!isset($header)) { |
|
130 | - $header = new Header(); |
|
131 | - } |
|
132 | - $header = $header->withParameters(...$algo->headerParameters()); |
|
133 | - // ensure that if b64 parameter is used, it's marked critical |
|
134 | - if ($header->hasB64Payload()) { |
|
135 | - if (!$header->hasCritical()) { |
|
136 | - $crit = new CriticalParameter(JWTParameter::P_B64); |
|
137 | - } else { |
|
138 | - $crit = $header->critical()->withParamName(JWTParameter::P_B64); |
|
139 | - } |
|
140 | - $header = $header->withParameters($crit); |
|
141 | - } |
|
142 | - $signature_input = self::_generateSignatureInput($payload, $header); |
|
143 | - $signature = $algo->computeSignature($signature_input); |
|
144 | - return new self($header, $payload, $signature_input, $signature); |
|
145 | - } |
|
114 | + /** |
|
115 | + * Initialize by signing the payload with given algorithm. |
|
116 | + * |
|
117 | + * @param string $payload JWS Payload |
|
118 | + * @param SignatureAlgorithm $algo Signature algorithm |
|
119 | + * @param null|Header $header Desired header. Algorithm specific |
|
120 | + * parameters are added automatically. |
|
121 | + * |
|
122 | + * @throws \RuntimeException If signature computation fails |
|
123 | + * |
|
124 | + * @return self |
|
125 | + */ |
|
126 | + public static function sign(string $payload, SignatureAlgorithm $algo, |
|
127 | + ?Header $header = null): self |
|
128 | + { |
|
129 | + if (!isset($header)) { |
|
130 | + $header = new Header(); |
|
131 | + } |
|
132 | + $header = $header->withParameters(...$algo->headerParameters()); |
|
133 | + // ensure that if b64 parameter is used, it's marked critical |
|
134 | + if ($header->hasB64Payload()) { |
|
135 | + if (!$header->hasCritical()) { |
|
136 | + $crit = new CriticalParameter(JWTParameter::P_B64); |
|
137 | + } else { |
|
138 | + $crit = $header->critical()->withParamName(JWTParameter::P_B64); |
|
139 | + } |
|
140 | + $header = $header->withParameters($crit); |
|
141 | + } |
|
142 | + $signature_input = self::_generateSignatureInput($payload, $header); |
|
143 | + $signature = $algo->computeSignature($signature_input); |
|
144 | + return new self($header, $payload, $signature_input, $signature); |
|
145 | + } |
|
146 | 146 | |
147 | - /** |
|
148 | - * Get JOSE header. |
|
149 | - * |
|
150 | - * @return JOSE |
|
151 | - */ |
|
152 | - public function header(): JOSE |
|
153 | - { |
|
154 | - return new JOSE($this->_protectedHeader); |
|
155 | - } |
|
147 | + /** |
|
148 | + * Get JOSE header. |
|
149 | + * |
|
150 | + * @return JOSE |
|
151 | + */ |
|
152 | + public function header(): JOSE |
|
153 | + { |
|
154 | + return new JOSE($this->_protectedHeader); |
|
155 | + } |
|
156 | 156 | |
157 | - /** |
|
158 | - * Get the signature algorithm name. |
|
159 | - * |
|
160 | - * @return string |
|
161 | - */ |
|
162 | - public function algorithmName(): string |
|
163 | - { |
|
164 | - return $this->header()->algorithm()->value(); |
|
165 | - } |
|
157 | + /** |
|
158 | + * Get the signature algorithm name. |
|
159 | + * |
|
160 | + * @return string |
|
161 | + */ |
|
162 | + public function algorithmName(): string |
|
163 | + { |
|
164 | + return $this->header()->algorithm()->value(); |
|
165 | + } |
|
166 | 166 | |
167 | - /** |
|
168 | - * Check whether JWS is unsecured, that is, contains no signature. |
|
169 | - * |
|
170 | - * @return bool |
|
171 | - */ |
|
172 | - public function isUnsecured(): bool |
|
173 | - { |
|
174 | - return JWA::ALGO_NONE === $this->algorithmName(); |
|
175 | - } |
|
167 | + /** |
|
168 | + * Check whether JWS is unsecured, that is, contains no signature. |
|
169 | + * |
|
170 | + * @return bool |
|
171 | + */ |
|
172 | + public function isUnsecured(): bool |
|
173 | + { |
|
174 | + return JWA::ALGO_NONE === $this->algorithmName(); |
|
175 | + } |
|
176 | 176 | |
177 | - /** |
|
178 | - * Get the payload. |
|
179 | - * |
|
180 | - * @return string |
|
181 | - */ |
|
182 | - public function payload(): string |
|
183 | - { |
|
184 | - return $this->_payload; |
|
185 | - } |
|
177 | + /** |
|
178 | + * Get the payload. |
|
179 | + * |
|
180 | + * @return string |
|
181 | + */ |
|
182 | + public function payload(): string |
|
183 | + { |
|
184 | + return $this->_payload; |
|
185 | + } |
|
186 | 186 | |
187 | - /** |
|
188 | - * Get the signature. |
|
189 | - * |
|
190 | - * @return string |
|
191 | - */ |
|
192 | - public function signature(): string |
|
193 | - { |
|
194 | - return $this->_signature; |
|
195 | - } |
|
187 | + /** |
|
188 | + * Get the signature. |
|
189 | + * |
|
190 | + * @return string |
|
191 | + */ |
|
192 | + public function signature(): string |
|
193 | + { |
|
194 | + return $this->_signature; |
|
195 | + } |
|
196 | 196 | |
197 | - /** |
|
198 | - * Validate the signature using explicit algorithm. |
|
199 | - * |
|
200 | - * @param SignatureAlgorithm $algo |
|
201 | - * |
|
202 | - * @throws \UnexpectedValueException If using different signature algorithm |
|
203 | - * then specified by the header |
|
204 | - * @throws \RuntimeException If signature computation fails |
|
205 | - * |
|
206 | - * @return bool True if signature is valid |
|
207 | - */ |
|
208 | - public function validate(SignatureAlgorithm $algo): bool |
|
209 | - { |
|
210 | - if ($algo->algorithmParamValue() !== $this->algorithmName()) { |
|
211 | - throw new \UnexpectedValueException('Invalid signature algorithm.'); |
|
212 | - } |
|
213 | - return $algo->validateSignature($this->_signatureInput, $this->_signature); |
|
214 | - } |
|
197 | + /** |
|
198 | + * Validate the signature using explicit algorithm. |
|
199 | + * |
|
200 | + * @param SignatureAlgorithm $algo |
|
201 | + * |
|
202 | + * @throws \UnexpectedValueException If using different signature algorithm |
|
203 | + * then specified by the header |
|
204 | + * @throws \RuntimeException If signature computation fails |
|
205 | + * |
|
206 | + * @return bool True if signature is valid |
|
207 | + */ |
|
208 | + public function validate(SignatureAlgorithm $algo): bool |
|
209 | + { |
|
210 | + if ($algo->algorithmParamValue() !== $this->algorithmName()) { |
|
211 | + throw new \UnexpectedValueException('Invalid signature algorithm.'); |
|
212 | + } |
|
213 | + return $algo->validateSignature($this->_signatureInput, $this->_signature); |
|
214 | + } |
|
215 | 215 | |
216 | - /** |
|
217 | - * Validate the signature using the given JWK. |
|
218 | - * |
|
219 | - * Signature algorithm is determined from the header. |
|
220 | - * |
|
221 | - * @param JWK $jwk JSON Web Key |
|
222 | - * |
|
223 | - * @throws \RuntimeException If algorithm initialization fails |
|
224 | - * |
|
225 | - * @return bool True if signature is valid |
|
226 | - */ |
|
227 | - public function validateWithJWK(JWK $jwk): bool |
|
228 | - { |
|
229 | - $algo = SignatureAlgorithm::fromJWK($jwk, $this->header()); |
|
230 | - return $this->validate($algo); |
|
231 | - } |
|
216 | + /** |
|
217 | + * Validate the signature using the given JWK. |
|
218 | + * |
|
219 | + * Signature algorithm is determined from the header. |
|
220 | + * |
|
221 | + * @param JWK $jwk JSON Web Key |
|
222 | + * |
|
223 | + * @throws \RuntimeException If algorithm initialization fails |
|
224 | + * |
|
225 | + * @return bool True if signature is valid |
|
226 | + */ |
|
227 | + public function validateWithJWK(JWK $jwk): bool |
|
228 | + { |
|
229 | + $algo = SignatureAlgorithm::fromJWK($jwk, $this->header()); |
|
230 | + return $this->validate($algo); |
|
231 | + } |
|
232 | 232 | |
233 | - /** |
|
234 | - * Validate the signature using a key from the given JWK set. |
|
235 | - * |
|
236 | - * Correct key shall be sought by the key ID indicated by the header. |
|
237 | - * |
|
238 | - * @param JWKSet $set Set of JSON Web Keys |
|
239 | - * |
|
240 | - * @throws \RuntimeException If algorithm initialization fails |
|
241 | - * |
|
242 | - * @return bool True if signature is valid |
|
243 | - */ |
|
244 | - public function validateWithJWKSet(JWKSet $set): bool |
|
245 | - { |
|
246 | - if (!count($set)) { |
|
247 | - throw new \RuntimeException('No keys.'); |
|
248 | - } |
|
249 | - $factory = new SignatureAlgorithmFactory($this->header()); |
|
250 | - $algo = $factory->algoByKeys($set); |
|
251 | - return $this->validate($algo); |
|
252 | - } |
|
233 | + /** |
|
234 | + * Validate the signature using a key from the given JWK set. |
|
235 | + * |
|
236 | + * Correct key shall be sought by the key ID indicated by the header. |
|
237 | + * |
|
238 | + * @param JWKSet $set Set of JSON Web Keys |
|
239 | + * |
|
240 | + * @throws \RuntimeException If algorithm initialization fails |
|
241 | + * |
|
242 | + * @return bool True if signature is valid |
|
243 | + */ |
|
244 | + public function validateWithJWKSet(JWKSet $set): bool |
|
245 | + { |
|
246 | + if (!count($set)) { |
|
247 | + throw new \RuntimeException('No keys.'); |
|
248 | + } |
|
249 | + $factory = new SignatureAlgorithmFactory($this->header()); |
|
250 | + $algo = $factory->algoByKeys($set); |
|
251 | + return $this->validate($algo); |
|
252 | + } |
|
253 | 253 | |
254 | - /** |
|
255 | - * Convert to compact serialization. |
|
256 | - * |
|
257 | - * @return string |
|
258 | - */ |
|
259 | - public function toCompact(): string |
|
260 | - { |
|
261 | - return Base64::urlEncode($this->_protectedHeader->toJSON()) . '.' . |
|
262 | - $this->_encodedPayload() . '.' . |
|
263 | - Base64::urlEncode($this->_signature); |
|
264 | - } |
|
254 | + /** |
|
255 | + * Convert to compact serialization. |
|
256 | + * |
|
257 | + * @return string |
|
258 | + */ |
|
259 | + public function toCompact(): string |
|
260 | + { |
|
261 | + return Base64::urlEncode($this->_protectedHeader->toJSON()) . '.' . |
|
262 | + $this->_encodedPayload() . '.' . |
|
263 | + Base64::urlEncode($this->_signature); |
|
264 | + } |
|
265 | 265 | |
266 | - /** |
|
267 | - * Convert to compact serialization with payload detached. |
|
268 | - * |
|
269 | - * @return string |
|
270 | - */ |
|
271 | - public function toCompactDetached(): string |
|
272 | - { |
|
273 | - return Base64::urlEncode($this->_protectedHeader->toJSON()) . '..' . |
|
274 | - Base64::urlEncode($this->_signature); |
|
275 | - } |
|
266 | + /** |
|
267 | + * Convert to compact serialization with payload detached. |
|
268 | + * |
|
269 | + * @return string |
|
270 | + */ |
|
271 | + public function toCompactDetached(): string |
|
272 | + { |
|
273 | + return Base64::urlEncode($this->_protectedHeader->toJSON()) . '..' . |
|
274 | + Base64::urlEncode($this->_signature); |
|
275 | + } |
|
276 | 276 | |
277 | - /** |
|
278 | - * Get the payload encoded for serialization. |
|
279 | - * |
|
280 | - * @return string |
|
281 | - */ |
|
282 | - protected function _encodedPayload(): string |
|
283 | - { |
|
284 | - $b64 = true; |
|
285 | - if ($this->_protectedHeader->hasB64Payload()) { |
|
286 | - $b64 = $this->_protectedHeader->B64Payload()->value(); |
|
287 | - } |
|
288 | - return $b64 ? Base64::urlEncode($this->_payload) : $this->_payload; |
|
289 | - } |
|
277 | + /** |
|
278 | + * Get the payload encoded for serialization. |
|
279 | + * |
|
280 | + * @return string |
|
281 | + */ |
|
282 | + protected function _encodedPayload(): string |
|
283 | + { |
|
284 | + $b64 = true; |
|
285 | + if ($this->_protectedHeader->hasB64Payload()) { |
|
286 | + $b64 = $this->_protectedHeader->B64Payload()->value(); |
|
287 | + } |
|
288 | + return $b64 ? Base64::urlEncode($this->_payload) : $this->_payload; |
|
289 | + } |
|
290 | 290 | |
291 | - /** |
|
292 | - * Generate input for the signature computation. |
|
293 | - * |
|
294 | - * @param string $payload Payload |
|
295 | - * @param Header $header Protected header |
|
296 | - * |
|
297 | - * @return string |
|
298 | - */ |
|
299 | - protected static function _generateSignatureInput(string $payload, |
|
300 | - Header $header): string |
|
301 | - { |
|
302 | - $b64 = $header->hasB64Payload() ? $header->B64Payload()->value() : true; |
|
303 | - $data = Base64::urlEncode($header->toJSON()) . '.'; |
|
304 | - $data .= $b64 ? Base64::urlEncode($payload) : $payload; |
|
305 | - return $data; |
|
306 | - } |
|
291 | + /** |
|
292 | + * Generate input for the signature computation. |
|
293 | + * |
|
294 | + * @param string $payload Payload |
|
295 | + * @param Header $header Protected header |
|
296 | + * |
|
297 | + * @return string |
|
298 | + */ |
|
299 | + protected static function _generateSignatureInput(string $payload, |
|
300 | + Header $header): string |
|
301 | + { |
|
302 | + $b64 = $header->hasB64Payload() ? $header->B64Payload()->value() : true; |
|
303 | + $data = Base64::urlEncode($header->toJSON()) . '.'; |
|
304 | + $data .= $b64 ? Base64::urlEncode($payload) : $payload; |
|
305 | + return $data; |
|
306 | + } |
|
307 | 307 | } |
@@ -1,6 +1,6 @@ |
||
1 | 1 | <?php |
2 | 2 | |
3 | -declare(strict_types = 1); |
|
3 | +declare(strict_types=1); |
|
4 | 4 | |
5 | 5 | namespace Sop\JWX\JWS; |
6 | 6 |