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