|
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\JWTParameter; |
|
9
|
|
|
use JWX\JWT\Parameter\RegisteredJWTParameter; |
|
10
|
|
|
|
|
11
|
|
|
|
|
12
|
|
|
/** |
|
13
|
|
|
* Trait for Header to provide parameter accessor methods for typed return |
|
14
|
|
|
* types. |
|
15
|
|
|
*/ |
|
16
|
|
|
trait TypedHeader |
|
17
|
|
|
{ |
|
18
|
|
|
/** |
|
19
|
|
|
* Whether parameters are present. |
|
20
|
|
|
* |
|
21
|
|
|
* @param string ...$names Parameter names |
|
22
|
|
|
* @return bool |
|
23
|
|
|
*/ |
|
24
|
|
|
abstract public function has(...$names); |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* Get a parameter. |
|
28
|
|
|
* |
|
29
|
|
|
* @param string $name Parameter name |
|
30
|
|
|
* @return JWTParameter |
|
31
|
|
|
*/ |
|
32
|
|
|
abstract public function get($name); |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* Check whether the algorithm parameter is present. |
|
36
|
|
|
* |
|
37
|
|
|
* @return bool |
|
38
|
|
|
*/ |
|
39
|
|
|
public function hasAlgorithm() { |
|
40
|
|
|
return $this->has(RegisteredJWTParameter::P_ALG); |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* Get the algorithm parameter. |
|
45
|
|
|
* |
|
46
|
|
|
* @throws \UnexpectedValueException |
|
47
|
|
|
* @return AlgorithmParameter |
|
48
|
|
|
*/ |
|
49
|
|
|
public function algorithm() { |
|
50
|
|
|
return $this->_checkType($this->get(RegisteredJWTParameter::P_ALG), |
|
51
|
|
|
AlgorithmParameter::class); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* Check whether the authentication tag parameter is present. |
|
56
|
|
|
* |
|
57
|
|
|
* @return bool |
|
58
|
|
|
*/ |
|
59
|
5 |
|
public function hasAuthenticationTag() { |
|
60
|
5 |
|
return $this->has(RegisteredJWTParameter::P_TAG); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* Get the authentication tag parameter. |
|
65
|
|
|
* |
|
66
|
|
|
* @throws \UnexpectedValueException |
|
67
|
|
|
* @return AuthenticationTagParameter |
|
68
|
|
|
*/ |
|
69
|
4 |
|
public function authenticationTag() { |
|
70
|
4 |
|
return $this->_checkType($this->get(RegisteredJWTParameter::P_TAG), |
|
71
|
4 |
|
AuthenticationTagParameter::class); |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* Check whether the 'base64url-encode payload' parameter is present. |
|
76
|
|
|
* |
|
77
|
|
|
* @return bool |
|
78
|
|
|
*/ |
|
79
|
|
|
public function hasB64Payload() { |
|
80
|
|
|
return $this->has(RegisteredJWTParameter::P_B64); |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* Get the 'base64url-encode payload' parameter. |
|
85
|
|
|
* |
|
86
|
|
|
* @throws \UnexpectedValueException |
|
87
|
|
|
* @return B64PayloadParameter |
|
88
|
|
|
*/ |
|
89
|
|
|
public function b64Payload() { |
|
90
|
|
|
return $this->_checkType($this->get(RegisteredJWTParameter::P_B64), |
|
91
|
|
|
B64PayloadParameter::class); |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
/** |
|
95
|
|
|
* |
|
96
|
|
|
* @param JWTParameter $param |
|
97
|
|
|
* @param string $cls |
|
98
|
|
|
* @throws \UnexpectedValueException |
|
99
|
|
|
* @return JWTParameter |
|
100
|
|
|
*/ |
|
101
|
4 |
|
private static function _checkType(JWTParameter $param, $cls) { |
|
102
|
4 |
|
if (!$param instanceof $cls) { |
|
103
|
|
|
throw new \UnexpectedValueException( |
|
104
|
|
|
"$cls expected, got " . get_class($param)); |
|
105
|
|
|
} |
|
106
|
4 |
|
return $param; |
|
107
|
|
|
} |
|
108
|
|
|
} |
|
109
|
|
|
|