| Conditions | 15 |
| Paths | 31 |
| Total Lines | 98 |
| Code Lines | 52 |
| 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 |
||
| 74 | public function respondToAccessTokenRequest( |
||
| 75 | ServerRequestInterface $request, |
||
| 76 | ResponseTypeInterface $responseType, |
||
| 77 | \DateInterval $accessTokenTTL |
||
| 78 | ) { |
||
| 79 | // Validate request |
||
| 80 | $client = $this->validateClient($request); |
||
| 81 | $encryptedAuthCode = $this->getRequestParameter('code', $request, null); |
||
| 82 | |||
| 83 | if ($encryptedAuthCode === null) { |
||
| 84 | throw OAuthServerException::invalidRequest('code'); |
||
| 85 | } |
||
| 86 | |||
| 87 | // Validate the authorization code |
||
| 88 | try { |
||
| 89 | $authCodePayload = json_decode($this->decrypt($encryptedAuthCode)); |
||
| 90 | if (time() > $authCodePayload->expire_time) { |
||
| 91 | throw OAuthServerException::invalidRequest('code', 'Authorization code has expired'); |
||
| 92 | } |
||
| 93 | |||
| 94 | if ($this->authCodeRepository->isAuthCodeRevoked($authCodePayload->auth_code_id) === true) { |
||
| 95 | throw OAuthServerException::invalidRequest('code', 'Authorization code has been revoked'); |
||
| 96 | } |
||
| 97 | |||
| 98 | if ($authCodePayload->client_id !== $client->getIdentifier()) { |
||
| 99 | throw OAuthServerException::invalidRequest('code', 'Authorization code was not issued to this client'); |
||
| 100 | } |
||
| 101 | |||
| 102 | // The redirect URI is required in this request |
||
| 103 | $redirectUri = $this->getRequestParameter('redirect_uri', $request, null); |
||
| 104 | if (empty($authCodePayload->redirect_uri) === false && $redirectUri === null) { |
||
| 105 | throw OAuthServerException::invalidRequest('redirect_uri'); |
||
| 106 | } |
||
| 107 | |||
| 108 | if ($authCodePayload->redirect_uri !== $redirectUri) { |
||
| 109 | throw OAuthServerException::invalidRequest('redirect_uri', 'Invalid redirect URI'); |
||
| 110 | } |
||
| 111 | |||
| 112 | $scopes = []; |
||
| 113 | foreach ($authCodePayload->scopes as $scopeId) { |
||
| 114 | $scope = $this->scopeRepository->getScopeEntityByIdentifier($scopeId); |
||
| 115 | |||
| 116 | if ($scope instanceof ScopeEntityInterface === false) { |
||
| 117 | // @codeCoverageIgnoreStart |
||
| 118 | throw OAuthServerException::invalidScope($scopeId); |
||
| 119 | // @codeCoverageIgnoreEnd |
||
| 120 | } |
||
| 121 | |||
| 122 | $scopes[] = $scope; |
||
| 123 | } |
||
| 124 | |||
| 125 | // Finalize the requested scopes |
||
| 126 | $scopes = $this->scopeRepository->finalizeScopes( |
||
| 127 | $scopes, |
||
| 128 | $this->getIdentifier(), |
||
| 129 | $client, |
||
| 130 | $authCodePayload->user_id |
||
| 131 | ); |
||
| 132 | } catch (\LogicException $e) { |
||
| 133 | throw OAuthServerException::invalidRequest('code', 'Cannot decrypt the authorization code'); |
||
| 134 | } |
||
| 135 | |||
| 136 | // Verify code challenge |
||
| 137 | if (empty($this->codeChallengeVerifiers) === false) { |
||
| 138 | $codeVerifier = $this->getRequestParameter('code_verifier', $request, null); |
||
| 139 | if ($codeVerifier === null) { |
||
| 140 | throw OAuthServerException::invalidRequest('code_verifier'); |
||
| 141 | } |
||
| 142 | |||
| 143 | if (array_key_exists($authCodePayload->code_challenge_method, $this->codeChallengeVerifiers)) { |
||
| 144 | if ($this->codeChallengeVerifiers[$authCodePayload->code_challenge_method]->verifyCodeChallenge($codeVerifier, $authCodePayload->code_challenge) === false) { |
||
| 145 | throw OAuthServerException::invalidGrant('Failed to verify `code_verifier`.'); |
||
| 146 | } |
||
| 147 | } else { |
||
| 148 | // @codeCoverageIgnoreStart |
||
| 149 | throw OAuthServerException::serverError( |
||
| 150 | sprintf( |
||
| 151 | 'Unsupported code challenge method `%s`', |
||
| 152 | $authCodePayload->code_challenge_method |
||
| 153 | ) |
||
| 154 | ); |
||
| 155 | // @codeCoverageIgnoreEnd |
||
| 156 | } |
||
| 157 | } |
||
| 158 | |||
| 159 | // Issue and persist access + refresh tokens |
||
| 160 | $accessToken = $this->issueAccessToken($accessTokenTTL, $client, $authCodePayload->user_id, $scopes); |
||
| 161 | $refreshToken = $this->issueRefreshToken($accessToken); |
||
|
|
|||
| 162 | |||
| 163 | // Inject tokens into response type |
||
| 164 | $responseType->setAccessToken($accessToken); |
||
| 165 | $responseType->setRefreshToken($refreshToken); |
||
| 166 | |||
| 167 | // Revoke used auth code |
||
| 168 | $this->authCodeRepository->revokeAuthCode($authCodePayload->auth_code_id); |
||
| 169 | |||
| 170 | return $responseType; |
||
| 171 | } |
||
| 172 | |||
| 342 |
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: