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 | * @var bool |
||
38 | */ |
||
39 | private $forceEnabledCodeExchangeProof = true; |
||
40 | |||
41 | /** |
||
42 | * @param AuthCodeRepositoryInterface $authCodeRepository |
||
43 | * @param RefreshTokenRepositoryInterface $refreshTokenRepository |
||
44 | * @param \DateInterval $authCodeTTL |
||
45 | */ |
||
46 | public function __construct( |
||
56 | |||
57 | public function enableCodeExchangeProof() |
||
61 | |||
62 | public function disableForceEnabledCodeExchangeProof() |
||
66 | |||
67 | /** |
||
68 | * Respond to an access token request. |
||
69 | * |
||
70 | * @param ServerRequestInterface $request |
||
71 | * @param ResponseTypeInterface $responseType |
||
72 | * @param \DateInterval $accessTokenTTL |
||
73 | * |
||
74 | * @throws OAuthServerException |
||
75 | * |
||
76 | * @return ResponseTypeInterface |
||
77 | */ |
||
78 | public function respondToAccessTokenRequest( |
||
79 | ServerRequestInterface $request, |
||
80 | ResponseTypeInterface $responseType, |
||
81 | \DateInterval $accessTokenTTL |
||
82 | ) { |
||
83 | // Validate request |
||
84 | $client = $this->validateClient($request); |
||
85 | $encryptedAuthCode = $this->getRequestParameter('code', $request, null); |
||
86 | |||
87 | if ($encryptedAuthCode === null) { |
||
88 | throw OAuthServerException::invalidRequest('code'); |
||
89 | } |
||
90 | |||
91 | // Validate the authorization code |
||
92 | try { |
||
93 | $authCodePayload = json_decode($this->decrypt($encryptedAuthCode)); |
||
94 | if (time() > $authCodePayload->expire_time) { |
||
95 | throw OAuthServerException::invalidRequest('code', 'Authorization code has expired'); |
||
96 | } |
||
97 | |||
98 | if ($this->authCodeRepository->isAuthCodeRevoked($authCodePayload->auth_code_id) === true) { |
||
99 | throw OAuthServerException::invalidRequest('code', 'Authorization code has been revoked'); |
||
100 | } |
||
101 | |||
102 | if ($authCodePayload->client_id !== $client->getIdentifier()) { |
||
103 | throw OAuthServerException::invalidRequest('code', 'Authorization code was not issued to this client'); |
||
104 | } |
||
105 | |||
106 | // The redirect URI is required in this request |
||
107 | $redirectUri = $this->getRequestParameter('redirect_uri', $request, null); |
||
108 | if (empty($authCodePayload->redirect_uri) === false && $redirectUri === null) { |
||
109 | throw OAuthServerException::invalidRequest('redirect_uri'); |
||
110 | } |
||
111 | |||
112 | if ($authCodePayload->redirect_uri !== $redirectUri) { |
||
113 | throw OAuthServerException::invalidRequest('redirect_uri', 'Invalid redirect URI'); |
||
114 | } |
||
115 | |||
116 | $scopes = []; |
||
117 | foreach ($authCodePayload->scopes as $scopeId) { |
||
118 | $scope = $this->scopeRepository->getScopeEntityByIdentifier($scopeId); |
||
119 | |||
120 | if ($scope instanceof ScopeEntityInterface === false) { |
||
121 | // @codeCoverageIgnoreStart |
||
122 | throw OAuthServerException::invalidScope($scopeId); |
||
123 | // @codeCoverageIgnoreEnd |
||
124 | } |
||
125 | |||
126 | $scopes[] = $scope; |
||
127 | } |
||
128 | |||
129 | // Finalize the requested scopes |
||
130 | $scopes = $this->scopeRepository->finalizeScopes( |
||
131 | $scopes, |
||
132 | $this->getIdentifier(), |
||
133 | $client, |
||
134 | $authCodePayload->user_id |
||
135 | ); |
||
136 | } catch (\LogicException $e) { |
||
137 | throw OAuthServerException::invalidRequest('code', 'Cannot decrypt the authorization code'); |
||
138 | } |
||
139 | |||
140 | // Validate code challenge |
||
141 | if ( |
||
142 | $this->enableCodeExchangeProof === true |
||
143 | && (isset($authCodePayload->code_challenge) || $this->forceEnabledCodeExchangeProof === true) |
||
144 | ) { |
||
145 | $codeVerifier = $this->getRequestParameter('code_verifier', $request, null); |
||
146 | if ($codeVerifier === null) { |
||
147 | throw OAuthServerException::invalidRequest('code_verifier'); |
||
148 | } |
||
149 | |||
150 | switch ($authCodePayload->code_challenge_method) { |
||
151 | case 'plain': |
||
152 | if (hash_equals($codeVerifier, $authCodePayload->code_challenge) === false) { |
||
153 | throw OAuthServerException::invalidGrant('Failed to verify `code_verifier`.'); |
||
154 | } |
||
155 | |||
156 | break; |
||
157 | case 'S256': |
||
158 | if ( |
||
159 | hash_equals( |
||
160 | urlencode(base64_encode(hash('sha256', $codeVerifier))), |
||
161 | $authCodePayload->code_challenge |
||
162 | ) === false |
||
163 | ) { |
||
164 | throw OAuthServerException::invalidGrant('Failed to verify `code_verifier`.'); |
||
165 | } |
||
166 | // @codeCoverageIgnoreStart |
||
167 | break; |
||
168 | default: |
||
169 | throw OAuthServerException::serverError( |
||
170 | sprintf( |
||
171 | 'Unsupported code challenge method `%s`', |
||
172 | $authCodePayload->code_challenge_method |
||
173 | ) |
||
174 | ); |
||
175 | // @codeCoverageIgnoreEnd |
||
176 | } |
||
177 | } |
||
178 | |||
179 | // Issue and persist access + refresh tokens |
||
180 | $accessToken = $this->issueAccessToken($accessTokenTTL, $client, $authCodePayload->user_id, $scopes); |
||
181 | $refreshToken = $this->issueRefreshToken($accessToken); |
||
|
|||
182 | |||
183 | // Inject tokens into response type |
||
184 | $responseType->setAccessToken($accessToken); |
||
185 | $responseType->setRefreshToken($refreshToken); |
||
186 | |||
187 | // Revoke used auth code |
||
188 | $this->authCodeRepository->revokeAuthCode($authCodePayload->auth_code_id); |
||
189 | |||
190 | return $responseType; |
||
191 | } |
||
192 | |||
193 | /** |
||
194 | * Return the grant identifier that can be used in matching up requests. |
||
195 | * |
||
196 | * @return string |
||
197 | */ |
||
198 | public function getIdentifier() |
||
202 | |||
203 | /** |
||
204 | * {@inheritdoc} |
||
205 | */ |
||
206 | public function canRespondToAuthorizationRequest(ServerRequestInterface $request) |
||
214 | |||
215 | /** |
||
216 | * {@inheritdoc} |
||
217 | */ |
||
218 | public function validateAuthorizationRequest(ServerRequestInterface $request) |
||
308 | |||
309 | /** |
||
310 | * {@inheritdoc} |
||
311 | */ |
||
312 | public function completeAuthorizationRequest(AuthorizationRequest $authorizationRequest) |
||
374 | } |
||
375 |
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: