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 | 11 | $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) |
|
| 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: