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 |
||
| 24 | class AuthCodeGrant extends AbstractAuthorizeGrant |
||
| 25 | { |
||
| 26 | /** |
||
| 27 | * @var \DateInterval |
||
| 28 | */ |
||
| 29 | private $authCodeTTL; |
||
| 30 | |||
| 31 | /** |
||
| 32 | * @var bool |
||
| 33 | */ |
||
| 34 | private $enableCodeExchangeProof = false; |
||
| 35 | |||
| 36 | /** |
||
| 37 | * @param AuthCodeRepositoryInterface $authCodeRepository |
||
| 38 | * @param RefreshTokenRepositoryInterface $refreshTokenRepository |
||
| 39 | * @param \DateInterval $authCodeTTL |
||
| 40 | * |
||
| 41 | * @throws \Exception |
||
| 42 | */ |
||
| 43 | 41 | public function __construct( |
|
| 44 | AuthCodeRepositoryInterface $authCodeRepository, |
||
| 45 | RefreshTokenRepositoryInterface $refreshTokenRepository, |
||
| 46 | \DateInterval $authCodeTTL |
||
| 47 | ) { |
||
| 48 | 41 | $this->setAuthCodeRepository($authCodeRepository); |
|
| 49 | 41 | $this->setRefreshTokenRepository($refreshTokenRepository); |
|
| 50 | 41 | $this->authCodeTTL = $authCodeTTL; |
|
| 51 | 41 | $this->refreshTokenTTL = new \DateInterval('P1M'); |
|
| 52 | 41 | } |
|
| 53 | |||
| 54 | 13 | public function enableCodeExchangeProof() |
|
| 55 | { |
||
| 56 | 13 | $this->enableCodeExchangeProof = true; |
|
| 57 | 13 | } |
|
| 58 | |||
| 59 | /** |
||
| 60 | * Respond to an access token request. |
||
| 61 | * |
||
| 62 | * @param ServerRequestInterface $request |
||
| 63 | * @param ResponseTypeInterface $responseType |
||
| 64 | * @param \DateInterval $accessTokenTTL |
||
| 65 | * |
||
| 66 | * @throws OAuthServerException |
||
| 67 | * |
||
| 68 | * @return ResponseTypeInterface |
||
| 69 | */ |
||
| 70 | 18 | public function respondToAccessTokenRequest( |
|
| 71 | ServerRequestInterface $request, |
||
| 72 | ResponseTypeInterface $responseType, |
||
| 73 | \DateInterval $accessTokenTTL |
||
| 74 | ) { |
||
| 75 | // Validate request |
||
| 76 | 18 | $client = $this->validateClient($request); |
|
| 77 | 18 | $encryptedAuthCode = $this->getRequestParameter('code', $request, null); |
|
| 78 | |||
| 79 | 18 | if ($encryptedAuthCode === null) { |
|
| 80 | 1 | throw OAuthServerException::invalidRequest('code'); |
|
| 81 | } |
||
| 82 | |||
| 83 | try { |
||
| 84 | 17 | $authCodePayload = json_decode($this->decrypt($encryptedAuthCode)); |
|
| 85 | |||
| 86 | 16 | $this->validateAuthorizationCode($authCodePayload, $client, $request); |
|
| 87 | |||
| 88 | 11 | $scopes = $this->scopeRepository->finalizeScopes( |
|
| 89 | 11 | $this->validateScopes($authCodePayload->scopes), |
|
| 90 | 11 | $this->getIdentifier(), |
|
| 91 | 11 | $client, |
|
| 92 | 11 | $authCodePayload->user_id |
|
| 93 | ); |
||
| 94 | 6 | } catch (\LogicException $e) { |
|
| 95 | 1 | throw OAuthServerException::invalidRequest('code', 'Cannot decrypt the authorization code'); |
|
| 96 | } |
||
| 97 | |||
| 98 | // Validate code challenge |
||
| 99 | 11 | if ($this->enableCodeExchangeProof === true) { |
|
| 100 | 7 | $codeVerifier = $this->getRequestParameter('code_verifier', $request, null); |
|
| 101 | |||
| 102 | 7 | if ($codeVerifier === null) { |
|
| 103 | 1 | throw OAuthServerException::invalidRequest('code_verifier'); |
|
| 104 | } |
||
| 105 | |||
| 106 | // Validate code_verifier according to RFC-7636 |
||
| 107 | // @see: https://tools.ietf.org/html/rfc7636#section-4.1 |
||
| 108 | 6 | if (preg_match('/^[A-Za-z0-9-._~]{43,128}$/', $codeVerifier) !== 1) { |
|
| 109 | 3 | throw OAuthServerException::invalidRequest( |
|
| 110 | 3 | 'code_verifier', |
|
| 111 | 3 | 'Code Verifier must follow the specifications of RFC-7636.' |
|
| 112 | ); |
||
| 113 | } |
||
| 114 | |||
| 115 | 3 | switch ($authCodePayload->code_challenge_method) { |
|
| 116 | 3 | case 'plain': |
|
| 117 | 2 | if (hash_equals($codeVerifier, $authCodePayload->code_challenge) === false) { |
|
| 118 | 1 | throw OAuthServerException::invalidGrant('Failed to verify `code_verifier`.'); |
|
| 119 | } |
||
| 120 | |||
| 121 | 1 | break; |
|
| 122 | 1 | case 'S256': |
|
| 123 | if ( |
||
| 124 | 1 | hash_equals( |
|
| 125 | 1 | strtr(rtrim(base64_encode(hash('sha256', $codeVerifier, true)), '='), '+/', '-_'), |
|
| 126 | 1 | $authCodePayload->code_challenge |
|
| 127 | 1 | ) === false |
|
| 128 | ) { |
||
| 129 | throw OAuthServerException::invalidGrant('Failed to verify `code_verifier`.'); |
||
| 130 | } |
||
| 131 | // @codeCoverageIgnoreStart |
||
| 132 | break; |
||
| 133 | default: |
||
| 134 | throw OAuthServerException::serverError( |
||
| 135 | sprintf( |
||
| 136 | 'Unsupported code challenge method `%s`', |
||
| 137 | $authCodePayload->code_challenge_method |
||
| 138 | ) |
||
| 139 | ); |
||
| 140 | // @codeCoverageIgnoreEnd |
||
| 141 | } |
||
| 142 | } |
||
| 143 | |||
| 144 | // Issue and persist access + refresh tokens |
||
| 145 | 6 | $accessToken = $this->issueAccessToken($accessTokenTTL, $client, $authCodePayload->user_id, $scopes); |
|
| 146 | 6 | $refreshToken = $this->issueRefreshToken($accessToken); |
|
| 147 | |||
| 148 | // Send events to emitter |
||
| 149 | 4 | $this->getEmitter()->emit(new RequestEvent(RequestEvent::ACCESS_TOKEN_ISSUED, $request)); |
|
| 150 | 4 | $this->getEmitter()->emit(new RequestEvent(RequestEvent::REFRESH_TOKEN_ISSUED, $request)); |
|
| 151 | |||
| 152 | // Inject tokens into response type |
||
| 153 | 4 | $responseType->setAccessToken($accessToken); |
|
|
|
|||
| 154 | 4 | $responseType->setRefreshToken($refreshToken); |
|
| 155 | |||
| 156 | // Revoke used auth code |
||
| 157 | 4 | $this->authCodeRepository->revokeAuthCode($authCodePayload->auth_code_id); |
|
| 158 | |||
| 159 | 4 | return $responseType; |
|
| 160 | } |
||
| 161 | |||
| 162 | /** |
||
| 163 | * Validate the authorization code. |
||
| 164 | * |
||
| 165 | * @param \stdClass $authCodePayload |
||
| 166 | * @param ClientEntityInterface $client |
||
| 167 | * @param ServerRequestInterface $request |
||
| 168 | */ |
||
| 169 | 16 | private function validateAuthorizationCode( |
|
| 170 | $authCodePayload, |
||
| 171 | ClientEntityInterface $client, |
||
| 172 | ServerRequestInterface $request |
||
| 173 | ) { |
||
| 174 | 16 | if (time() > $authCodePayload->expire_time) { |
|
| 175 | 1 | throw OAuthServerException::invalidRequest('code', 'Authorization code has expired'); |
|
| 176 | } |
||
| 177 | |||
| 178 | 15 | if ($this->authCodeRepository->isAuthCodeRevoked($authCodePayload->auth_code_id) === true) { |
|
| 179 | 1 | throw OAuthServerException::invalidRequest('code', 'Authorization code has been revoked'); |
|
| 180 | } |
||
| 181 | |||
| 182 | 14 | if ($authCodePayload->client_id !== $client->getIdentifier()) { |
|
| 183 | 1 | throw OAuthServerException::invalidRequest('code', 'Authorization code was not issued to this client'); |
|
| 184 | } |
||
| 185 | |||
| 186 | // The redirect URI is required in this request |
||
| 187 | 13 | $redirectUri = $this->getRequestParameter('redirect_uri', $request, null); |
|
| 188 | 13 | if (empty($authCodePayload->redirect_uri) === false && $redirectUri === null) { |
|
| 189 | 1 | throw OAuthServerException::invalidRequest('redirect_uri'); |
|
| 190 | } |
||
| 191 | |||
| 192 | 12 | if ($authCodePayload->redirect_uri !== $redirectUri) { |
|
| 193 | 1 | throw OAuthServerException::invalidRequest('redirect_uri', 'Invalid redirect URI'); |
|
| 194 | } |
||
| 195 | 11 | } |
|
| 196 | |||
| 197 | /** |
||
| 198 | * Return the grant identifier that can be used in matching up requests. |
||
| 199 | * |
||
| 200 | * @return string |
||
| 201 | */ |
||
| 202 | 33 | public function getIdentifier() |
|
| 206 | |||
| 207 | /** |
||
| 208 | * {@inheritdoc} |
||
| 209 | */ |
||
| 210 | 3 | public function canRespondToAuthorizationRequest(ServerRequestInterface $request) |
|
| 218 | |||
| 219 | /** |
||
| 220 | * {@inheritdoc} |
||
| 221 | */ |
||
| 222 | 14 | public function validateAuthorizationRequest(ServerRequestInterface $request) |
|
| 308 | |||
| 309 | /** |
||
| 310 | * {@inheritdoc} |
||
| 311 | */ |
||
| 312 | 7 | public function completeAuthorizationRequest(AuthorizationRequest $authorizationRequest) |
|
| 371 | |||
| 372 | /** |
||
| 373 | * Get the client redirect URI if not set in the request. |
||
| 374 | * |
||
| 375 | * @param AuthorizationRequest $authorizationRequest |
||
| 376 | * |
||
| 377 | * @return string |
||
| 378 | */ |
||
| 379 | 6 | private function getClientRedirectUri(AuthorizationRequest $authorizationRequest) |
|
| 385 | } |
||
| 386 |
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: