| Conditions | 17 |
| Paths | 37 |
| Total Lines | 111 |
| Code Lines | 62 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 4 | ||
| Bugs | 0 | Features | 0 |
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 |
||
| 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 | |||
| 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: