Complex classes like JWT often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use JWT, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
27 | class JWT |
||
28 | { |
||
29 | /** |
||
30 | * Type identifier for the signed JWT. |
||
31 | * |
||
32 | * @internal |
||
33 | * |
||
34 | * @var int |
||
35 | */ |
||
36 | const TYPE_JWS = 0; |
||
37 | |||
38 | /** |
||
39 | * Type identifier for the encrypted JWT. |
||
40 | * |
||
41 | * @internal |
||
42 | * |
||
43 | * @var int |
||
44 | */ |
||
45 | const TYPE_JWE = 1; |
||
46 | |||
47 | /** |
||
48 | * JWT parts. |
||
49 | * |
||
50 | * @var string[] |
||
51 | */ |
||
52 | protected $_parts; |
||
53 | |||
54 | /** |
||
55 | * JWT type. |
||
56 | * |
||
57 | * @var int |
||
58 | */ |
||
59 | protected $_type; |
||
60 | |||
61 | /** |
||
62 | * Constructor. |
||
63 | * |
||
64 | * @param string $token JWT string |
||
65 | * |
||
66 | * @throws \UnexpectedValueException |
||
67 | */ |
||
68 | 14 | public function __construct(string $token) |
|
69 | { |
||
70 | 14 | $this->_parts = explode('.', $token); |
|
71 | 14 | switch (count($this->_parts)) { |
|
72 | 14 | case 3: |
|
73 | 9 | $this->_type = self::TYPE_JWS; |
|
74 | 9 | break; |
|
75 | 5 | case 5: |
|
76 | 4 | $this->_type = self::TYPE_JWE; |
|
77 | 4 | break; |
|
78 | default: |
||
79 | 1 | throw new \UnexpectedValueException('Not a JWT token.'); |
|
80 | } |
||
81 | 13 | } |
|
82 | |||
83 | /** |
||
84 | * Convert JWT to string. |
||
85 | * |
||
86 | * @return string |
||
87 | */ |
||
88 | 1 | public function __toString(): string |
|
89 | { |
||
90 | 1 | return $this->token(); |
|
91 | } |
||
92 | |||
93 | /** |
||
94 | * Convert claims set to an unsecured JWT. |
||
95 | * |
||
96 | * Unsecured JWT is not signed nor encrypted neither integrity protected, |
||
97 | * and should thus be handled with care! |
||
98 | * |
||
99 | * @see https://tools.ietf.org/html/rfc7519#section-6 |
||
100 | * |
||
101 | * @param Claims $claims Claims set |
||
102 | * @param null|Header $header Optional header |
||
103 | * |
||
104 | * @throws \RuntimeException For generic errors |
||
105 | * |
||
106 | * @return self |
||
107 | */ |
||
108 | 3 | public static function unsecuredFromClaims(Claims $claims, |
|
112 | } |
||
113 | |||
114 | /** |
||
115 | * Convert claims set to a signed JWS token. |
||
116 | * |
||
117 | * @param Claims $claims Claims set |
||
118 | * @param SignatureAlgorithm $algo Signature algorithm |
||
119 | * @param null|Header $header Optional header |
||
120 | * |
||
121 | * @throws \RuntimeException For generic errors |
||
122 | * |
||
123 | * @return self |
||
124 | */ |
||
125 | 5 | public static function signedFromClaims(Claims $claims, |
|
131 | } |
||
132 | |||
133 | /** |
||
134 | * Convert claims set to an encrypted JWE token. |
||
135 | * |
||
136 | * @param Claims $claims Claims set |
||
137 | * @param KeyManagementAlgorithm $key_algo Key management algorithm |
||
138 | * @param ContentEncryptionAlgorithm $enc_algo Content encryption algorithm |
||
139 | * @param null|CompressionAlgorithm $zip_algo Optional compression algorithm |
||
140 | * @param null|Header $header Optional header |
||
141 | * |
||
142 | * @throws \RuntimeException For generic errors |
||
143 | * |
||
144 | * @return self |
||
145 | */ |
||
146 | 2 | public static function encryptedFromClaims(Claims $claims, |
|
147 | KeyManagementAlgorithm $key_algo, ContentEncryptionAlgorithm $enc_algo, |
||
148 | ?CompressionAlgorithm $zip_algo = null, ?Header $header = null): self |
||
149 | { |
||
150 | 2 | $payload = $claims->toJSON(); |
|
151 | 2 | $jwe = JWE::encrypt($payload, $key_algo, $enc_algo, $zip_algo, $header); |
|
152 | 2 | return new self($jwe->toCompact()); |
|
153 | } |
||
154 | |||
155 | /** |
||
156 | * Get claims from the JWT. |
||
157 | * |
||
158 | * Claims shall be validated according to given validation context. |
||
159 | * Validation context must contain all the necessary keys for the signature |
||
160 | * validation and/or content decryption. |
||
161 | * |
||
162 | * If validation context contains only one key, it shall be used explicitly. |
||
163 | * If multiple keys are provided, they must contain a JWK ID parameter for |
||
164 | * the key identification. |
||
165 | * |
||
166 | * @param ValidationContext $ctx |
||
167 | * |
||
168 | * @throws ValidationException if signature is invalid, or decryption fails, |
||
169 | * or claims validation fails |
||
170 | * @throws \RuntimeException For generic errors |
||
171 | * |
||
172 | * @return Claims |
||
173 | */ |
||
174 | 11 | public function claims(ValidationContext $ctx): Claims |
|
190 | } |
||
191 | |||
192 | /** |
||
193 | * Sign self producing a nested JWT. |
||
194 | * |
||
195 | * Note that if JWT is to be signed and encrypted, it should be done in |
||
196 | * sign-then-encrypt order. Please refer to links for security information. |
||
197 | * |
||
198 | * @see https://tools.ietf.org/html/rfc7519#section-11.2 |
||
199 | * |
||
200 | * @param SignatureAlgorithm $algo Signature algorithm |
||
201 | * @param null|Header $header Optional header |
||
202 | * |
||
203 | * @throws \RuntimeException For generic errors |
||
204 | * |
||
205 | * @return self |
||
206 | */ |
||
207 | 1 | public function signNested(SignatureAlgorithm $algo, ?Header $header = null): self |
|
208 | { |
||
209 | 1 | if (!isset($header)) { |
|
210 | 1 | $header = new Header(); |
|
211 | } |
||
212 | // add JWT content type parameter |
||
213 | 1 | $header = $header->withParameters( |
|
214 | 1 | new ContentTypeParameter(ContentTypeParameter::TYPE_JWT)); |
|
215 | 1 | $jws = JWS::sign($this->token(), $algo, $header); |
|
216 | 1 | return new self($jws->toCompact()); |
|
217 | } |
||
218 | |||
219 | /** |
||
220 | * Encrypt self producing a nested JWT. |
||
221 | * |
||
222 | * This JWT should be a JWS, that is, the order of nesting should be |
||
223 | * sign-then-encrypt. |
||
224 | * |
||
225 | * @see https://tools.ietf.org/html/rfc7519#section-11.2 |
||
226 | * |
||
227 | * @param KeyManagementAlgorithm $key_algo Key management algorithm |
||
228 | * @param ContentEncryptionAlgorithm $enc_algo Content encryption algorithm |
||
229 | * @param null|CompressionAlgorithm $zip_algo Optional compression algorithm |
||
230 | * @param null|Header $header Optional header |
||
231 | * |
||
232 | * @throws \RuntimeException For generic errors |
||
233 | * |
||
234 | * @return self |
||
235 | */ |
||
236 | 1 | public function encryptNested(KeyManagementAlgorithm $key_algo, |
|
237 | ContentEncryptionAlgorithm $enc_algo, |
||
238 | ?CompressionAlgorithm $zip_algo = null, ?Header $header = null): self |
||
239 | { |
||
240 | 1 | if (!isset($header)) { |
|
241 | 1 | $header = new Header(); |
|
242 | } |
||
243 | // add JWT content type parameter |
||
244 | 1 | $header = $header->withParameters( |
|
245 | 1 | new ContentTypeParameter(ContentTypeParameter::TYPE_JWT)); |
|
246 | 1 | $jwe = JWE::encrypt($this->token(), $key_algo, $enc_algo, $zip_algo, |
|
247 | 1 | $header); |
|
248 | 1 | return new self($jwe->toCompact()); |
|
249 | } |
||
250 | |||
251 | /** |
||
252 | * Whether JWT is a JWS. |
||
253 | * |
||
254 | * @return bool |
||
255 | */ |
||
256 | 16 | public function isJWS(): bool |
|
257 | { |
||
258 | 16 | return self::TYPE_JWS === $this->_type; |
|
259 | } |
||
260 | |||
261 | /** |
||
262 | * Get JWT as a JWS. |
||
263 | * |
||
264 | * @throws \LogicException |
||
265 | * |
||
266 | * @return JWS |
||
267 | */ |
||
268 | 12 | public function JWS(): JWS |
|
269 | { |
||
270 | 12 | if (!$this->isJWS()) { |
|
271 | 1 | throw new \LogicException('Not a JWS.'); |
|
272 | } |
||
273 | 11 | return JWS::fromParts($this->_parts); |
|
274 | } |
||
275 | |||
276 | /** |
||
277 | * Whether JWT is a JWE. |
||
278 | * |
||
279 | * @return bool |
||
280 | */ |
||
281 | 11 | public function isJWE(): bool |
|
282 | { |
||
283 | 11 | return self::TYPE_JWE === $this->_type; |
|
284 | } |
||
285 | |||
286 | /** |
||
287 | * Get JWT as a JWE. |
||
288 | * |
||
289 | * @throws \LogicException |
||
290 | * |
||
291 | * @return JWE |
||
292 | */ |
||
293 | 7 | public function JWE(): JWE |
|
294 | { |
||
295 | 7 | if (!$this->isJWE()) { |
|
296 | 1 | throw new \LogicException('Not a JWE.'); |
|
297 | } |
||
298 | 6 | return JWE::fromParts($this->_parts); |
|
299 | } |
||
300 | |||
301 | /** |
||
302 | * Check whether JWT contains another nested JWT. |
||
303 | * |
||
304 | * @return bool |
||
305 | */ |
||
306 | 9 | public function isNested(): bool |
|
307 | { |
||
308 | 9 | $header = $this->header(); |
|
309 | 9 | if (!$header->hasContentType()) { |
|
310 | 7 | return false; |
|
311 | } |
||
312 | 3 | $cty = $header->contentType()->value(); |
|
313 | 3 | if (ContentTypeParameter::TYPE_JWT !== $cty) { |
|
314 | 1 | return false; |
|
315 | } |
||
316 | 2 | return true; |
|
317 | } |
||
318 | |||
319 | /** |
||
320 | * Check whether JWT is unsecured, that is, it's neither integrity protected |
||
321 | * nor encrypted. |
||
322 | * |
||
323 | * @return bool |
||
324 | */ |
||
325 | 4 | public function isUnsecured(): bool |
|
333 | } |
||
334 | |||
335 | /** |
||
336 | * Get JWT header. |
||
337 | * |
||
338 | * @return JOSE |
||
339 | */ |
||
340 | 11 | public function header(): JOSE |
|
341 | { |
||
342 | 11 | $header = Header::fromJSON(Base64::urlDecode($this->_parts[0])); |
|
343 | 11 | return new JOSE($header); |
|
344 | } |
||
345 | |||
346 | /** |
||
347 | * Get JWT as a string. |
||
348 | * |
||
349 | * @return string |
||
350 | */ |
||
351 | 6 | public function token(): string |
|
352 | { |
||
353 | 6 | return implode('.', $this->_parts); |
|
354 | } |
||
355 | |||
356 | /** |
||
357 | * Get claims from a nested payload. |
||
358 | * |
||
359 | * @param string $payload JWT payload |
||
360 | * @param ValidationContext $ctx Validation context |
||
361 | * |
||
362 | * @return Claims |
||
363 | */ |
||
364 | 1 | private function _claimsFromNestedPayload(string $payload, |
|
365 | ValidationContext $ctx): Claims |
||
366 | { |
||
367 | 1 | $jwt = new JWT($payload); |
|
368 | // if this token secured, allow nested tokens to be unsecured. |
||
369 | 1 | if (!$this->isUnsecured()) { |
|
370 | 1 | $ctx = $ctx->withUnsecuredAllowed(true); |
|
371 | } |
||
372 | 1 | return $jwt->claims($ctx); |
|
373 | } |
||
374 | |||
375 | /** |
||
376 | * Get validated payload from JWS. |
||
377 | * |
||
378 | * @param JWS $jws JWS |
||
379 | * @param ValidationContext $ctx Validation context |
||
380 | * |
||
381 | * @throws ValidationException If signature validation fails |
||
382 | * |
||
383 | * @return string |
||
384 | */ |
||
385 | 8 | private static function _validatedPayloadFromJWS(JWS $jws, |
|
393 | } |
||
394 | |||
395 | /** |
||
396 | * Get validated payload from an unsecured JWS. |
||
397 | * |
||
398 | * @param JWS $jws JWS |
||
399 | * @param ValidationContext $ctx Validation context |
||
400 | * |
||
401 | * @throws ValidationException If unsecured JWT's are not allowed, or JWS |
||
402 | * token is malformed |
||
403 | * |
||
404 | * @return string |
||
405 | */ |
||
406 | 3 | private static function _validatedPayloadFromUnsecuredJWS(JWS $jws, |
|
416 | } |
||
417 | |||
418 | /** |
||
419 | * Get validated payload from a signed JWS. |
||
420 | * |
||
421 | * @param JWS $jws JWS |
||
422 | * @param JWKSet $keys Set of allowed keys for the signature validation |
||
423 | * |
||
424 | * @throws ValidationException If validation fails |
||
425 | * |
||
426 | * @return string |
||
427 | */ |
||
428 | 5 | private static function _validatedPayloadFromSignedJWS(JWS $jws, JWKSet $keys): string |
|
444 | } |
||
445 | |||
446 | /** |
||
447 | * Get validated payload from an encrypted JWE. |
||
448 | * |
||
449 | * @param JWE $jwe JWE |
||
450 | * @param ValidationContext $ctx Validation context |
||
451 | * |
||
452 | * @throws ValidationException If decryption fails |
||
453 | * |
||
454 | * @return string |
||
455 | */ |
||
471 |