@@ -12,15 +12,15 @@ |
||
12 | 12 | */ |
13 | 13 | class OtherPrimesInfoParameter extends JWKParameter |
14 | 14 | { |
15 | - use ArrayParameterValue; |
|
15 | + use ArrayParameterValue; |
|
16 | 16 | |
17 | - /** |
|
18 | - * Constructor. |
|
19 | - * |
|
20 | - * @param array[] ...$primes |
|
21 | - */ |
|
22 | - public function __construct(...$primes) |
|
23 | - { |
|
24 | - parent::__construct(self::PARAM_OTHER_PRIMES_INFO, $primes); |
|
25 | - } |
|
17 | + /** |
|
18 | + * Constructor. |
|
19 | + * |
|
20 | + * @param array[] ...$primes |
|
21 | + */ |
|
22 | + public function __construct(...$primes) |
|
23 | + { |
|
24 | + parent::__construct(self::PARAM_OTHER_PRIMES_INFO, $primes); |
|
25 | + } |
|
26 | 26 | } |
@@ -66,14 +66,14 @@ |
||
66 | 66 | { |
67 | 67 | $this->_parts = explode(".", $token); |
68 | 68 | switch (count($this->_parts)) { |
69 | - case 3: |
|
70 | - $this->_type = self::TYPE_JWS; |
|
71 | - break; |
|
72 | - case 5: |
|
73 | - $this->_type = self::TYPE_JWE; |
|
74 | - break; |
|
75 | - default: |
|
76 | - throw new \UnexpectedValueException("Not a JWT token."); |
|
69 | + case 3: |
|
70 | + $this->_type = self::TYPE_JWS; |
|
71 | + break; |
|
72 | + case 5: |
|
73 | + $this->_type = self::TYPE_JWE; |
|
74 | + break; |
|
75 | + default: |
|
76 | + throw new \UnexpectedValueException("Not a JWT token."); |
|
77 | 77 | } |
78 | 78 | } |
79 | 79 |
@@ -26,418 +26,418 @@ |
||
26 | 26 | */ |
27 | 27 | class JWT |
28 | 28 | { |
29 | - /** |
|
30 | - * Type identifier for the signed JWT. |
|
31 | - * |
|
32 | - * @internal |
|
33 | - * |
|
34 | - * @var int |
|
35 | - */ |
|
36 | - const TYPE_JWS = 0; |
|
29 | + /** |
|
30 | + * Type identifier for the signed JWT. |
|
31 | + * |
|
32 | + * @internal |
|
33 | + * |
|
34 | + * @var int |
|
35 | + */ |
|
36 | + const TYPE_JWS = 0; |
|
37 | 37 | |
38 | - /** |
|
39 | - * Type identifier for the encrypted JWT. |
|
40 | - * |
|
41 | - * @internal |
|
42 | - * |
|
43 | - * @var int |
|
44 | - */ |
|
45 | - const TYPE_JWE = 1; |
|
38 | + /** |
|
39 | + * Type identifier for the encrypted JWT. |
|
40 | + * |
|
41 | + * @internal |
|
42 | + * |
|
43 | + * @var int |
|
44 | + */ |
|
45 | + const TYPE_JWE = 1; |
|
46 | 46 | |
47 | - /** |
|
48 | - * JWT parts. |
|
49 | - * |
|
50 | - * @var string[] $_parts |
|
51 | - */ |
|
52 | - protected $_parts; |
|
47 | + /** |
|
48 | + * JWT parts. |
|
49 | + * |
|
50 | + * @var string[] $_parts |
|
51 | + */ |
|
52 | + protected $_parts; |
|
53 | 53 | |
54 | - /** |
|
55 | - * JWT type. |
|
56 | - * |
|
57 | - * @var int $_type |
|
58 | - */ |
|
59 | - protected $_type; |
|
54 | + /** |
|
55 | + * JWT type. |
|
56 | + * |
|
57 | + * @var int $_type |
|
58 | + */ |
|
59 | + protected $_type; |
|
60 | 60 | |
61 | - /** |
|
62 | - * Constructor. |
|
63 | - * |
|
64 | - * @param string $token JWT string |
|
65 | - * @throws \UnexpectedValueException |
|
66 | - */ |
|
67 | - public function __construct(string $token) |
|
68 | - { |
|
69 | - $this->_parts = explode(".", $token); |
|
70 | - switch (count($this->_parts)) { |
|
71 | - case 3: |
|
72 | - $this->_type = self::TYPE_JWS; |
|
73 | - break; |
|
74 | - case 5: |
|
75 | - $this->_type = self::TYPE_JWE; |
|
76 | - break; |
|
77 | - default: |
|
78 | - throw new \UnexpectedValueException("Not a JWT token."); |
|
79 | - } |
|
80 | - } |
|
61 | + /** |
|
62 | + * Constructor. |
|
63 | + * |
|
64 | + * @param string $token JWT string |
|
65 | + * @throws \UnexpectedValueException |
|
66 | + */ |
|
67 | + public function __construct(string $token) |
|
68 | + { |
|
69 | + $this->_parts = explode(".", $token); |
|
70 | + switch (count($this->_parts)) { |
|
71 | + case 3: |
|
72 | + $this->_type = self::TYPE_JWS; |
|
73 | + break; |
|
74 | + case 5: |
|
75 | + $this->_type = self::TYPE_JWE; |
|
76 | + break; |
|
77 | + default: |
|
78 | + throw new \UnexpectedValueException("Not a JWT token."); |
|
79 | + } |
|
80 | + } |
|
81 | 81 | |
82 | - /** |
|
83 | - * Convert claims set to an unsecured JWT. |
|
84 | - * |
|
85 | - * Unsecured JWT is not signed nor encrypted neither integrity protected, |
|
86 | - * and should thus be handled with care! |
|
87 | - * |
|
88 | - * @link https://tools.ietf.org/html/rfc7519#section-6 |
|
89 | - * @param Claims $claims Claims set |
|
90 | - * @param Header|null $header Optional header |
|
91 | - * @throws \RuntimeException For generic errors |
|
92 | - * @return self |
|
93 | - */ |
|
94 | - public static function unsecuredFromClaims(Claims $claims, |
|
95 | - Header $header = null): self |
|
96 | - { |
|
97 | - return self::signedFromClaims($claims, new NoneAlgorithm(), $header); |
|
98 | - } |
|
82 | + /** |
|
83 | + * Convert claims set to an unsecured JWT. |
|
84 | + * |
|
85 | + * Unsecured JWT is not signed nor encrypted neither integrity protected, |
|
86 | + * and should thus be handled with care! |
|
87 | + * |
|
88 | + * @link https://tools.ietf.org/html/rfc7519#section-6 |
|
89 | + * @param Claims $claims Claims set |
|
90 | + * @param Header|null $header Optional header |
|
91 | + * @throws \RuntimeException For generic errors |
|
92 | + * @return self |
|
93 | + */ |
|
94 | + public static function unsecuredFromClaims(Claims $claims, |
|
95 | + Header $header = null): self |
|
96 | + { |
|
97 | + return self::signedFromClaims($claims, new NoneAlgorithm(), $header); |
|
98 | + } |
|
99 | 99 | |
100 | - /** |
|
101 | - * Convert claims set to a signed JWS token. |
|
102 | - * |
|
103 | - * @param Claims $claims Claims set |
|
104 | - * @param SignatureAlgorithm $algo Signature algorithm |
|
105 | - * @param Header|null $header Optional header |
|
106 | - * @throws \RuntimeException For generic errors |
|
107 | - * @return self |
|
108 | - */ |
|
109 | - public static function signedFromClaims(Claims $claims, |
|
110 | - SignatureAlgorithm $algo, Header $header = null): self |
|
111 | - { |
|
112 | - $payload = $claims->toJSON(); |
|
113 | - $jws = JWS::sign($payload, $algo, $header); |
|
114 | - return new self($jws->toCompact()); |
|
115 | - } |
|
100 | + /** |
|
101 | + * Convert claims set to a signed JWS token. |
|
102 | + * |
|
103 | + * @param Claims $claims Claims set |
|
104 | + * @param SignatureAlgorithm $algo Signature algorithm |
|
105 | + * @param Header|null $header Optional header |
|
106 | + * @throws \RuntimeException For generic errors |
|
107 | + * @return self |
|
108 | + */ |
|
109 | + public static function signedFromClaims(Claims $claims, |
|
110 | + SignatureAlgorithm $algo, Header $header = null): self |
|
111 | + { |
|
112 | + $payload = $claims->toJSON(); |
|
113 | + $jws = JWS::sign($payload, $algo, $header); |
|
114 | + return new self($jws->toCompact()); |
|
115 | + } |
|
116 | 116 | |
117 | - /** |
|
118 | - * Convert claims set to an encrypted JWE token. |
|
119 | - * |
|
120 | - * @param Claims $claims Claims set |
|
121 | - * @param KeyManagementAlgorithm $key_algo Key management algorithm |
|
122 | - * @param ContentEncryptionAlgorithm $enc_algo Content encryption algorithm |
|
123 | - * @param CompressionAlgorithm|null $zip_algo Optional compression algorithm |
|
124 | - * @param Header|null $header Optional header |
|
125 | - * @throws \RuntimeException For generic errors |
|
126 | - * @return self |
|
127 | - */ |
|
128 | - public static function encryptedFromClaims(Claims $claims, |
|
129 | - KeyManagementAlgorithm $key_algo, ContentEncryptionAlgorithm $enc_algo, |
|
130 | - CompressionAlgorithm $zip_algo = null, Header $header = null): self |
|
131 | - { |
|
132 | - $payload = $claims->toJSON(); |
|
133 | - $jwe = JWE::encrypt($payload, $key_algo, $enc_algo, $zip_algo, $header); |
|
134 | - return new self($jwe->toCompact()); |
|
135 | - } |
|
117 | + /** |
|
118 | + * Convert claims set to an encrypted JWE token. |
|
119 | + * |
|
120 | + * @param Claims $claims Claims set |
|
121 | + * @param KeyManagementAlgorithm $key_algo Key management algorithm |
|
122 | + * @param ContentEncryptionAlgorithm $enc_algo Content encryption algorithm |
|
123 | + * @param CompressionAlgorithm|null $zip_algo Optional compression algorithm |
|
124 | + * @param Header|null $header Optional header |
|
125 | + * @throws \RuntimeException For generic errors |
|
126 | + * @return self |
|
127 | + */ |
|
128 | + public static function encryptedFromClaims(Claims $claims, |
|
129 | + KeyManagementAlgorithm $key_algo, ContentEncryptionAlgorithm $enc_algo, |
|
130 | + CompressionAlgorithm $zip_algo = null, Header $header = null): self |
|
131 | + { |
|
132 | + $payload = $claims->toJSON(); |
|
133 | + $jwe = JWE::encrypt($payload, $key_algo, $enc_algo, $zip_algo, $header); |
|
134 | + return new self($jwe->toCompact()); |
|
135 | + } |
|
136 | 136 | |
137 | - /** |
|
138 | - * Get claims from the JWT. |
|
139 | - * |
|
140 | - * Claims shall be validated according to given validation context. |
|
141 | - * Validation context must contain all the necessary keys for the signature |
|
142 | - * validation and/or content decryption. |
|
143 | - * |
|
144 | - * If validation context contains only one key, it shall be used explicitly. |
|
145 | - * If multiple keys are provided, they must contain a JWK ID parameter for |
|
146 | - * the key identification. |
|
147 | - * |
|
148 | - * @param ValidationContext $ctx |
|
149 | - * @throws ValidationException If signature is invalid, or decryption fails, |
|
150 | - * or claims validation fails. |
|
151 | - * @throws \RuntimeException For generic errors |
|
152 | - * @return Claims |
|
153 | - */ |
|
154 | - public function claims(ValidationContext $ctx): Claims |
|
155 | - { |
|
156 | - // check signature or decrypt depending on the JWT type. |
|
157 | - if ($this->isJWS()) { |
|
158 | - $payload = self::_validatedPayloadFromJWS($this->JWS(), $ctx); |
|
159 | - } else { |
|
160 | - $payload = self::_validatedPayloadFromJWE($this->JWE(), $ctx); |
|
161 | - } |
|
162 | - // if JWT contains a nested token |
|
163 | - if ($this->isNested()) { |
|
164 | - return $this->_claimsFromNestedPayload($payload, $ctx); |
|
165 | - } |
|
166 | - // decode claims and validate |
|
167 | - $claims = Claims::fromJSON($payload); |
|
168 | - $ctx->validate($claims); |
|
169 | - return $claims; |
|
170 | - } |
|
137 | + /** |
|
138 | + * Get claims from the JWT. |
|
139 | + * |
|
140 | + * Claims shall be validated according to given validation context. |
|
141 | + * Validation context must contain all the necessary keys for the signature |
|
142 | + * validation and/or content decryption. |
|
143 | + * |
|
144 | + * If validation context contains only one key, it shall be used explicitly. |
|
145 | + * If multiple keys are provided, they must contain a JWK ID parameter for |
|
146 | + * the key identification. |
|
147 | + * |
|
148 | + * @param ValidationContext $ctx |
|
149 | + * @throws ValidationException If signature is invalid, or decryption fails, |
|
150 | + * or claims validation fails. |
|
151 | + * @throws \RuntimeException For generic errors |
|
152 | + * @return Claims |
|
153 | + */ |
|
154 | + public function claims(ValidationContext $ctx): Claims |
|
155 | + { |
|
156 | + // check signature or decrypt depending on the JWT type. |
|
157 | + if ($this->isJWS()) { |
|
158 | + $payload = self::_validatedPayloadFromJWS($this->JWS(), $ctx); |
|
159 | + } else { |
|
160 | + $payload = self::_validatedPayloadFromJWE($this->JWE(), $ctx); |
|
161 | + } |
|
162 | + // if JWT contains a nested token |
|
163 | + if ($this->isNested()) { |
|
164 | + return $this->_claimsFromNestedPayload($payload, $ctx); |
|
165 | + } |
|
166 | + // decode claims and validate |
|
167 | + $claims = Claims::fromJSON($payload); |
|
168 | + $ctx->validate($claims); |
|
169 | + return $claims; |
|
170 | + } |
|
171 | 171 | |
172 | - /** |
|
173 | - * Sign self producing a nested JWT. |
|
174 | - * |
|
175 | - * Note that if JWT is to be signed and encrypted, it should be done in |
|
176 | - * sign-then-encrypt order. Please refer to links for security information. |
|
177 | - * |
|
178 | - * @link https://tools.ietf.org/html/rfc7519#section-11.2 |
|
179 | - * @param SignatureAlgorithm $algo Signature algorithm |
|
180 | - * @param Header|null $header Optional header |
|
181 | - * @throws \RuntimeException For generic errors |
|
182 | - * @return self |
|
183 | - */ |
|
184 | - public function signNested(SignatureAlgorithm $algo, Header $header = null): self |
|
185 | - { |
|
186 | - if (!isset($header)) { |
|
187 | - $header = new Header(); |
|
188 | - } |
|
189 | - // add JWT content type parameter |
|
190 | - $header = $header->withParameters( |
|
191 | - new ContentTypeParameter(ContentTypeParameter::TYPE_JWT)); |
|
192 | - $jws = JWS::sign($this->token(), $algo, $header); |
|
193 | - return new self($jws->toCompact()); |
|
194 | - } |
|
172 | + /** |
|
173 | + * Sign self producing a nested JWT. |
|
174 | + * |
|
175 | + * Note that if JWT is to be signed and encrypted, it should be done in |
|
176 | + * sign-then-encrypt order. Please refer to links for security information. |
|
177 | + * |
|
178 | + * @link https://tools.ietf.org/html/rfc7519#section-11.2 |
|
179 | + * @param SignatureAlgorithm $algo Signature algorithm |
|
180 | + * @param Header|null $header Optional header |
|
181 | + * @throws \RuntimeException For generic errors |
|
182 | + * @return self |
|
183 | + */ |
|
184 | + public function signNested(SignatureAlgorithm $algo, Header $header = null): self |
|
185 | + { |
|
186 | + if (!isset($header)) { |
|
187 | + $header = new Header(); |
|
188 | + } |
|
189 | + // add JWT content type parameter |
|
190 | + $header = $header->withParameters( |
|
191 | + new ContentTypeParameter(ContentTypeParameter::TYPE_JWT)); |
|
192 | + $jws = JWS::sign($this->token(), $algo, $header); |
|
193 | + return new self($jws->toCompact()); |
|
194 | + } |
|
195 | 195 | |
196 | - /** |
|
197 | - * Encrypt self producing a nested JWT. |
|
198 | - * |
|
199 | - * This JWT should be a JWS, that is, the order of nesting should be |
|
200 | - * sign-then-encrypt. |
|
201 | - * |
|
202 | - * @link https://tools.ietf.org/html/rfc7519#section-11.2 |
|
203 | - * @param KeyManagementAlgorithm $key_algo Key management algorithm |
|
204 | - * @param ContentEncryptionAlgorithm $enc_algo Content encryption algorithm |
|
205 | - * @param CompressionAlgorithm|null $zip_algo Optional compression algorithm |
|
206 | - * @param Header|null $header Optional header |
|
207 | - * @throws \RuntimeException For generic errors |
|
208 | - * @return self |
|
209 | - */ |
|
210 | - public function encryptNested(KeyManagementAlgorithm $key_algo, |
|
211 | - ContentEncryptionAlgorithm $enc_algo, |
|
212 | - CompressionAlgorithm $zip_algo = null, Header $header = null): self |
|
213 | - { |
|
214 | - if (!isset($header)) { |
|
215 | - $header = new Header(); |
|
216 | - } |
|
217 | - // add JWT content type parameter |
|
218 | - $header = $header->withParameters( |
|
219 | - new ContentTypeParameter(ContentTypeParameter::TYPE_JWT)); |
|
220 | - $jwe = JWE::encrypt($this->token(), $key_algo, $enc_algo, $zip_algo, |
|
221 | - $header); |
|
222 | - return new self($jwe->toCompact()); |
|
223 | - } |
|
196 | + /** |
|
197 | + * Encrypt self producing a nested JWT. |
|
198 | + * |
|
199 | + * This JWT should be a JWS, that is, the order of nesting should be |
|
200 | + * sign-then-encrypt. |
|
201 | + * |
|
202 | + * @link https://tools.ietf.org/html/rfc7519#section-11.2 |
|
203 | + * @param KeyManagementAlgorithm $key_algo Key management algorithm |
|
204 | + * @param ContentEncryptionAlgorithm $enc_algo Content encryption algorithm |
|
205 | + * @param CompressionAlgorithm|null $zip_algo Optional compression algorithm |
|
206 | + * @param Header|null $header Optional header |
|
207 | + * @throws \RuntimeException For generic errors |
|
208 | + * @return self |
|
209 | + */ |
|
210 | + public function encryptNested(KeyManagementAlgorithm $key_algo, |
|
211 | + ContentEncryptionAlgorithm $enc_algo, |
|
212 | + CompressionAlgorithm $zip_algo = null, Header $header = null): self |
|
213 | + { |
|
214 | + if (!isset($header)) { |
|
215 | + $header = new Header(); |
|
216 | + } |
|
217 | + // add JWT content type parameter |
|
218 | + $header = $header->withParameters( |
|
219 | + new ContentTypeParameter(ContentTypeParameter::TYPE_JWT)); |
|
220 | + $jwe = JWE::encrypt($this->token(), $key_algo, $enc_algo, $zip_algo, |
|
221 | + $header); |
|
222 | + return new self($jwe->toCompact()); |
|
223 | + } |
|
224 | 224 | |
225 | - /** |
|
226 | - * Whether JWT is a JWS. |
|
227 | - * |
|
228 | - * @return bool |
|
229 | - */ |
|
230 | - public function isJWS(): bool |
|
231 | - { |
|
232 | - return $this->_type == self::TYPE_JWS; |
|
233 | - } |
|
225 | + /** |
|
226 | + * Whether JWT is a JWS. |
|
227 | + * |
|
228 | + * @return bool |
|
229 | + */ |
|
230 | + public function isJWS(): bool |
|
231 | + { |
|
232 | + return $this->_type == self::TYPE_JWS; |
|
233 | + } |
|
234 | 234 | |
235 | - /** |
|
236 | - * Get JWT as a JWS. |
|
237 | - * |
|
238 | - * @throws \LogicException |
|
239 | - * @return JWS |
|
240 | - */ |
|
241 | - public function JWS(): JWS |
|
242 | - { |
|
243 | - if (!$this->isJWS()) { |
|
244 | - throw new \LogicException("Not a JWS."); |
|
245 | - } |
|
246 | - return JWS::fromParts($this->_parts); |
|
247 | - } |
|
235 | + /** |
|
236 | + * Get JWT as a JWS. |
|
237 | + * |
|
238 | + * @throws \LogicException |
|
239 | + * @return JWS |
|
240 | + */ |
|
241 | + public function JWS(): JWS |
|
242 | + { |
|
243 | + if (!$this->isJWS()) { |
|
244 | + throw new \LogicException("Not a JWS."); |
|
245 | + } |
|
246 | + return JWS::fromParts($this->_parts); |
|
247 | + } |
|
248 | 248 | |
249 | - /** |
|
250 | - * Whether JWT is a JWE. |
|
251 | - * |
|
252 | - * @return bool |
|
253 | - */ |
|
254 | - public function isJWE(): bool |
|
255 | - { |
|
256 | - return $this->_type == self::TYPE_JWE; |
|
257 | - } |
|
249 | + /** |
|
250 | + * Whether JWT is a JWE. |
|
251 | + * |
|
252 | + * @return bool |
|
253 | + */ |
|
254 | + public function isJWE(): bool |
|
255 | + { |
|
256 | + return $this->_type == self::TYPE_JWE; |
|
257 | + } |
|
258 | 258 | |
259 | - /** |
|
260 | - * Get JWT as a JWE. |
|
261 | - * |
|
262 | - * @throws \LogicException |
|
263 | - * @return JWE |
|
264 | - */ |
|
265 | - public function JWE(): JWE |
|
266 | - { |
|
267 | - if (!$this->isJWE()) { |
|
268 | - throw new \LogicException("Not a JWE."); |
|
269 | - } |
|
270 | - return JWE::fromParts($this->_parts); |
|
271 | - } |
|
259 | + /** |
|
260 | + * Get JWT as a JWE. |
|
261 | + * |
|
262 | + * @throws \LogicException |
|
263 | + * @return JWE |
|
264 | + */ |
|
265 | + public function JWE(): JWE |
|
266 | + { |
|
267 | + if (!$this->isJWE()) { |
|
268 | + throw new \LogicException("Not a JWE."); |
|
269 | + } |
|
270 | + return JWE::fromParts($this->_parts); |
|
271 | + } |
|
272 | 272 | |
273 | - /** |
|
274 | - * Check whether JWT contains another nested JWT. |
|
275 | - * |
|
276 | - * @return bool |
|
277 | - */ |
|
278 | - public function isNested(): bool |
|
279 | - { |
|
280 | - $header = $this->header(); |
|
281 | - if (!$header->hasContentType()) { |
|
282 | - return false; |
|
283 | - } |
|
284 | - $cty = $header->contentType()->value(); |
|
285 | - if ($cty != ContentTypeParameter::TYPE_JWT) { |
|
286 | - return false; |
|
287 | - } |
|
288 | - return true; |
|
289 | - } |
|
273 | + /** |
|
274 | + * Check whether JWT contains another nested JWT. |
|
275 | + * |
|
276 | + * @return bool |
|
277 | + */ |
|
278 | + public function isNested(): bool |
|
279 | + { |
|
280 | + $header = $this->header(); |
|
281 | + if (!$header->hasContentType()) { |
|
282 | + return false; |
|
283 | + } |
|
284 | + $cty = $header->contentType()->value(); |
|
285 | + if ($cty != ContentTypeParameter::TYPE_JWT) { |
|
286 | + return false; |
|
287 | + } |
|
288 | + return true; |
|
289 | + } |
|
290 | 290 | |
291 | - /** |
|
292 | - * Check whether JWT is unsecured, that is, it's neither integrity protected |
|
293 | - * nor encrypted. |
|
294 | - * |
|
295 | - * @return bool |
|
296 | - */ |
|
297 | - public function isUnsecured(): bool |
|
298 | - { |
|
299 | - // encrypted JWT shall be considered secure |
|
300 | - if ($this->isJWE()) { |
|
301 | - return false; |
|
302 | - } |
|
303 | - // check whether JWS is unsecured |
|
304 | - return $this->JWS()->isUnsecured(); |
|
305 | - } |
|
291 | + /** |
|
292 | + * Check whether JWT is unsecured, that is, it's neither integrity protected |
|
293 | + * nor encrypted. |
|
294 | + * |
|
295 | + * @return bool |
|
296 | + */ |
|
297 | + public function isUnsecured(): bool |
|
298 | + { |
|
299 | + // encrypted JWT shall be considered secure |
|
300 | + if ($this->isJWE()) { |
|
301 | + return false; |
|
302 | + } |
|
303 | + // check whether JWS is unsecured |
|
304 | + return $this->JWS()->isUnsecured(); |
|
305 | + } |
|
306 | 306 | |
307 | - /** |
|
308 | - * Get JWT header. |
|
309 | - * |
|
310 | - * @return JOSE |
|
311 | - */ |
|
312 | - public function header(): JOSE |
|
313 | - { |
|
314 | - $header = Header::fromJSON(Base64::urlDecode($this->_parts[0])); |
|
315 | - return new JOSE($header); |
|
316 | - } |
|
307 | + /** |
|
308 | + * Get JWT header. |
|
309 | + * |
|
310 | + * @return JOSE |
|
311 | + */ |
|
312 | + public function header(): JOSE |
|
313 | + { |
|
314 | + $header = Header::fromJSON(Base64::urlDecode($this->_parts[0])); |
|
315 | + return new JOSE($header); |
|
316 | + } |
|
317 | 317 | |
318 | - /** |
|
319 | - * Get JWT as a string. |
|
320 | - * |
|
321 | - * @return string |
|
322 | - */ |
|
323 | - public function token(): string |
|
324 | - { |
|
325 | - return implode(".", $this->_parts); |
|
326 | - } |
|
318 | + /** |
|
319 | + * Get JWT as a string. |
|
320 | + * |
|
321 | + * @return string |
|
322 | + */ |
|
323 | + public function token(): string |
|
324 | + { |
|
325 | + return implode(".", $this->_parts); |
|
326 | + } |
|
327 | 327 | |
328 | - /** |
|
329 | - * Get claims from a nested payload. |
|
330 | - * |
|
331 | - * @param string $payload JWT payload |
|
332 | - * @param ValidationContext $ctx Validation context |
|
333 | - * @return Claims |
|
334 | - */ |
|
335 | - private function _claimsFromNestedPayload(string $payload, |
|
336 | - ValidationContext $ctx): Claims |
|
337 | - { |
|
338 | - $jwt = new JWT($payload); |
|
339 | - // if this token secured, allow nested tokens to be unsecured. |
|
340 | - if (!$this->isUnsecured()) { |
|
341 | - $ctx = $ctx->withUnsecuredAllowed(true); |
|
342 | - } |
|
343 | - return $jwt->claims($ctx); |
|
344 | - } |
|
328 | + /** |
|
329 | + * Get claims from a nested payload. |
|
330 | + * |
|
331 | + * @param string $payload JWT payload |
|
332 | + * @param ValidationContext $ctx Validation context |
|
333 | + * @return Claims |
|
334 | + */ |
|
335 | + private function _claimsFromNestedPayload(string $payload, |
|
336 | + ValidationContext $ctx): Claims |
|
337 | + { |
|
338 | + $jwt = new JWT($payload); |
|
339 | + // if this token secured, allow nested tokens to be unsecured. |
|
340 | + if (!$this->isUnsecured()) { |
|
341 | + $ctx = $ctx->withUnsecuredAllowed(true); |
|
342 | + } |
|
343 | + return $jwt->claims($ctx); |
|
344 | + } |
|
345 | 345 | |
346 | - /** |
|
347 | - * Get validated payload from JWS. |
|
348 | - * |
|
349 | - * @param JWS $jws JWS |
|
350 | - * @param ValidationContext $ctx Validation context |
|
351 | - * @throws ValidationException If signature validation fails |
|
352 | - * @return string |
|
353 | - */ |
|
354 | - private static function _validatedPayloadFromJWS(JWS $jws, |
|
355 | - ValidationContext $ctx): string |
|
356 | - { |
|
357 | - // if JWS is unsecured |
|
358 | - if ($jws->isUnsecured()) { |
|
359 | - return self::_validatedPayloadFromUnsecuredJWS($jws, $ctx); |
|
360 | - } |
|
361 | - return self::_validatedPayloadFromSignedJWS($jws, $ctx->keys()); |
|
362 | - } |
|
346 | + /** |
|
347 | + * Get validated payload from JWS. |
|
348 | + * |
|
349 | + * @param JWS $jws JWS |
|
350 | + * @param ValidationContext $ctx Validation context |
|
351 | + * @throws ValidationException If signature validation fails |
|
352 | + * @return string |
|
353 | + */ |
|
354 | + private static function _validatedPayloadFromJWS(JWS $jws, |
|
355 | + ValidationContext $ctx): string |
|
356 | + { |
|
357 | + // if JWS is unsecured |
|
358 | + if ($jws->isUnsecured()) { |
|
359 | + return self::_validatedPayloadFromUnsecuredJWS($jws, $ctx); |
|
360 | + } |
|
361 | + return self::_validatedPayloadFromSignedJWS($jws, $ctx->keys()); |
|
362 | + } |
|
363 | 363 | |
364 | - /** |
|
365 | - * Get validated payload from an unsecured JWS. |
|
366 | - * |
|
367 | - * @param JWS $jws JWS |
|
368 | - * @param ValidationContext $ctx Validation context |
|
369 | - * @throws ValidationException If unsecured JWT's are not allowed, or JWS |
|
370 | - * token is malformed |
|
371 | - * @return string |
|
372 | - */ |
|
373 | - private static function _validatedPayloadFromUnsecuredJWS(JWS $jws, |
|
374 | - ValidationContext $ctx): string |
|
375 | - { |
|
376 | - if (!$ctx->isUnsecuredAllowed()) { |
|
377 | - throw new ValidationException("Unsecured JWS not allowed."); |
|
378 | - } |
|
379 | - if (!$jws->validate(new NoneAlgorithm())) { |
|
380 | - throw new ValidationException("Malformed unsecured token."); |
|
381 | - } |
|
382 | - return $jws->payload(); |
|
383 | - } |
|
364 | + /** |
|
365 | + * Get validated payload from an unsecured JWS. |
|
366 | + * |
|
367 | + * @param JWS $jws JWS |
|
368 | + * @param ValidationContext $ctx Validation context |
|
369 | + * @throws ValidationException If unsecured JWT's are not allowed, or JWS |
|
370 | + * token is malformed |
|
371 | + * @return string |
|
372 | + */ |
|
373 | + private static function _validatedPayloadFromUnsecuredJWS(JWS $jws, |
|
374 | + ValidationContext $ctx): string |
|
375 | + { |
|
376 | + if (!$ctx->isUnsecuredAllowed()) { |
|
377 | + throw new ValidationException("Unsecured JWS not allowed."); |
|
378 | + } |
|
379 | + if (!$jws->validate(new NoneAlgorithm())) { |
|
380 | + throw new ValidationException("Malformed unsecured token."); |
|
381 | + } |
|
382 | + return $jws->payload(); |
|
383 | + } |
|
384 | 384 | |
385 | - /** |
|
386 | - * Get validated payload from a signed JWS. |
|
387 | - * |
|
388 | - * @param JWS $jws JWS |
|
389 | - * @param JWKSet $keys Set of allowed keys for the signature validation |
|
390 | - * @throws ValidationException If validation fails |
|
391 | - * @return string |
|
392 | - */ |
|
393 | - private static function _validatedPayloadFromSignedJWS(JWS $jws, JWKSet $keys): string |
|
394 | - { |
|
395 | - try { |
|
396 | - // explicitly defined key |
|
397 | - if (1 == count($keys)) { |
|
398 | - $valid = $jws->validateWithJWK($keys->first()); |
|
399 | - } else { |
|
400 | - $valid = $jws->validateWithJWKSet($keys); |
|
401 | - } |
|
402 | - } catch (\RuntimeException $e) { |
|
403 | - throw new ValidationException("JWS validation failed.", 0, $e); |
|
404 | - } |
|
405 | - if (!$valid) { |
|
406 | - throw new ValidationException("JWS signature is invalid."); |
|
407 | - } |
|
408 | - return $jws->payload(); |
|
409 | - } |
|
385 | + /** |
|
386 | + * Get validated payload from a signed JWS. |
|
387 | + * |
|
388 | + * @param JWS $jws JWS |
|
389 | + * @param JWKSet $keys Set of allowed keys for the signature validation |
|
390 | + * @throws ValidationException If validation fails |
|
391 | + * @return string |
|
392 | + */ |
|
393 | + private static function _validatedPayloadFromSignedJWS(JWS $jws, JWKSet $keys): string |
|
394 | + { |
|
395 | + try { |
|
396 | + // explicitly defined key |
|
397 | + if (1 == count($keys)) { |
|
398 | + $valid = $jws->validateWithJWK($keys->first()); |
|
399 | + } else { |
|
400 | + $valid = $jws->validateWithJWKSet($keys); |
|
401 | + } |
|
402 | + } catch (\RuntimeException $e) { |
|
403 | + throw new ValidationException("JWS validation failed.", 0, $e); |
|
404 | + } |
|
405 | + if (!$valid) { |
|
406 | + throw new ValidationException("JWS signature is invalid."); |
|
407 | + } |
|
408 | + return $jws->payload(); |
|
409 | + } |
|
410 | 410 | |
411 | - /** |
|
412 | - * Get validated payload from an encrypted JWE. |
|
413 | - * |
|
414 | - * @param JWE $jwe JWE |
|
415 | - * @param ValidationContext $ctx Validation context |
|
416 | - * @throws ValidationException If decryption fails |
|
417 | - * @return string |
|
418 | - */ |
|
419 | - private static function _validatedPayloadFromJWE(JWE $jwe, |
|
420 | - ValidationContext $ctx): string |
|
421 | - { |
|
422 | - try { |
|
423 | - $keys = $ctx->keys(); |
|
424 | - // explicitly defined key |
|
425 | - if (1 == count($keys)) { |
|
426 | - return $jwe->decryptWithJWK($keys->first()); |
|
427 | - } |
|
428 | - return $jwe->decryptWithJWKSet($keys); |
|
429 | - } catch (\RuntimeException $e) { |
|
430 | - throw new ValidationException("JWE validation failed.", 0, $e); |
|
431 | - } |
|
432 | - } |
|
411 | + /** |
|
412 | + * Get validated payload from an encrypted JWE. |
|
413 | + * |
|
414 | + * @param JWE $jwe JWE |
|
415 | + * @param ValidationContext $ctx Validation context |
|
416 | + * @throws ValidationException If decryption fails |
|
417 | + * @return string |
|
418 | + */ |
|
419 | + private static function _validatedPayloadFromJWE(JWE $jwe, |
|
420 | + ValidationContext $ctx): string |
|
421 | + { |
|
422 | + try { |
|
423 | + $keys = $ctx->keys(); |
|
424 | + // explicitly defined key |
|
425 | + if (1 == count($keys)) { |
|
426 | + return $jwe->decryptWithJWK($keys->first()); |
|
427 | + } |
|
428 | + return $jwe->decryptWithJWKSet($keys); |
|
429 | + } catch (\RuntimeException $e) { |
|
430 | + throw new ValidationException("JWE validation failed.", 0, $e); |
|
431 | + } |
|
432 | + } |
|
433 | 433 | |
434 | - /** |
|
435 | - * Convert JWT to string. |
|
436 | - * |
|
437 | - * @return string |
|
438 | - */ |
|
439 | - public function __toString() |
|
440 | - { |
|
441 | - return $this->token(); |
|
442 | - } |
|
434 | + /** |
|
435 | + * Convert JWT to string. |
|
436 | + * |
|
437 | + * @return string |
|
438 | + */ |
|
439 | + public function __toString() |
|
440 | + { |
|
441 | + return $this->token(); |
|
442 | + } |
|
443 | 443 | } |
@@ -13,18 +13,18 @@ |
||
13 | 13 | */ |
14 | 14 | class ExpirationTimeClaim extends RegisteredClaim |
15 | 15 | { |
16 | - use NumericDateClaim; |
|
17 | - use ReferenceTimeValidation; |
|
16 | + use NumericDateClaim; |
|
17 | + use ReferenceTimeValidation; |
|
18 | 18 | |
19 | - /** |
|
20 | - * Constructor. |
|
21 | - * |
|
22 | - * @param int $exp_time Expiration time |
|
23 | - */ |
|
24 | - public function __construct($exp_time) |
|
25 | - { |
|
26 | - // validate that claim is after the constraint (reference time) |
|
27 | - parent::__construct(self::NAME_EXPIRATION_TIME, intval($exp_time), |
|
28 | - new GreaterValidator()); |
|
29 | - } |
|
19 | + /** |
|
20 | + * Constructor. |
|
21 | + * |
|
22 | + * @param int $exp_time Expiration time |
|
23 | + */ |
|
24 | + public function __construct($exp_time) |
|
25 | + { |
|
26 | + // validate that claim is after the constraint (reference time) |
|
27 | + parent::__construct(self::NAME_EXPIRATION_TIME, intval($exp_time), |
|
28 | + new GreaterValidator()); |
|
29 | + } |
|
30 | 30 | } |
@@ -9,85 +9,85 @@ |
||
9 | 9 | */ |
10 | 10 | abstract class RegisteredClaim extends Claim |
11 | 11 | { |
12 | - // JWT claims |
|
13 | - const NAME_ISSUER = "iss"; |
|
14 | - const NAME_SUBJECT = "sub"; |
|
15 | - const NAME_AUDIENCE = "aud"; |
|
16 | - const NAME_EXPIRATION_TIME = "exp"; |
|
17 | - const NAME_NOT_BEFORE = "nbf"; |
|
18 | - const NAME_ISSUED_AT = "iat"; |
|
19 | - const NAME_JWT_ID = "jti"; |
|
12 | + // JWT claims |
|
13 | + const NAME_ISSUER = "iss"; |
|
14 | + const NAME_SUBJECT = "sub"; |
|
15 | + const NAME_AUDIENCE = "aud"; |
|
16 | + const NAME_EXPIRATION_TIME = "exp"; |
|
17 | + const NAME_NOT_BEFORE = "nbf"; |
|
18 | + const NAME_ISSUED_AT = "iat"; |
|
19 | + const NAME_JWT_ID = "jti"; |
|
20 | 20 | |
21 | - // OpenID claims |
|
22 | - const NAME_FULL_NAME = "name"; |
|
23 | - const NAME_GIVEN_NAME = "given_name"; |
|
24 | - const NAME_FAMILY_NAME = "family_name"; |
|
25 | - const NAME_MIDDLE_NAME = "middle_name"; |
|
26 | - const NAME_NICKNAME = "nickname"; |
|
27 | - const NAME_PREFERRED_USERNAME = "preferred_username"; |
|
28 | - const NAME_PROFILE_URL = "profile"; |
|
29 | - const NAME_PICTURE_URL = "picture"; |
|
30 | - const NAME_WEBSITE_URL = "website"; |
|
31 | - const NAME_EMAIL = "email"; |
|
32 | - const NAME_EMAIL_VERIFIED = "email_verified"; |
|
33 | - const NAME_GENDER = "gender"; |
|
34 | - const NAME_BIRTHDATE = "birthdate"; |
|
35 | - const NAME_TIMEZONE = "zoneinfo"; |
|
36 | - const NAME_LOCALE = "locale"; |
|
37 | - const NAME_PHONE_NUMBER = "phone_number"; |
|
38 | - const NAME_PHONE_NUMBER_VERIFIED = "phone_number_verified"; |
|
39 | - const NAME_ADDRESS = "address"; |
|
40 | - const NAME_UPDATED_AT = "updated_at"; |
|
41 | - const NAME_AUTHORIZED_PARTY = "azp"; |
|
42 | - const NAME_NONCE = "nonce"; |
|
43 | - const NAME_AUTH_TIME = "auth_time"; |
|
44 | - const NAME_ACCESS_TOKEN_HASH = "at_hash"; |
|
45 | - const NAME_CODE_HASH = "c_hash"; |
|
46 | - const NAME_ACR = "acr"; |
|
47 | - const NAME_AMR = "amr"; |
|
48 | - const NAME_SUB_JWK = "sub_jwk"; |
|
21 | + // OpenID claims |
|
22 | + const NAME_FULL_NAME = "name"; |
|
23 | + const NAME_GIVEN_NAME = "given_name"; |
|
24 | + const NAME_FAMILY_NAME = "family_name"; |
|
25 | + const NAME_MIDDLE_NAME = "middle_name"; |
|
26 | + const NAME_NICKNAME = "nickname"; |
|
27 | + const NAME_PREFERRED_USERNAME = "preferred_username"; |
|
28 | + const NAME_PROFILE_URL = "profile"; |
|
29 | + const NAME_PICTURE_URL = "picture"; |
|
30 | + const NAME_WEBSITE_URL = "website"; |
|
31 | + const NAME_EMAIL = "email"; |
|
32 | + const NAME_EMAIL_VERIFIED = "email_verified"; |
|
33 | + const NAME_GENDER = "gender"; |
|
34 | + const NAME_BIRTHDATE = "birthdate"; |
|
35 | + const NAME_TIMEZONE = "zoneinfo"; |
|
36 | + const NAME_LOCALE = "locale"; |
|
37 | + const NAME_PHONE_NUMBER = "phone_number"; |
|
38 | + const NAME_PHONE_NUMBER_VERIFIED = "phone_number_verified"; |
|
39 | + const NAME_ADDRESS = "address"; |
|
40 | + const NAME_UPDATED_AT = "updated_at"; |
|
41 | + const NAME_AUTHORIZED_PARTY = "azp"; |
|
42 | + const NAME_NONCE = "nonce"; |
|
43 | + const NAME_AUTH_TIME = "auth_time"; |
|
44 | + const NAME_ACCESS_TOKEN_HASH = "at_hash"; |
|
45 | + const NAME_CODE_HASH = "c_hash"; |
|
46 | + const NAME_ACR = "acr"; |
|
47 | + const NAME_AMR = "amr"; |
|
48 | + const NAME_SUB_JWK = "sub_jwk"; |
|
49 | 49 | |
50 | - /** |
|
51 | - * Mapping from registered claim name to class name. |
|
52 | - * |
|
53 | - * @internal |
|
54 | - * |
|
55 | - * @var array |
|
56 | - */ |
|
57 | - const MAP_NAME_TO_CLASS = array( |
|
58 | - /* @formatter:off */ |
|
59 | - self::NAME_ISSUER => IssuerClaim::class, |
|
60 | - self::NAME_SUBJECT => SubjectClaim::class, |
|
61 | - self::NAME_AUDIENCE => AudienceClaim::class, |
|
62 | - self::NAME_EXPIRATION_TIME => ExpirationTimeClaim::class, |
|
63 | - self::NAME_NOT_BEFORE => NotBeforeClaim::class, |
|
64 | - self::NAME_ISSUED_AT => IssuedAtClaim::class, |
|
65 | - self::NAME_JWT_ID => JWTIDClaim::class |
|
66 | - /* @formatter:on */ |
|
67 | - ); |
|
50 | + /** |
|
51 | + * Mapping from registered claim name to class name. |
|
52 | + * |
|
53 | + * @internal |
|
54 | + * |
|
55 | + * @var array |
|
56 | + */ |
|
57 | + const MAP_NAME_TO_CLASS = array( |
|
58 | + /* @formatter:off */ |
|
59 | + self::NAME_ISSUER => IssuerClaim::class, |
|
60 | + self::NAME_SUBJECT => SubjectClaim::class, |
|
61 | + self::NAME_AUDIENCE => AudienceClaim::class, |
|
62 | + self::NAME_EXPIRATION_TIME => ExpirationTimeClaim::class, |
|
63 | + self::NAME_NOT_BEFORE => NotBeforeClaim::class, |
|
64 | + self::NAME_ISSUED_AT => IssuedAtClaim::class, |
|
65 | + self::NAME_JWT_ID => JWTIDClaim::class |
|
66 | + /* @formatter:on */ |
|
67 | + ); |
|
68 | 68 | |
69 | - /** |
|
70 | - * Constructor. |
|
71 | - * |
|
72 | - * Defined here for type strictness. Parameters are passed to the |
|
73 | - * superclass. |
|
74 | - * |
|
75 | - * @param mixed ...$args |
|
76 | - */ |
|
77 | - public function __construct(...$args) |
|
78 | - { |
|
79 | - parent::__construct((string) $args[0], $args[1], |
|
80 | - isset($args[2]) ? $args[2] : null); |
|
81 | - } |
|
69 | + /** |
|
70 | + * Constructor. |
|
71 | + * |
|
72 | + * Defined here for type strictness. Parameters are passed to the |
|
73 | + * superclass. |
|
74 | + * |
|
75 | + * @param mixed ...$args |
|
76 | + */ |
|
77 | + public function __construct(...$args) |
|
78 | + { |
|
79 | + parent::__construct((string) $args[0], $args[1], |
|
80 | + isset($args[2]) ? $args[2] : null); |
|
81 | + } |
|
82 | 82 | |
83 | - /** |
|
84 | - * Initialize concrete claim instance from a JSON value. |
|
85 | - * |
|
86 | - * @param mixed $value |
|
87 | - * @return RegisteredClaim |
|
88 | - */ |
|
89 | - public static function fromJSONValue($value) |
|
90 | - { |
|
91 | - return new static($value); |
|
92 | - } |
|
83 | + /** |
|
84 | + * Initialize concrete claim instance from a JSON value. |
|
85 | + * |
|
86 | + * @param mixed $value |
|
87 | + * @return RegisteredClaim |
|
88 | + */ |
|
89 | + public static function fromJSONValue($value) |
|
90 | + { |
|
91 | + return new static($value); |
|
92 | + } |
|
93 | 93 | } |
@@ -11,13 +11,13 @@ |
||
11 | 11 | */ |
12 | 12 | class ReplicatedClaimParameter extends JWTParameter |
13 | 13 | { |
14 | - /** |
|
15 | - * Constructor. |
|
16 | - * |
|
17 | - * @param Claim $claim |
|
18 | - */ |
|
19 | - public function __construct(Claim $claim) |
|
20 | - { |
|
21 | - parent::__construct($claim->name(), $claim->value()); |
|
22 | - } |
|
14 | + /** |
|
15 | + * Constructor. |
|
16 | + * |
|
17 | + * @param Claim $claim |
|
18 | + */ |
|
19 | + public function __construct(Claim $claim) |
|
20 | + { |
|
21 | + parent::__construct($claim->name(), $claim->value()); |
|
22 | + } |
|
23 | 23 | } |
@@ -31,17 +31,17 @@ |
||
31 | 31 | { |
32 | 32 | $data = strtr($data, "-_", "+/"); |
33 | 33 | switch (strlen($data) % 4) { |
34 | - case 0: |
|
35 | - break; |
|
36 | - case 2: |
|
37 | - $data .= "=="; |
|
38 | - break; |
|
39 | - case 3: |
|
40 | - $data .= "="; |
|
41 | - break; |
|
42 | - default: |
|
43 | - throw new \UnexpectedValueException( |
|
44 | - "Malformed base64url encoding."); |
|
34 | + case 0: |
|
35 | + break; |
|
36 | + case 2: |
|
37 | + $data .= "=="; |
|
38 | + break; |
|
39 | + case 3: |
|
40 | + $data .= "="; |
|
41 | + break; |
|
42 | + default: |
|
43 | + throw new \UnexpectedValueException( |
|
44 | + "Malformed base64url encoding."); |
|
45 | 45 | } |
46 | 46 | return self::decode($data); |
47 | 47 | } |
@@ -9,104 +9,104 @@ |
||
9 | 9 | */ |
10 | 10 | class Base64 |
11 | 11 | { |
12 | - /** |
|
13 | - * Encode a string using base64url variant. |
|
14 | - * |
|
15 | - * @link https://en.wikipedia.org/wiki/Base64#URL_applications |
|
16 | - * @param string $data |
|
17 | - * @return string |
|
18 | - */ |
|
19 | - public static function urlEncode(string $data): string |
|
20 | - { |
|
21 | - return strtr(rtrim(self::encode($data), "="), "+/", "-_"); |
|
22 | - } |
|
12 | + /** |
|
13 | + * Encode a string using base64url variant. |
|
14 | + * |
|
15 | + * @link https://en.wikipedia.org/wiki/Base64#URL_applications |
|
16 | + * @param string $data |
|
17 | + * @return string |
|
18 | + */ |
|
19 | + public static function urlEncode(string $data): string |
|
20 | + { |
|
21 | + return strtr(rtrim(self::encode($data), "="), "+/", "-_"); |
|
22 | + } |
|
23 | 23 | |
24 | - /** |
|
25 | - * Decode a string using base64url variant. |
|
26 | - * |
|
27 | - * @link https://en.wikipedia.org/wiki/Base64#URL_applications |
|
28 | - * @param string $data |
|
29 | - * @throws \UnexpectedValueException |
|
30 | - * @return string |
|
31 | - */ |
|
32 | - public static function urlDecode(string $data): string |
|
33 | - { |
|
34 | - $data = strtr($data, "-_", "+/"); |
|
35 | - switch (strlen($data) % 4) { |
|
36 | - case 0: |
|
37 | - break; |
|
38 | - case 2: |
|
39 | - $data .= "=="; |
|
40 | - break; |
|
41 | - case 3: |
|
42 | - $data .= "="; |
|
43 | - break; |
|
44 | - default: |
|
45 | - throw new \UnexpectedValueException( |
|
46 | - "Malformed base64url encoding."); |
|
47 | - } |
|
48 | - return self::decode($data); |
|
49 | - } |
|
24 | + /** |
|
25 | + * Decode a string using base64url variant. |
|
26 | + * |
|
27 | + * @link https://en.wikipedia.org/wiki/Base64#URL_applications |
|
28 | + * @param string $data |
|
29 | + * @throws \UnexpectedValueException |
|
30 | + * @return string |
|
31 | + */ |
|
32 | + public static function urlDecode(string $data): string |
|
33 | + { |
|
34 | + $data = strtr($data, "-_", "+/"); |
|
35 | + switch (strlen($data) % 4) { |
|
36 | + case 0: |
|
37 | + break; |
|
38 | + case 2: |
|
39 | + $data .= "=="; |
|
40 | + break; |
|
41 | + case 3: |
|
42 | + $data .= "="; |
|
43 | + break; |
|
44 | + default: |
|
45 | + throw new \UnexpectedValueException( |
|
46 | + "Malformed base64url encoding."); |
|
47 | + } |
|
48 | + return self::decode($data); |
|
49 | + } |
|
50 | 50 | |
51 | - /** |
|
52 | - * Check whether string is validly base64url encoded. |
|
53 | - * |
|
54 | - * @link https://en.wikipedia.org/wiki/Base64#URL_applications |
|
55 | - * @param string $data |
|
56 | - * @return bool |
|
57 | - */ |
|
58 | - public static function isValidURLEncoding(string $data): bool |
|
59 | - { |
|
60 | - return preg_match('#^[A-Za-z0-9\-_]*$#', $data) == 1; |
|
61 | - } |
|
51 | + /** |
|
52 | + * Check whether string is validly base64url encoded. |
|
53 | + * |
|
54 | + * @link https://en.wikipedia.org/wiki/Base64#URL_applications |
|
55 | + * @param string $data |
|
56 | + * @return bool |
|
57 | + */ |
|
58 | + public static function isValidURLEncoding(string $data): bool |
|
59 | + { |
|
60 | + return preg_match('#^[A-Za-z0-9\-_]*$#', $data) == 1; |
|
61 | + } |
|
62 | 62 | |
63 | - /** |
|
64 | - * Encode a string in base64. |
|
65 | - * |
|
66 | - * @link https://tools.ietf.org/html/rfc4648#section-4 |
|
67 | - * @param string $data |
|
68 | - * @throws \RuntimeException If encoding fails |
|
69 | - * @return string |
|
70 | - */ |
|
71 | - public static function encode(string $data): string |
|
72 | - { |
|
73 | - $ret = @base64_encode($data); |
|
74 | - if (!is_string($ret)) { |
|
75 | - $err = error_get_last(); |
|
76 | - $msg = isset($err) && __FILE__ == $err['file'] ? $err['message'] : null; |
|
77 | - throw new \RuntimeException($msg ?? "base64_encode() failed."); |
|
78 | - } |
|
79 | - return $ret; |
|
80 | - } |
|
63 | + /** |
|
64 | + * Encode a string in base64. |
|
65 | + * |
|
66 | + * @link https://tools.ietf.org/html/rfc4648#section-4 |
|
67 | + * @param string $data |
|
68 | + * @throws \RuntimeException If encoding fails |
|
69 | + * @return string |
|
70 | + */ |
|
71 | + public static function encode(string $data): string |
|
72 | + { |
|
73 | + $ret = @base64_encode($data); |
|
74 | + if (!is_string($ret)) { |
|
75 | + $err = error_get_last(); |
|
76 | + $msg = isset($err) && __FILE__ == $err['file'] ? $err['message'] : null; |
|
77 | + throw new \RuntimeException($msg ?? "base64_encode() failed."); |
|
78 | + } |
|
79 | + return $ret; |
|
80 | + } |
|
81 | 81 | |
82 | - /** |
|
83 | - * Decode a string from base64 encoding. |
|
84 | - * |
|
85 | - * @link https://tools.ietf.org/html/rfc4648#section-4 |
|
86 | - * @param string $data |
|
87 | - * @throws \RuntimeException If decoding fails |
|
88 | - * @return string |
|
89 | - */ |
|
90 | - public static function decode(string $data): string |
|
91 | - { |
|
92 | - $ret = base64_decode($data, true); |
|
93 | - if (!is_string($ret)) { |
|
94 | - $err = error_get_last(); |
|
95 | - $msg = isset($err) && __FILE__ == $err['file'] ? $err['message'] : null; |
|
96 | - throw new \RuntimeException($msg ?? "base64_decode() failed."); |
|
97 | - } |
|
98 | - return $ret; |
|
99 | - } |
|
82 | + /** |
|
83 | + * Decode a string from base64 encoding. |
|
84 | + * |
|
85 | + * @link https://tools.ietf.org/html/rfc4648#section-4 |
|
86 | + * @param string $data |
|
87 | + * @throws \RuntimeException If decoding fails |
|
88 | + * @return string |
|
89 | + */ |
|
90 | + public static function decode(string $data): string |
|
91 | + { |
|
92 | + $ret = base64_decode($data, true); |
|
93 | + if (!is_string($ret)) { |
|
94 | + $err = error_get_last(); |
|
95 | + $msg = isset($err) && __FILE__ == $err['file'] ? $err['message'] : null; |
|
96 | + throw new \RuntimeException($msg ?? "base64_decode() failed."); |
|
97 | + } |
|
98 | + return $ret; |
|
99 | + } |
|
100 | 100 | |
101 | - /** |
|
102 | - * Check whether string is validly base64 encoded. |
|
103 | - * |
|
104 | - * @link https://tools.ietf.org/html/rfc4648#section-4 |
|
105 | - * @param string $data |
|
106 | - * @return bool |
|
107 | - */ |
|
108 | - public static function isValid(string $data): bool |
|
109 | - { |
|
110 | - return preg_match('#^[A-Za-z0-9+/]*={0,2}$#', $data) == 1; |
|
111 | - } |
|
101 | + /** |
|
102 | + * Check whether string is validly base64 encoded. |
|
103 | + * |
|
104 | + * @link https://tools.ietf.org/html/rfc4648#section-4 |
|
105 | + * @param string $data |
|
106 | + * @return bool |
|
107 | + */ |
|
108 | + public static function isValid(string $data): bool |
|
109 | + { |
|
110 | + return preg_match('#^[A-Za-z0-9+/]*={0,2}$#', $data) == 1; |
|
111 | + } |
|
112 | 112 | } |
@@ -7,25 +7,25 @@ |
||
7 | 7 | */ |
8 | 8 | trait ArrayParameterValue |
9 | 9 | { |
10 | - /** |
|
11 | - * Constructor. |
|
12 | - * |
|
13 | - * @param mixed ...$values |
|
14 | - */ |
|
15 | - abstract public function __construct(...$values); |
|
10 | + /** |
|
11 | + * Constructor. |
|
12 | + * |
|
13 | + * @param mixed ...$values |
|
14 | + */ |
|
15 | + abstract public function __construct(...$values); |
|
16 | 16 | |
17 | - /** |
|
18 | - * Initialize from a JSON value. |
|
19 | - * |
|
20 | - * @param array $value |
|
21 | - * @return static |
|
22 | - */ |
|
23 | - public static function fromJSONValue($value) |
|
24 | - { |
|
25 | - if (!is_array($value)) { |
|
26 | - throw new \UnexpectedValueException( |
|
27 | - get_called_class() . " expects an array parameter."); |
|
28 | - } |
|
29 | - return new static(...$value); |
|
30 | - } |
|
17 | + /** |
|
18 | + * Initialize from a JSON value. |
|
19 | + * |
|
20 | + * @param array $value |
|
21 | + * @return static |
|
22 | + */ |
|
23 | + public static function fromJSONValue($value) |
|
24 | + { |
|
25 | + if (!is_array($value)) { |
|
26 | + throw new \UnexpectedValueException( |
|
27 | + get_called_class() . " expects an array parameter."); |
|
28 | + } |
|
29 | + return new static(...$value); |
|
30 | + } |
|
31 | 31 | } |
@@ -18,92 +18,92 @@ |
||
18 | 18 | */ |
19 | 19 | abstract class HMACAlgorithm extends SignatureAlgorithm |
20 | 20 | { |
21 | - /** |
|
22 | - * Shared secret key. |
|
23 | - * |
|
24 | - * @var string $_key |
|
25 | - */ |
|
26 | - protected $_key; |
|
21 | + /** |
|
22 | + * Shared secret key. |
|
23 | + * |
|
24 | + * @var string $_key |
|
25 | + */ |
|
26 | + protected $_key; |
|
27 | 27 | |
28 | - /** |
|
29 | - * Mapping from algorithm name to class name. |
|
30 | - * |
|
31 | - * @internal |
|
32 | - * |
|
33 | - * @var array |
|
34 | - */ |
|
35 | - const MAP_ALGO_TO_CLASS = array( |
|
36 | - /* @formatter:off */ |
|
37 | - JWA::ALGO_HS256 => HS256Algorithm::class, |
|
38 | - JWA::ALGO_HS384 => HS384Algorithm::class, |
|
39 | - JWA::ALGO_HS512 => HS512Algorithm::class |
|
40 | - /* @formatter:on */ |
|
41 | - ); |
|
28 | + /** |
|
29 | + * Mapping from algorithm name to class name. |
|
30 | + * |
|
31 | + * @internal |
|
32 | + * |
|
33 | + * @var array |
|
34 | + */ |
|
35 | + const MAP_ALGO_TO_CLASS = array( |
|
36 | + /* @formatter:off */ |
|
37 | + JWA::ALGO_HS256 => HS256Algorithm::class, |
|
38 | + JWA::ALGO_HS384 => HS384Algorithm::class, |
|
39 | + JWA::ALGO_HS512 => HS512Algorithm::class |
|
40 | + /* @formatter:on */ |
|
41 | + ); |
|
42 | 42 | |
43 | - /** |
|
44 | - * Get algorithm name recognized by the Hash extension. |
|
45 | - * |
|
46 | - * @return string |
|
47 | - */ |
|
48 | - abstract protected function _hashAlgo(): string; |
|
43 | + /** |
|
44 | + * Get algorithm name recognized by the Hash extension. |
|
45 | + * |
|
46 | + * @return string |
|
47 | + */ |
|
48 | + abstract protected function _hashAlgo(): string; |
|
49 | 49 | |
50 | - /** |
|
51 | - * Constructor. |
|
52 | - * |
|
53 | - * @param string $key Shared secret key |
|
54 | - */ |
|
55 | - public function __construct(string $key) |
|
56 | - { |
|
57 | - $this->_key = $key; |
|
58 | - } |
|
50 | + /** |
|
51 | + * Constructor. |
|
52 | + * |
|
53 | + * @param string $key Shared secret key |
|
54 | + */ |
|
55 | + public function __construct(string $key) |
|
56 | + { |
|
57 | + $this->_key = $key; |
|
58 | + } |
|
59 | 59 | |
60 | - /** |
|
61 | - * |
|
62 | - * {@inheritdoc} |
|
63 | - */ |
|
64 | - public static function fromJWK(JWK $jwk, Header $header): self |
|
65 | - { |
|
66 | - $jwk = SymmetricKeyJWK::fromJWK($jwk); |
|
67 | - $alg = JWA::deriveAlgorithmName($header, $jwk); |
|
68 | - if (!array_key_exists($alg, self::MAP_ALGO_TO_CLASS)) { |
|
69 | - throw new \UnexpectedValueException("Unsupported algorithm '$alg'."); |
|
70 | - } |
|
71 | - $cls = self::MAP_ALGO_TO_CLASS[$alg]; |
|
72 | - return new $cls($jwk->key()); |
|
73 | - } |
|
60 | + /** |
|
61 | + * |
|
62 | + * {@inheritdoc} |
|
63 | + */ |
|
64 | + public static function fromJWK(JWK $jwk, Header $header): self |
|
65 | + { |
|
66 | + $jwk = SymmetricKeyJWK::fromJWK($jwk); |
|
67 | + $alg = JWA::deriveAlgorithmName($header, $jwk); |
|
68 | + if (!array_key_exists($alg, self::MAP_ALGO_TO_CLASS)) { |
|
69 | + throw new \UnexpectedValueException("Unsupported algorithm '$alg'."); |
|
70 | + } |
|
71 | + $cls = self::MAP_ALGO_TO_CLASS[$alg]; |
|
72 | + return new $cls($jwk->key()); |
|
73 | + } |
|
74 | 74 | |
75 | - /** |
|
76 | - * |
|
77 | - * {@inheritdoc} |
|
78 | - */ |
|
79 | - public function computeSignature(string $data): string |
|
80 | - { |
|
81 | - $result = @hash_hmac($this->_hashAlgo(), $data, $this->_key, true); |
|
82 | - if (false === $result) { |
|
83 | - $err = error_get_last(); |
|
84 | - $msg = isset($err) && __FILE__ == $err['file'] ? $err['message'] : null; |
|
85 | - throw new \RuntimeException($msg ?? 'hash_hmac() failed.'); |
|
86 | - } |
|
87 | - return $result; |
|
88 | - } |
|
75 | + /** |
|
76 | + * |
|
77 | + * {@inheritdoc} |
|
78 | + */ |
|
79 | + public function computeSignature(string $data): string |
|
80 | + { |
|
81 | + $result = @hash_hmac($this->_hashAlgo(), $data, $this->_key, true); |
|
82 | + if (false === $result) { |
|
83 | + $err = error_get_last(); |
|
84 | + $msg = isset($err) && __FILE__ == $err['file'] ? $err['message'] : null; |
|
85 | + throw new \RuntimeException($msg ?? 'hash_hmac() failed.'); |
|
86 | + } |
|
87 | + return $result; |
|
88 | + } |
|
89 | 89 | |
90 | - /** |
|
91 | - * |
|
92 | - * {@inheritdoc} |
|
93 | - */ |
|
94 | - public function validateSignature(string $data, string $signature): bool |
|
95 | - { |
|
96 | - return $this->computeSignature($data) === $signature; |
|
97 | - } |
|
90 | + /** |
|
91 | + * |
|
92 | + * {@inheritdoc} |
|
93 | + */ |
|
94 | + public function validateSignature(string $data, string $signature): bool |
|
95 | + { |
|
96 | + return $this->computeSignature($data) === $signature; |
|
97 | + } |
|
98 | 98 | |
99 | - /** |
|
100 | - * |
|
101 | - * @see \JWX\JWS\SignatureAlgorithm::headerParameters() |
|
102 | - * @return \JWX\JWT\Parameter\JWTParameter[] |
|
103 | - */ |
|
104 | - public function headerParameters(): array |
|
105 | - { |
|
106 | - return array_merge(parent::headerParameters(), |
|
107 | - array(AlgorithmParameter::fromAlgorithm($this))); |
|
108 | - } |
|
99 | + /** |
|
100 | + * |
|
101 | + * @see \JWX\JWS\SignatureAlgorithm::headerParameters() |
|
102 | + * @return \JWX\JWT\Parameter\JWTParameter[] |
|
103 | + */ |
|
104 | + public function headerParameters(): array |
|
105 | + { |
|
106 | + return array_merge(parent::headerParameters(), |
|
107 | + array(AlgorithmParameter::fromAlgorithm($this))); |
|
108 | + } |
|
109 | 109 | } |
@@ -13,21 +13,21 @@ |
||
13 | 13 | */ |
14 | 14 | class HS256Algorithm extends HMACAlgorithm |
15 | 15 | { |
16 | - /** |
|
17 | - * |
|
18 | - * {@inheritdoc} |
|
19 | - */ |
|
20 | - protected function _hashAlgo(): string |
|
21 | - { |
|
22 | - return "sha256"; |
|
23 | - } |
|
16 | + /** |
|
17 | + * |
|
18 | + * {@inheritdoc} |
|
19 | + */ |
|
20 | + protected function _hashAlgo(): string |
|
21 | + { |
|
22 | + return "sha256"; |
|
23 | + } |
|
24 | 24 | |
25 | - /** |
|
26 | - * |
|
27 | - * {@inheritdoc} |
|
28 | - */ |
|
29 | - public function algorithmParamValue(): string |
|
30 | - { |
|
31 | - return JWA::ALGO_HS256; |
|
32 | - } |
|
25 | + /** |
|
26 | + * |
|
27 | + * {@inheritdoc} |
|
28 | + */ |
|
29 | + public function algorithmParamValue(): string |
|
30 | + { |
|
31 | + return JWA::ALGO_HS256; |
|
32 | + } |
|
33 | 33 | } |