1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types = 1); |
4
|
|
|
|
5
|
|
|
namespace Sop\JWX\JWT\Header; |
6
|
|
|
|
7
|
|
|
use Sop\JWX\JWT\Parameter; |
8
|
|
|
use Sop\JWX\JWT\Parameter\JWTParameter; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Trait for Header to provide parameter accessor methods for typed return |
12
|
|
|
* values. |
13
|
|
|
*/ |
14
|
|
|
trait TypedHeader |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* Whether parameters are present. |
18
|
|
|
* |
19
|
|
|
* @param string ...$names Parameter names |
20
|
|
|
*/ |
21
|
|
|
abstract public function has(string ...$names): bool; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Get a parameter. |
25
|
|
|
* |
26
|
|
|
* @param string $name Parameter name |
27
|
|
|
* |
28
|
|
|
* @throws \LogicException If the parameter is not present |
29
|
|
|
*/ |
30
|
|
|
abstract public function get(string $name): JWTParameter; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Check whether the algorithm parameter is present. |
34
|
|
|
*/ |
35
|
74 |
|
public function hasAlgorithm(): bool |
36
|
|
|
{ |
37
|
74 |
|
return $this->has(Parameter\JWTParameter::P_ALG); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Get the algorithm parameter. |
42
|
|
|
* |
43
|
|
|
* @throws \UnexpectedValueException If the parameter has a wrong class |
44
|
|
|
* @throws \LogicException If the parameter is not present |
45
|
|
|
* |
46
|
|
|
* @return \Sop\JWX\JWT\Parameter\AlgorithmParameter |
47
|
|
|
*/ |
48
|
80 |
|
public function algorithm(): Parameter\AlgorithmParameter |
49
|
|
|
{ |
50
|
80 |
|
return self::_checkType($this->get(Parameter\JWTParameter::P_ALG), |
51
|
80 |
|
Parameter\AlgorithmParameter::class); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Check whether the authentication tag parameter is present. |
56
|
|
|
*/ |
57
|
6 |
|
public function hasAuthenticationTag(): bool |
58
|
|
|
{ |
59
|
6 |
|
return $this->has(Parameter\JWTParameter::P_TAG); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Get the authentication tag parameter. |
64
|
|
|
* |
65
|
|
|
* @throws \UnexpectedValueException If the parameter has a wrong class |
66
|
|
|
* @throws \LogicException If the parameter is not present |
67
|
|
|
* |
68
|
|
|
* @return \Sop\JWX\JWT\Parameter\AuthenticationTagParameter |
69
|
|
|
*/ |
70
|
5 |
|
public function authenticationTag(): Parameter\AuthenticationTagParameter |
71
|
|
|
{ |
72
|
5 |
|
return self::_checkType($this->get(Parameter\JWTParameter::P_TAG), |
73
|
5 |
|
Parameter\AuthenticationTagParameter::class); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Check whether the 'base64url-encode payload' parameter is present. |
78
|
|
|
*/ |
79
|
39 |
|
public function hasB64Payload(): bool |
80
|
|
|
{ |
81
|
39 |
|
return $this->has(Parameter\JWTParameter::P_B64); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Get the 'base64url-encode payload' parameter. |
86
|
|
|
* |
87
|
|
|
* @throws \UnexpectedValueException If the parameter has a wrong class |
88
|
|
|
* @throws \LogicException If the parameter is not present |
89
|
|
|
* |
90
|
|
|
* @return \Sop\JWX\JWT\Parameter\B64PayloadParameter |
91
|
|
|
*/ |
92
|
7 |
|
public function B64Payload(): Parameter\B64PayloadParameter |
93
|
|
|
{ |
94
|
7 |
|
return self::_checkType($this->get(Parameter\JWTParameter::P_B64), |
95
|
7 |
|
Parameter\B64PayloadParameter::class); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* Check whether the compression algorithm parameter is present. |
100
|
|
|
*/ |
101
|
25 |
|
public function hasCompressionAlgorithm(): bool |
102
|
|
|
{ |
103
|
25 |
|
return $this->has(Parameter\JWTParameter::P_ZIP); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* Get the compression algorithm parameter. |
108
|
|
|
* |
109
|
|
|
* @throws \UnexpectedValueException If the parameter has a wrong class |
110
|
|
|
* @throws \LogicException If the parameter is not present |
111
|
|
|
* |
112
|
|
|
* @return \Sop\JWX\JWT\Parameter\CompressionAlgorithmParameter |
113
|
|
|
*/ |
114
|
5 |
|
public function compressionAlgorithm(): Parameter\CompressionAlgorithmParameter |
115
|
|
|
{ |
116
|
5 |
|
return self::_checkType($this->get(Parameter\JWTParameter::P_ZIP), |
117
|
5 |
|
Parameter\CompressionAlgorithmParameter::class); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* Check whether the content type parameter is present. |
122
|
|
|
*/ |
123
|
10 |
|
public function hasContentType(): bool |
124
|
|
|
{ |
125
|
10 |
|
return $this->has(Parameter\JWTParameter::P_CTY); |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* Get the content type parameter. |
130
|
|
|
* |
131
|
|
|
* @throws \UnexpectedValueException If the parameter has a wrong class |
132
|
|
|
* @throws \LogicException If the parameter is not present |
133
|
|
|
* |
134
|
|
|
* @return \Sop\JWX\JWT\Parameter\ContentTypeParameter |
135
|
|
|
*/ |
136
|
4 |
|
public function contentType(): Parameter\ContentTypeParameter |
137
|
|
|
{ |
138
|
4 |
|
return self::_checkType($this->get(Parameter\JWTParameter::P_CTY), |
139
|
4 |
|
Parameter\ContentTypeParameter::class); |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* Check whether the critical parameter is present. |
144
|
|
|
*/ |
145
|
5 |
|
public function hasCritical(): bool |
146
|
|
|
{ |
147
|
5 |
|
return $this->has(Parameter\JWTParameter::P_CRIT); |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* Get the critical parameter. |
152
|
|
|
* |
153
|
|
|
* @throws \UnexpectedValueException If the parameter has a wrong class |
154
|
|
|
* @throws \LogicException If the parameter is not present |
155
|
|
|
* |
156
|
|
|
* @return \Sop\JWX\JWT\Parameter\CriticalParameter |
157
|
|
|
*/ |
158
|
3 |
|
public function critical(): Parameter\CriticalParameter |
159
|
|
|
{ |
160
|
3 |
|
return self::_checkType($this->get(Parameter\JWTParameter::P_CRIT), |
161
|
3 |
|
Parameter\CriticalParameter::class); |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
/** |
165
|
|
|
* Check whether the encryption algorithm parameter is present. |
166
|
|
|
*/ |
167
|
19 |
|
public function hasEncryptionAlgorithm(): bool |
168
|
|
|
{ |
169
|
19 |
|
return $this->has(Parameter\JWTParameter::P_ENC); |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
/** |
173
|
|
|
* Get the encryption algorithm parameter. |
174
|
|
|
* |
175
|
|
|
* @throws \UnexpectedValueException If the parameter has a wrong class |
176
|
|
|
* @throws \LogicException If the parameter is not present |
177
|
|
|
* |
178
|
|
|
* @return \Sop\JWX\JWT\Parameter\EncryptionAlgorithmParameter |
179
|
|
|
*/ |
180
|
17 |
|
public function encryptionAlgorithm(): Parameter\EncryptionAlgorithmParameter |
181
|
|
|
{ |
182
|
17 |
|
return self::_checkType($this->get(Parameter\JWTParameter::P_ENC), |
183
|
17 |
|
Parameter\EncryptionAlgorithmParameter::class); |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
/** |
187
|
|
|
* Check whether the initialization vector parameter is present. |
188
|
|
|
*/ |
189
|
8 |
|
public function hasInitializationVector(): bool |
190
|
|
|
{ |
191
|
8 |
|
return $this->has(Parameter\JWTParameter::P_IV); |
192
|
|
|
} |
193
|
|
|
|
194
|
|
|
/** |
195
|
|
|
* Get the initialization vector parameter. |
196
|
|
|
* |
197
|
|
|
* @throws \UnexpectedValueException If the parameter has a wrong class |
198
|
|
|
* @throws \LogicException If the parameter is not present |
199
|
|
|
* |
200
|
|
|
* @return \Sop\JWX\JWT\Parameter\InitializationVectorParameter |
201
|
|
|
*/ |
202
|
7 |
|
public function initializationVector(): Parameter\InitializationVectorParameter |
203
|
|
|
{ |
204
|
7 |
|
return self::_checkType($this->get(Parameter\JWTParameter::P_IV), |
205
|
7 |
|
Parameter\InitializationVectorParameter::class); |
206
|
|
|
} |
207
|
|
|
|
208
|
|
|
/** |
209
|
|
|
* Check whether the JSON web key parameter is present. |
210
|
|
|
*/ |
211
|
1 |
|
public function hasJSONWebKey(): bool |
212
|
|
|
{ |
213
|
1 |
|
return $this->has(Parameter\JWTParameter::P_JWK); |
214
|
|
|
} |
215
|
|
|
|
216
|
|
|
/** |
217
|
|
|
* Get the JSON web key parameter. |
218
|
|
|
* |
219
|
|
|
* @throws \UnexpectedValueException If the parameter has a wrong class |
220
|
|
|
* @throws \LogicException If the parameter is not present |
221
|
|
|
* |
222
|
|
|
* @return \Sop\JWX\JWT\Parameter\JSONWebKeyParameter |
223
|
|
|
*/ |
224
|
1 |
|
public function JSONWebKey(): Parameter\JSONWebKeyParameter |
225
|
|
|
{ |
226
|
1 |
|
return self::_checkType($this->get(Parameter\JWTParameter::P_JWK), |
227
|
1 |
|
Parameter\JSONWebKeyParameter::class); |
228
|
|
|
} |
229
|
|
|
|
230
|
|
|
/** |
231
|
|
|
* Check whether the JWK set URL parameter is present. |
232
|
|
|
*/ |
233
|
1 |
|
public function hasJWKSetURL(): bool |
234
|
|
|
{ |
235
|
1 |
|
return $this->has(Parameter\JWTParameter::P_JKU); |
236
|
|
|
} |
237
|
|
|
|
238
|
|
|
/** |
239
|
|
|
* Get the JWK set URL parameter. |
240
|
|
|
* |
241
|
|
|
* @throws \UnexpectedValueException If the parameter has a wrong class |
242
|
|
|
* @throws \LogicException If the parameter is not present |
243
|
|
|
* |
244
|
|
|
* @return \Sop\JWX\JWT\Parameter\JWKSetURLParameter |
245
|
|
|
*/ |
246
|
1 |
|
public function JWKSetURL(): Parameter\JWKSetURLParameter |
247
|
|
|
{ |
248
|
1 |
|
return self::_checkType($this->get(Parameter\JWTParameter::P_JKU), |
249
|
1 |
|
Parameter\JWKSetURLParameter::class); |
250
|
|
|
} |
251
|
|
|
|
252
|
|
|
/** |
253
|
|
|
* Check whether the key ID parameter is present. |
254
|
|
|
*/ |
255
|
14 |
|
public function hasKeyID(): bool |
256
|
|
|
{ |
257
|
14 |
|
return $this->has(Parameter\JWTParameter::P_KID); |
258
|
|
|
} |
259
|
|
|
|
260
|
|
|
/** |
261
|
|
|
* Get the key ID parameter. |
262
|
|
|
* |
263
|
|
|
* @throws \UnexpectedValueException If the parameter has a wrong class |
264
|
|
|
* @throws \LogicException If the parameter is not present |
265
|
|
|
* |
266
|
|
|
* @return \Sop\JWX\JWT\Parameter\KeyIDParameter |
267
|
|
|
*/ |
268
|
12 |
|
public function keyID(): Parameter\KeyIDParameter |
269
|
|
|
{ |
270
|
12 |
|
return self::_checkType($this->get(Parameter\JWTParameter::P_KID), |
271
|
12 |
|
Parameter\KeyIDParameter::class); |
272
|
|
|
} |
273
|
|
|
|
274
|
|
|
/** |
275
|
|
|
* Check whether the PBES2 count parameter is present. |
276
|
|
|
*/ |
277
|
7 |
|
public function hasPBES2Count(): bool |
278
|
|
|
{ |
279
|
7 |
|
return $this->has(Parameter\JWTParameter::P_P2C); |
280
|
|
|
} |
281
|
|
|
|
282
|
|
|
/** |
283
|
|
|
* Get the PBES2 count parameter. |
284
|
|
|
* |
285
|
|
|
* @throws \UnexpectedValueException If the parameter has a wrong class |
286
|
|
|
* @throws \LogicException If the parameter is not present |
287
|
|
|
* |
288
|
|
|
* @return \Sop\JWX\JWT\Parameter\PBES2CountParameter |
289
|
|
|
*/ |
290
|
6 |
|
public function PBES2Count(): Parameter\PBES2CountParameter |
291
|
|
|
{ |
292
|
6 |
|
return self::_checkType($this->get(Parameter\JWTParameter::P_P2C), |
293
|
6 |
|
Parameter\PBES2CountParameter::class); |
294
|
|
|
} |
295
|
|
|
|
296
|
|
|
/** |
297
|
|
|
* Check whether the PBES2 salt input parameter is present. |
298
|
|
|
*/ |
299
|
8 |
|
public function hasPBES2SaltInput(): bool |
300
|
|
|
{ |
301
|
8 |
|
return $this->has(Parameter\JWTParameter::P_P2S); |
302
|
|
|
} |
303
|
|
|
|
304
|
|
|
/** |
305
|
|
|
* Get the PBES2 salt input parameter. |
306
|
|
|
* |
307
|
|
|
* @throws \UnexpectedValueException If the parameter has a wrong class |
308
|
|
|
* @throws \LogicException If the parameter is not present |
309
|
|
|
* |
310
|
|
|
* @return \Sop\JWX\JWT\Parameter\PBES2SaltInputParameter |
311
|
|
|
*/ |
312
|
7 |
|
public function PBES2SaltInput(): Parameter\PBES2SaltInputParameter |
313
|
|
|
{ |
314
|
7 |
|
return self::_checkType($this->get(Parameter\JWTParameter::P_P2S), |
315
|
7 |
|
Parameter\PBES2SaltInputParameter::class); |
316
|
|
|
} |
317
|
|
|
|
318
|
|
|
/** |
319
|
|
|
* Check whether the type parameter is present. |
320
|
|
|
*/ |
321
|
1 |
|
public function hasType(): bool |
322
|
|
|
{ |
323
|
1 |
|
return $this->has(Parameter\JWTParameter::P_TYP); |
324
|
|
|
} |
325
|
|
|
|
326
|
|
|
/** |
327
|
|
|
* Get the type parameter. |
328
|
|
|
* |
329
|
|
|
* @throws \UnexpectedValueException If the parameter has a wrong class |
330
|
|
|
* @throws \LogicException If the parameter is not present |
331
|
|
|
* |
332
|
|
|
* @return \Sop\JWX\JWT\Parameter\TypeParameter |
333
|
|
|
*/ |
334
|
1 |
|
public function type(): Parameter\TypeParameter |
335
|
|
|
{ |
336
|
1 |
|
return self::_checkType($this->get(Parameter\JWTParameter::P_TYP), |
337
|
1 |
|
Parameter\TypeParameter::class); |
338
|
|
|
} |
339
|
|
|
|
340
|
|
|
/** |
341
|
|
|
* Check whether the X.509 certificate chain parameter is present. |
342
|
|
|
*/ |
343
|
1 |
|
public function hasX509CertificateChain(): bool |
344
|
|
|
{ |
345
|
1 |
|
return $this->has(Parameter\JWTParameter::P_X5C); |
346
|
|
|
} |
347
|
|
|
|
348
|
|
|
/** |
349
|
|
|
* Get the X.509 certificate chain parameter. |
350
|
|
|
* |
351
|
|
|
* @throws \UnexpectedValueException If the parameter has a wrong class |
352
|
|
|
* @throws \LogicException If the parameter is not present |
353
|
|
|
* |
354
|
|
|
* @return \Sop\JWX\JWT\Parameter\X509CertificateChainParameter |
355
|
|
|
*/ |
356
|
1 |
|
public function X509CertificateChain(): Parameter\X509CertificateChainParameter |
357
|
|
|
{ |
358
|
1 |
|
return self::_checkType($this->get(Parameter\JWTParameter::P_X5C), |
359
|
1 |
|
Parameter\X509CertificateChainParameter::class); |
360
|
|
|
} |
361
|
|
|
|
362
|
|
|
/** |
363
|
|
|
* Check whether the X.509 certificate SHA-1 thumbprint parameter is |
364
|
|
|
* present. |
365
|
|
|
*/ |
366
|
1 |
|
public function hasX509CertificateSHA1Thumbprint(): bool |
367
|
|
|
{ |
368
|
1 |
|
return $this->has(Parameter\JWTParameter::P_X5T); |
369
|
|
|
} |
370
|
|
|
|
371
|
|
|
/** |
372
|
|
|
* Get the X.509 certificate SHA-1 thumbprint parameter. |
373
|
|
|
* |
374
|
|
|
* @throws \UnexpectedValueException If the parameter has a wrong class |
375
|
|
|
* @throws \LogicException If the parameter is not present |
376
|
|
|
* |
377
|
|
|
* @return \Sop\JWX\JWT\Parameter\X509CertificateSHA1ThumbprintParameter |
378
|
|
|
*/ |
379
|
1 |
|
public function X509CertificateSHA1Thumbprint(): Parameter\X509CertificateSHA1ThumbprintParameter |
380
|
|
|
{ |
381
|
1 |
|
return self::_checkType($this->get(Parameter\JWTParameter::P_X5T), |
382
|
1 |
|
Parameter\X509CertificateSHA1ThumbprintParameter::class); |
383
|
|
|
} |
384
|
|
|
|
385
|
|
|
/** |
386
|
|
|
* Check whether the X.509 certificate SHA-256 thumbprint parameter is |
387
|
|
|
* present. |
388
|
|
|
*/ |
389
|
1 |
|
public function hasX509CertificateSHA256Thumbprint(): bool |
390
|
|
|
{ |
391
|
1 |
|
return $this->has(Parameter\JWTParameter::P_X5TS256); |
392
|
|
|
} |
393
|
|
|
|
394
|
|
|
/** |
395
|
|
|
* Get the X.509 certificate SHA-256 thumbprint parameter. |
396
|
|
|
* |
397
|
|
|
* @throws \UnexpectedValueException If the parameter has a wrong class |
398
|
|
|
* @throws \LogicException If the parameter is not present |
399
|
|
|
* |
400
|
|
|
* @return \Sop\JWX\JWT\Parameter\X509CertificateSHA256ThumbprintParameter |
401
|
|
|
*/ |
402
|
1 |
|
public function X509CertificateSHA256Thumbprint(): Parameter\X509CertificateSHA256ThumbprintParameter |
403
|
|
|
{ |
404
|
1 |
|
return self::_checkType($this->get(Parameter\JWTParameter::P_X5TS256), |
405
|
1 |
|
Parameter\X509CertificateSHA256ThumbprintParameter::class); |
406
|
|
|
} |
407
|
|
|
|
408
|
|
|
/** |
409
|
|
|
* Check whether the X.509 URL parameter is present. |
410
|
|
|
*/ |
411
|
1 |
|
public function hasX509URL(): bool |
412
|
|
|
{ |
413
|
1 |
|
return $this->has(Parameter\JWTParameter::P_X5U); |
414
|
|
|
} |
415
|
|
|
|
416
|
|
|
/** |
417
|
|
|
* Get the X.509 URL parameter. |
418
|
|
|
* |
419
|
|
|
* @throws \UnexpectedValueException If the parameter has a wrong class |
420
|
|
|
* @throws \LogicException If the parameter is not present |
421
|
|
|
* |
422
|
|
|
* @return \Sop\JWX\JWT\Parameter\X509URLParameter |
423
|
|
|
*/ |
424
|
1 |
|
public function X509URL(): Parameter\X509URLParameter |
425
|
|
|
{ |
426
|
1 |
|
return self::_checkType($this->get(Parameter\JWTParameter::P_X5U), |
427
|
1 |
|
Parameter\X509URLParameter::class); |
428
|
|
|
} |
429
|
|
|
|
430
|
|
|
/** |
431
|
|
|
* Check that the parameter is an instance of the given class. |
432
|
|
|
* |
433
|
|
|
* @param \Sop\JWX\JWT\Parameter\JWTParameter $param Parameter |
434
|
|
|
* @param string $cls Class name |
435
|
|
|
* |
436
|
|
|
* @throws \UnexpectedValueException |
437
|
|
|
* |
438
|
|
|
* @return \Sop\JWX\JWT\Parameter\JWTParameter |
439
|
|
|
*/ |
440
|
115 |
|
private static function _checkType(Parameter\JWTParameter $param, string $cls): Parameter\JWTParameter |
441
|
|
|
{ |
442
|
115 |
|
if (!$param instanceof $cls) { |
443
|
1 |
|
throw new \UnexpectedValueException( |
444
|
1 |
|
"{$cls} expected, got " . get_class($param)); |
445
|
|
|
} |
446
|
114 |
|
return $param; |
447
|
|
|
} |
448
|
|
|
} |
449
|
|
|
|