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 |
||
34 | class ClientAssertionJwt implements AuthenticationMethod |
||
35 | { |
||
36 | private const CLIENT_ASSERTION_TYPE = 'urn:ietf:params:oauth:client-assertion-type:jwt-bearer'; |
||
37 | |||
38 | /** |
||
39 | * @var JWSVerifier |
||
40 | */ |
||
41 | private $jwsVerifier; |
||
42 | |||
43 | /** |
||
44 | * @var null|TrustedIssuerRepository |
||
45 | */ |
||
46 | private $trustedIssuerRepository = null; |
||
47 | |||
48 | /** |
||
49 | * @var null|JKUFactory |
||
50 | */ |
||
51 | private $jkuFactory = null; |
||
52 | |||
53 | /** |
||
54 | * @var null|JWELoader |
||
55 | */ |
||
56 | private $jweLoader = null; |
||
57 | |||
58 | /** |
||
59 | * @var null|JWKSet |
||
60 | */ |
||
61 | private $keyEncryptionKeySet = null; |
||
62 | |||
63 | /** |
||
64 | * @var bool |
||
65 | */ |
||
66 | private $encryptionRequired = false; |
||
67 | |||
68 | /** |
||
69 | * @var int |
||
70 | */ |
||
71 | private $secretLifetime; |
||
72 | |||
73 | /** |
||
74 | * @var HeaderCheckerManager |
||
75 | */ |
||
76 | private $headerCheckerManager; |
||
77 | |||
78 | /** |
||
79 | * @var ClaimCheckerManager |
||
80 | */ |
||
81 | private $claimCheckerManager; |
||
82 | |||
83 | /** |
||
84 | * @var JsonConverter |
||
85 | */ |
||
86 | private $jsonConverter; |
||
87 | |||
88 | /** |
||
89 | * ClientAssertionJwt constructor. |
||
90 | * |
||
91 | * @param JsonConverter $jsonConverter |
||
92 | * @param JWSVerifier $jwsVerifier |
||
93 | * @param HeaderCheckerManager $headerCheckerManager |
||
94 | * @param ClaimCheckerManager $claimCheckerManager |
||
95 | * @param int $secretLifetime |
||
96 | */ |
||
97 | public function __construct(JsonConverter $jsonConverter, JWSVerifier $jwsVerifier, HeaderCheckerManager $headerCheckerManager, ClaimCheckerManager $claimCheckerManager, int $secretLifetime = 0) |
||
98 | { |
||
99 | if ($secretLifetime < 0) { |
||
100 | throw new \InvalidArgumentException('The secret lifetime must be at least 0 (= unlimited).'); |
||
101 | } |
||
102 | $this->jsonConverter = $jsonConverter; |
||
103 | $this->jwsVerifier = $jwsVerifier; |
||
104 | $this->headerCheckerManager = $headerCheckerManager; |
||
105 | $this->claimCheckerManager = $claimCheckerManager; |
||
106 | $this->secretLifetime = $secretLifetime; |
||
107 | } |
||
108 | |||
109 | /** |
||
110 | * @param TrustedIssuerRepository $trustedIssuerRepository |
||
111 | */ |
||
112 | public function enableTrustedIssuerSupport(TrustedIssuerRepository $trustedIssuerRepository) |
||
113 | { |
||
114 | $this->trustedIssuerRepository = $trustedIssuerRepository; |
||
115 | } |
||
116 | |||
117 | /** |
||
118 | * @param JKUFactory $jkuFactory |
||
119 | */ |
||
120 | public function enableJkuSupport(JKUFactory $jkuFactory) |
||
121 | { |
||
122 | $this->jkuFactory = $jkuFactory; |
||
123 | } |
||
124 | |||
125 | /** |
||
126 | * @param JWELoader $jweLoader |
||
127 | * @param JWKSet $keyEncryptionKeySet |
||
128 | * @param bool $encryptionRequired |
||
129 | */ |
||
130 | public function enableEncryptedAssertions(JWELoader $jweLoader, JWKSet $keyEncryptionKeySet, bool $encryptionRequired) |
||
131 | { |
||
132 | $this->jweLoader = $jweLoader; |
||
133 | $this->encryptionRequired = $encryptionRequired; |
||
134 | $this->keyEncryptionKeySet = $keyEncryptionKeySet; |
||
135 | } |
||
136 | |||
137 | /** |
||
138 | * @return string[] |
||
139 | */ |
||
140 | public function getSupportedSignatureAlgorithms(): array |
||
141 | { |
||
142 | return $this->jwsVerifier->getSignatureAlgorithmManager()->list(); |
||
143 | } |
||
144 | |||
145 | /** |
||
146 | * @return string[] |
||
147 | */ |
||
148 | public function getSupportedContentEncryptionAlgorithms(): array |
||
149 | { |
||
150 | return null === $this->jweLoader ? [] : $this->jweLoader->getJweDecrypter()->getContentEncryptionAlgorithmManager()->list(); |
||
151 | } |
||
152 | |||
153 | /** |
||
154 | * @return string[] |
||
155 | */ |
||
156 | public function getSupportedKeyEncryptionAlgorithms(): array |
||
157 | { |
||
158 | return null === $this->jweLoader ? [] : $this->jweLoader->getJweDecrypter()->getKeyEncryptionAlgorithmManager()->list(); |
||
159 | } |
||
160 | |||
161 | /** |
||
162 | * {@inheritdoc} |
||
163 | */ |
||
164 | public function getSchemesParameters(): array |
||
165 | { |
||
166 | return []; |
||
167 | } |
||
168 | |||
169 | /** |
||
170 | * {@inheritdoc} |
||
171 | */ |
||
172 | public function findClientIdAndCredentials(ServerRequestInterface $request, &$clientCredentials = null): ? ClientId |
||
173 | { |
||
174 | $parameters = $request->getParsedBody() ?? []; |
||
175 | if (!array_key_exists('client_assertion_type', $parameters)) { |
||
176 | return null; |
||
177 | } |
||
178 | $clientAssertionType = $parameters['client_assertion_type']; |
||
179 | |||
180 | if (self::CLIENT_ASSERTION_TYPE !== $clientAssertionType) { |
||
181 | return null; |
||
182 | } |
||
183 | if (!array_key_exists('client_assertion', $parameters)) { |
||
184 | throw new OAuth2Exception(400, OAuth2Exception::ERROR_INVALID_REQUEST, 'Parameter "client_assertion" is missing.'); |
||
185 | } |
||
186 | |||
187 | try { |
||
188 | $client_assertion = $parameters['client_assertion']; |
||
189 | $client_assertion = $this->tryToDecryptClientAssertion($client_assertion); |
||
190 | $serializer = new CompactSerializer($this->jsonConverter); |
||
191 | $jws = $serializer->unserialize($client_assertion); |
||
192 | $this->headerCheckerManager->check($jws, 0); |
||
193 | $claims = $this->jsonConverter->decode($jws->getPayload()); |
||
194 | $this->claimCheckerManager->check($claims); |
||
195 | } catch (\Exception $e) { |
||
196 | throw new OAuth2Exception(400, OAuth2Exception::ERROR_INVALID_REQUEST, 'Unable to load, decrypt or verify the client assertion.', $e); |
||
197 | } |
||
198 | |||
199 | // FIXME: Other claims can be considered as mandatory by the server |
||
200 | $diff = array_diff(['iss', 'sub', 'aud', 'exp'], array_keys($claims)); |
||
201 | if (!empty($diff)) { |
||
202 | throw new OAuth2Exception(400, OAuth2Exception::ERROR_INVALID_REQUEST, sprintf('The following claim(s) is/are mandatory: "%s".', implode(', ', array_values($diff)))); |
||
203 | } |
||
204 | |||
205 | $clientCredentials = $jws; |
||
206 | |||
207 | return ClientId::create($claims['sub']); |
||
208 | } |
||
209 | |||
210 | /** |
||
211 | * @param string $assertion |
||
212 | * |
||
213 | * @return string |
||
214 | * |
||
215 | * @throws OAuth2Exception |
||
216 | */ |
||
217 | private function tryToDecryptClientAssertion(string $assertion): string |
||
238 | |||
239 | /** |
||
240 | * {@inheritdoc} |
||
241 | */ |
||
242 | public function isClientAuthenticated(Client $client, $clientCredentials, ServerRequestInterface $request): bool |
||
257 | |||
258 | /** |
||
259 | * {@inheritdoc} |
||
260 | */ |
||
261 | public function getSupportedMethods(): array |
||
265 | |||
266 | /** |
||
267 | * {@inheritdoc} |
||
268 | */ |
||
269 | public function checkClientConfiguration(DataBag $commandParameters, DataBag $validatedParameters): DataBag |
||
270 | { |
||
271 | switch ($commandParameters->get('token_endpoint_auth_method')) { |
||
272 | case 'client_secret_jwt': |
||
273 | return $this->processClientSecretJwtConfiguration($validatedParameters); |
||
274 | case 'private_key_jwt': |
||
280 | |||
281 | /** |
||
282 | * @param DataBag $validatedParameters |
||
283 | * |
||
284 | * @return DataBag |
||
285 | */ |
||
286 | private function processClientSecretJwtConfiguration(DataBag $validatedParameters): DataBag |
||
293 | |||
294 | /** |
||
295 | * @param DataBag $commandParameters |
||
296 | * @param DataBag $validatedParameters |
||
297 | * |
||
298 | * @return DataBag |
||
299 | */ |
||
300 | private function processPrivateKeyJwtConfiguration(DataBag $commandParameters, DataBag $validatedParameters): DataBag |
||
336 | |||
337 | /** |
||
338 | * @return string |
||
339 | */ |
||
340 | private function createClientSecret(): string |
||
344 | |||
345 | /** |
||
346 | * @param Client $client |
||
347 | * @param JWS $jws |
||
348 | * @param array $claims |
||
349 | * |
||
350 | * @return JWKSet |
||
351 | */ |
||
352 | private function retrieveIssuerKeySet(Client $client, JWS $jws, array $claims): JWKSet |
||
373 | |||
374 | /** |
||
375 | * @param Client $client |
||
376 | * |
||
377 | * @return JWKSet |
||
378 | */ |
||
379 | private function getClientKeySet(Client $client): JWKSet |
||
398 | } |
||
399 |
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.