Complex classes like IdTokenBuilder 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 IdTokenBuilder, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
34 | class IdTokenBuilder |
||
|
|||
35 | { |
||
36 | /** |
||
37 | * @var string |
||
38 | */ |
||
39 | private $issuer; |
||
40 | |||
41 | /** |
||
42 | * @var Client |
||
43 | */ |
||
44 | private $client; |
||
45 | |||
46 | /** |
||
47 | * @var UserAccount |
||
48 | */ |
||
49 | private $userAccount; |
||
50 | |||
51 | /** |
||
52 | * @var string |
||
53 | */ |
||
54 | private $redirectUri; |
||
55 | |||
56 | /** |
||
57 | * @var UserInfo |
||
58 | */ |
||
59 | private $userinfo; |
||
60 | |||
61 | /** |
||
62 | * @var JWKSet |
||
63 | */ |
||
64 | private $signatureKeys; |
||
65 | |||
66 | /** |
||
67 | * @var int |
||
68 | */ |
||
69 | private $lifetime; |
||
70 | |||
71 | /** |
||
72 | * @var string|null |
||
73 | */ |
||
74 | private $scope = null; |
||
75 | |||
76 | /** |
||
77 | * @var array |
||
78 | */ |
||
79 | private $requestedClaims = []; |
||
80 | |||
81 | /** |
||
82 | * @var string|null |
||
83 | */ |
||
84 | private $claimsLocales = null; |
||
85 | |||
86 | /** |
||
87 | * @var TokenId|null |
||
88 | */ |
||
89 | private $accessTokenId = null; |
||
90 | |||
91 | /** |
||
92 | * @var TokenId|null |
||
93 | */ |
||
94 | private $authorizationCodeId = null; |
||
95 | |||
96 | /** |
||
97 | * @var string|null |
||
98 | */ |
||
99 | private $nonce = null; |
||
100 | |||
101 | /** |
||
102 | * @var bool |
||
103 | */ |
||
104 | private $withAuthenticationTime = false; |
||
105 | |||
106 | /** |
||
107 | * @var JWSBuilder|null |
||
108 | */ |
||
109 | private $jwsBuilder = null; |
||
110 | |||
111 | /** |
||
112 | * @var string|null |
||
113 | */ |
||
114 | private $signatureAlgorithm = null; |
||
115 | |||
116 | /** |
||
117 | * @var JWEBuilder|null |
||
118 | */ |
||
119 | private $jweBuilder; |
||
120 | |||
121 | /** |
||
122 | * @var string|null |
||
123 | */ |
||
124 | private $keyEncryptionAlgorithm = null; |
||
125 | |||
126 | /** |
||
127 | * @var string|null |
||
128 | */ |
||
129 | private $contentEncryptionAlgorithm = null; |
||
130 | |||
131 | /** |
||
132 | * @var \DateTimeImmutable|null |
||
133 | */ |
||
134 | private $expiresAt = null; |
||
135 | |||
136 | /** |
||
137 | * @var null|JKUFactory |
||
138 | */ |
||
139 | private $jkuFactory = null; |
||
140 | |||
141 | /** |
||
142 | * @var null|AuthorizationCodeRepository |
||
143 | */ |
||
144 | private $authorizationCodeRepository = null; |
||
145 | |||
146 | private function __construct(string $issuer, UserInfo $userinfo, int $lifetime, Client $client, UserAccount $userAccount, string $redirectUri, ?JKUFactory $jkuFactory, ?AuthorizationCodeRepository $authorizationCodeRepository) |
||
157 | |||
158 | public static function create(string $issuer, UserInfo $userinfo, int $lifetime, Client $client, UserAccount $userAccount, string $redirectUri, ?JKUFactory $jkuFactory, ?AuthorizationCodeRepository $authorizationCodeRepository): self |
||
162 | |||
163 | /** |
||
164 | * @param AccessToken $accessToken |
||
165 | * |
||
166 | * @return IdTokenBuilder |
||
167 | */ |
||
168 | public function withAccessToken(AccessToken $accessToken): self |
||
193 | |||
194 | /** |
||
195 | * @param AccessTokenId $accessTokenId |
||
196 | * |
||
197 | * @return IdTokenBuilder |
||
198 | */ |
||
199 | public function withAccessTokenId(AccessTokenId $accessTokenId): self |
||
206 | |||
207 | /** |
||
208 | * @param AuthorizationCodeId $authorizationCodeId |
||
209 | * |
||
210 | * @return IdTokenBuilder |
||
211 | */ |
||
212 | public function withAuthorizationCodeId(AuthorizationCodeId $authorizationCodeId): self |
||
219 | |||
220 | /** |
||
221 | * @param string $claimsLocales |
||
222 | * |
||
223 | * @return IdTokenBuilder |
||
224 | */ |
||
225 | public function withClaimsLocales(string $claimsLocales): self |
||
232 | |||
233 | /** |
||
234 | * @return IdTokenBuilder |
||
235 | */ |
||
236 | public function withAuthenticationTime(): self |
||
243 | |||
244 | /** |
||
245 | * @param string $scope |
||
246 | * |
||
247 | * @return IdTokenBuilder |
||
248 | */ |
||
249 | public function withScope(string $scope): self |
||
256 | |||
257 | /** |
||
258 | * @param array $requestedClaims |
||
259 | * |
||
260 | * @return IdTokenBuilder |
||
261 | */ |
||
262 | public function withRequestedClaims(array $requestedClaims): self |
||
269 | |||
270 | /** |
||
271 | * @param string $nonce |
||
272 | * |
||
273 | * @return IdTokenBuilder |
||
274 | */ |
||
275 | public function withNonce(string $nonce): self |
||
282 | |||
283 | /** |
||
284 | * @param \DateTimeImmutable $expiresAt |
||
285 | * |
||
286 | * @return IdTokenBuilder |
||
287 | */ |
||
288 | public function withExpirationAt(\DateTimeImmutable $expiresAt): self |
||
295 | |||
296 | /** |
||
297 | * @return IdTokenBuilder |
||
298 | */ |
||
299 | public function withoutAuthenticationTime(): self |
||
306 | |||
307 | /** |
||
308 | * @param JWSBuilder $jwsBuilder |
||
309 | * @param JWKSet $signatureKeys |
||
310 | * @param string $signatureAlgorithm |
||
311 | * |
||
312 | * @return IdTokenBuilder |
||
313 | */ |
||
314 | public function withSignature(JWSBuilder $jwsBuilder, JWKSet $signatureKeys, string $signatureAlgorithm): self |
||
329 | |||
330 | /** |
||
331 | * @param JWEBuilder $jweBuilder |
||
332 | * @param string $keyEncryptionAlgorithm |
||
333 | * @param string $contentEncryptionAlgorithm |
||
334 | * |
||
335 | * @return IdTokenBuilder |
||
336 | */ |
||
337 | public function withEncryption(JWEBuilder $jweBuilder, string $keyEncryptionAlgorithm, string $contentEncryptionAlgorithm): self |
||
352 | |||
353 | /** |
||
354 | * @return string |
||
355 | */ |
||
356 | public function build(): string |
||
380 | |||
381 | /** |
||
382 | * @param array $claims |
||
383 | * |
||
384 | * @return array |
||
385 | */ |
||
386 | private function updateClaimsWithJwtClaims(array $claims): array |
||
401 | |||
402 | /** |
||
403 | * @param array $claims |
||
404 | * @param UserAccount $userAccount |
||
405 | * |
||
406 | * @return array |
||
407 | */ |
||
408 | private function updateClaimsWithAuthenticationTime(array $claims, UserAccount $userAccount): array |
||
416 | |||
417 | /** |
||
418 | * @param array $claims |
||
419 | * |
||
420 | * @return array |
||
421 | */ |
||
422 | private function updateClaimsWithNonce(array $claims): array |
||
430 | |||
431 | /** |
||
432 | * @param array $claims |
||
433 | * |
||
434 | * @return array |
||
435 | */ |
||
436 | private function updateClaimsAudience(array $claims): array |
||
446 | |||
447 | /** |
||
448 | * @param array $claims |
||
449 | * @param UserAccount $userAccount |
||
450 | * |
||
451 | * @return array |
||
452 | */ |
||
453 | private function updateClaimsWithAmrAndAcrInfo(array $claims, UserAccount $userAccount): array |
||
463 | |||
464 | /** |
||
465 | * @param array $claims |
||
466 | * |
||
467 | * @return string |
||
468 | */ |
||
469 | private function computeIdToken(array $claims): string |
||
484 | |||
485 | /** |
||
486 | * @param Client $client |
||
487 | * @param string $jwt |
||
488 | * |
||
489 | * @return string |
||
490 | */ |
||
491 | private function tryToEncrypt(Client $client, string $jwt): string |
||
516 | |||
517 | /** |
||
518 | * @param string $signatureAlgorithm |
||
519 | * |
||
520 | * @return JWK |
||
521 | */ |
||
522 | private function getSignatureKey(string $signatureAlgorithm): JWK |
||
544 | |||
545 | /** |
||
546 | * @param JWK $signatureKey |
||
547 | * @param string $signatureAlgorithm |
||
548 | * |
||
549 | * @return array |
||
550 | */ |
||
551 | private function getHeaders(JWK $signatureKey, string $signatureAlgorithm): array |
||
563 | |||
564 | /** |
||
565 | * @param array $claims |
||
566 | * |
||
567 | * @return array |
||
568 | */ |
||
569 | private function updateClaimsWithTokenHash(array $claims): array |
||
583 | |||
584 | /** |
||
585 | * @param TokenId $tokenId |
||
586 | * |
||
587 | * @return string |
||
588 | */ |
||
589 | private function getHash(TokenId $tokenId): string |
||
593 | |||
594 | /** |
||
595 | * @throws \InvalidArgumentException |
||
596 | * |
||
597 | * @return string |
||
598 | */ |
||
599 | private function getHashMethod(): string |
||
622 | |||
623 | /** |
||
624 | * @throws \InvalidArgumentException |
||
625 | * |
||
626 | * @return int |
||
627 | */ |
||
628 | private function getHashSize(): int |
||
651 | |||
652 | /** |
||
653 | * @param Client $client |
||
654 | * |
||
655 | * @return JWKSet |
||
656 | */ |
||
657 | private function getClientKeySet(Client $client): JWKSet |
||
689 | } |
||
690 |