@@ -11,162 +11,162 @@ |
||
11 | 11 | */ |
12 | 12 | class Header implements \Countable, \IteratorAggregate |
13 | 13 | { |
14 | - use TypedHeader; |
|
14 | + use TypedHeader; |
|
15 | 15 | |
16 | - /** |
|
17 | - * Parameters. |
|
18 | - * |
|
19 | - * @var JWTParameter[] |
|
20 | - */ |
|
21 | - protected $_parameters; |
|
16 | + /** |
|
17 | + * Parameters. |
|
18 | + * |
|
19 | + * @var JWTParameter[] |
|
20 | + */ |
|
21 | + protected $_parameters; |
|
22 | 22 | |
23 | - /** |
|
24 | - * Constructor. |
|
25 | - * |
|
26 | - * @param JWTParameter ...$params Parameters |
|
27 | - */ |
|
28 | - public function __construct(JWTParameter ...$params) |
|
29 | - { |
|
30 | - $this->_parameters = []; |
|
31 | - foreach ($params as $param) { |
|
32 | - $this->_parameters[$param->name()] = $param; |
|
33 | - } |
|
34 | - } |
|
23 | + /** |
|
24 | + * Constructor. |
|
25 | + * |
|
26 | + * @param JWTParameter ...$params Parameters |
|
27 | + */ |
|
28 | + public function __construct(JWTParameter ...$params) |
|
29 | + { |
|
30 | + $this->_parameters = []; |
|
31 | + foreach ($params as $param) { |
|
32 | + $this->_parameters[$param->name()] = $param; |
|
33 | + } |
|
34 | + } |
|
35 | 35 | |
36 | - /** |
|
37 | - * Initialize from an array representing a JSON object. |
|
38 | - * |
|
39 | - * @param array $members |
|
40 | - * |
|
41 | - * @return self |
|
42 | - */ |
|
43 | - public static function fromArray(array $members): self |
|
44 | - { |
|
45 | - $params = []; |
|
46 | - foreach ($members as $name => $value) { |
|
47 | - $params[] = JWTParameter::fromNameAndValue($name, $value); |
|
48 | - } |
|
49 | - return new self(...$params); |
|
50 | - } |
|
36 | + /** |
|
37 | + * Initialize from an array representing a JSON object. |
|
38 | + * |
|
39 | + * @param array $members |
|
40 | + * |
|
41 | + * @return self |
|
42 | + */ |
|
43 | + public static function fromArray(array $members): self |
|
44 | + { |
|
45 | + $params = []; |
|
46 | + foreach ($members as $name => $value) { |
|
47 | + $params[] = JWTParameter::fromNameAndValue($name, $value); |
|
48 | + } |
|
49 | + return new self(...$params); |
|
50 | + } |
|
51 | 51 | |
52 | - /** |
|
53 | - * Initialize from a JSON. |
|
54 | - * |
|
55 | - * @param string $json |
|
56 | - * |
|
57 | - * @throws \UnexpectedValueException |
|
58 | - * |
|
59 | - * @return self |
|
60 | - */ |
|
61 | - public static function fromJSON(string $json): self |
|
62 | - { |
|
63 | - $members = json_decode($json, true, 32, JSON_BIGINT_AS_STRING); |
|
64 | - if (!is_array($members)) { |
|
65 | - throw new \UnexpectedValueException('Invalid JSON.'); |
|
66 | - } |
|
67 | - return self::fromArray($members); |
|
68 | - } |
|
52 | + /** |
|
53 | + * Initialize from a JSON. |
|
54 | + * |
|
55 | + * @param string $json |
|
56 | + * |
|
57 | + * @throws \UnexpectedValueException |
|
58 | + * |
|
59 | + * @return self |
|
60 | + */ |
|
61 | + public static function fromJSON(string $json): self |
|
62 | + { |
|
63 | + $members = json_decode($json, true, 32, JSON_BIGINT_AS_STRING); |
|
64 | + if (!is_array($members)) { |
|
65 | + throw new \UnexpectedValueException('Invalid JSON.'); |
|
66 | + } |
|
67 | + return self::fromArray($members); |
|
68 | + } |
|
69 | 69 | |
70 | - /** |
|
71 | - * Get self with parameters added. |
|
72 | - * |
|
73 | - * @param JWTParameter ...$param |
|
74 | - * |
|
75 | - * @return self |
|
76 | - */ |
|
77 | - public function withParameters(JWTParameter ...$params): self |
|
78 | - { |
|
79 | - $obj = clone $this; |
|
80 | - foreach ($params as $param) { |
|
81 | - $obj->_parameters[$param->name()] = $param; |
|
82 | - } |
|
83 | - return $obj; |
|
84 | - } |
|
70 | + /** |
|
71 | + * Get self with parameters added. |
|
72 | + * |
|
73 | + * @param JWTParameter ...$param |
|
74 | + * |
|
75 | + * @return self |
|
76 | + */ |
|
77 | + public function withParameters(JWTParameter ...$params): self |
|
78 | + { |
|
79 | + $obj = clone $this; |
|
80 | + foreach ($params as $param) { |
|
81 | + $obj->_parameters[$param->name()] = $param; |
|
82 | + } |
|
83 | + return $obj; |
|
84 | + } |
|
85 | 85 | |
86 | - /** |
|
87 | - * Get all parameters. |
|
88 | - * |
|
89 | - * @return JWTParameter[] |
|
90 | - */ |
|
91 | - public function parameters(): array |
|
92 | - { |
|
93 | - return $this->_parameters; |
|
94 | - } |
|
86 | + /** |
|
87 | + * Get all parameters. |
|
88 | + * |
|
89 | + * @return JWTParameter[] |
|
90 | + */ |
|
91 | + public function parameters(): array |
|
92 | + { |
|
93 | + return $this->_parameters; |
|
94 | + } |
|
95 | 95 | |
96 | - /** |
|
97 | - * Whether parameters are present. |
|
98 | - * |
|
99 | - * Returns false if any of the given parameters is not set. |
|
100 | - * |
|
101 | - * @param string ...$names Parameter names |
|
102 | - * |
|
103 | - * @return bool |
|
104 | - */ |
|
105 | - public function has(string ...$names): bool |
|
106 | - { |
|
107 | - foreach ($names as $name) { |
|
108 | - if (!isset($this->_parameters[$name])) { |
|
109 | - return false; |
|
110 | - } |
|
111 | - } |
|
112 | - return true; |
|
113 | - } |
|
96 | + /** |
|
97 | + * Whether parameters are present. |
|
98 | + * |
|
99 | + * Returns false if any of the given parameters is not set. |
|
100 | + * |
|
101 | + * @param string ...$names Parameter names |
|
102 | + * |
|
103 | + * @return bool |
|
104 | + */ |
|
105 | + public function has(string ...$names): bool |
|
106 | + { |
|
107 | + foreach ($names as $name) { |
|
108 | + if (!isset($this->_parameters[$name])) { |
|
109 | + return false; |
|
110 | + } |
|
111 | + } |
|
112 | + return true; |
|
113 | + } |
|
114 | 114 | |
115 | - /** |
|
116 | - * Get a parameter. |
|
117 | - * |
|
118 | - * @param string $name Parameter name |
|
119 | - * |
|
120 | - * @throws \LogicException |
|
121 | - * |
|
122 | - * @return JWTParameter |
|
123 | - */ |
|
124 | - public function get(string $name): JWTParameter |
|
125 | - { |
|
126 | - if (!$this->has($name)) { |
|
127 | - throw new \LogicException("Parameter {$name} doesn't exists."); |
|
128 | - } |
|
129 | - return $this->_parameters[$name]; |
|
130 | - } |
|
115 | + /** |
|
116 | + * Get a parameter. |
|
117 | + * |
|
118 | + * @param string $name Parameter name |
|
119 | + * |
|
120 | + * @throws \LogicException |
|
121 | + * |
|
122 | + * @return JWTParameter |
|
123 | + */ |
|
124 | + public function get(string $name): JWTParameter |
|
125 | + { |
|
126 | + if (!$this->has($name)) { |
|
127 | + throw new \LogicException("Parameter {$name} doesn't exists."); |
|
128 | + } |
|
129 | + return $this->_parameters[$name]; |
|
130 | + } |
|
131 | 131 | |
132 | - /** |
|
133 | - * Convert to a JSON. |
|
134 | - * |
|
135 | - * @return string |
|
136 | - */ |
|
137 | - public function toJSON(): string |
|
138 | - { |
|
139 | - if (empty($this->_parameters)) { |
|
140 | - return ''; |
|
141 | - } |
|
142 | - $data = []; |
|
143 | - foreach ($this->_parameters as $param) { |
|
144 | - $data[$param->name()] = $param->value(); |
|
145 | - } |
|
146 | - return json_encode((object) $data, JSON_UNESCAPED_SLASHES); |
|
147 | - } |
|
132 | + /** |
|
133 | + * Convert to a JSON. |
|
134 | + * |
|
135 | + * @return string |
|
136 | + */ |
|
137 | + public function toJSON(): string |
|
138 | + { |
|
139 | + if (empty($this->_parameters)) { |
|
140 | + return ''; |
|
141 | + } |
|
142 | + $data = []; |
|
143 | + foreach ($this->_parameters as $param) { |
|
144 | + $data[$param->name()] = $param->value(); |
|
145 | + } |
|
146 | + return json_encode((object) $data, JSON_UNESCAPED_SLASHES); |
|
147 | + } |
|
148 | 148 | |
149 | - /** |
|
150 | - * Get the number of parameters. |
|
151 | - * |
|
152 | - * @see \Countable::count() |
|
153 | - * |
|
154 | - * @return int |
|
155 | - */ |
|
156 | - public function count(): int |
|
157 | - { |
|
158 | - return count($this->_parameters); |
|
159 | - } |
|
149 | + /** |
|
150 | + * Get the number of parameters. |
|
151 | + * |
|
152 | + * @see \Countable::count() |
|
153 | + * |
|
154 | + * @return int |
|
155 | + */ |
|
156 | + public function count(): int |
|
157 | + { |
|
158 | + return count($this->_parameters); |
|
159 | + } |
|
160 | 160 | |
161 | - /** |
|
162 | - * Get iterator for the parameters. |
|
163 | - * |
|
164 | - * @see \IteratorAggregate::getIterator() |
|
165 | - * |
|
166 | - * @return \ArrayIterator |
|
167 | - */ |
|
168 | - public function getIterator(): \ArrayIterator |
|
169 | - { |
|
170 | - return new \ArrayIterator($this->_parameters); |
|
171 | - } |
|
161 | + /** |
|
162 | + * Get iterator for the parameters. |
|
163 | + * |
|
164 | + * @see \IteratorAggregate::getIterator() |
|
165 | + * |
|
166 | + * @return \ArrayIterator |
|
167 | + */ |
|
168 | + public function getIterator(): \ArrayIterator |
|
169 | + { |
|
170 | + return new \ArrayIterator($this->_parameters); |
|
171 | + } |
|
172 | 172 | } |
@@ -11,10 +11,10 @@ |
||
11 | 11 | */ |
12 | 12 | interface HeaderParameters |
13 | 13 | { |
14 | - /** |
|
15 | - * Get an array of JOSE header parameters representing this object. |
|
16 | - * |
|
17 | - * @return JWTParameter[] |
|
18 | - */ |
|
19 | - public function headerParameters(): array; |
|
14 | + /** |
|
15 | + * Get an array of JOSE header parameters representing this object. |
|
16 | + * |
|
17 | + * @return JWTParameter[] |
|
18 | + */ |
|
19 | + public function headerParameters(): array; |
|
20 | 20 | } |
@@ -13,476 +13,476 @@ |
||
13 | 13 | */ |
14 | 14 | trait TypedHeader |
15 | 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 | - public function hasAlgorithm(): bool |
|
42 | - { |
|
43 | - 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 | - public function algorithm(): Parameter\AlgorithmParameter |
|
55 | - { |
|
56 | - return self::_checkType($this->get(Parameter\JWTParameter::P_ALG), |
|
57 | - Parameter\AlgorithmParameter::class); |
|
58 | - } |
|
59 | - |
|
60 | - /** |
|
61 | - * Check whether the authentication tag parameter is present. |
|
62 | - * |
|
63 | - * @return bool |
|
64 | - */ |
|
65 | - public function hasAuthenticationTag(): bool |
|
66 | - { |
|
67 | - 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 | - public function authenticationTag(): Parameter\AuthenticationTagParameter |
|
79 | - { |
|
80 | - return self::_checkType($this->get(Parameter\JWTParameter::P_TAG), |
|
81 | - Parameter\AuthenticationTagParameter::class); |
|
82 | - } |
|
83 | - |
|
84 | - /** |
|
85 | - * Check whether the 'base64url-encode payload' parameter is present. |
|
86 | - * |
|
87 | - * @return bool |
|
88 | - */ |
|
89 | - public function hasB64Payload(): bool |
|
90 | - { |
|
91 | - 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 | - public function B64Payload(): Parameter\B64PayloadParameter |
|
103 | - { |
|
104 | - return self::_checkType($this->get(Parameter\JWTParameter::P_B64), |
|
105 | - Parameter\B64PayloadParameter::class); |
|
106 | - } |
|
107 | - |
|
108 | - /** |
|
109 | - * Check whether the compression algorithm parameter is present. |
|
110 | - * |
|
111 | - * @return bool |
|
112 | - */ |
|
113 | - public function hasCompressionAlgorithm(): bool |
|
114 | - { |
|
115 | - 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 | - public function compressionAlgorithm(): Parameter\CompressionAlgorithmParameter |
|
127 | - { |
|
128 | - return self::_checkType($this->get(Parameter\JWTParameter::P_ZIP), |
|
129 | - Parameter\CompressionAlgorithmParameter::class); |
|
130 | - } |
|
131 | - |
|
132 | - /** |
|
133 | - * Check whether the content type parameter is present. |
|
134 | - * |
|
135 | - * @return bool |
|
136 | - */ |
|
137 | - public function hasContentType(): bool |
|
138 | - { |
|
139 | - 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 | - public function contentType(): Parameter\ContentTypeParameter |
|
151 | - { |
|
152 | - return self::_checkType($this->get(Parameter\JWTParameter::P_CTY), |
|
153 | - Parameter\ContentTypeParameter::class); |
|
154 | - } |
|
155 | - |
|
156 | - /** |
|
157 | - * Check whether the critical parameter is present. |
|
158 | - * |
|
159 | - * @return bool |
|
160 | - */ |
|
161 | - public function hasCritical(): bool |
|
162 | - { |
|
163 | - 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 | - public function critical(): Parameter\CriticalParameter |
|
175 | - { |
|
176 | - return self::_checkType($this->get(Parameter\JWTParameter::P_CRIT), |
|
177 | - Parameter\CriticalParameter::class); |
|
178 | - } |
|
179 | - |
|
180 | - /** |
|
181 | - * Check whether the encryption algorithm parameter is present. |
|
182 | - * |
|
183 | - * @return bool |
|
184 | - */ |
|
185 | - public function hasEncryptionAlgorithm(): bool |
|
186 | - { |
|
187 | - 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 | - public function encryptionAlgorithm(): Parameter\EncryptionAlgorithmParameter |
|
199 | - { |
|
200 | - return self::_checkType($this->get(Parameter\JWTParameter::P_ENC), |
|
201 | - Parameter\EncryptionAlgorithmParameter::class); |
|
202 | - } |
|
203 | - |
|
204 | - /** |
|
205 | - * Check whether the initialization vector parameter is present. |
|
206 | - * |
|
207 | - * @return bool |
|
208 | - */ |
|
209 | - public function hasInitializationVector(): bool |
|
210 | - { |
|
211 | - 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 | - public function initializationVector(): Parameter\InitializationVectorParameter |
|
223 | - { |
|
224 | - return self::_checkType($this->get(Parameter\JWTParameter::P_IV), |
|
225 | - Parameter\InitializationVectorParameter::class); |
|
226 | - } |
|
227 | - |
|
228 | - /** |
|
229 | - * Check whether the JSON web key parameter is present. |
|
230 | - * |
|
231 | - * @return bool |
|
232 | - */ |
|
233 | - public function hasJSONWebKey(): bool |
|
234 | - { |
|
235 | - 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 | - public function JSONWebKey(): Parameter\JSONWebKeyParameter |
|
247 | - { |
|
248 | - return self::_checkType($this->get(Parameter\JWTParameter::P_JWK), |
|
249 | - Parameter\JSONWebKeyParameter::class); |
|
250 | - } |
|
251 | - |
|
252 | - /** |
|
253 | - * Check whether the JWK set URL parameter is present. |
|
254 | - * |
|
255 | - * @return bool |
|
256 | - */ |
|
257 | - public function hasJWKSetURL(): bool |
|
258 | - { |
|
259 | - 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 | - public function JWKSetURL(): Parameter\JWKSetURLParameter |
|
271 | - { |
|
272 | - return self::_checkType($this->get(Parameter\JWTParameter::P_JKU), |
|
273 | - Parameter\JWKSetURLParameter::class); |
|
274 | - } |
|
275 | - |
|
276 | - /** |
|
277 | - * Check whether the key ID parameter is present. |
|
278 | - * |
|
279 | - * @return bool |
|
280 | - */ |
|
281 | - public function hasKeyID(): bool |
|
282 | - { |
|
283 | - 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 | - public function keyID(): Parameter\KeyIDParameter |
|
295 | - { |
|
296 | - return self::_checkType($this->get(Parameter\JWTParameter::P_KID), |
|
297 | - Parameter\KeyIDParameter::class); |
|
298 | - } |
|
299 | - |
|
300 | - /** |
|
301 | - * Check whether the PBES2 count parameter is present. |
|
302 | - * |
|
303 | - * @return bool |
|
304 | - */ |
|
305 | - public function hasPBES2Count(): bool |
|
306 | - { |
|
307 | - 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 | - public function PBES2Count(): Parameter\PBES2CountParameter |
|
319 | - { |
|
320 | - return self::_checkType($this->get(Parameter\JWTParameter::P_P2C), |
|
321 | - Parameter\PBES2CountParameter::class); |
|
322 | - } |
|
323 | - |
|
324 | - /** |
|
325 | - * Check whether the PBES2 salt input parameter is present. |
|
326 | - * |
|
327 | - * @return bool |
|
328 | - */ |
|
329 | - public function hasPBES2SaltInput(): bool |
|
330 | - { |
|
331 | - 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 | - public function PBES2SaltInput(): Parameter\PBES2SaltInputParameter |
|
343 | - { |
|
344 | - return self::_checkType($this->get(Parameter\JWTParameter::P_P2S), |
|
345 | - Parameter\PBES2SaltInputParameter::class); |
|
346 | - } |
|
347 | - |
|
348 | - /** |
|
349 | - * Check whether the type parameter is present. |
|
350 | - * |
|
351 | - * @return bool |
|
352 | - */ |
|
353 | - public function hasType(): bool |
|
354 | - { |
|
355 | - 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 | - public function type(): Parameter\TypeParameter |
|
367 | - { |
|
368 | - return self::_checkType($this->get(Parameter\JWTParameter::P_TYP), |
|
369 | - Parameter\TypeParameter::class); |
|
370 | - } |
|
371 | - |
|
372 | - /** |
|
373 | - * Check whether the X.509 certificate chain parameter is present. |
|
374 | - * |
|
375 | - * @return bool |
|
376 | - */ |
|
377 | - public function hasX509CertificateChain(): bool |
|
378 | - { |
|
379 | - 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 | - public function X509CertificateChain(): Parameter\X509CertificateChainParameter |
|
391 | - { |
|
392 | - return self::_checkType($this->get(Parameter\JWTParameter::P_X5C), |
|
393 | - 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 | - public function hasX509CertificateSHA1Thumbprint(): bool |
|
403 | - { |
|
404 | - 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 | - public function X509CertificateSHA1Thumbprint(): Parameter\X509CertificateSHA1ThumbprintParameter |
|
416 | - { |
|
417 | - return self::_checkType($this->get(Parameter\JWTParameter::P_X5T), |
|
418 | - 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 | - public function hasX509CertificateSHA256Thumbprint(): bool |
|
428 | - { |
|
429 | - 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 | - public function X509CertificateSHA256Thumbprint(): Parameter\X509CertificateSHA256ThumbprintParameter |
|
441 | - { |
|
442 | - return self::_checkType($this->get(Parameter\JWTParameter::P_X5TS256), |
|
443 | - Parameter\X509CertificateSHA256ThumbprintParameter::class); |
|
444 | - } |
|
445 | - |
|
446 | - /** |
|
447 | - * Check whether the X.509 URL parameter is present. |
|
448 | - * |
|
449 | - * @return bool |
|
450 | - */ |
|
451 | - public function hasX509URL(): bool |
|
452 | - { |
|
453 | - 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 | - public function X509URL(): Parameter\X509URLParameter |
|
465 | - { |
|
466 | - return self::_checkType($this->get(Parameter\JWTParameter::P_X5U), |
|
467 | - 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 | - private static function _checkType(Parameter\JWTParameter $param, string $cls): Parameter\JWTParameter |
|
481 | - { |
|
482 | - if (!$param instanceof $cls) { |
|
483 | - throw new \UnexpectedValueException( |
|
484 | - "{$cls} expected, got " . get_class($param)); |
|
485 | - } |
|
486 | - return $param; |
|
487 | - } |
|
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 | + public function hasAlgorithm(): bool |
|
42 | + { |
|
43 | + 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 | + public function algorithm(): Parameter\AlgorithmParameter |
|
55 | + { |
|
56 | + return self::_checkType($this->get(Parameter\JWTParameter::P_ALG), |
|
57 | + Parameter\AlgorithmParameter::class); |
|
58 | + } |
|
59 | + |
|
60 | + /** |
|
61 | + * Check whether the authentication tag parameter is present. |
|
62 | + * |
|
63 | + * @return bool |
|
64 | + */ |
|
65 | + public function hasAuthenticationTag(): bool |
|
66 | + { |
|
67 | + 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 | + public function authenticationTag(): Parameter\AuthenticationTagParameter |
|
79 | + { |
|
80 | + return self::_checkType($this->get(Parameter\JWTParameter::P_TAG), |
|
81 | + Parameter\AuthenticationTagParameter::class); |
|
82 | + } |
|
83 | + |
|
84 | + /** |
|
85 | + * Check whether the 'base64url-encode payload' parameter is present. |
|
86 | + * |
|
87 | + * @return bool |
|
88 | + */ |
|
89 | + public function hasB64Payload(): bool |
|
90 | + { |
|
91 | + 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 | + public function B64Payload(): Parameter\B64PayloadParameter |
|
103 | + { |
|
104 | + return self::_checkType($this->get(Parameter\JWTParameter::P_B64), |
|
105 | + Parameter\B64PayloadParameter::class); |
|
106 | + } |
|
107 | + |
|
108 | + /** |
|
109 | + * Check whether the compression algorithm parameter is present. |
|
110 | + * |
|
111 | + * @return bool |
|
112 | + */ |
|
113 | + public function hasCompressionAlgorithm(): bool |
|
114 | + { |
|
115 | + 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 | + public function compressionAlgorithm(): Parameter\CompressionAlgorithmParameter |
|
127 | + { |
|
128 | + return self::_checkType($this->get(Parameter\JWTParameter::P_ZIP), |
|
129 | + Parameter\CompressionAlgorithmParameter::class); |
|
130 | + } |
|
131 | + |
|
132 | + /** |
|
133 | + * Check whether the content type parameter is present. |
|
134 | + * |
|
135 | + * @return bool |
|
136 | + */ |
|
137 | + public function hasContentType(): bool |
|
138 | + { |
|
139 | + 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 | + public function contentType(): Parameter\ContentTypeParameter |
|
151 | + { |
|
152 | + return self::_checkType($this->get(Parameter\JWTParameter::P_CTY), |
|
153 | + Parameter\ContentTypeParameter::class); |
|
154 | + } |
|
155 | + |
|
156 | + /** |
|
157 | + * Check whether the critical parameter is present. |
|
158 | + * |
|
159 | + * @return bool |
|
160 | + */ |
|
161 | + public function hasCritical(): bool |
|
162 | + { |
|
163 | + 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 | + public function critical(): Parameter\CriticalParameter |
|
175 | + { |
|
176 | + return self::_checkType($this->get(Parameter\JWTParameter::P_CRIT), |
|
177 | + Parameter\CriticalParameter::class); |
|
178 | + } |
|
179 | + |
|
180 | + /** |
|
181 | + * Check whether the encryption algorithm parameter is present. |
|
182 | + * |
|
183 | + * @return bool |
|
184 | + */ |
|
185 | + public function hasEncryptionAlgorithm(): bool |
|
186 | + { |
|
187 | + 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 | + public function encryptionAlgorithm(): Parameter\EncryptionAlgorithmParameter |
|
199 | + { |
|
200 | + return self::_checkType($this->get(Parameter\JWTParameter::P_ENC), |
|
201 | + Parameter\EncryptionAlgorithmParameter::class); |
|
202 | + } |
|
203 | + |
|
204 | + /** |
|
205 | + * Check whether the initialization vector parameter is present. |
|
206 | + * |
|
207 | + * @return bool |
|
208 | + */ |
|
209 | + public function hasInitializationVector(): bool |
|
210 | + { |
|
211 | + 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 | + public function initializationVector(): Parameter\InitializationVectorParameter |
|
223 | + { |
|
224 | + return self::_checkType($this->get(Parameter\JWTParameter::P_IV), |
|
225 | + Parameter\InitializationVectorParameter::class); |
|
226 | + } |
|
227 | + |
|
228 | + /** |
|
229 | + * Check whether the JSON web key parameter is present. |
|
230 | + * |
|
231 | + * @return bool |
|
232 | + */ |
|
233 | + public function hasJSONWebKey(): bool |
|
234 | + { |
|
235 | + 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 | + public function JSONWebKey(): Parameter\JSONWebKeyParameter |
|
247 | + { |
|
248 | + return self::_checkType($this->get(Parameter\JWTParameter::P_JWK), |
|
249 | + Parameter\JSONWebKeyParameter::class); |
|
250 | + } |
|
251 | + |
|
252 | + /** |
|
253 | + * Check whether the JWK set URL parameter is present. |
|
254 | + * |
|
255 | + * @return bool |
|
256 | + */ |
|
257 | + public function hasJWKSetURL(): bool |
|
258 | + { |
|
259 | + 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 | + public function JWKSetURL(): Parameter\JWKSetURLParameter |
|
271 | + { |
|
272 | + return self::_checkType($this->get(Parameter\JWTParameter::P_JKU), |
|
273 | + Parameter\JWKSetURLParameter::class); |
|
274 | + } |
|
275 | + |
|
276 | + /** |
|
277 | + * Check whether the key ID parameter is present. |
|
278 | + * |
|
279 | + * @return bool |
|
280 | + */ |
|
281 | + public function hasKeyID(): bool |
|
282 | + { |
|
283 | + 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 | + public function keyID(): Parameter\KeyIDParameter |
|
295 | + { |
|
296 | + return self::_checkType($this->get(Parameter\JWTParameter::P_KID), |
|
297 | + Parameter\KeyIDParameter::class); |
|
298 | + } |
|
299 | + |
|
300 | + /** |
|
301 | + * Check whether the PBES2 count parameter is present. |
|
302 | + * |
|
303 | + * @return bool |
|
304 | + */ |
|
305 | + public function hasPBES2Count(): bool |
|
306 | + { |
|
307 | + 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 | + public function PBES2Count(): Parameter\PBES2CountParameter |
|
319 | + { |
|
320 | + return self::_checkType($this->get(Parameter\JWTParameter::P_P2C), |
|
321 | + Parameter\PBES2CountParameter::class); |
|
322 | + } |
|
323 | + |
|
324 | + /** |
|
325 | + * Check whether the PBES2 salt input parameter is present. |
|
326 | + * |
|
327 | + * @return bool |
|
328 | + */ |
|
329 | + public function hasPBES2SaltInput(): bool |
|
330 | + { |
|
331 | + 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 | + public function PBES2SaltInput(): Parameter\PBES2SaltInputParameter |
|
343 | + { |
|
344 | + return self::_checkType($this->get(Parameter\JWTParameter::P_P2S), |
|
345 | + Parameter\PBES2SaltInputParameter::class); |
|
346 | + } |
|
347 | + |
|
348 | + /** |
|
349 | + * Check whether the type parameter is present. |
|
350 | + * |
|
351 | + * @return bool |
|
352 | + */ |
|
353 | + public function hasType(): bool |
|
354 | + { |
|
355 | + 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 | + public function type(): Parameter\TypeParameter |
|
367 | + { |
|
368 | + return self::_checkType($this->get(Parameter\JWTParameter::P_TYP), |
|
369 | + Parameter\TypeParameter::class); |
|
370 | + } |
|
371 | + |
|
372 | + /** |
|
373 | + * Check whether the X.509 certificate chain parameter is present. |
|
374 | + * |
|
375 | + * @return bool |
|
376 | + */ |
|
377 | + public function hasX509CertificateChain(): bool |
|
378 | + { |
|
379 | + 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 | + public function X509CertificateChain(): Parameter\X509CertificateChainParameter |
|
391 | + { |
|
392 | + return self::_checkType($this->get(Parameter\JWTParameter::P_X5C), |
|
393 | + 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 | + public function hasX509CertificateSHA1Thumbprint(): bool |
|
403 | + { |
|
404 | + 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 | + public function X509CertificateSHA1Thumbprint(): Parameter\X509CertificateSHA1ThumbprintParameter |
|
416 | + { |
|
417 | + return self::_checkType($this->get(Parameter\JWTParameter::P_X5T), |
|
418 | + 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 | + public function hasX509CertificateSHA256Thumbprint(): bool |
|
428 | + { |
|
429 | + 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 | + public function X509CertificateSHA256Thumbprint(): Parameter\X509CertificateSHA256ThumbprintParameter |
|
441 | + { |
|
442 | + return self::_checkType($this->get(Parameter\JWTParameter::P_X5TS256), |
|
443 | + Parameter\X509CertificateSHA256ThumbprintParameter::class); |
|
444 | + } |
|
445 | + |
|
446 | + /** |
|
447 | + * Check whether the X.509 URL parameter is present. |
|
448 | + * |
|
449 | + * @return bool |
|
450 | + */ |
|
451 | + public function hasX509URL(): bool |
|
452 | + { |
|
453 | + 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 | + public function X509URL(): Parameter\X509URLParameter |
|
465 | + { |
|
466 | + return self::_checkType($this->get(Parameter\JWTParameter::P_X5U), |
|
467 | + 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 | + private static function _checkType(Parameter\JWTParameter $param, string $cls): Parameter\JWTParameter |
|
481 | + { |
|
482 | + if (!$param instanceof $cls) { |
|
483 | + throw new \UnexpectedValueException( |
|
484 | + "{$cls} expected, got " . get_class($param)); |
|
485 | + } |
|
486 | + return $param; |
|
487 | + } |
|
488 | 488 | } |
@@ -26,326 +26,326 @@ |
||
26 | 26 | */ |
27 | 27 | class ValidationContext |
28 | 28 | { |
29 | - /** |
|
30 | - * Reference time. |
|
31 | - * |
|
32 | - * @var int |
|
33 | - */ |
|
34 | - protected $_refTime; |
|
29 | + /** |
|
30 | + * Reference time. |
|
31 | + * |
|
32 | + * @var int |
|
33 | + */ |
|
34 | + protected $_refTime; |
|
35 | 35 | |
36 | - /** |
|
37 | - * Leeway in seconds for the reference time constraints. |
|
38 | - * |
|
39 | - * @var int |
|
40 | - */ |
|
41 | - protected $_leeway; |
|
36 | + /** |
|
37 | + * Leeway in seconds for the reference time constraints. |
|
38 | + * |
|
39 | + * @var int |
|
40 | + */ |
|
41 | + protected $_leeway; |
|
42 | 42 | |
43 | - /** |
|
44 | - * Validation constraints. |
|
45 | - * |
|
46 | - * @var array |
|
47 | - */ |
|
48 | - protected $_constraints; |
|
43 | + /** |
|
44 | + * Validation constraints. |
|
45 | + * |
|
46 | + * @var array |
|
47 | + */ |
|
48 | + protected $_constraints; |
|
49 | 49 | |
50 | - /** |
|
51 | - * Explicitly defined validators for named claims. |
|
52 | - * |
|
53 | - * @var Validator[] |
|
54 | - */ |
|
55 | - protected $_validators; |
|
50 | + /** |
|
51 | + * Explicitly defined validators for named claims. |
|
52 | + * |
|
53 | + * @var Validator[] |
|
54 | + */ |
|
55 | + protected $_validators; |
|
56 | 56 | |
57 | - /** |
|
58 | - * Set of JSON Web Keys usable for the validation. |
|
59 | - * |
|
60 | - * @var JWKSet |
|
61 | - */ |
|
62 | - protected $_keys; |
|
57 | + /** |
|
58 | + * Set of JSON Web Keys usable for the validation. |
|
59 | + * |
|
60 | + * @var JWKSet |
|
61 | + */ |
|
62 | + protected $_keys; |
|
63 | 63 | |
64 | - /** |
|
65 | - * Whether to allow unsecured JWT's, that is, claims without integrity |
|
66 | - * protection nor encryption. |
|
67 | - * |
|
68 | - * @var bool |
|
69 | - */ |
|
70 | - protected $_allowUnsecured; |
|
64 | + /** |
|
65 | + * Whether to allow unsecured JWT's, that is, claims without integrity |
|
66 | + * protection nor encryption. |
|
67 | + * |
|
68 | + * @var bool |
|
69 | + */ |
|
70 | + protected $_allowUnsecured; |
|
71 | 71 | |
72 | - /** |
|
73 | - * Constructor. |
|
74 | - * |
|
75 | - * @param null|array $constraints Optional array of constraints for the |
|
76 | - * registered claims |
|
77 | - * @param null|JWKSet $keys Optional set of JSON Web Keys used for |
|
78 | - * signature validation and/or decryption |
|
79 | - */ |
|
80 | - public function __construct(?array $constraints = null, ?JWKSet $keys = null) |
|
81 | - { |
|
82 | - $this->_refTime = time(); |
|
83 | - $this->_leeway = 60; |
|
84 | - $this->_constraints = $constraints ? $constraints : []; |
|
85 | - $this->_validators = []; |
|
86 | - $this->_keys = $keys ? $keys : new JWKSet(); |
|
87 | - $this->_allowUnsecured = false; |
|
88 | - } |
|
72 | + /** |
|
73 | + * Constructor. |
|
74 | + * |
|
75 | + * @param null|array $constraints Optional array of constraints for the |
|
76 | + * registered claims |
|
77 | + * @param null|JWKSet $keys Optional set of JSON Web Keys used for |
|
78 | + * signature validation and/or decryption |
|
79 | + */ |
|
80 | + public function __construct(?array $constraints = null, ?JWKSet $keys = null) |
|
81 | + { |
|
82 | + $this->_refTime = time(); |
|
83 | + $this->_leeway = 60; |
|
84 | + $this->_constraints = $constraints ? $constraints : []; |
|
85 | + $this->_validators = []; |
|
86 | + $this->_keys = $keys ? $keys : new JWKSet(); |
|
87 | + $this->_allowUnsecured = false; |
|
88 | + } |
|
89 | 89 | |
90 | - /** |
|
91 | - * Initialize with a single JSON Web Key. |
|
92 | - * |
|
93 | - * @param JWK $key JSON Web Key |
|
94 | - * @param null|array $constraints Optional constraints |
|
95 | - * |
|
96 | - * @return self |
|
97 | - */ |
|
98 | - public static function fromJWK(JWK $key, ?array $constraints = null): self |
|
99 | - { |
|
100 | - return new self($constraints, new JWKSet($key)); |
|
101 | - } |
|
90 | + /** |
|
91 | + * Initialize with a single JSON Web Key. |
|
92 | + * |
|
93 | + * @param JWK $key JSON Web Key |
|
94 | + * @param null|array $constraints Optional constraints |
|
95 | + * |
|
96 | + * @return self |
|
97 | + */ |
|
98 | + public static function fromJWK(JWK $key, ?array $constraints = null): self |
|
99 | + { |
|
100 | + return new self($constraints, new JWKSet($key)); |
|
101 | + } |
|
102 | 102 | |
103 | - /** |
|
104 | - * Get self with the reference time. |
|
105 | - * |
|
106 | - * @param null|int $ts Unix timestamp |
|
107 | - * |
|
108 | - * @return self |
|
109 | - */ |
|
110 | - public function withReferenceTime(?int $ts): self |
|
111 | - { |
|
112 | - $obj = clone $this; |
|
113 | - $obj->_refTime = $ts; |
|
114 | - return $obj; |
|
115 | - } |
|
103 | + /** |
|
104 | + * Get self with the reference time. |
|
105 | + * |
|
106 | + * @param null|int $ts Unix timestamp |
|
107 | + * |
|
108 | + * @return self |
|
109 | + */ |
|
110 | + public function withReferenceTime(?int $ts): self |
|
111 | + { |
|
112 | + $obj = clone $this; |
|
113 | + $obj->_refTime = $ts; |
|
114 | + return $obj; |
|
115 | + } |
|
116 | 116 | |
117 | - /** |
|
118 | - * Check whether the reference time is set. |
|
119 | - * |
|
120 | - * @return bool |
|
121 | - */ |
|
122 | - public function hasReferenceTime(): bool |
|
123 | - { |
|
124 | - return isset($this->_refTime); |
|
125 | - } |
|
117 | + /** |
|
118 | + * Check whether the reference time is set. |
|
119 | + * |
|
120 | + * @return bool |
|
121 | + */ |
|
122 | + public function hasReferenceTime(): bool |
|
123 | + { |
|
124 | + return isset($this->_refTime); |
|
125 | + } |
|
126 | 126 | |
127 | - /** |
|
128 | - * Get the reference time. |
|
129 | - * |
|
130 | - * @throws \LogicException |
|
131 | - * |
|
132 | - * @return int |
|
133 | - */ |
|
134 | - public function referenceTime(): int |
|
135 | - { |
|
136 | - if (!$this->hasReferenceTime()) { |
|
137 | - throw new \LogicException('Reference time not set.'); |
|
138 | - } |
|
139 | - return $this->_refTime; |
|
140 | - } |
|
127 | + /** |
|
128 | + * Get the reference time. |
|
129 | + * |
|
130 | + * @throws \LogicException |
|
131 | + * |
|
132 | + * @return int |
|
133 | + */ |
|
134 | + public function referenceTime(): int |
|
135 | + { |
|
136 | + if (!$this->hasReferenceTime()) { |
|
137 | + throw new \LogicException('Reference time not set.'); |
|
138 | + } |
|
139 | + return $this->_refTime; |
|
140 | + } |
|
141 | 141 | |
142 | - /** |
|
143 | - * Get self with the reference time leeway. |
|
144 | - * |
|
145 | - * @param int $seconds |
|
146 | - * |
|
147 | - * @return self |
|
148 | - */ |
|
149 | - public function withLeeway(int $seconds): self |
|
150 | - { |
|
151 | - $obj = clone $this; |
|
152 | - $obj->_leeway = $seconds; |
|
153 | - return $obj; |
|
154 | - } |
|
142 | + /** |
|
143 | + * Get self with the reference time leeway. |
|
144 | + * |
|
145 | + * @param int $seconds |
|
146 | + * |
|
147 | + * @return self |
|
148 | + */ |
|
149 | + public function withLeeway(int $seconds): self |
|
150 | + { |
|
151 | + $obj = clone $this; |
|
152 | + $obj->_leeway = $seconds; |
|
153 | + return $obj; |
|
154 | + } |
|
155 | 155 | |
156 | - /** |
|
157 | - * Get the reference time leeway. |
|
158 | - * |
|
159 | - * @return int |
|
160 | - */ |
|
161 | - public function leeway(): int |
|
162 | - { |
|
163 | - return $this->_leeway; |
|
164 | - } |
|
156 | + /** |
|
157 | + * Get the reference time leeway. |
|
158 | + * |
|
159 | + * @return int |
|
160 | + */ |
|
161 | + public function leeway(): int |
|
162 | + { |
|
163 | + return $this->_leeway; |
|
164 | + } |
|
165 | 165 | |
166 | - /** |
|
167 | - * Get self with a validation constraint. |
|
168 | - * |
|
169 | - * If the claim does not provide its own validator, an explicit validator |
|
170 | - * must be given. |
|
171 | - * |
|
172 | - * @param string $name Claim name |
|
173 | - * @param mixed $constraint Value to check claim against |
|
174 | - * @param null|Validator $validator Optional explicit validator |
|
175 | - * |
|
176 | - * @return self |
|
177 | - */ |
|
178 | - public function withConstraint(string $name, $constraint, |
|
179 | - ?Validator $validator = null): self |
|
180 | - { |
|
181 | - $obj = clone $this; |
|
182 | - $obj->_constraints[$name] = $constraint; |
|
183 | - if ($validator) { |
|
184 | - $obj->_validators[$name] = $validator; |
|
185 | - } |
|
186 | - return $obj; |
|
187 | - } |
|
166 | + /** |
|
167 | + * Get self with a validation constraint. |
|
168 | + * |
|
169 | + * If the claim does not provide its own validator, an explicit validator |
|
170 | + * must be given. |
|
171 | + * |
|
172 | + * @param string $name Claim name |
|
173 | + * @param mixed $constraint Value to check claim against |
|
174 | + * @param null|Validator $validator Optional explicit validator |
|
175 | + * |
|
176 | + * @return self |
|
177 | + */ |
|
178 | + public function withConstraint(string $name, $constraint, |
|
179 | + ?Validator $validator = null): self |
|
180 | + { |
|
181 | + $obj = clone $this; |
|
182 | + $obj->_constraints[$name] = $constraint; |
|
183 | + if ($validator) { |
|
184 | + $obj->_validators[$name] = $validator; |
|
185 | + } |
|
186 | + return $obj; |
|
187 | + } |
|
188 | 188 | |
189 | - /** |
|
190 | - * Get self with the issuer constraint. |
|
191 | - * |
|
192 | - * @param string $issuer Issuer name |
|
193 | - * |
|
194 | - * @return self |
|
195 | - */ |
|
196 | - public function withIssuer(string $issuer): self |
|
197 | - { |
|
198 | - return $this->withConstraint(RegisteredClaim::NAME_ISSUER, $issuer); |
|
199 | - } |
|
189 | + /** |
|
190 | + * Get self with the issuer constraint. |
|
191 | + * |
|
192 | + * @param string $issuer Issuer name |
|
193 | + * |
|
194 | + * @return self |
|
195 | + */ |
|
196 | + public function withIssuer(string $issuer): self |
|
197 | + { |
|
198 | + return $this->withConstraint(RegisteredClaim::NAME_ISSUER, $issuer); |
|
199 | + } |
|
200 | 200 | |
201 | - /** |
|
202 | - * Get self with the subject constraint. |
|
203 | - * |
|
204 | - * @param string $subject Subject name |
|
205 | - * |
|
206 | - * @return self |
|
207 | - */ |
|
208 | - public function withSubject(string $subject): self |
|
209 | - { |
|
210 | - return $this->withConstraint(RegisteredClaim::NAME_SUBJECT, $subject); |
|
211 | - } |
|
201 | + /** |
|
202 | + * Get self with the subject constraint. |
|
203 | + * |
|
204 | + * @param string $subject Subject name |
|
205 | + * |
|
206 | + * @return self |
|
207 | + */ |
|
208 | + public function withSubject(string $subject): self |
|
209 | + { |
|
210 | + return $this->withConstraint(RegisteredClaim::NAME_SUBJECT, $subject); |
|
211 | + } |
|
212 | 212 | |
213 | - /** |
|
214 | - * Get self with the audience constraint. |
|
215 | - * |
|
216 | - * @param string $audience Audience name |
|
217 | - * |
|
218 | - * @return self |
|
219 | - */ |
|
220 | - public function withAudience(string $audience): self |
|
221 | - { |
|
222 | - return $this->withConstraint(RegisteredClaim::NAME_AUDIENCE, $audience); |
|
223 | - } |
|
213 | + /** |
|
214 | + * Get self with the audience constraint. |
|
215 | + * |
|
216 | + * @param string $audience Audience name |
|
217 | + * |
|
218 | + * @return self |
|
219 | + */ |
|
220 | + public function withAudience(string $audience): self |
|
221 | + { |
|
222 | + return $this->withConstraint(RegisteredClaim::NAME_AUDIENCE, $audience); |
|
223 | + } |
|
224 | 224 | |
225 | - /** |
|
226 | - * Get self with the JWT ID constraint. |
|
227 | - * |
|
228 | - * @param string $id JWT ID |
|
229 | - * |
|
230 | - * @return self |
|
231 | - */ |
|
232 | - public function withID(string $id): self |
|
233 | - { |
|
234 | - return $this->withConstraint(RegisteredClaim::NAME_JWT_ID, $id); |
|
235 | - } |
|
225 | + /** |
|
226 | + * Get self with the JWT ID constraint. |
|
227 | + * |
|
228 | + * @param string $id JWT ID |
|
229 | + * |
|
230 | + * @return self |
|
231 | + */ |
|
232 | + public function withID(string $id): self |
|
233 | + { |
|
234 | + return $this->withConstraint(RegisteredClaim::NAME_JWT_ID, $id); |
|
235 | + } |
|
236 | 236 | |
237 | - /** |
|
238 | - * Check whether a named constraint is present. |
|
239 | - * |
|
240 | - * @param string $name Claim name |
|
241 | - * |
|
242 | - * @return bool |
|
243 | - */ |
|
244 | - public function hasConstraint(string $name): bool |
|
245 | - { |
|
246 | - return isset($this->_constraints[$name]); |
|
247 | - } |
|
237 | + /** |
|
238 | + * Check whether a named constraint is present. |
|
239 | + * |
|
240 | + * @param string $name Claim name |
|
241 | + * |
|
242 | + * @return bool |
|
243 | + */ |
|
244 | + public function hasConstraint(string $name): bool |
|
245 | + { |
|
246 | + return isset($this->_constraints[$name]); |
|
247 | + } |
|
248 | 248 | |
249 | - /** |
|
250 | - * Get a constraint value by the claim name. |
|
251 | - * |
|
252 | - * @param string $name Claim name |
|
253 | - * |
|
254 | - * @throws \LogicException If constraint is not set |
|
255 | - * |
|
256 | - * @return mixed Constraint value |
|
257 | - */ |
|
258 | - public function constraint(string $name) |
|
259 | - { |
|
260 | - if (!$this->hasConstraint($name)) { |
|
261 | - throw new \LogicException("Constraint {$name} not set."); |
|
262 | - } |
|
263 | - return $this->_constraints[$name]; |
|
264 | - } |
|
249 | + /** |
|
250 | + * Get a constraint value by the claim name. |
|
251 | + * |
|
252 | + * @param string $name Claim name |
|
253 | + * |
|
254 | + * @throws \LogicException If constraint is not set |
|
255 | + * |
|
256 | + * @return mixed Constraint value |
|
257 | + */ |
|
258 | + public function constraint(string $name) |
|
259 | + { |
|
260 | + if (!$this->hasConstraint($name)) { |
|
261 | + throw new \LogicException("Constraint {$name} not set."); |
|
262 | + } |
|
263 | + return $this->_constraints[$name]; |
|
264 | + } |
|
265 | 265 | |
266 | - /** |
|
267 | - * Check whether a validator is defined for the given claim name. |
|
268 | - * |
|
269 | - * @param string $name Claim name |
|
270 | - * |
|
271 | - * @return bool |
|
272 | - */ |
|
273 | - public function hasValidator(string $name): bool |
|
274 | - { |
|
275 | - return isset($this->_validators[$name]); |
|
276 | - } |
|
266 | + /** |
|
267 | + * Check whether a validator is defined for the given claim name. |
|
268 | + * |
|
269 | + * @param string $name Claim name |
|
270 | + * |
|
271 | + * @return bool |
|
272 | + */ |
|
273 | + public function hasValidator(string $name): bool |
|
274 | + { |
|
275 | + return isset($this->_validators[$name]); |
|
276 | + } |
|
277 | 277 | |
278 | - /** |
|
279 | - * Get explicitly defined validator by the claim name. |
|
280 | - * |
|
281 | - * @param string $name Claim name |
|
282 | - * |
|
283 | - * @throws \LogicException If validator is not set |
|
284 | - * |
|
285 | - * @return Validator |
|
286 | - */ |
|
287 | - public function validator(string $name): Validator |
|
288 | - { |
|
289 | - if (!$this->hasValidator($name)) { |
|
290 | - throw new \LogicException("Validator {$name} not set."); |
|
291 | - } |
|
292 | - return $this->_validators[$name]; |
|
293 | - } |
|
278 | + /** |
|
279 | + * Get explicitly defined validator by the claim name. |
|
280 | + * |
|
281 | + * @param string $name Claim name |
|
282 | + * |
|
283 | + * @throws \LogicException If validator is not set |
|
284 | + * |
|
285 | + * @return Validator |
|
286 | + */ |
|
287 | + public function validator(string $name): Validator |
|
288 | + { |
|
289 | + if (!$this->hasValidator($name)) { |
|
290 | + throw new \LogicException("Validator {$name} not set."); |
|
291 | + } |
|
292 | + return $this->_validators[$name]; |
|
293 | + } |
|
294 | 294 | |
295 | - /** |
|
296 | - * Get a set of JSON Web Keys defined in this context. |
|
297 | - * |
|
298 | - * @return JWKSet |
|
299 | - */ |
|
300 | - public function keys(): JWKSet |
|
301 | - { |
|
302 | - return $this->_keys; |
|
303 | - } |
|
295 | + /** |
|
296 | + * Get a set of JSON Web Keys defined in this context. |
|
297 | + * |
|
298 | + * @return JWKSet |
|
299 | + */ |
|
300 | + public function keys(): JWKSet |
|
301 | + { |
|
302 | + return $this->_keys; |
|
303 | + } |
|
304 | 304 | |
305 | - /** |
|
306 | - * Get self with 'allow unsecured' flag set. |
|
307 | - * |
|
308 | - * If the unsecured JWT's are allowed, claims shall be considered valid even |
|
309 | - * though they are not signed nor encrypted. |
|
310 | - * |
|
311 | - * @param bool $allow Whether to allow unsecured JWT's |
|
312 | - * |
|
313 | - * @return self |
|
314 | - */ |
|
315 | - public function withUnsecuredAllowed(bool $allow): self |
|
316 | - { |
|
317 | - $obj = clone $this; |
|
318 | - $obj->_allowUnsecured = $allow; |
|
319 | - return $obj; |
|
320 | - } |
|
305 | + /** |
|
306 | + * Get self with 'allow unsecured' flag set. |
|
307 | + * |
|
308 | + * If the unsecured JWT's are allowed, claims shall be considered valid even |
|
309 | + * though they are not signed nor encrypted. |
|
310 | + * |
|
311 | + * @param bool $allow Whether to allow unsecured JWT's |
|
312 | + * |
|
313 | + * @return self |
|
314 | + */ |
|
315 | + public function withUnsecuredAllowed(bool $allow): self |
|
316 | + { |
|
317 | + $obj = clone $this; |
|
318 | + $obj->_allowUnsecured = $allow; |
|
319 | + return $obj; |
|
320 | + } |
|
321 | 321 | |
322 | - /** |
|
323 | - * Check whether the unsecured JWT's are allowed. |
|
324 | - * |
|
325 | - * @return bool |
|
326 | - */ |
|
327 | - public function isUnsecuredAllowed(): bool |
|
328 | - { |
|
329 | - return $this->_allowUnsecured; |
|
330 | - } |
|
322 | + /** |
|
323 | + * Check whether the unsecured JWT's are allowed. |
|
324 | + * |
|
325 | + * @return bool |
|
326 | + */ |
|
327 | + public function isUnsecuredAllowed(): bool |
|
328 | + { |
|
329 | + return $this->_allowUnsecured; |
|
330 | + } |
|
331 | 331 | |
332 | - /** |
|
333 | - * Validate claims. |
|
334 | - * |
|
335 | - * @param Claims $claims |
|
336 | - * |
|
337 | - * @throws ValidationException If any of the claims is not valid |
|
338 | - * |
|
339 | - * @return self |
|
340 | - */ |
|
341 | - public function validate(Claims $claims): self |
|
342 | - { |
|
343 | - foreach ($claims as $claim) { |
|
344 | - if (!$claim->validateWithContext($this)) { |
|
345 | - throw new ValidationException( |
|
346 | - "Validation of claim '" . $claim->name() . "' failed."); |
|
347 | - } |
|
348 | - } |
|
349 | - return $this; |
|
350 | - } |
|
332 | + /** |
|
333 | + * Validate claims. |
|
334 | + * |
|
335 | + * @param Claims $claims |
|
336 | + * |
|
337 | + * @throws ValidationException If any of the claims is not valid |
|
338 | + * |
|
339 | + * @return self |
|
340 | + */ |
|
341 | + public function validate(Claims $claims): self |
|
342 | + { |
|
343 | + foreach ($claims as $claim) { |
|
344 | + if (!$claim->validateWithContext($this)) { |
|
345 | + throw new ValidationException( |
|
346 | + "Validation of claim '" . $claim->name() . "' failed."); |
|
347 | + } |
|
348 | + } |
|
349 | + return $this; |
|
350 | + } |
|
351 | 351 | } |
@@ -12,54 +12,54 @@ |
||
12 | 12 | */ |
13 | 13 | trait Base64URLValue |
14 | 14 | { |
15 | - use StringParameterValue; |
|
15 | + use StringParameterValue; |
|
16 | 16 | |
17 | - /** |
|
18 | - * Get the parameter value. |
|
19 | - * |
|
20 | - * @return string |
|
21 | - */ |
|
22 | - abstract public function value(); |
|
17 | + /** |
|
18 | + * Get the parameter value. |
|
19 | + * |
|
20 | + * @return string |
|
21 | + */ |
|
22 | + abstract public function value(); |
|
23 | 23 | |
24 | - /** |
|
25 | - * Initialize from native value. |
|
26 | - * |
|
27 | - * Value shall be encoded using Base64url encoding. |
|
28 | - * |
|
29 | - * @param string $value |
|
30 | - * |
|
31 | - * @return self |
|
32 | - */ |
|
33 | - public static function fromString(string $value): Parameter |
|
34 | - { |
|
35 | - return new static(Base64::urlEncode($value)); |
|
36 | - } |
|
24 | + /** |
|
25 | + * Initialize from native value. |
|
26 | + * |
|
27 | + * Value shall be encoded using Base64url encoding. |
|
28 | + * |
|
29 | + * @param string $value |
|
30 | + * |
|
31 | + * @return self |
|
32 | + */ |
|
33 | + public static function fromString(string $value): Parameter |
|
34 | + { |
|
35 | + return new static(Base64::urlEncode($value)); |
|
36 | + } |
|
37 | 37 | |
38 | - /** |
|
39 | - * Get the parameter value as a decoded string. |
|
40 | - * |
|
41 | - * @return string |
|
42 | - */ |
|
43 | - public function string(): string |
|
44 | - { |
|
45 | - return Base64::urlDecode($this->value()); |
|
46 | - } |
|
38 | + /** |
|
39 | + * Get the parameter value as a decoded string. |
|
40 | + * |
|
41 | + * @return string |
|
42 | + */ |
|
43 | + public function string(): string |
|
44 | + { |
|
45 | + return Base64::urlDecode($this->value()); |
|
46 | + } |
|
47 | 47 | |
48 | - /** |
|
49 | - * Validate that value is validly base64url encoded. |
|
50 | - * |
|
51 | - * @param string $value |
|
52 | - * |
|
53 | - * @throws \UnexpectedValueException |
|
54 | - * |
|
55 | - * @return self |
|
56 | - */ |
|
57 | - protected function _validateEncoding(string $value) |
|
58 | - { |
|
59 | - if (!Base64::isValidURLEncoding($value)) { |
|
60 | - throw new \UnexpectedValueException( |
|
61 | - 'Value must be base64url encoded.'); |
|
62 | - } |
|
63 | - return $this; |
|
64 | - } |
|
48 | + /** |
|
49 | + * Validate that value is validly base64url encoded. |
|
50 | + * |
|
51 | + * @param string $value |
|
52 | + * |
|
53 | + * @throws \UnexpectedValueException |
|
54 | + * |
|
55 | + * @return self |
|
56 | + */ |
|
57 | + protected function _validateEncoding(string $value) |
|
58 | + { |
|
59 | + if (!Base64::isValidURLEncoding($value)) { |
|
60 | + throw new \UnexpectedValueException( |
|
61 | + 'Value must be base64url encoded.'); |
|
62 | + } |
|
63 | + return $this; |
|
64 | + } |
|
65 | 65 | } |
@@ -11,26 +11,26 @@ |
||
11 | 11 | */ |
12 | 12 | trait ArrayParameterValue |
13 | 13 | { |
14 | - /** |
|
15 | - * Constructor. |
|
16 | - * |
|
17 | - * @param mixed ...$values |
|
18 | - */ |
|
19 | - abstract public function __construct(...$values); |
|
14 | + /** |
|
15 | + * Constructor. |
|
16 | + * |
|
17 | + * @param mixed ...$values |
|
18 | + */ |
|
19 | + abstract public function __construct(...$values); |
|
20 | 20 | |
21 | - /** |
|
22 | - * Initialize from a JSON value. |
|
23 | - * |
|
24 | - * @param array $value |
|
25 | - * |
|
26 | - * @return static |
|
27 | - */ |
|
28 | - public static function fromJSONValue($value): Parameter |
|
29 | - { |
|
30 | - if (!is_array($value)) { |
|
31 | - throw new \UnexpectedValueException( |
|
32 | - get_called_class() . ' expects an array parameter.'); |
|
33 | - } |
|
34 | - return new static(...$value); |
|
35 | - } |
|
21 | + /** |
|
22 | + * Initialize from a JSON value. |
|
23 | + * |
|
24 | + * @param array $value |
|
25 | + * |
|
26 | + * @return static |
|
27 | + */ |
|
28 | + public static function fromJSONValue($value): Parameter |
|
29 | + { |
|
30 | + if (!is_array($value)) { |
|
31 | + throw new \UnexpectedValueException( |
|
32 | + get_called_class() . ' expects an array parameter.'); |
|
33 | + } |
|
34 | + return new static(...$value); |
|
35 | + } |
|
36 | 36 | } |
@@ -11,22 +11,22 @@ |
||
11 | 11 | */ |
12 | 12 | trait StringParameterValue |
13 | 13 | { |
14 | - /** |
|
15 | - * Constructor. |
|
16 | - * |
|
17 | - * @param string $value Parameter value |
|
18 | - */ |
|
19 | - abstract public function __construct(string $value); |
|
14 | + /** |
|
15 | + * Constructor. |
|
16 | + * |
|
17 | + * @param string $value Parameter value |
|
18 | + */ |
|
19 | + abstract public function __construct(string $value); |
|
20 | 20 | |
21 | - /** |
|
22 | - * Initialize from a JSON value. |
|
23 | - * |
|
24 | - * @param string $value |
|
25 | - * |
|
26 | - * @return static |
|
27 | - */ |
|
28 | - public static function fromJSONValue($value): Parameter |
|
29 | - { |
|
30 | - return new static(strval($value)); |
|
31 | - } |
|
21 | + /** |
|
22 | + * Initialize from a JSON value. |
|
23 | + * |
|
24 | + * @param string $value |
|
25 | + * |
|
26 | + * @return static |
|
27 | + */ |
|
28 | + public static function fromJSONValue($value): Parameter |
|
29 | + { |
|
30 | + return new static(strval($value)); |
|
31 | + } |
|
32 | 32 | } |
@@ -9,37 +9,37 @@ |
||
9 | 9 | */ |
10 | 10 | abstract class Parameter |
11 | 11 | { |
12 | - /** |
|
13 | - * Parameter name. |
|
14 | - * |
|
15 | - * @var string |
|
16 | - */ |
|
17 | - protected $_name; |
|
12 | + /** |
|
13 | + * Parameter name. |
|
14 | + * |
|
15 | + * @var string |
|
16 | + */ |
|
17 | + protected $_name; |
|
18 | 18 | |
19 | - /** |
|
20 | - * Parameter value. |
|
21 | - * |
|
22 | - * @var mixed |
|
23 | - */ |
|
24 | - protected $_value; |
|
19 | + /** |
|
20 | + * Parameter value. |
|
21 | + * |
|
22 | + * @var mixed |
|
23 | + */ |
|
24 | + protected $_value; |
|
25 | 25 | |
26 | - /** |
|
27 | - * Get the parameter name. |
|
28 | - * |
|
29 | - * @return string |
|
30 | - */ |
|
31 | - public function name(): string |
|
32 | - { |
|
33 | - return $this->_name; |
|
34 | - } |
|
26 | + /** |
|
27 | + * Get the parameter name. |
|
28 | + * |
|
29 | + * @return string |
|
30 | + */ |
|
31 | + public function name(): string |
|
32 | + { |
|
33 | + return $this->_name; |
|
34 | + } |
|
35 | 35 | |
36 | - /** |
|
37 | - * Get the parameter value. |
|
38 | - * |
|
39 | - * @return mixed |
|
40 | - */ |
|
41 | - public function value() |
|
42 | - { |
|
43 | - return $this->_value; |
|
44 | - } |
|
36 | + /** |
|
37 | + * Get the parameter value. |
|
38 | + * |
|
39 | + * @return mixed |
|
40 | + */ |
|
41 | + public function value() |
|
42 | + { |
|
43 | + return $this->_value; |
|
44 | + } |
|
45 | 45 | } |
@@ -17,61 +17,61 @@ |
||
17 | 17 | */ |
18 | 18 | class SymmetricKeyJWK extends JWK |
19 | 19 | { |
20 | - /** |
|
21 | - * Parameter names managed by this class. |
|
22 | - * |
|
23 | - * @internal |
|
24 | - * |
|
25 | - * @var string[] |
|
26 | - */ |
|
27 | - const MANAGED_PARAMS = [ |
|
28 | - JWKParameter::PARAM_KEY_TYPE, |
|
29 | - JWKParameter::PARAM_KEY_VALUE, |
|
30 | - ]; |
|
20 | + /** |
|
21 | + * Parameter names managed by this class. |
|
22 | + * |
|
23 | + * @internal |
|
24 | + * |
|
25 | + * @var string[] |
|
26 | + */ |
|
27 | + const MANAGED_PARAMS = [ |
|
28 | + JWKParameter::PARAM_KEY_TYPE, |
|
29 | + JWKParameter::PARAM_KEY_VALUE, |
|
30 | + ]; |
|
31 | 31 | |
32 | - /** |
|
33 | - * Constructor. |
|
34 | - * |
|
35 | - * @param JWKParameter ...$params |
|
36 | - * |
|
37 | - * @throws \UnexpectedValueException If missing required parameter |
|
38 | - */ |
|
39 | - public function __construct(JWKParameter ...$params) |
|
40 | - { |
|
41 | - parent::__construct(...$params); |
|
42 | - foreach (self::MANAGED_PARAMS as $name) { |
|
43 | - if (!$this->has($name)) { |
|
44 | - throw new \UnexpectedValueException( |
|
45 | - "Missing '{$name}' parameter."); |
|
46 | - } |
|
47 | - } |
|
48 | - if (KeyTypeParameter::TYPE_OCT !== $this->keyTypeParameter()->value()) { |
|
49 | - throw new \UnexpectedValueException('Invalid key type.'); |
|
50 | - } |
|
51 | - } |
|
32 | + /** |
|
33 | + * Constructor. |
|
34 | + * |
|
35 | + * @param JWKParameter ...$params |
|
36 | + * |
|
37 | + * @throws \UnexpectedValueException If missing required parameter |
|
38 | + */ |
|
39 | + public function __construct(JWKParameter ...$params) |
|
40 | + { |
|
41 | + parent::__construct(...$params); |
|
42 | + foreach (self::MANAGED_PARAMS as $name) { |
|
43 | + if (!$this->has($name)) { |
|
44 | + throw new \UnexpectedValueException( |
|
45 | + "Missing '{$name}' parameter."); |
|
46 | + } |
|
47 | + } |
|
48 | + if (KeyTypeParameter::TYPE_OCT !== $this->keyTypeParameter()->value()) { |
|
49 | + throw new \UnexpectedValueException('Invalid key type.'); |
|
50 | + } |
|
51 | + } |
|
52 | 52 | |
53 | - /** |
|
54 | - * Initialize from a key string. |
|
55 | - * |
|
56 | - * @param string $key Symmetric key |
|
57 | - * @param JWKParameter ...$params Optional additional parameters |
|
58 | - * |
|
59 | - * @return self |
|
60 | - */ |
|
61 | - public static function fromKey(string $key, JWKParameter ...$params): self |
|
62 | - { |
|
63 | - $params[] = new KeyTypeParameter(KeyTypeParameter::TYPE_OCT); |
|
64 | - $params[] = KeyValueParameter::fromString($key); |
|
65 | - return new self(...$params); |
|
66 | - } |
|
53 | + /** |
|
54 | + * Initialize from a key string. |
|
55 | + * |
|
56 | + * @param string $key Symmetric key |
|
57 | + * @param JWKParameter ...$params Optional additional parameters |
|
58 | + * |
|
59 | + * @return self |
|
60 | + */ |
|
61 | + public static function fromKey(string $key, JWKParameter ...$params): self |
|
62 | + { |
|
63 | + $params[] = new KeyTypeParameter(KeyTypeParameter::TYPE_OCT); |
|
64 | + $params[] = KeyValueParameter::fromString($key); |
|
65 | + return new self(...$params); |
|
66 | + } |
|
67 | 67 | |
68 | - /** |
|
69 | - * Get the symmetric key. |
|
70 | - * |
|
71 | - * @return string |
|
72 | - */ |
|
73 | - public function key(): string |
|
74 | - { |
|
75 | - return Base64::urlDecode($this->keyValueParameter()->value()); |
|
76 | - } |
|
68 | + /** |
|
69 | + * Get the symmetric key. |
|
70 | + * |
|
71 | + * @return string |
|
72 | + */ |
|
73 | + public function key(): string |
|
74 | + { |
|
75 | + return Base64::urlDecode($this->keyValueParameter()->value()); |
|
76 | + } |
|
77 | 77 | } |