| Conditions | 17 |
| Paths | 37 |
| Total Lines | 111 |
| Code Lines | 62 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 7 | ||
| Bugs | 1 | Features | 1 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 69 | public function respondToAccessTokenRequest( |
||
| 70 | ServerRequestInterface $request, |
||
| 71 | ResponseTypeInterface $responseType, |
||
| 72 | DateInterval $accessTokenTTL |
||
| 73 | ) { |
||
| 74 | // Validate request |
||
| 75 | $client = $this->validateClient($request); |
||
| 76 | $encryptedAuthCode = $this->getRequestParameter('code', $request, null); |
||
| 77 | |||
| 78 | if ($encryptedAuthCode === null) { |
||
| 79 | throw OAuthServerException::invalidRequest('code'); |
||
| 80 | } |
||
| 81 | |||
| 82 | // Validate the authorization code |
||
| 83 | try { |
||
| 84 | $authCodePayload = json_decode($this->decrypt($encryptedAuthCode)); |
||
| 85 | if (time() > $authCodePayload->expire_time) { |
||
| 86 | throw OAuthServerException::invalidRequest('code', 'Authorization code has expired'); |
||
| 87 | } |
||
| 88 | |||
| 89 | if ($this->authCodeRepository->isAuthCodeRevoked($authCodePayload->auth_code_id) === true) { |
||
| 90 | throw OAuthServerException::invalidRequest('code', 'Authorization code has been revoked'); |
||
| 91 | } |
||
| 92 | |||
| 93 | if ($authCodePayload->client_id !== $client->getIdentifier()) { |
||
| 94 | throw OAuthServerException::invalidRequest('code', 'Authorization code was not issued to this client'); |
||
| 95 | } |
||
| 96 | |||
| 97 | // The redirect URI is required in this request |
||
| 98 | $redirectUri = $this->getRequestParameter('redirect_uri', $request, null); |
||
| 99 | if (empty($authCodePayload->redirect_uri) === false && $redirectUri === null) { |
||
| 100 | throw OAuthServerException::invalidRequest('redirect_uri'); |
||
| 101 | } |
||
| 102 | |||
| 103 | if ($authCodePayload->redirect_uri !== $redirectUri) { |
||
| 104 | throw OAuthServerException::invalidRequest('redirect_uri', 'Invalid redirect URI'); |
||
| 105 | } |
||
| 106 | |||
| 107 | $scopes = []; |
||
| 108 | foreach ($authCodePayload->scopes as $scopeId) { |
||
| 109 | $scope = $this->scopeRepository->getScopeEntityByIdentifier($scopeId); |
||
| 110 | |||
| 111 | if (!$scope instanceof ScopeEntityInterface) { |
||
| 112 | // @codeCoverageIgnoreStart |
||
| 113 | throw OAuthServerException::invalidScope($scopeId); |
||
| 114 | // @codeCoverageIgnoreEnd |
||
| 115 | } |
||
| 116 | |||
| 117 | $scopes[] = $scope; |
||
| 118 | } |
||
| 119 | |||
| 120 | // Finalize the requested scopes |
||
| 121 | $scopes = $this->scopeRepository->finalizeScopes( |
||
| 122 | $scopes, |
||
| 123 | $this->getIdentifier(), |
||
| 124 | $client, |
||
| 125 | $authCodePayload->user_id |
||
| 126 | ); |
||
| 127 | } catch (\LogicException $e) { |
||
| 128 | throw OAuthServerException::invalidRequest('code', 'Cannot decrypt the authorization code'); |
||
| 129 | } |
||
| 130 | |||
| 131 | // Validate code challenge |
||
| 132 | if ($this->enableCodeExchangeProof === true) { |
||
| 133 | $codeVerifier = $this->getRequestParameter('code_verifier', $request, null); |
||
| 134 | if ($codeVerifier === null) { |
||
| 135 | throw OAuthServerException::invalidRequest('code_verifier'); |
||
| 136 | } |
||
| 137 | |||
| 138 | switch ($authCodePayload->code_challenge_method) { |
||
| 139 | case 'plain': |
||
| 140 | if (hash_equals($codeVerifier, $authCodePayload->code_challenge) === false) { |
||
| 141 | throw OAuthServerException::invalidGrant('Failed to verify `code_verifier`.'); |
||
| 142 | } |
||
| 143 | |||
| 144 | break; |
||
| 145 | case 'S256': |
||
| 146 | if ( |
||
| 147 | hash_equals( |
||
| 148 | urlencode(base64_encode(hash('sha256', $codeVerifier))), |
||
| 149 | $authCodePayload->code_challenge |
||
| 150 | ) === false |
||
| 151 | ) { |
||
| 152 | throw OAuthServerException::invalidGrant('Failed to verify `code_verifier`.'); |
||
| 153 | } |
||
| 154 | // @codeCoverageIgnoreStart |
||
| 155 | break; |
||
| 156 | default: |
||
| 157 | throw OAuthServerException::serverError( |
||
| 158 | sprintf( |
||
| 159 | 'Unsupported code challenge method `%s`', |
||
| 160 | $authCodePayload->code_challenge_method |
||
| 161 | ) |
||
| 162 | ); |
||
| 163 | // @codeCoverageIgnoreEnd |
||
| 164 | } |
||
| 165 | } |
||
| 166 | |||
| 167 | // Issue and persist access + refresh tokens |
||
| 168 | $accessToken = $this->issueAccessToken($accessTokenTTL, $client, $authCodePayload->user_id, $scopes); |
||
| 169 | $refreshToken = $this->issueRefreshToken($accessToken); |
||
| 170 | |||
| 171 | // Inject tokens into response type |
||
| 172 | $responseType->setAccessToken($accessToken); |
||
| 173 | $responseType->setRefreshToken($refreshToken); |
||
| 174 | |||
| 175 | // Revoke used auth code |
||
| 176 | $this->authCodeRepository->revokeAuthCode($authCodePayload->auth_code_id); |
||
| 177 | |||
| 178 | return $responseType; |
||
| 179 | } |
||
| 180 | |||
| 347 |