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) |
||
| 108 | |||
| 109 | /** |
||
| 110 | * @param TrustedIssuerRepository $trustedIssuerRepository |
||
| 111 | */ |
||
| 112 | public function enableTrustedIssuerSupport(TrustedIssuerRepository $trustedIssuerRepository) |
||
| 116 | |||
| 117 | /** |
||
| 118 | * @param JKUFactory $jkuFactory |
||
| 119 | */ |
||
| 120 | public function enableJkuSupport(JKUFactory $jkuFactory) |
||
| 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) |
||
| 136 | |||
| 137 | /** |
||
| 138 | * @return string[] |
||
| 139 | */ |
||
| 140 | public function getSupportedSignatureAlgorithms(): array |
||
| 144 | |||
| 145 | /** |
||
| 146 | * @return string[] |
||
| 147 | */ |
||
| 148 | public function getSupportedContentEncryptionAlgorithms(): array |
||
| 152 | |||
| 153 | /** |
||
| 154 | * @return string[] |
||
| 155 | */ |
||
| 156 | public function getSupportedKeyEncryptionAlgorithms(): array |
||
| 160 | |||
| 161 | /** |
||
| 162 | * {@inheritdoc} |
||
| 163 | */ |
||
| 164 | public function getSchemesParameters(): array |
||
| 168 | |||
| 169 | /** |
||
| 170 | * {@inheritdoc} |
||
| 171 | */ |
||
| 172 | public function findClientIdAndCredentials(ServerRequestInterface $request, &$clientCredentials = null): ? ClientId |
||
| 212 | |||
| 213 | /** |
||
| 214 | * @param string $assertion |
||
| 215 | * |
||
| 216 | * @return string |
||
| 217 | * |
||
| 218 | * @throws OAuth2Exception |
||
| 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 |
||
| 315 | |||
| 316 | /** |
||
| 317 | * @return string |
||
| 318 | */ |
||
| 319 | private function createClientSecret(): string |
||
| 323 | |||
| 324 | /** |
||
| 325 | * @param Client $client |
||
| 326 | * @param JWS $jws |
||
| 327 | * @param array $claims |
||
| 328 | * |
||
| 329 | * @return JWKSet |
||
| 330 | */ |
||
| 331 | private function retrieveIssuerKeySet(Client $client, JWS $jws, array $claims): JWKSet |
||
| 352 | |||
| 353 | /** |
||
| 354 | * @param Client $client |
||
| 355 | * |
||
| 356 | * @return JWKSet |
||
| 357 | */ |
||
| 358 | private function getClientKeySet(Client $client): JWKSet |
||
| 377 | } |
||
| 378 |
Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code: