Conditions | 19 |
Paths | 37 |
Total Lines | 114 |
Code Lines | 64 |
Lines | 0 |
Ratio | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | <?php |
||
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 | && ($authCodePayload->code_challenge !== null || $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 | |||
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: