Complex classes like AuthCodeGrant 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 AuthCodeGrant, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 28 | class AuthCodeGrant extends AbstractAuthorizeGrant |
||
| 29 | { |
||
| 30 | /** |
||
| 31 | * @var DateInterval |
||
| 32 | */ |
||
| 33 | private $authCodeTTL; |
||
| 34 | |||
| 35 | /** |
||
| 36 | * @var bool |
||
| 37 | */ |
||
| 38 | private $enableCodeExchangeProof = false; |
||
| 39 | |||
| 40 | /** |
||
| 41 | * @param AuthCodeRepositoryInterface $authCodeRepository |
||
| 42 | * @param RefreshTokenRepositoryInterface $refreshTokenRepository |
||
| 43 | * @param DateInterval $authCodeTTL |
||
| 44 | * |
||
| 45 | * @throws Exception |
||
| 46 | */ |
||
| 47 | 41 | public function __construct( |
|
| 48 | AuthCodeRepositoryInterface $authCodeRepository, |
||
| 49 | RefreshTokenRepositoryInterface $refreshTokenRepository, |
||
| 50 | DateInterval $authCodeTTL |
||
| 51 | ) { |
||
| 52 | 41 | $this->setAuthCodeRepository($authCodeRepository); |
|
| 53 | 41 | $this->setRefreshTokenRepository($refreshTokenRepository); |
|
| 54 | 41 | $this->authCodeTTL = $authCodeTTL; |
|
| 55 | 41 | $this->refreshTokenTTL = new DateInterval('P1M'); |
|
| 56 | 41 | } |
|
| 57 | |||
| 58 | 13 | public function enableCodeExchangeProof() |
|
| 59 | { |
||
| 60 | 13 | $this->enableCodeExchangeProof = true; |
|
| 61 | 13 | } |
|
| 62 | |||
| 63 | /** |
||
| 64 | * Respond to an access token request. |
||
| 65 | * |
||
| 66 | * @param ServerRequestInterface $request |
||
| 67 | * @param ResponseTypeInterface $responseType |
||
| 68 | * @param DateInterval $accessTokenTTL |
||
| 69 | * |
||
| 70 | * @throws OAuthServerException |
||
| 71 | * |
||
| 72 | * @return ResponseTypeInterface |
||
| 73 | */ |
||
| 74 | 18 | public function respondToAccessTokenRequest( |
|
| 75 | ServerRequestInterface $request, |
||
| 76 | ResponseTypeInterface $responseType, |
||
| 77 | DateInterval $accessTokenTTL |
||
| 78 | ) { |
||
| 79 | // Validate request |
||
| 80 | 18 | $client = $this->validateClient($request); |
|
| 81 | 18 | $encryptedAuthCode = $this->getRequestParameter('code', $request, null); |
|
| 82 | |||
| 83 | 18 | if ($encryptedAuthCode === null) { |
|
| 84 | 1 | throw OAuthServerException::invalidRequest('code'); |
|
| 85 | } |
||
| 86 | |||
| 87 | try { |
||
| 88 | 17 | $authCodePayload = json_decode($this->decrypt($encryptedAuthCode)); |
|
| 89 | |||
| 90 | 16 | $this->validateAuthorizationCode($authCodePayload, $client, $request); |
|
| 91 | |||
| 92 | 11 | $scopes = $this->scopeRepository->finalizeScopes( |
|
| 93 | 11 | $this->validateScopes($authCodePayload->scopes), |
|
| 94 | 11 | $this->getIdentifier(), |
|
| 95 | $client, |
||
| 96 | 11 | $authCodePayload->user_id |
|
| 97 | ); |
||
| 98 | 6 | } catch (LogicException $e) { |
|
| 99 | 1 | throw OAuthServerException::invalidRequest('code', 'Cannot decrypt the authorization code', $e); |
|
| 100 | } |
||
| 101 | |||
| 102 | // Validate code challenge |
||
| 103 | 11 | if ($this->enableCodeExchangeProof === true) { |
|
| 104 | 7 | $codeVerifier = $this->getRequestParameter('code_verifier', $request, null); |
|
| 105 | |||
| 106 | 7 | if ($codeVerifier === null) { |
|
| 107 | 1 | throw OAuthServerException::invalidRequest('code_verifier'); |
|
| 108 | } |
||
| 109 | |||
| 110 | // Validate code_verifier according to RFC-7636 |
||
| 111 | // @see: https://tools.ietf.org/html/rfc7636#section-4.1 |
||
| 112 | 6 | if (preg_match('/^[A-Za-z0-9-._~]{43,128}$/', $codeVerifier) !== 1) { |
|
| 113 | 3 | throw OAuthServerException::invalidRequest( |
|
| 114 | 3 | 'code_verifier', |
|
| 115 | 3 | 'Code Verifier must follow the specifications of RFC-7636.' |
|
| 116 | ); |
||
| 117 | } |
||
| 118 | |||
| 119 | 3 | switch ($authCodePayload->code_challenge_method) { |
|
| 120 | 3 | case 'plain': |
|
| 121 | 2 | if (hash_equals($codeVerifier, $authCodePayload->code_challenge) === false) { |
|
| 122 | 1 | throw OAuthServerException::invalidGrant('Failed to verify `code_verifier`.'); |
|
| 123 | } |
||
| 124 | |||
| 125 | 1 | break; |
|
| 126 | 1 | case 'S256': |
|
| 127 | if ( |
||
| 128 | 1 | hash_equals( |
|
| 129 | 1 | strtr(rtrim(base64_encode(hash('sha256', $codeVerifier, true)), '='), '+/', '-_'), |
|
| 130 | 1 | $authCodePayload->code_challenge |
|
| 131 | 1 | ) === false |
|
| 132 | ) { |
||
| 133 | throw OAuthServerException::invalidGrant('Failed to verify `code_verifier`.'); |
||
| 134 | } |
||
| 135 | // @codeCoverageIgnoreStart |
||
| 136 | break; |
||
| 137 | default: |
||
| 138 | throw OAuthServerException::serverError( |
||
| 139 | sprintf( |
||
| 140 | 'Unsupported code challenge method `%s`', |
||
| 141 | $authCodePayload->code_challenge_method |
||
| 142 | ) |
||
| 143 | ); |
||
| 144 | // @codeCoverageIgnoreEnd |
||
| 145 | } |
||
| 146 | } |
||
| 147 | |||
| 148 | // Issue and persist access + refresh tokens |
||
| 149 | 6 | $accessToken = $this->issueAccessToken($accessTokenTTL, $client, $authCodePayload->user_id, $scopes); |
|
| 150 | 6 | $refreshToken = $this->issueRefreshToken($accessToken); |
|
| 151 | |||
| 152 | // Send events to emitter |
||
| 153 | 4 | $this->getEmitter()->emit(new RequestEvent(RequestEvent::ACCESS_TOKEN_ISSUED, $request)); |
|
| 154 | 4 | $this->getEmitter()->emit(new RequestEvent(RequestEvent::REFRESH_TOKEN_ISSUED, $request)); |
|
| 155 | |||
| 156 | // Inject tokens into response type |
||
| 157 | 4 | $responseType->setAccessToken($accessToken); |
|
|
|
|||
| 158 | 4 | $responseType->setRefreshToken($refreshToken); |
|
| 159 | |||
| 160 | // Revoke used auth code |
||
| 161 | 4 | $this->authCodeRepository->revokeAuthCode($authCodePayload->auth_code_id); |
|
| 162 | |||
| 163 | 4 | return $responseType; |
|
| 164 | } |
||
| 165 | |||
| 166 | /** |
||
| 167 | * Validate the authorization code. |
||
| 168 | * |
||
| 169 | * @param stdClass $authCodePayload |
||
| 170 | * @param ClientEntityInterface $client |
||
| 171 | * @param ServerRequestInterface $request |
||
| 172 | */ |
||
| 173 | 16 | private function validateAuthorizationCode( |
|
| 200 | |||
| 201 | /** |
||
| 202 | * Return the grant identifier that can be used in matching up requests. |
||
| 203 | * |
||
| 204 | * @return string |
||
| 205 | */ |
||
| 206 | 33 | public function getIdentifier() |
|
| 210 | |||
| 211 | /** |
||
| 212 | * {@inheritdoc} |
||
| 213 | */ |
||
| 214 | 3 | public function canRespondToAuthorizationRequest(ServerRequestInterface $request) |
|
| 222 | |||
| 223 | /** |
||
| 224 | * {@inheritdoc} |
||
| 225 | */ |
||
| 226 | 14 | public function validateAuthorizationRequest(ServerRequestInterface $request) |
|
| 227 | { |
||
| 228 | 14 | $clientId = $this->getQueryStringParameter( |
|
| 229 | 14 | 'client_id', |
|
| 230 | $request, |
||
| 231 | 14 | $this->getServerParameter('PHP_AUTH_USER', $request) |
|
| 232 | ); |
||
| 233 | |||
| 234 | 14 | if ($clientId === null) { |
|
| 235 | 1 | throw OAuthServerException::invalidRequest('client_id'); |
|
| 236 | } |
||
| 237 | |||
| 238 | 13 | $client = $this->clientRepository->getClientEntity( |
|
| 239 | 13 | $clientId, |
|
| 240 | 13 | $this->getIdentifier(), |
|
| 241 | 13 | null, |
|
| 242 | 13 | false |
|
| 243 | ); |
||
| 244 | |||
| 245 | 13 | if ($client instanceof ClientEntityInterface === false) { |
|
| 246 | 1 | $this->getEmitter()->emit(new RequestEvent(RequestEvent::CLIENT_AUTHENTICATION_FAILED, $request)); |
|
| 247 | 1 | throw OAuthServerException::invalidClient(); |
|
| 248 | } |
||
| 249 | |||
| 250 | 12 | $redirectUri = $this->getQueryStringParameter('redirect_uri', $request); |
|
| 251 | |||
| 252 | 12 | if ($redirectUri !== null) { |
|
| 253 | 10 | $this->validateRedirectUri($redirectUri, $client, $request); |
|
| 254 | 2 | } elseif (empty($client->getRedirectUri()) || |
|
| 255 | 2 | (\is_array($client->getRedirectUri()) && \count($client->getRedirectUri()) !== 1)) { |
|
| 256 | 1 | $this->getEmitter()->emit(new RequestEvent(RequestEvent::CLIENT_AUTHENTICATION_FAILED, $request)); |
|
| 257 | 1 | throw OAuthServerException::invalidClient(); |
|
| 258 | } else { |
||
| 259 | 1 | $redirectUri = \is_array($client->getRedirectUri()) |
|
| 260 | ? $client->getRedirectUri()[0] |
||
| 261 | 1 | : $client->getRedirectUri(); |
|
| 262 | } |
||
| 263 | |||
| 264 | 9 | $scopes = $this->validateScopes( |
|
| 265 | 9 | $this->getQueryStringParameter('scope', $request, $this->defaultScope), |
|
| 266 | $redirectUri |
||
| 267 | ); |
||
| 268 | |||
| 269 | 9 | $stateParameter = $this->getQueryStringParameter('state', $request); |
|
| 270 | |||
| 271 | 9 | $authorizationRequest = new AuthorizationRequest(); |
|
| 272 | 9 | $authorizationRequest->setGrantTypeId($this->getIdentifier()); |
|
| 273 | 9 | $authorizationRequest->setClient($client); |
|
| 274 | 9 | $authorizationRequest->setRedirectUri($redirectUri); |
|
| 275 | |||
| 276 | 9 | if ($stateParameter !== null) { |
|
| 277 | $authorizationRequest->setState($stateParameter); |
||
| 278 | } |
||
| 279 | |||
| 280 | 9 | $authorizationRequest->setScopes($scopes); |
|
| 281 | |||
| 282 | 9 | if ($this->enableCodeExchangeProof === true) { |
|
| 283 | 6 | $codeChallenge = $this->getQueryStringParameter('code_challenge', $request); |
|
| 284 | 6 | if ($codeChallenge === null) { |
|
| 285 | 1 | throw OAuthServerException::invalidRequest('code_challenge'); |
|
| 286 | } |
||
| 287 | |||
| 288 | 5 | $codeChallengeMethod = $this->getQueryStringParameter('code_challenge_method', $request, 'plain'); |
|
| 289 | |||
| 290 | 5 | if (\in_array($codeChallengeMethod, ['plain', 'S256'], true) === false) { |
|
| 291 | 1 | throw OAuthServerException::invalidRequest( |
|
| 292 | 1 | 'code_challenge_method', |
|
| 293 | 1 | 'Code challenge method must be `plain` or `S256`' |
|
| 294 | ); |
||
| 295 | } |
||
| 296 | |||
| 297 | // Validate code_challenge according to RFC-7636 |
||
| 298 | // @see: https://tools.ietf.org/html/rfc7636#section-4.2 |
||
| 299 | 4 | if (preg_match('/^[A-Za-z0-9-._~]{43,128}$/', $codeChallenge) !== 1) { |
|
| 300 | 3 | throw OAuthServerException::invalidRequest( |
|
| 301 | 3 | 'code_challenged', |
|
| 302 | 3 | 'Code challenge must follow the specifications of RFC-7636.' |
|
| 303 | ); |
||
| 304 | } |
||
| 305 | |||
| 306 | 1 | $authorizationRequest->setCodeChallenge($codeChallenge); |
|
| 307 | 1 | $authorizationRequest->setCodeChallengeMethod($codeChallengeMethod); |
|
| 308 | } |
||
| 309 | |||
| 310 | 4 | return $authorizationRequest; |
|
| 311 | } |
||
| 312 | |||
| 313 | /** |
||
| 314 | * {@inheritdoc} |
||
| 315 | */ |
||
| 316 | 7 | public function completeAuthorizationRequest(AuthorizationRequest $authorizationRequest) |
|
| 375 | |||
| 376 | /** |
||
| 377 | * Get the client redirect URI if not set in the request. |
||
| 378 | * |
||
| 379 | * @param AuthorizationRequest $authorizationRequest |
||
| 380 | * |
||
| 381 | * @return string |
||
| 382 | */ |
||
| 383 | 6 | private function getClientRedirectUri(AuthorizationRequest $authorizationRequest) |
|
| 389 | } |
||
| 390 |
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: