| Total Complexity | 42 |
| Total Lines | 304 |
| Duplicated Lines | 0 % |
| Changes | 3 | ||
| Bugs | 0 | Features | 0 |
Complex classes like AuthorizationServer 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.
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 AuthorizationServer, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 55 | class AuthorizationServer implements AuthorizationServerInterface |
||
| 56 | { |
||
| 57 | /** |
||
| 58 | * The grant list |
||
| 59 | * @var array<string, GrantInterface> |
||
| 60 | */ |
||
| 61 | protected array $grants = []; |
||
| 62 | |||
| 63 | /** |
||
| 64 | * A list of grant that can answer to an authorization request |
||
| 65 | * @var array<string, GrantInterface> |
||
| 66 | */ |
||
| 67 | protected array $responseTypes = []; |
||
| 68 | |||
| 69 | /** |
||
| 70 | * Create new instance |
||
| 71 | * @param ClientService $clientService |
||
| 72 | * @param AccessTokenService $accessTokenService |
||
| 73 | * @param RefreshTokenService $refreshTokenService |
||
| 74 | * @param LoggerInterface $logger |
||
| 75 | * @param GrantInterface[] $grants |
||
| 76 | */ |
||
| 77 | public function __construct( |
||
| 78 | protected ClientService $clientService, |
||
| 79 | protected AccessTokenService $accessTokenService, |
||
| 80 | protected RefreshTokenService $refreshTokenService, |
||
| 81 | protected LoggerInterface $logger, |
||
| 82 | array $grants = [] |
||
| 83 | ) { |
||
| 84 | foreach ($grants as /** @var GrantInterface $grant */ $grant) { |
||
| 85 | if ($grant instanceof AuthorizationServerAwareInterface) { |
||
| 86 | $grant->setAuthorizationServer($this); |
||
| 87 | } |
||
| 88 | |||
| 89 | $this->grants[$grant->getType()] = $grant; |
||
|
|
|||
| 90 | |||
| 91 | $responseType = $grant->getResponseType(); |
||
| 92 | if (!empty($responseType)) { |
||
| 93 | $this->responseTypes[$responseType] = $grant; |
||
| 94 | } |
||
| 95 | } |
||
| 96 | } |
||
| 97 | |||
| 98 | /** |
||
| 99 | * {@inheritdoc} |
||
| 100 | */ |
||
| 101 | public function handleAuthorizationRequest( |
||
| 102 | ServerRequestInterface $request, |
||
| 103 | ?TokenOwnerInterface $owner = null |
||
| 104 | ): ResponseInterface { |
||
| 105 | try { |
||
| 106 | $queryParams = $request->getQueryParams(); |
||
| 107 | $responseTypeParam = $queryParams['response_type'] ?? null; |
||
| 108 | if ($responseTypeParam === null) { |
||
| 109 | throw OAuth2Exception::invalidRequest('No grant response type was found in the request'); |
||
| 110 | } |
||
| 111 | |||
| 112 | $responseType = $this->getResponseType((string) $responseTypeParam); |
||
| 113 | $client = $this->getClient($request, $responseType->allowPublicClients()); |
||
| 114 | |||
| 115 | if ($client === null) { |
||
| 116 | throw OAuth2Exception::invalidClient('No client could be authenticated'); |
||
| 117 | } |
||
| 118 | $response = $responseType->createAuthorizationResponse($request, $client, $owner); |
||
| 119 | } catch (OAuth2Exception $ex) { |
||
| 120 | $response = $this->createResponseFromException($ex); |
||
| 121 | } |
||
| 122 | |||
| 123 | return $response->withHeader('Content-Type', 'application/json'); |
||
| 124 | } |
||
| 125 | |||
| 126 | /** |
||
| 127 | * {@inheritdoc} |
||
| 128 | */ |
||
| 129 | public function handleTokenRequest( |
||
| 130 | ServerRequestInterface $request, |
||
| 131 | ?TokenOwnerInterface $owner = null |
||
| 132 | ): ResponseInterface { |
||
| 133 | $postParams = (array) $request->getParsedBody(); |
||
| 134 | |||
| 135 | try { |
||
| 136 | $grantParam = $postParams['grant_type'] ?? null; |
||
| 137 | if ($grantParam === null) { |
||
| 138 | throw OAuth2Exception::invalidRequest('No grant type was found in the request'); |
||
| 139 | } |
||
| 140 | |||
| 141 | $grant = $this->getGrant((string) $grantParam); |
||
| 142 | $client = $this->getClient($request, $grant->allowPublicClients()); |
||
| 143 | |||
| 144 | if ($client === null) { |
||
| 145 | throw OAuth2Exception::invalidClient('No client could be authenticated'); |
||
| 146 | } |
||
| 147 | |||
| 148 | $response = $grant->createTokenResponse($request, $client, $owner); |
||
| 149 | } catch (OAuth2Exception $ex) { |
||
| 150 | $response = $this->createResponseFromException($ex); |
||
| 151 | } |
||
| 152 | |||
| 153 | // According to the spec, we must set those headers |
||
| 154 | // (http://tools.ietf.org/html/rfc6749#section-5.1) |
||
| 155 | return $response->withHeader('Content-Type', 'application/json') |
||
| 156 | ->withHeader('Cache-Control', 'no-store') |
||
| 157 | ->withHeader('Pragma', 'no-cache'); |
||
| 158 | } |
||
| 159 | |||
| 160 | /** |
||
| 161 | * {@inheritdoc} |
||
| 162 | */ |
||
| 163 | public function handleTokenRevocationRequest(ServerRequestInterface $request): ResponseInterface |
||
| 164 | { |
||
| 165 | $response = new Response(); |
||
| 166 | try { |
||
| 167 | $postParams = (array) $request->getParsedBody(); |
||
| 168 | $tokenParam = $postParams['token'] ?? null; |
||
| 169 | $tokenHint = $postParams['token_type_hint'] ?? null; |
||
| 170 | if ($tokenParam === null || $tokenHint === null) { |
||
| 171 | throw OAuth2Exception::invalidRequest( |
||
| 172 | 'Cannot revoke a token as the "token" and/or "token_type_hint" parameters are missing' |
||
| 173 | ); |
||
| 174 | } |
||
| 175 | |||
| 176 | if (in_array($tokenHint, ['access_token', 'refresh_token']) === false) { |
||
| 177 | throw OAuth2Exception::unsupportedTokenType(sprintf( |
||
| 178 | 'Authorization server does not support revocation of token of type "%s"', |
||
| 179 | $tokenHint |
||
| 180 | )); |
||
| 181 | } |
||
| 182 | |||
| 183 | if ($tokenHint === 'access_token') { |
||
| 184 | $token = $this->accessTokenService->getToken((string) $tokenParam); |
||
| 185 | } else { |
||
| 186 | $token = $this->refreshTokenService->getToken((string) $tokenParam); |
||
| 187 | } |
||
| 188 | |||
| 189 | // According to spec, we should return 200 if token is invalid |
||
| 190 | if ($token === null) { |
||
| 191 | return $response; |
||
| 192 | } |
||
| 193 | |||
| 194 | // Now, we must validate the client if the token was generated against a non-public client |
||
| 195 | if ($token->getClient() !== null && $token->getClient()->isPublic() === false) { |
||
| 196 | $requestClient = $this->getClient($request, false); |
||
| 197 | |||
| 198 | if ($requestClient !== null && $requestClient->getId() !== $token->getClient()->getId()) { |
||
| 199 | throw OAuth2Exception::invalidClient( |
||
| 200 | 'Token was issued for another client and cannot be revoked' |
||
| 201 | ); |
||
| 202 | } |
||
| 203 | } |
||
| 204 | |||
| 205 | if ($tokenHint === 'access_token') { |
||
| 206 | $this->accessTokenService->delete($token); |
||
| 207 | } else { |
||
| 208 | $this->refreshTokenService->delete($token); |
||
| 209 | } |
||
| 210 | } catch (OAuth2Exception $exception) { |
||
| 211 | $response = $this->createResponseFromException($exception); |
||
| 212 | } catch (Throwable $exception) { |
||
| 213 | // According to spec (https://tools.ietf.org/html/rfc7009#section-2.2.1), |
||
| 214 | // we should return a server 503 |
||
| 215 | // error if we cannot delete the token for any reason |
||
| 216 | $response = $response->withStatus(503, 'An error occurred while trying to delete the token'); |
||
| 217 | |||
| 218 | $this->logger->error('OAuth2 Error when revoke token: {type}::{code}:{description}', [ |
||
| 219 | 'code' => $exception->getCode(), |
||
| 220 | 'description' => $exception->getMessage(), |
||
| 221 | 'type' => get_class($exception), |
||
| 222 | ]); |
||
| 223 | } |
||
| 224 | |||
| 225 | return $response; |
||
| 226 | } |
||
| 227 | |||
| 228 | /** |
||
| 229 | * {@inheritdoc} |
||
| 230 | */ |
||
| 231 | public function hasGrant(string $grant): bool |
||
| 232 | { |
||
| 233 | return array_key_exists($grant, $this->grants); |
||
| 234 | } |
||
| 235 | |||
| 236 | /** |
||
| 237 | * {@inheritdoc} |
||
| 238 | */ |
||
| 239 | public function hasResponseType(string $responseType): bool |
||
| 240 | { |
||
| 241 | return array_key_exists($responseType, $this->responseTypes); |
||
| 242 | } |
||
| 243 | |||
| 244 | /** |
||
| 245 | * Return the grant |
||
| 246 | * @param string $grantType |
||
| 247 | * @return GrantInterface |
||
| 248 | */ |
||
| 249 | public function getGrant(string $grantType): GrantInterface |
||
| 258 | )); |
||
| 259 | } |
||
| 260 | |||
| 261 | /** |
||
| 262 | * Return the grant response type |
||
| 263 | * @param string $responseType |
||
| 264 | * @return GrantInterface |
||
| 265 | */ |
||
| 266 | public function getResponseType(string $responseType): GrantInterface |
||
| 267 | { |
||
| 268 | if ($this->hasResponseType($responseType)) { |
||
| 269 | return $this->responseTypes[$responseType]; |
||
| 270 | } |
||
| 271 | |||
| 272 | throw OAuth2Exception::unsupportedResponseType(sprintf( |
||
| 273 | 'Response type "%s" is not supported by this server', |
||
| 274 | $responseType |
||
| 275 | )); |
||
| 276 | } |
||
| 277 | |||
| 278 | /** |
||
| 279 | * Get the client (after authenticating it) |
||
| 280 | * |
||
| 281 | * According to the spec (http://tools.ietf.org/html/rfc6749#section-2.3), for public clients we do |
||
| 282 | * not need to authenticate them |
||
| 283 | * |
||
| 284 | * @param ServerRequestInterface $request |
||
| 285 | * @param bool $allowPublicClients |
||
| 286 | * @return Client|null |
||
| 287 | */ |
||
| 288 | protected function getClient( |
||
| 289 | ServerRequestInterface $request, |
||
| 290 | bool $allowPublicClients |
||
| 291 | ): ?Client { |
||
| 292 | [$id, $secret] = $this->getClientCredentialsFromRequest($request); |
||
| 293 | |||
| 294 | // If the grant type we are issuing does not allow public clients, and that the secret is |
||
| 295 | // missing, then we have an error... |
||
| 296 | if ($allowPublicClients === false && empty($secret)) { |
||
| 297 | throw OAuth2Exception::invalidClient('Client secret is missing'); |
||
| 298 | } |
||
| 299 | |||
| 300 | // If we allow public clients and no client id was set, we can return null |
||
| 301 | if ($allowPublicClients && empty($id)) { |
||
| 302 | return null; |
||
| 303 | } |
||
| 304 | |||
| 305 | $client = $this->clientService->find($id); |
||
| 306 | // We delegate all the checks to the client service |
||
| 307 | if ($client === null || ($allowPublicClients === false && $client->authenticate($secret) === false)) { |
||
| 308 | throw OAuth2Exception::invalidClient('Client authentication failed'); |
||
| 309 | } |
||
| 310 | |||
| 311 | return $client; |
||
| 312 | } |
||
| 313 | |||
| 314 | /** |
||
| 315 | * Create a response from the exception, using the format of the spec |
||
| 316 | * @link http://tools.ietf.org/html/rfc6749#section-5.2 |
||
| 317 | * |
||
| 318 | * @param OAuth2Exception $exception |
||
| 319 | * @return ResponseInterface |
||
| 320 | */ |
||
| 321 | protected function createResponseFromException(OAuth2Exception $exception): ResponseInterface |
||
| 335 | } |
||
| 336 | |||
| 337 | /** |
||
| 338 | * Return the client id and secret from request data |
||
| 339 | * @param ServerRequestInterface $request |
||
| 340 | * @return array<string> |
||
| 341 | */ |
||
| 342 | protected function getClientCredentialsFromRequest(ServerRequestInterface $request): array |
||
| 359 | } |
||
| 360 | } |
||
| 361 |