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( |
|
69 | ServerRequestInterface $request, |
||
70 | ResponseTypeInterface $responseType, |
||
71 | \DateInterval $accessTokenTTL |
||
72 | ) { |
||
73 | // Validate request |
||
74 | 18 | $client = $this->validateClient($request); |
|
75 | 18 | $encryptedAuthCode = $this->getRequestParameter('code', $request, null); |
|
76 | |||
77 | 18 | if ($encryptedAuthCode === null) { |
|
78 | 1 | throw OAuthServerException::invalidRequest('code'); |
|
79 | } |
||
80 | |||
81 | // Validate the authorization code |
||
82 | try { |
||
83 | 17 | $authCodePayload = json_decode($this->decrypt($encryptedAuthCode)); |
|
84 | 16 | if (time() > $authCodePayload->expire_time) { |
|
85 | 1 | throw OAuthServerException::invalidRequest('code', 'Authorization code has expired'); |
|
86 | } |
||
87 | |||
88 | 15 | if ($this->authCodeRepository->isAuthCodeRevoked($authCodePayload->auth_code_id) === true) { |
|
89 | 1 | throw OAuthServerException::invalidRequest('code', 'Authorization code has been revoked'); |
|
90 | } |
||
91 | |||
92 | 14 | if ($authCodePayload->client_id !== $client->getIdentifier()) { |
|
93 | 1 | throw OAuthServerException::invalidRequest('code', 'Authorization code was not issued to this client'); |
|
94 | } |
||
95 | |||
96 | // The redirect URI is required in this request |
||
97 | 13 | $redirectUri = $this->getRequestParameter('redirect_uri', $request, null); |
|
98 | 13 | if (empty($authCodePayload->redirect_uri) === false && $redirectUri === null) { |
|
99 | 1 | throw OAuthServerException::invalidRequest('redirect_uri'); |
|
100 | } |
||
101 | |||
102 | 12 | if ($authCodePayload->redirect_uri !== $redirectUri) { |
|
103 | 1 | throw OAuthServerException::invalidRequest('redirect_uri', 'Invalid redirect URI'); |
|
104 | } |
||
105 | |||
106 | 11 | $scopes = []; |
|
107 | 11 | foreach ($authCodePayload->scopes as $scopeId) { |
|
108 | 11 | $scope = $this->scopeRepository->getScopeEntityByIdentifier($scopeId); |
|
109 | |||
110 | 11 | if ($scope instanceof ScopeEntityInterface === false) { |
|
111 | // @codeCoverageIgnoreStart |
||
112 | throw OAuthServerException::invalidScope($scopeId); |
||
113 | // @codeCoverageIgnoreEnd |
||
114 | } |
||
115 | |||
116 | 11 | $scopes[] = $scope; |
|
117 | } |
||
118 | |||
119 | // Finalize the requested scopes |
||
120 | 11 | $scopes = $this->scopeRepository->finalizeScopes( |
|
121 | 11 | $scopes, |
|
122 | 11 | $this->getIdentifier(), |
|
123 | 11 | $client, |
|
124 | 11 | $authCodePayload->user_id |
|
125 | ); |
||
126 | 6 | } catch (\LogicException $e) { |
|
127 | 1 | throw OAuthServerException::invalidRequest('code', 'Cannot decrypt the authorization code'); |
|
128 | } |
||
129 | |||
130 | // Validate code challenge |
||
131 | 11 | if ($this->enableCodeExchangeProof === true) { |
|
132 | 7 | $codeVerifier = $this->getRequestParameter('code_verifier', $request, null); |
|
133 | 7 | if ($codeVerifier === null) { |
|
134 | 1 | throw OAuthServerException::invalidRequest('code_verifier'); |
|
135 | } |
||
136 | |||
137 | // Validate code_verifier according to RFC-7636 |
||
138 | // @see: https://tools.ietf.org/html/rfc7636#section-4.1 |
||
139 | 6 | if (preg_match('/^[A-Za-z0-9-._~]{43,128}$/', $codeVerifier) !== 1) { |
|
140 | 3 | throw OAuthServerException::invalidRequest( |
|
141 | 3 | 'code_verifier', |
|
142 | 3 | 'Code Verifier must follow the specifications of RFC-7636.' |
|
143 | ); |
||
144 | } |
||
145 | |||
146 | 3 | switch ($authCodePayload->code_challenge_method) { |
|
147 | 3 | case 'plain': |
|
148 | 2 | if (hash_equals($codeVerifier, $authCodePayload->code_challenge) === false) { |
|
149 | 1 | throw OAuthServerException::invalidGrant('Failed to verify `code_verifier`.'); |
|
150 | } |
||
151 | |||
152 | 1 | break; |
|
153 | 1 | case 'S256': |
|
154 | if ( |
||
155 | 1 | hash_equals( |
|
156 | 1 | strtr(rtrim(base64_encode(hash('sha256', $codeVerifier, true)), '='), '+/', '-_'), |
|
157 | 1 | $authCodePayload->code_challenge |
|
158 | 1 | ) === false |
|
159 | ) { |
||
160 | throw OAuthServerException::invalidGrant('Failed to verify `code_verifier`.'); |
||
161 | } |
||
162 | // @codeCoverageIgnoreStart |
||
163 | break; |
||
164 | default: |
||
165 | throw OAuthServerException::serverError( |
||
166 | sprintf( |
||
167 | 'Unsupported code challenge method `%s`', |
||
168 | $authCodePayload->code_challenge_method |
||
169 | ) |
||
170 | ); |
||
171 | // @codeCoverageIgnoreEnd |
||
172 | } |
||
173 | } |
||
174 | |||
175 | // Handle extra authorization code parameters |
||
176 | 6 | $this->handleExtraAuthCodeParams($authCodePayload); |
|
177 | |||
178 | // Issue and persist access + refresh tokens |
||
179 | 6 | $accessToken = $this->issueAccessToken($accessTokenTTL, $client, $authCodePayload->user_id, $scopes); |
|
180 | 6 | $refreshToken = $this->issueRefreshToken($accessToken); |
|
181 | |||
182 | // Send events to emitter |
||
183 | 4 | $this->getEmitter()->emit(new RequestEvent(RequestEvent::ACCESS_TOKEN_ISSUED, $request)); |
|
184 | 4 | $this->getEmitter()->emit(new RequestEvent(RequestEvent::REFRESH_TOKEN_ISSUED, $request)); |
|
185 | |||
186 | // Inject tokens into response type |
||
187 | 4 | $responseType->setAccessToken($accessToken); |
|
|
|||
188 | 4 | $responseType->setRefreshToken($refreshToken); |
|
189 | |||
190 | // Revoke used auth code |
||
191 | 4 | $this->authCodeRepository->revokeAuthCode($authCodePayload->auth_code_id); |
|
192 | |||
193 | 4 | return $responseType; |
|
194 | } |
||
195 | |||
196 | /** |
||
197 | * Return the grant identifier that can be used in matching up requests. |
||
198 | * |
||
199 | * @return string |
||
200 | */ |
||
201 | 33 | public function getIdentifier() |
|
205 | |||
206 | /** |
||
207 | * {@inheritdoc} |
||
208 | */ |
||
209 | 3 | public function canRespondToAuthorizationRequest(ServerRequestInterface $request) |
|
217 | |||
218 | /** |
||
219 | * {@inheritdoc} |
||
220 | */ |
||
221 | 14 | public function validateAuthorizationRequest(ServerRequestInterface $request) |
|
307 | |||
308 | /** |
||
309 | * {@inheritdoc} |
||
310 | */ |
||
311 | 7 | public function completeAuthorizationRequest(AuthorizationRequest $authorizationRequest) |
|
312 | { |
||
313 | 7 | if ($authorizationRequest->getUser() instanceof UserEntityInterface === false) { |
|
314 | 1 | throw new \LogicException('An instance of UserEntityInterface should be set on the AuthorizationRequest'); |
|
315 | } |
||
316 | |||
317 | 6 | $finalRedirectUri = ($authorizationRequest->getRedirectUri() === null) |
|
318 | 6 | ? is_array($authorizationRequest->getClient()->getRedirectUri()) |
|
319 | ? $authorizationRequest->getClient()->getRedirectUri()[0] |
||
320 | 6 | : $authorizationRequest->getClient()->getRedirectUri() |
|
321 | 6 | : $authorizationRequest->getRedirectUri(); |
|
322 | |||
323 | // The user approved the client, redirect them back with an auth code |
||
324 | 6 | if ($authorizationRequest->isAuthorizationApproved() === true) { |
|
325 | 5 | $authCode = $this->issueAuthCode( |
|
326 | 5 | $this->authCodeTTL, |
|
327 | 5 | $authorizationRequest->getClient(), |
|
328 | 5 | $authorizationRequest->getUser()->getIdentifier(), |
|
329 | 5 | $authorizationRequest->getRedirectUri(), |
|
330 | 5 | $authorizationRequest->getScopes() |
|
331 | ); |
||
332 | |||
333 | $payload = [ |
||
334 | 3 | 'client_id' => $authCode->getClient()->getIdentifier(), |
|
335 | 3 | 'redirect_uri' => $authCode->getRedirectUri(), |
|
336 | 3 | 'auth_code_id' => $authCode->getIdentifier(), |
|
337 | 3 | 'scopes' => $authCode->getScopes(), |
|
338 | 3 | 'user_id' => $authCode->getUserIdentifier(), |
|
339 | 3 | 'expire_time' => (new \DateTime())->add($this->authCodeTTL)->format('U'), |
|
340 | 3 | 'code_challenge' => $authorizationRequest->getCodeChallenge(), |
|
341 | 3 | 'code_challenge_method' => $authorizationRequest->getCodeChallengeMethod(), |
|
342 | ]; |
||
343 | |||
344 | 3 | $payload = array_merge($this->getExtraAuthCodeParams($authorizationRequest), $payload); |
|
345 | |||
346 | 3 | $response = new RedirectResponse(); |
|
347 | 3 | $response->setRedirectUri( |
|
348 | 3 | $this->makeRedirectUri( |
|
349 | 3 | $finalRedirectUri, |
|
350 | [ |
||
351 | 3 | 'code' => $this->encrypt( |
|
352 | 3 | json_encode( |
|
353 | 3 | $payload |
|
354 | ) |
||
355 | ), |
||
356 | 3 | 'state' => $authorizationRequest->getState(), |
|
357 | ] |
||
358 | ) |
||
359 | ); |
||
360 | |||
361 | 3 | return $response; |
|
362 | } |
||
363 | |||
364 | // The user denied the client, redirect them back with an error |
||
365 | 1 | throw OAuthServerException::accessDenied( |
|
366 | 1 | 'The user denied the request', |
|
367 | 1 | $this->makeRedirectUri( |
|
368 | 1 | $finalRedirectUri, |
|
369 | [ |
||
370 | 1 | 'state' => $authorizationRequest->getState(), |
|
371 | ] |
||
372 | ) |
||
373 | ); |
||
374 | } |
||
375 | |||
376 | /** |
||
377 | * Add custom fields to your authorization code to save some data from the previous (authorize) state |
||
378 | * for when you are issuing the token at the token endpoint |
||
379 | * |
||
380 | * @param AuthorizationRequest $authorizationRequest |
||
381 | * |
||
382 | * @return array |
||
383 | */ |
||
384 | 3 | protected function getExtraAuthCodeParams(AuthorizationRequest $authorizationRequest) |
|
388 | |||
389 | /** |
||
390 | * Handle the extra params specified in getExtraAuthCodeParams |
||
391 | * |
||
392 | * @param object $authCodePayload |
||
393 | */ |
||
394 | 6 | protected function handleExtraAuthCodeParams(\stdClass $authCodePayload) |
|
397 | } |
||
398 |
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: