| Conditions | 11 |
| Paths | 20 |
| Total Lines | 67 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 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 |
||
| 114 | public function handleAccessTokenRequest(TokenEndpoint $tokenEndpoint, array $requestData): array |
||
| 115 | { |
||
| 116 | if (empty($requestData['refresh_token'])) { |
||
| 117 | throw new OAuthException('invalid_request', |
||
| 118 | 'The request is missing the required parameter refresh_token.', |
||
| 119 | 'https://tools.ietf.org/html/rfc7636#section-4.4'); |
||
| 120 | } |
||
| 121 | |||
| 122 | $refreshToken = $this->refreshTokenStorage->get($requestData['refresh_token']); |
||
| 123 | |||
| 124 | if (!$refreshToken || $refreshToken->getClientIdentifier() !== $tokenEndpoint->getClient()->getIdentifier()) { |
||
| 125 | throw new OAuthException('invalid_grant', |
||
| 126 | 'The request includes the invalid parameter refresh_token.', |
||
| 127 | 'https://tools.ietf.org/html/rfc7636#section-4.4'); |
||
| 128 | } |
||
| 129 | |||
| 130 | if ($this->refreshTokenStorage->hasExpired($refreshToken)) { |
||
| 131 | throw new OAuthException('invalid_grant', |
||
| 132 | 'The request includes the invalid parameter refresh_token. The token has expired.', |
||
| 133 | 'https://tools.ietf.org/html/rfc7636#section-4.4'); |
||
| 134 | } |
||
| 135 | |||
| 136 | $scopes = $refreshToken->getScopes(); |
||
| 137 | $requestedScopes = $this->scopePolicyManager->scopeStringToArray($requestData['scope'] ?? null); |
||
| 138 | |||
| 139 | if (!empty($requestedScopes)) { |
||
| 140 | if (!empty(array_diff($requestedScopes, $refreshToken->getScopes()))) { |
||
| 141 | throw new OAuthException('invalid_request', |
||
| 142 | 'The request includes the invalid parameter scope.', |
||
| 143 | 'https://tools.ietf.org/html/rfc7636#section-4.4'); |
||
| 144 | } |
||
| 145 | $scopes = $requestedScopes; |
||
| 146 | } |
||
| 147 | |||
| 148 | $responseData = $this->issueAccessToken( |
||
| 149 | $scopes, |
||
| 150 | $refreshToken->getClientIdentifier(), |
||
| 151 | $refreshToken->getResourceOwnerIdentifier(), |
||
| 152 | $refreshToken->getAuthorizationCode() |
||
| 153 | ); |
||
| 154 | |||
| 155 | /** |
||
| 156 | * @see https://tools.ietf.org/html/rfc6749#section-3.3 |
||
| 157 | * The authorization and token endpoints allow the client to specify the |
||
| 158 | * scope of the access request using the "scope" request parameter. In |
||
| 159 | * turn, the authorization server uses the "scope" response parameter to |
||
| 160 | * inform the client of the scope of the access token issued. |
||
| 161 | */ |
||
| 162 | if (Helper::array_equals($requestedScopes, $scopes)) { |
||
| 163 | $responseData['scope'] = implode(' ', $scopes); |
||
| 164 | } |
||
| 165 | |||
| 166 | if ($this->config->mayRevokeOldRefreshToken() || $this->config->mayIssueNewRefreshToken()) { |
||
| 167 | $this->refreshTokenStorage->revoke($refreshToken); |
||
| 168 | } |
||
| 169 | |||
| 170 | if ($this->config->mayIssueNewRefreshToken()) { |
||
| 171 | $refreshToken = $this->refreshTokenStorage->generate( |
||
| 172 | $refreshToken->getScopes(), |
||
| 173 | $refreshToken->getClientIdentifier(), |
||
| 174 | $refreshToken->getResourceOwnerIdentifier(), |
||
| 175 | $refreshToken->getAuthorizationCode() |
||
| 176 | ); |
||
| 177 | $responseData['refresh_token'] = $refreshToken->getToken(); |
||
| 178 | } |
||
| 179 | |||
| 180 | return $responseData; |
||
| 181 | } |
||
| 182 | } |