Complex classes like AuthCodeGrant 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. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
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 AuthCodeGrant, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
31 | class AuthCodeGrant extends AbstractAuthorizeGrant |
||
32 | { |
||
33 | /** |
||
34 | * @var DateInterval |
||
35 | */ |
||
36 | private $authCodeTTL; |
||
37 | |||
38 | /** |
||
39 | * @var bool |
||
40 | */ |
||
41 | private $requireCodeChallengeForPublicClients = true; |
||
42 | |||
43 | /** |
||
44 | * @var CodeChallengeVerifierInterface[] |
||
45 | */ |
||
46 | private $codeChallengeVerifiers = []; |
||
47 | |||
48 | /** |
||
49 | * @param AuthCodeRepositoryInterface $authCodeRepository |
||
50 | * @param RefreshTokenRepositoryInterface $refreshTokenRepository |
||
51 | * @param DateInterval $authCodeTTL |
||
52 | * |
||
53 | * @throws Exception |
||
54 | */ |
||
55 | 45 | public function __construct( |
|
73 | |||
74 | /** |
||
75 | * Disable the requirement for a code challenge for public clients. |
||
76 | */ |
||
77 | public function disableRequireCodeChallengeForPublicClients() |
||
81 | |||
82 | /** |
||
83 | * Respond to an access token request. |
||
84 | * |
||
85 | * @param ServerRequestInterface $request |
||
86 | * @param ResponseTypeInterface $responseType |
||
87 | * @param DateInterval $accessTokenTTL |
||
88 | * |
||
89 | * @throws OAuthServerException |
||
90 | * |
||
91 | * @return ResponseTypeInterface |
||
92 | */ |
||
93 | 22 | public function respondToAccessTokenRequest( |
|
181 | |||
182 | /** |
||
183 | * Validate the authorization code. |
||
184 | * |
||
185 | * @param stdClass $authCodePayload |
||
186 | * @param ClientEntityInterface $client |
||
187 | * @param ServerRequestInterface $request |
||
188 | */ |
||
189 | 20 | private function validateAuthorizationCode( |
|
220 | |||
221 | /** |
||
222 | * Return the grant identifier that can be used in matching up requests. |
||
223 | * |
||
224 | * @return string |
||
225 | */ |
||
226 | 32 | public function getIdentifier() |
|
230 | |||
231 | /** |
||
232 | * {@inheritdoc} |
||
233 | */ |
||
234 | 3 | public function canRespondToAuthorizationRequest(ServerRequestInterface $request) |
|
242 | |||
243 | /** |
||
244 | * {@inheritdoc} |
||
245 | */ |
||
246 | 14 | public function validateAuthorizationRequest(ServerRequestInterface $request) |
|
247 | { |
||
248 | 14 | $clientId = $this->getQueryStringParameter( |
|
249 | 14 | 'client_id', |
|
250 | $request, |
||
251 | 14 | $this->getServerParameter('PHP_AUTH_USER', $request) |
|
252 | ); |
||
253 | |||
254 | 14 | if ($clientId === null) { |
|
255 | 1 | throw OAuthServerException::invalidRequest('client_id'); |
|
256 | } |
||
257 | |||
258 | 13 | $client = $this->getClientEntityOrFail($clientId, $request); |
|
259 | |||
260 | 12 | $redirectUri = $this->getQueryStringParameter('redirect_uri', $request); |
|
261 | |||
262 | 12 | if ($redirectUri !== null) { |
|
263 | 10 | $this->validateRedirectUri($redirectUri, $client, $request); |
|
264 | 2 | } elseif (empty($client->getRedirectUri()) || |
|
265 | 2 | (\is_array($client->getRedirectUri()) && \count($client->getRedirectUri()) !== 1)) { |
|
266 | 1 | $this->getEmitter()->emit(new RequestEvent(RequestEvent::CLIENT_AUTHENTICATION_FAILED, $request)); |
|
267 | 1 | throw OAuthServerException::invalidClient($request); |
|
268 | } else { |
||
269 | 1 | $redirectUri = \is_array($client->getRedirectUri()) |
|
270 | ? $client->getRedirectUri()[0] |
||
271 | 1 | : $client->getRedirectUri(); |
|
272 | } |
||
273 | |||
274 | 9 | $scopes = $this->validateScopes( |
|
275 | 9 | $this->getQueryStringParameter('scope', $request, $this->defaultScope), |
|
276 | $redirectUri |
||
277 | ); |
||
278 | |||
279 | 9 | $stateParameter = $this->getQueryStringParameter('state', $request); |
|
280 | |||
281 | 9 | $authorizationRequest = new AuthorizationRequest(); |
|
282 | 9 | $authorizationRequest->setGrantTypeId($this->getIdentifier()); |
|
283 | 9 | $authorizationRequest->setClient($client); |
|
284 | 9 | $authorizationRequest->setRedirectUri($redirectUri); |
|
285 | |||
286 | 9 | if ($stateParameter !== null) { |
|
287 | $authorizationRequest->setState($stateParameter); |
||
288 | } |
||
289 | |||
290 | 9 | $authorizationRequest->setScopes($scopes); |
|
291 | |||
292 | 9 | $codeChallenge = $this->getQueryStringParameter('code_challenge', $request); |
|
293 | |||
294 | 9 | if ($codeChallenge !== null) { |
|
295 | 5 | $codeChallengeMethod = $this->getQueryStringParameter('code_challenge_method', $request, 'plain'); |
|
296 | |||
297 | 5 | if (\array_key_exists($codeChallengeMethod, $this->codeChallengeVerifiers) === false) { |
|
298 | 1 | throw OAuthServerException::invalidRequest( |
|
299 | 1 | 'code_challenge_method', |
|
300 | 1 | 'Code challenge method must be one of ' . \implode(', ', \array_map( |
|
301 | function ($method) { |
||
302 | 1 | return '`' . $method . '`'; |
|
303 | 1 | }, |
|
304 | 1 | \array_keys($this->codeChallengeVerifiers) |
|
305 | )) |
||
306 | ); |
||
307 | } |
||
308 | |||
309 | // Validate code_challenge according to RFC-7636 |
||
310 | // @see: https://tools.ietf.org/html/rfc7636#section-4.2 |
||
311 | 4 | if (\preg_match('/^[A-Za-z0-9-._~]{43,128}$/', $codeChallenge) !== 1) { |
|
312 | 3 | throw OAuthServerException::invalidRequest( |
|
313 | 3 | 'code_challenged', |
|
314 | 3 | 'Code challenge must follow the specifications of RFC-7636.' |
|
315 | ); |
||
316 | } |
||
317 | |||
318 | 1 | $authorizationRequest->setCodeChallenge($codeChallenge); |
|
319 | 1 | $authorizationRequest->setCodeChallengeMethod($codeChallengeMethod); |
|
320 | 4 | } elseif ($this->requireCodeChallengeForPublicClients && !$client->isConfidential()) { |
|
321 | 1 | throw OAuthServerException::invalidRequest('code_challenge', 'Code challenge must be provided for public clients'); |
|
322 | } |
||
323 | |||
324 | 4 | return $authorizationRequest; |
|
325 | } |
||
326 | |||
327 | /** |
||
328 | * {@inheritdoc} |
||
329 | */ |
||
330 | 7 | public function completeAuthorizationRequest(AuthorizationRequest $authorizationRequest) |
|
391 | |||
392 | /** |
||
393 | * Get the client redirect URI if not set in the request. |
||
394 | * |
||
395 | * @param AuthorizationRequest $authorizationRequest |
||
396 | * |
||
397 | * @return string |
||
398 | */ |
||
399 | 6 | private function getClientRedirectUri(AuthorizationRequest $authorizationRequest) |
|
405 | } |
||
406 |
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: