Complex classes like ClientAssertionJwt 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 ClientAssertionJwt, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
35 | class ClientAssertionJwt implements AuthenticationMethod |
||
36 | { |
||
37 | private const CLIENT_ASSERTION_TYPE = 'urn:ietf:params:oauth:client-assertion-type:jwt-bearer'; |
||
38 | |||
39 | /** |
||
40 | * @var JWSVerifier |
||
41 | */ |
||
42 | private $jwsVerifier; |
||
43 | |||
44 | /** |
||
45 | * @var null|TrustedIssuerRepository |
||
46 | */ |
||
47 | private $trustedIssuerRepository = null; |
||
48 | |||
49 | /** |
||
50 | * @var null|JKUFactory |
||
51 | */ |
||
52 | private $jkuFactory = null; |
||
53 | |||
54 | /** |
||
55 | * @var null|JWELoader |
||
56 | */ |
||
57 | private $jweLoader = null; |
||
58 | |||
59 | /** |
||
60 | * @var null|JWKSet |
||
61 | */ |
||
62 | private $keyEncryptionKeySet = null; |
||
63 | |||
64 | /** |
||
65 | * @var bool |
||
66 | */ |
||
67 | private $encryptionRequired = false; |
||
68 | |||
69 | /** |
||
70 | * @var int |
||
71 | */ |
||
72 | private $secretLifetime; |
||
73 | |||
74 | /** |
||
75 | * @var HeaderCheckerManager |
||
76 | */ |
||
77 | private $headerCheckerManager; |
||
78 | |||
79 | /** |
||
80 | * @var ClaimCheckerManager |
||
81 | */ |
||
82 | private $claimCheckerManager; |
||
83 | |||
84 | /** |
||
85 | * @var JsonConverter |
||
86 | */ |
||
87 | private $jsonConverter; |
||
88 | |||
89 | /** |
||
90 | * ClientAssertionJwt constructor. |
||
91 | * |
||
92 | * @param JsonConverter $jsonConverter |
||
93 | * @param JWSVerifier $jwsVerifier |
||
94 | * @param HeaderCheckerManager $headerCheckerManager |
||
95 | * @param ClaimCheckerManager $claimCheckerManager |
||
96 | * @param int $secretLifetime |
||
97 | */ |
||
98 | public function __construct(JsonConverter $jsonConverter, JWSVerifier $jwsVerifier, HeaderCheckerManager $headerCheckerManager, ClaimCheckerManager $claimCheckerManager, int $secretLifetime = 0) |
||
99 | { |
||
100 | if ($secretLifetime < 0) { |
||
101 | throw new \InvalidArgumentException('The secret lifetime must be at least 0 (= unlimited).'); |
||
102 | } |
||
103 | $this->jsonConverter = $jsonConverter; |
||
104 | $this->jwsVerifier = $jwsVerifier; |
||
105 | $this->headerCheckerManager = $headerCheckerManager; |
||
106 | $this->claimCheckerManager = $claimCheckerManager; |
||
107 | $this->secretLifetime = $secretLifetime; |
||
108 | } |
||
109 | |||
110 | /** |
||
111 | * @param TrustedIssuerRepository $trustedIssuerRepository |
||
112 | */ |
||
113 | public function enableTrustedIssuerSupport(TrustedIssuerRepository $trustedIssuerRepository) |
||
114 | { |
||
115 | $this->trustedIssuerRepository = $trustedIssuerRepository; |
||
116 | } |
||
117 | |||
118 | /** |
||
119 | * @param JKUFactory $jkuFactory |
||
120 | */ |
||
121 | public function enableJkuSupport(JKUFactory $jkuFactory) |
||
122 | { |
||
123 | $this->jkuFactory = $jkuFactory; |
||
124 | } |
||
125 | |||
126 | /** |
||
127 | * @param JWELoader $jweLoader |
||
128 | * @param JWKSet $keyEncryptionKeySet |
||
129 | * @param bool $encryptionRequired |
||
130 | */ |
||
131 | public function enableEncryptedAssertions(JWELoader $jweLoader, JWKSet $keyEncryptionKeySet, bool $encryptionRequired) |
||
132 | { |
||
133 | $this->jweLoader = $jweLoader; |
||
134 | $this->encryptionRequired = $encryptionRequired; |
||
135 | $this->keyEncryptionKeySet = $keyEncryptionKeySet; |
||
136 | } |
||
137 | |||
138 | /** |
||
139 | * @return string[] |
||
140 | */ |
||
141 | public function getSupportedSignatureAlgorithms(): array |
||
142 | { |
||
143 | return $this->jwsVerifier->getSignatureAlgorithmManager()->list(); |
||
144 | } |
||
145 | |||
146 | /** |
||
147 | * @return string[] |
||
148 | */ |
||
149 | public function getSupportedContentEncryptionAlgorithms(): array |
||
150 | { |
||
151 | return null === $this->jweLoader ? [] : $this->jweLoader->getJweDecrypter()->getContentEncryptionAlgorithmManager()->list(); |
||
152 | } |
||
153 | |||
154 | /** |
||
155 | * @return string[] |
||
156 | */ |
||
157 | public function getSupportedKeyEncryptionAlgorithms(): array |
||
158 | { |
||
159 | return null === $this->jweLoader ? [] : $this->jweLoader->getJweDecrypter()->getKeyEncryptionAlgorithmManager()->list(); |
||
160 | } |
||
161 | |||
162 | /** |
||
163 | * {@inheritdoc} |
||
164 | */ |
||
165 | public function getSchemesParameters(): array |
||
166 | { |
||
167 | return []; |
||
168 | } |
||
169 | |||
170 | /** |
||
171 | * {@inheritdoc} |
||
172 | */ |
||
173 | public function findClientIdAndCredentials(ServerRequestInterface $request, &$clientCredentials = null): ? ClientId |
||
174 | { |
||
175 | $parameters = RequestBodyParser::parseFormUrlEncoded($request); |
||
176 | if (!array_key_exists('client_assertion_type', $parameters)) { |
||
177 | return null; |
||
178 | } |
||
179 | $clientAssertionType = $parameters['client_assertion_type']; |
||
180 | |||
181 | if (self::CLIENT_ASSERTION_TYPE !== $clientAssertionType) { |
||
182 | return null; |
||
183 | } |
||
184 | if (!array_key_exists('client_assertion', $parameters)) { |
||
185 | throw new OAuth2Message(400, OAuth2Message::ERROR_INVALID_REQUEST, 'Parameter "client_assertion" is missing.'); |
||
186 | } |
||
187 | |||
188 | try { |
||
189 | $client_assertion = $parameters['client_assertion']; |
||
190 | $client_assertion = $this->tryToDecryptClientAssertion($client_assertion); |
||
191 | $serializer = new CompactSerializer($this->jsonConverter); |
||
192 | $jws = $serializer->unserialize($client_assertion); |
||
193 | $this->headerCheckerManager->check($jws, 0); |
||
194 | $claims = $this->jsonConverter->decode($jws->getPayload()); |
||
195 | $this->claimCheckerManager->check($claims); |
||
196 | } catch (OAuth2Message $e) { |
||
197 | throw $e; |
||
198 | } catch (\Exception $e) { |
||
199 | throw new OAuth2Message(400, OAuth2Message::ERROR_INVALID_REQUEST, 'Unable to load, decrypt or verify the client assertion.', [], $e); |
||
200 | } |
||
201 | |||
202 | // FIXME: Other claims can be considered as mandatory by the server |
||
203 | $diff = array_diff(['iss', 'sub', 'aud', 'exp'], array_keys($claims)); |
||
204 | if (!empty($diff)) { |
||
205 | throw new OAuth2Message(400, OAuth2Message::ERROR_INVALID_REQUEST, sprintf('The following claim(s) is/are mandatory: "%s".', implode(', ', array_values($diff)))); |
||
206 | } |
||
207 | |||
208 | $clientCredentials = $jws; |
||
209 | |||
210 | return ClientId::create($claims['sub']); |
||
211 | } |
||
212 | |||
213 | /** |
||
214 | * @param string $assertion |
||
215 | * |
||
216 | * @return string |
||
217 | * |
||
218 | * @throws OAuth2Message |
||
219 | */ |
||
220 | private function tryToDecryptClientAssertion(string $assertion): string |
||
241 | |||
242 | /** |
||
243 | * {@inheritdoc} |
||
244 | */ |
||
245 | public function isClientAuthenticated(Client $client, $clientCredentials, ServerRequestInterface $request): bool |
||
260 | |||
261 | /** |
||
262 | * {@inheritdoc} |
||
263 | */ |
||
264 | public function getSupportedMethods(): array |
||
268 | |||
269 | /** |
||
270 | * {@inheritdoc} |
||
271 | */ |
||
272 | public function checkClientConfiguration(DataBag $commandParameters, DataBag $validatedParameters): DataBag |
||
273 | { |
||
274 | switch ($commandParameters->get('token_endpoint_auth_method')) { |
||
275 | case 'client_secret_jwt': |
||
276 | return $this->checkClientSecretJwtConfiguration($commandParameters, $validatedParameters); |
||
277 | case 'private_key_jwt': |
||
283 | |||
284 | /** |
||
285 | * @param DataBag $commandParameters |
||
286 | * @param DataBag $validatedParameters |
||
287 | * |
||
288 | * @return DataBag |
||
289 | */ |
||
290 | private function checkClientSecretJwtConfiguration(DataBag $commandParameters, DataBag $validatedParameters): DataBag |
||
298 | |||
299 | /** |
||
300 | * @param DataBag $commandParameters |
||
301 | * @param DataBag $validatedParameters |
||
302 | * |
||
303 | * @return DataBag |
||
304 | */ |
||
305 | private function checkPrivateKeyJwtConfiguration(DataBag $commandParameters, DataBag $validatedParameters): DataBag |
||
326 | |||
327 | /** |
||
328 | * @return string |
||
329 | */ |
||
330 | private function createClientSecret(): string |
||
334 | |||
335 | /** |
||
336 | * @param Client $client |
||
337 | * @param JWS $jws |
||
338 | * @param array $claims |
||
339 | * |
||
340 | * @return JWKSet |
||
341 | */ |
||
342 | private function retrieveIssuerKeySet(Client $client, JWS $jws, array $claims): JWKSet |
||
363 | |||
364 | /** |
||
365 | * @param Client $client |
||
366 | * |
||
367 | * @return JWKSet |
||
368 | */ |
||
369 | private function getClientKeySet(Client $client): JWKSet |
||
390 | } |
||
391 |
This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.