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 |
||
24 | class AuthCodeGrant extends AbstractAuthorizeGrant |
||
25 | { |
||
26 | /** |
||
27 | * @var \DateInterval |
||
28 | */ |
||
29 | private $authCodeTTL; |
||
30 | |||
31 | /** |
||
32 | * @var bool |
||
33 | */ |
||
34 | private $enableCodeExchangeProof = false; |
||
35 | |||
36 | /** |
||
37 | * @param AuthCodeRepositoryInterface $authCodeRepository |
||
38 | * @param RefreshTokenRepositoryInterface $refreshTokenRepository |
||
39 | * @param \DateInterval $authCodeTTL |
||
40 | */ |
||
41 | 41 | public function __construct( |
|
51 | |||
52 | 13 | public function enableCodeExchangeProof() |
|
56 | |||
57 | /** |
||
58 | * Respond to an access token request. |
||
59 | * |
||
60 | * @param ServerRequestInterface $request |
||
61 | * @param ResponseTypeInterface $responseType |
||
62 | * @param \DateInterval $accessTokenTTL |
||
63 | * |
||
64 | * @throws OAuthServerException |
||
65 | * |
||
66 | * @return ResponseTypeInterface |
||
67 | */ |
||
68 | 18 | public function respondToAccessTokenRequest( |
|
192 | |||
193 | /** |
||
194 | * Return the grant identifier that can be used in matching up requests. |
||
195 | * |
||
196 | * @return string |
||
197 | */ |
||
198 | 33 | public function getIdentifier() |
|
202 | |||
203 | /** |
||
204 | * Fetch the client_id parameter from the query string. |
||
205 | * |
||
206 | * @return string|null |
||
207 | * @throws OAuthServerException |
||
208 | */ |
||
209 | 15 | protected function getClientIdFromRequest($request) |
|
223 | |||
224 | /** |
||
225 | * {@inheritdoc} |
||
226 | */ |
||
227 | 4 | public function canRespondToAuthorizationRequest(ServerRequestInterface $request) |
|
235 | |||
236 | /** |
||
237 | * {@inheritdoc} |
||
238 | */ |
||
239 | 13 | public function validateAuthorizationRequest(ServerRequestInterface $request) |
|
240 | { |
||
241 | 13 | $clientId = $this->getClientIdFromRequest($request); |
|
242 | |||
243 | 13 | $client = $this->clientRepository->getClientEntity( |
|
244 | 13 | $clientId, |
|
245 | 13 | $this->getIdentifier(), |
|
246 | 13 | null, |
|
247 | 13 | false |
|
248 | ); |
||
249 | |||
250 | 13 | if ($client instanceof ClientEntityInterface === false) { |
|
251 | 1 | $this->getEmitter()->emit(new RequestEvent(RequestEvent::CLIENT_AUTHENTICATION_FAILED, $request)); |
|
252 | 1 | throw OAuthServerException::invalidClient(); |
|
253 | } |
||
254 | |||
255 | 12 | $redirectUri = $this->getQueryStringParameter('redirect_uri', $request); |
|
256 | 12 | if ($redirectUri !== null) { |
|
257 | if ( |
||
258 | 10 | is_string($client->getRedirectUri()) |
|
259 | 10 | && (strcmp($client->getRedirectUri(), $redirectUri) !== 0) |
|
260 | ) { |
||
261 | 1 | $this->getEmitter()->emit(new RequestEvent(RequestEvent::CLIENT_AUTHENTICATION_FAILED, $request)); |
|
262 | 1 | throw OAuthServerException::invalidClient(); |
|
263 | } elseif ( |
||
264 | 9 | is_array($client->getRedirectUri()) |
|
265 | 9 | && in_array($redirectUri, $client->getRedirectUri(), true) === false |
|
266 | ) { |
||
267 | 1 | $this->getEmitter()->emit(new RequestEvent(RequestEvent::CLIENT_AUTHENTICATION_FAILED, $request)); |
|
268 | 9 | throw OAuthServerException::invalidClient(); |
|
269 | } |
||
270 | 2 | } elseif (is_array($client->getRedirectUri()) && count($client->getRedirectUri()) !== 1 |
|
271 | 2 | || empty($client->getRedirectUri())) { |
|
272 | 1 | $this->getEmitter()->emit(new RequestEvent(RequestEvent::CLIENT_AUTHENTICATION_FAILED, $request)); |
|
273 | 1 | throw OAuthServerException::invalidClient(); |
|
274 | } else { |
||
275 | 1 | $redirectUri = is_array($client->getRedirectUri()) |
|
276 | ? $client->getRedirectUri()[0] |
||
277 | 1 | : $client->getRedirectUri(); |
|
278 | } |
||
279 | |||
280 | 9 | $scopes = $this->validateScopes( |
|
281 | 9 | $client, |
|
282 | 9 | $this->getQueryStringParameter('scope', $request, $this->defaultScope), |
|
283 | 9 | $redirectUri |
|
284 | ); |
||
285 | |||
286 | 9 | $stateParameter = $this->getQueryStringParameter('state', $request); |
|
287 | |||
288 | 9 | $authorizationRequest = new AuthorizationRequest(); |
|
289 | 9 | $authorizationRequest->setGrantTypeId($this->getIdentifier()); |
|
290 | 9 | $authorizationRequest->setClient($client); |
|
291 | 9 | $authorizationRequest->setRedirectUri($redirectUri); |
|
292 | 9 | $authorizationRequest->setState($stateParameter); |
|
293 | 9 | $authorizationRequest->setScopes($scopes); |
|
294 | |||
295 | 9 | if ($this->enableCodeExchangeProof === true) { |
|
296 | 6 | $codeChallenge = $this->getQueryStringParameter('code_challenge', $request); |
|
297 | 6 | if ($codeChallenge === null) { |
|
298 | 1 | throw OAuthServerException::invalidRequest('code_challenge'); |
|
299 | } |
||
300 | |||
301 | 5 | $codeChallengeMethod = $this->getQueryStringParameter('code_challenge_method', $request, 'plain'); |
|
302 | 5 | if (in_array($codeChallengeMethod, ['plain', 'S256'], true) === false) { |
|
303 | 1 | throw OAuthServerException::invalidRequest( |
|
304 | 1 | 'code_challenge_method', |
|
305 | 1 | 'Code challenge method must be `plain` or `S256`' |
|
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 | } |
||
321 | |||
322 | 4 | return $authorizationRequest; |
|
323 | } |
||
324 | |||
325 | /** |
||
326 | * {@inheritdoc} |
||
327 | */ |
||
328 | 7 | public function completeAuthorizationRequest(AuthorizationRequest $authorizationRequest) |
|
390 | } |
||
391 |
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: