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 | public function __construct( |
||
| 42 | AuthCodeRepositoryInterface $authCodeRepository, |
||
| 43 | RefreshTokenRepositoryInterface $refreshTokenRepository, |
||
| 44 | \DateInterval $authCodeTTL |
||
| 45 | ) { |
||
| 46 | $this->setAuthCodeRepository($authCodeRepository); |
||
| 47 | $this->setRefreshTokenRepository($refreshTokenRepository); |
||
| 48 | $this->authCodeTTL = $authCodeTTL; |
||
| 49 | $this->refreshTokenTTL = new \DateInterval('P1M'); |
||
| 50 | } |
||
| 51 | |||
| 52 | public function enableCodeExchangeProof() |
||
| 56 | |||
| 57 | /** |
||
| 58 | * Respond to an access token request. |
||
| 59 | * |
||
| 60 | * @param ServerRequestInterface $request |
||
| 61 | * @param ResponseTypeInterface $responseType |
||
| 62 | * @param \DateInterval $accessTokenTTL |
||
| 63 | * |
||
| 64 | * @throws OAuthServerException |
||
| 65 | * |
||
| 66 | * @return ResponseTypeInterface |
||
| 67 | */ |
||
| 68 | public function respondToAccessTokenRequest( |
||
| 69 | ServerRequestInterface $request, |
||
| 70 | ResponseTypeInterface $responseType, |
||
| 71 | \DateInterval $accessTokenTTL |
||
| 72 | ) { |
||
| 73 | // Validate request |
||
| 74 | $client = $this->validateClient($request); |
||
| 75 | $encryptedAuthCode = $this->getRequestParameter('code', $request, null); |
||
| 76 | |||
| 77 | if ($encryptedAuthCode === null) { |
||
| 78 | throw OAuthServerException::invalidRequest('code'); |
||
| 79 | } |
||
| 80 | |||
| 81 | // Validate the authorization code |
||
| 82 | try { |
||
| 83 | $authCodePayload = json_decode($this->decrypt($encryptedAuthCode)); |
||
| 84 | if (time() > $authCodePayload->expire_time) { |
||
| 85 | throw OAuthServerException::invalidRequest('code', 'Authorization code has expired'); |
||
| 86 | } |
||
| 87 | |||
| 88 | if ($this->authCodeRepository->isAuthCodeRevoked($authCodePayload->auth_code_id) === true) { |
||
| 89 | throw OAuthServerException::invalidRequest('code', 'Authorization code has been revoked'); |
||
| 90 | } |
||
| 91 | |||
| 92 | if ($authCodePayload->client_id !== $client->getIdentifier()) { |
||
| 93 | throw OAuthServerException::invalidRequest('code', 'Authorization code was not issued to this client'); |
||
| 94 | } |
||
| 95 | |||
| 96 | // The redirect URI is required in this request |
||
| 97 | $redirectUri = $this->getRequestParameter('redirect_uri', $request, null); |
||
| 98 | if (empty($authCodePayload->redirect_uri) === false && $redirectUri === null) { |
||
| 99 | throw OAuthServerException::invalidRequest('redirect_uri'); |
||
| 100 | } |
||
| 101 | |||
| 102 | if ($authCodePayload->redirect_uri !== $redirectUri) { |
||
| 103 | throw OAuthServerException::invalidRequest('redirect_uri', 'Invalid redirect URI'); |
||
| 104 | } |
||
| 105 | |||
| 106 | $scopes = []; |
||
| 107 | foreach ($authCodePayload->scopes as $scopeId) { |
||
| 108 | $scope = $this->scopeRepository->getScopeEntityByIdentifier($scopeId); |
||
| 109 | |||
| 110 | if ($scope instanceof ScopeEntityInterface === false) { |
||
| 111 | // @codeCoverageIgnoreStart |
||
| 112 | throw OAuthServerException::invalidScope($scopeId); |
||
| 113 | // @codeCoverageIgnoreEnd |
||
| 114 | } |
||
| 115 | |||
| 116 | $scopes[] = $scope; |
||
| 117 | } |
||
| 118 | |||
| 119 | // Finalize the requested scopes |
||
| 120 | $scopes = $this->scopeRepository->finalizeScopes( |
||
| 121 | $scopes, |
||
| 122 | $this->getIdentifier(), |
||
| 123 | $client, |
||
| 124 | $authCodePayload->user_id |
||
| 125 | ); |
||
| 126 | } catch (\LogicException $e) { |
||
| 127 | throw OAuthServerException::invalidRequest('code', 'Cannot decrypt the authorization code'); |
||
| 128 | } |
||
| 129 | |||
| 130 | // Validate code challenge |
||
| 131 | if ($this->enableCodeExchangeProof === true) { |
||
| 132 | $codeVerifier = $this->getRequestParameter('code_verifier', $request, null); |
||
| 133 | if ($codeVerifier === null) { |
||
| 134 | throw OAuthServerException::invalidRequest('code_verifier'); |
||
| 135 | } |
||
| 136 | |||
| 137 | switch ($authCodePayload->code_challenge_method) { |
||
| 138 | case 'plain': |
||
| 139 | if (hash_equals($codeVerifier, $authCodePayload->code_challenge) === false) { |
||
| 140 | throw OAuthServerException::invalidGrant('Failed to verify `code_verifier`.'); |
||
| 141 | } |
||
| 142 | |||
| 143 | break; |
||
| 144 | case 'S256': |
||
| 145 | if ( |
||
| 146 | hash_equals( |
||
| 147 | urlencode(base64_encode(hash('sha256', $codeVerifier))), |
||
| 148 | $authCodePayload->code_challenge |
||
| 149 | ) === false |
||
| 150 | ) { |
||
| 151 | throw OAuthServerException::invalidGrant('Failed to verify `code_verifier`.'); |
||
| 152 | } |
||
| 153 | // @codeCoverageIgnoreStart |
||
| 154 | break; |
||
| 155 | default: |
||
| 156 | throw OAuthServerException::serverError( |
||
| 157 | sprintf( |
||
| 158 | 'Unsupported code challenge method `%s`', |
||
| 159 | $authCodePayload->code_challenge_method |
||
| 160 | ) |
||
| 161 | ); |
||
| 162 | // @codeCoverageIgnoreEnd |
||
| 163 | } |
||
| 164 | } |
||
| 165 | |||
| 166 | // Issue and persist access + refresh tokens |
||
| 167 | $accessToken = $this->issueAccessToken($accessTokenTTL, $client, $authCodePayload->user_id, $scopes); |
||
| 168 | $refreshToken = $this->issueRefreshToken($accessToken); |
||
|
|
|||
| 169 | |||
| 170 | // Inject tokens into response type |
||
| 171 | $responseType->setAccessToken($accessToken); |
||
| 172 | $responseType->setRefreshToken($refreshToken); |
||
| 173 | |||
| 174 | // Revoke used auth code |
||
| 175 | $this->authCodeRepository->revokeAuthCode($authCodePayload->auth_code_id); |
||
| 176 | |||
| 177 | return $responseType; |
||
| 178 | } |
||
| 179 | |||
| 180 | /** |
||
| 181 | * Return the grant identifier that can be used in matching up requests. |
||
| 182 | * |
||
| 183 | * @return string |
||
| 184 | */ |
||
| 185 | public function getIdentifier() |
||
| 189 | |||
| 190 | /** |
||
| 191 | * {@inheritdoc} |
||
| 192 | */ |
||
| 193 | public function canRespondToAuthorizationRequest(ServerRequestInterface $request) |
||
| 201 | |||
| 202 | /** |
||
| 203 | * {@inheritdoc} |
||
| 204 | */ |
||
| 205 | public function validateAuthorizationRequest(ServerRequestInterface $request) |
||
| 288 | |||
| 289 | /** |
||
| 290 | * {@inheritdoc} |
||
| 291 | */ |
||
| 292 | public function completeAuthorizationRequest(AuthorizationRequest $authorizationRequest) |
||
| 354 | } |
||
| 355 |
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: