Total Complexity | 41 |
Total Lines | 311 |
Duplicated Lines | 0 % |
Changes | 1 | ||
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 |
||
54 | class AuthorizationServer implements AuthorizationServerInterface |
||
55 | { |
||
56 | /** |
||
57 | * The ClientService |
||
58 | * @var ClientService |
||
59 | */ |
||
60 | protected ClientService $clientService; |
||
61 | |||
62 | /** |
||
63 | * The AccessTokenService |
||
64 | * @var AccessTokenService |
||
65 | */ |
||
66 | protected AccessTokenService $accessTokenService; |
||
67 | |||
68 | /** |
||
69 | * The RefreshTokenService |
||
70 | * @var RefreshTokenService |
||
71 | */ |
||
72 | protected RefreshTokenService $refreshTokenService; |
||
73 | |||
74 | |||
75 | /** |
||
76 | * The grant list |
||
77 | * @var array<string, GrantInterface> |
||
78 | */ |
||
79 | protected array $grants = []; |
||
80 | |||
81 | /** |
||
82 | * A list of grant that can answer to an authorization request |
||
83 | * @var array<string, GrantInterface> |
||
84 | */ |
||
85 | protected array $responseTypes = []; |
||
86 | |||
87 | /** |
||
88 | * Create new instance |
||
89 | * @param ClientService $clientService |
||
90 | * @param AccessTokenService $accessTokenService |
||
91 | * @param RefreshTokenService $refreshTokenService |
||
92 | * @param array<GrantInterface> $grants |
||
93 | */ |
||
94 | public function __construct( |
||
114 | } |
||
115 | } |
||
116 | } |
||
117 | |||
118 | /** |
||
119 | * {@inheritdoc} |
||
120 | */ |
||
121 | public function handleAuthorizationRequest( |
||
144 | } |
||
145 | |||
146 | /** |
||
147 | * {@inheritdoc} |
||
148 | */ |
||
149 | public function handleTokenRequest( |
||
178 | } |
||
179 | |||
180 | /** |
||
181 | * {@inheritdoc} |
||
182 | */ |
||
183 | public function handleTokenRevocationRequest(ServerRequestInterface $request): ResponseInterface |
||
184 | { |
||
185 | $response = new Response(); |
||
186 | try { |
||
187 | $postParams = (array) $request->getParsedBody(); |
||
188 | $tokenParam = $postParams['token'] ?? null; |
||
189 | $tokenHint = $postParams['token_type_hint'] ?? null; |
||
190 | if ($tokenParam === null || $tokenHint === null) { |
||
191 | throw OAuth2Exception::invalidRequest( |
||
192 | 'Cannot revoke a token as the "token" and/or "token_type_hint" parameters are missing' |
||
193 | ); |
||
194 | } |
||
195 | |||
196 | if (in_array($tokenHint, ['access_token', 'refresh_token']) === false) { |
||
197 | throw OAuth2Exception::unsupportedTokenType(sprintf( |
||
198 | 'Authorization server does not support revocation of token of type "%s"', |
||
199 | $tokenHint |
||
200 | )); |
||
201 | } |
||
202 | |||
203 | if ($tokenHint === 'access_token') { |
||
204 | $token = $this->accessTokenService->getToken((string) $tokenParam); |
||
205 | } else { |
||
206 | $token = $this->refreshTokenService->getToken((string) $tokenParam); |
||
207 | } |
||
208 | |||
209 | // According to spec, we should return 200 if token is invalid |
||
210 | if ($token === null) { |
||
211 | return $response; |
||
212 | } |
||
213 | |||
214 | // Now, we must validate the client if the token was generated against a non-public client |
||
215 | if ($token->getClient() !== null && $token->getClient()->isPublic() === false) { |
||
216 | $requestClient = $this->getClient($request, false); |
||
217 | |||
218 | if ($requestClient !== $token->getClient()) { |
||
219 | throw OAuth2Exception::invalidClient( |
||
220 | 'Token was issued for another client and cannot be revoked' |
||
221 | ); |
||
222 | } |
||
223 | } |
||
224 | |||
225 | if ($tokenHint === 'access_token') { |
||
226 | $this->accessTokenService->delete($token); |
||
227 | } else { |
||
228 | $this->refreshTokenService->delete($token); |
||
229 | } |
||
230 | } catch (OAuth2Exception $ex) { |
||
231 | $response = $this->createResponsFromException($ex); |
||
232 | } catch (Throwable $ex) { |
||
233 | // According to spec (https://tools.ietf.org/html/rfc7009#section-2.2.1), |
||
234 | // we should return a server 503 |
||
235 | // error if we cannot delete the token for any reason |
||
236 | $response = $response->withStatus(503, 'An error occurred while trying to delete the token'); |
||
237 | } |
||
238 | |||
239 | return $response; |
||
240 | } |
||
241 | |||
242 | /** |
||
243 | * {@inheritdoc} |
||
244 | */ |
||
245 | public function hasGrant(string $grant): bool |
||
246 | { |
||
247 | return array_key_exists($grant, $this->grants); |
||
248 | } |
||
249 | |||
250 | /** |
||
251 | * {@inheritdoc} |
||
252 | */ |
||
253 | public function hasResponseType(string $responseType): bool |
||
254 | { |
||
255 | return array_key_exists($responseType, $this->responseTypes); |
||
256 | } |
||
257 | |||
258 | /** |
||
259 | * Return the grant |
||
260 | * @param string $grantType |
||
261 | * @return GrantInterface |
||
262 | */ |
||
263 | public function getGrant(string $grantType): GrantInterface |
||
264 | { |
||
265 | if ($this->hasGrant($grantType)) { |
||
266 | return $this->grants[$grantType]; |
||
267 | } |
||
268 | |||
269 | throw OAuth2Exception::unsupportedGrantType(sprintf( |
||
270 | 'Grant type "%s" is not supported by this server', |
||
271 | $grantType |
||
272 | )); |
||
273 | } |
||
274 | |||
275 | /** |
||
276 | * Return the grant response type |
||
277 | * @param string $responseType |
||
278 | * @return GrantInterface |
||
279 | */ |
||
280 | public function getResponseType(string $responseType): GrantInterface |
||
281 | { |
||
282 | if ($this->hasResponseType($responseType)) { |
||
283 | return $this->responseTypes[$responseType]; |
||
284 | } |
||
285 | |||
286 | throw OAuth2Exception::unsupportedResponseType(sprintf( |
||
287 | 'Response type "%s" is not supported by this server', |
||
288 | $responseType |
||
289 | )); |
||
290 | } |
||
291 | |||
292 | /** |
||
293 | * Get the client (after authenticating it) |
||
294 | * |
||
295 | * According to the spec (http://tools.ietf.org/html/rfc6749#section-2.3), for public clients we do |
||
296 | * not need to authenticate them |
||
297 | * |
||
298 | * @param ServerRequestInterface $request |
||
299 | * @param bool $allowPublicClients |
||
300 | * @return Client|null |
||
301 | */ |
||
302 | protected function getClient(ServerRequestInterface $request, bool $allowPublicClients): ?Client |
||
303 | { |
||
304 | [$id, $secret] = $this->getClientCredentialsFromRequest($request); |
||
305 | |||
306 | // If the grant type we are issuing does not allow public clients, and that the secret is |
||
307 | // missing, then we have an error... |
||
308 | if ($allowPublicClients === false && empty($secret)) { |
||
309 | throw OAuth2Exception::invalidClient('Client secret is missing'); |
||
310 | } |
||
311 | |||
312 | // If we allow public clients and no client id was set, we can return null |
||
313 | if ($allowPublicClients && empty($id)) { |
||
314 | return null; |
||
315 | } |
||
316 | |||
317 | $client = $this->clientService->find($id); |
||
318 | // We delegate all the checks to the client service |
||
319 | if ($client === null || ($allowPublicClients === false && $client->authenticate($secret) === false)) { |
||
320 | throw OAuth2Exception::invalidClient('Client authentication failed'); |
||
321 | } |
||
322 | |||
323 | return $client; |
||
324 | } |
||
325 | |||
326 | /** |
||
327 | * Create a response from the exception, using the format of the spec |
||
328 | * @link http://tools.ietf.org/html/rfc6749#section-5.2 |
||
329 | * |
||
330 | * @param OAuth2Exception $exception |
||
331 | * @return ResponseInterface |
||
332 | */ |
||
333 | protected function createResponsFromException(OAuth2Exception $exception): ResponseInterface |
||
341 | } |
||
342 | |||
343 | /** |
||
344 | * Return the client id and secret from request data |
||
345 | * @param ServerRequestInterface $request |
||
346 | * @return array<string> |
||
347 | */ |
||
348 | protected function getClientCredentialsFromRequest(ServerRequestInterface $request): array |
||
365 | } |
||
366 | } |
||
367 |