Conditions | 29 |
Paths | 154 |
Total Lines | 145 |
Code Lines | 72 |
Lines | 0 |
Ratio | 0 % |
Changes | 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 |
||
129 | public function validateAuthorizeRequest(RequestInterface $request, ResponseInterface $response) |
||
130 | { |
||
131 | // Make sure a valid client id was supplied (we can not redirect because we were unable to verify the URI) |
||
132 | if (!$client_id = $request->query('client_id', $request->request('client_id'))) { |
||
133 | // We don't have a good URI to use |
||
134 | $response->setError(400, 'invalid_client', "No client id supplied"); |
||
135 | |||
136 | return false; |
||
137 | } |
||
138 | |||
139 | // Get client details |
||
140 | if (!$clientData = $this->clientStorage->getClientDetails($client_id)) { |
||
141 | $response->setError(400, 'invalid_client', 'The client id supplied is invalid'); |
||
142 | |||
143 | return false; |
||
144 | } |
||
145 | |||
146 | $registered_redirect_uri = isset($clientData['redirect_uri']) ? $clientData['redirect_uri'] : ''; |
||
147 | |||
148 | // Make sure a valid redirect_uri was supplied. If specified, it must match the clientData URI. |
||
149 | // @see http://tools.ietf.org/html/rfc6749#section-3.1.2 |
||
150 | // @see http://tools.ietf.org/html/rfc6749#section-4.1.2.1 |
||
151 | // @see http://tools.ietf.org/html/rfc6749#section-4.2.2.1 |
||
152 | if ($supplied_redirect_uri = $request->query('redirect_uri', $request->request('redirect_uri'))) { |
||
153 | // validate there is no fragment supplied |
||
154 | $parts = parse_url($supplied_redirect_uri); |
||
155 | if (isset($parts['fragment']) && $parts['fragment']) { |
||
156 | $response->setError(400, 'invalid_uri', 'The redirect URI must not contain a fragment'); |
||
157 | |||
158 | return false; |
||
159 | } |
||
160 | |||
161 | // validate against the registered redirect uri(s) if available |
||
162 | if ($registered_redirect_uri && !$this->validateRedirectUri($supplied_redirect_uri, $registered_redirect_uri)) { |
||
163 | $response->setError(400, 'redirect_uri_mismatch', 'The redirect URI provided is missing or does not match', '#section-3.1.2'); |
||
164 | |||
165 | return false; |
||
166 | } |
||
167 | $redirect_uri = $supplied_redirect_uri; |
||
168 | } else { |
||
169 | // use the registered redirect_uri if none has been supplied, if possible |
||
170 | if (!$registered_redirect_uri) { |
||
171 | $response->setError(400, 'invalid_uri', 'No redirect URI was supplied or stored'); |
||
172 | |||
173 | return false; |
||
174 | } |
||
175 | |||
176 | if (count(explode(' ', $registered_redirect_uri)) > 1) { |
||
177 | $response->setError(400, 'invalid_uri', 'A redirect URI must be supplied when multiple redirect URIs are registered', '#section-3.1.2.3'); |
||
178 | |||
179 | return false; |
||
180 | } |
||
181 | $redirect_uri = $registered_redirect_uri; |
||
182 | } |
||
183 | |||
184 | // Select the redirect URI |
||
185 | $response_type = $request->query('response_type', $request->request('response_type')); |
||
186 | |||
187 | // for multiple-valued response types - make them alphabetical |
||
188 | if (false !== strpos($response_type, ' ')) { |
||
189 | $types = explode(' ', $response_type); |
||
190 | sort($types); |
||
191 | $response_type = ltrim(implode(' ', $types)); |
||
192 | } |
||
193 | |||
194 | $state = $request->query('state', $request->request('state')); |
||
195 | |||
196 | // type and client_id are required |
||
197 | if (!$response_type || !in_array($response_type, $this->getValidResponseTypes())) { |
||
198 | $response->setRedirect($this->config['redirect_status_code'], $redirect_uri, $state, 'invalid_request', 'Invalid or missing response type', null); |
||
199 | |||
200 | return false; |
||
201 | } |
||
202 | |||
203 | if ($response_type == self::RESPONSE_TYPE_AUTHORIZATION_CODE) { |
||
204 | if (!isset($this->responseTypes['code'])) { |
||
205 | $response->setRedirect($this->config['redirect_status_code'], $redirect_uri, $state, 'unsupported_response_type', 'authorization code grant type not supported', null); |
||
206 | |||
207 | return false; |
||
208 | } |
||
209 | if (!$this->clientStorage->checkRestrictedGrantType($client_id, 'authorization_code')) { |
||
210 | $response->setRedirect($this->config['redirect_status_code'], $redirect_uri, $state, 'unauthorized_client', 'The grant type is unauthorized for this client_id', null); |
||
211 | |||
212 | return false; |
||
213 | } |
||
214 | if ($this->responseTypes['code']->enforceRedirect() && !$redirect_uri) { |
||
215 | $response->setError(400, 'redirect_uri_mismatch', 'The redirect URI is mandatory and was not supplied'); |
||
216 | |||
217 | return false; |
||
218 | } |
||
219 | } else { |
||
220 | if (!$this->config['allow_implicit']) { |
||
221 | $response->setRedirect($this->config['redirect_status_code'], $redirect_uri, $state, 'unsupported_response_type', 'implicit grant type not supported', null); |
||
222 | |||
223 | return false; |
||
224 | } |
||
225 | if (!$this->clientStorage->checkRestrictedGrantType($client_id, 'implicit')) { |
||
226 | $response->setRedirect($this->config['redirect_status_code'], $redirect_uri, $state, 'unauthorized_client', 'The grant type is unauthorized for this client_id', null); |
||
227 | |||
228 | return false; |
||
229 | } |
||
230 | } |
||
231 | |||
232 | // validate requested scope if it exists |
||
233 | $requestedScope = $this->scopeUtil->getScopeFromRequest($request); |
||
234 | |||
235 | if ($requestedScope) { |
||
236 | // restrict scope by client specific scope if applicable, |
||
237 | // otherwise verify the scope exists |
||
238 | $clientScope = $this->clientStorage->getClientScope($client_id); |
||
239 | if ((is_null($clientScope) && !$this->scopeUtil->scopeExists($requestedScope)) |
||
240 | || ($clientScope && !$this->scopeUtil->checkScope($requestedScope, $clientScope))) { |
||
241 | $response->setRedirect($this->config['redirect_status_code'], $redirect_uri, $state, 'invalid_scope', 'An unsupported scope was requested', null); |
||
242 | |||
243 | return false; |
||
244 | } |
||
245 | } else { |
||
246 | // use a globally-defined default scope |
||
247 | $defaultScope = $this->scopeUtil->getDefaultScope($client_id); |
||
248 | |||
249 | if (false === $defaultScope) { |
||
250 | $response->setRedirect($this->config['redirect_status_code'], $redirect_uri, $state, 'invalid_client', 'This application requires you specify a scope parameter', null); |
||
251 | |||
252 | return false; |
||
253 | } |
||
254 | |||
255 | $requestedScope = $defaultScope; |
||
256 | } |
||
257 | |||
258 | // Validate state parameter exists (if configured to enforce this) |
||
259 | if ($this->config['enforce_state'] && !$state) { |
||
260 | $response->setRedirect($this->config['redirect_status_code'], $redirect_uri, null, 'invalid_request', 'The state parameter is required'); |
||
261 | |||
262 | return false; |
||
263 | } |
||
264 | |||
265 | // save the input data and return true |
||
266 | $this->scope = $requestedScope; |
||
267 | $this->state = $state; |
||
268 | $this->client_id = $client_id; |
||
269 | // Only save the SUPPLIED redirect URI (@see http://tools.ietf.org/html/rfc6749#section-4.1.3) |
||
270 | $this->redirect_uri = $supplied_redirect_uri; |
||
271 | $this->response_type = $response_type; |
||
272 | |||
273 | return true; |
||
274 | } |
||
384 |