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