Total Complexity | 66 |
Total Lines | 368 |
Duplicated Lines | 0 % |
Changes | 0 |
Complex classes like SessionManagementController 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.
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 SessionManagementController, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
36 | class SessionManagementController extends Controller |
||
37 | { |
||
38 | |||
39 | /** |
||
40 | * @Route("/session/check", name="oidc_check_session_iframe") |
||
41 | * @Method({"GET"}) |
||
42 | * @Template |
||
43 | */ |
||
44 | public function checkSessionAction(Request $request) |
||
47 | } |
||
48 | |||
49 | /** |
||
50 | * @Route("/session/origins", name="oidc_get_origins") |
||
51 | * @Method({"GET"}) |
||
52 | * @Template |
||
53 | */ |
||
54 | public function getOriginsAction(Request $request) |
||
97 | } |
||
98 | |||
99 | /** |
||
100 | * @Route("/session/end", name="oidc_end_session_endpoint") |
||
101 | * @Template |
||
102 | */ |
||
103 | public function endSessionAction(Request $request) |
||
104 | { |
||
105 | $alwaysGetRedirectConsent = $this->alwaysGetRedirectConsent(); |
||
106 | |||
107 | $view = 'LoginCidadaoOpenIDBundle:SessionManagement:endSession.html.twig'; |
||
108 | $finishedView = 'LoginCidadaoOpenIDBundle:SessionManagement:endSession.finished.html.twig'; |
||
109 | try { |
||
110 | $idToken = $request->get('id_token_hint'); |
||
111 | $postLogoutUri = $request->get('post_logout_redirect_uri', null); |
||
112 | $loggedOut = !$this->isGranted('IS_AUTHENTICATED_REMEMBERED'); |
||
113 | try { |
||
114 | $getLogoutConsent = $this->shouldGetLogoutConsent($idToken, $loggedOut); |
||
115 | } catch (IdTokenSubMismatchException $e) { |
||
116 | $getLogoutConsent = true; |
||
117 | } |
||
118 | |||
119 | list($postLogoutUri, $postLogoutHost) = $this->getPostLogoutInfo($request, $postLogoutUri, $idToken); |
||
120 | } catch (IdTokenValidationException $e) { |
||
121 | return $this->render($finishedView, ['error' => 'openid.session.end.invalid_id_token']); |
||
122 | } |
||
123 | |||
124 | $getRedirectConsent = $alwaysGetRedirectConsent && $postLogoutUri; |
||
125 | $authorizedRedirect = !$getLogoutConsent && !$getRedirectConsent; |
||
126 | $authorizedLogout = !$getLogoutConsent; |
||
127 | $formChecked = false; |
||
128 | |||
129 | $form = $this->createForm(EndSessionForm::class, ['logout' => true, 'redirect' => true], [ |
||
130 | 'getLogoutConsent' => $getLogoutConsent, |
||
131 | 'getRedirectConsent' => $getRedirectConsent, |
||
132 | ]); |
||
133 | $form->handleRequest($request); |
||
134 | if ($form->isValid()) { |
||
135 | $data = $form->getData(); |
||
136 | |||
137 | $authorizedRedirect = false === $getRedirectConsent || $data['redirect']; |
||
138 | $authorizedLogout = false === $getLogoutConsent || $data['logout']; |
||
139 | $formChecked = true; |
||
140 | } |
||
141 | |||
142 | $params = [ |
||
143 | 'form' => $form->createView(), |
||
144 | 'client' => $this->getLogoutClient($idToken), |
||
145 | 'postLogoutUri' => $postLogoutUri, |
||
146 | 'postLogoutHost' => $postLogoutHost, |
||
147 | 'getLogoutConsent' => $getLogoutConsent, |
||
148 | 'getRedirectConsent' => $getRedirectConsent, |
||
149 | 'loggedOut' => $loggedOut, |
||
150 | ]; |
||
151 | |||
152 | if (($getLogoutConsent || $getRedirectConsent) |
||
153 | && !$authorizedRedirect |
||
154 | && $formChecked |
||
155 | ) { |
||
156 | $view = $finishedView; |
||
157 | } |
||
158 | |||
159 | $response = null; |
||
160 | if ($postLogoutUri && $authorizedRedirect) { |
||
161 | $response = $this->redirect($postLogoutUri); |
||
162 | } |
||
163 | |||
164 | if ($authorizedLogout && !$loggedOut) { |
||
165 | if (!$response) { |
||
166 | $params['loggedOut'] = true; |
||
167 | $response = $this->render($view, $params); |
||
168 | } |
||
169 | $response = $this->getSecurityHelper()->logout($request, $response); |
||
170 | } |
||
171 | |||
172 | return $response ?: $this->render($view, $params); |
||
173 | } |
||
174 | |||
175 | /** |
||
176 | * @param string $clientId |
||
177 | * @return \LoginCidadao\OAuthBundle\Entity\Client|object |
||
178 | */ |
||
179 | private function getClient($clientId) |
||
180 | { |
||
181 | $clientId = explode('_', $clientId); |
||
182 | $id = $clientId[0]; |
||
183 | |||
184 | return $this->getDoctrine()->getManager() |
||
185 | ->getRepository('LoginCidadaoOAuthBundle:Client')->find($id); |
||
186 | } |
||
187 | |||
188 | private function unparseUrl($parsed_url) |
||
189 | { |
||
190 | $scheme = isset($parsed_url['scheme']) ? $parsed_url['scheme'].'://' : ''; |
||
191 | $host = isset($parsed_url['host']) ? $parsed_url['host'] : ''; |
||
192 | $port = isset($parsed_url['port']) ? ':'.$parsed_url['port'] : ''; |
||
193 | $user = isset($parsed_url['user']) ? $parsed_url['user'] : ''; |
||
194 | $pass = isset($parsed_url['pass']) ? ':'.$parsed_url['pass'] : ''; |
||
195 | $pass = ($user || $pass) ? "$pass@" : ''; |
||
196 | $path = isset($parsed_url['path']) ? $parsed_url['path'] : ''; |
||
197 | $query = isset($parsed_url['query']) ? '?'.$parsed_url['query'] : ''; |
||
198 | $fragment = isset($parsed_url['fragment']) ? '#'.$parsed_url['fragment'] |
||
199 | : ''; |
||
200 | |||
201 | return "$scheme$user$pass$host$port$path$query$fragment"; |
||
202 | } |
||
203 | |||
204 | /** |
||
205 | * @param mixed $idToken a JWT ID Token as a \JOSE_JWT object or string |
||
206 | * @return bool true if $idToken is valid, false otherwise |
||
207 | * @throws IdTokenSubMismatchException |
||
208 | * @throws IdTokenValidationException |
||
209 | */ |
||
210 | private function checkIdToken($idToken) |
||
211 | { |
||
212 | $idToken = $this->getIdToken($idToken); |
||
213 | |||
214 | /** @var PublicKey $publicKeyStorage */ |
||
215 | $publicKeyStorage = $this->get('oauth2.storage.public_key'); |
||
216 | try { |
||
217 | $idToken->verify($publicKeyStorage->getPublicKey($idToken->claims['aud'])); |
||
218 | |||
219 | if (false === $this->checkIdTokenSub($this->getUser(), $idToken)) { |
||
220 | throw new IdTokenSubMismatchException('Invalid subject identifier', Response::HTTP_BAD_REQUEST); |
||
221 | } |
||
222 | |||
223 | return true; |
||
224 | } catch (IdTokenSubMismatchException $e) { |
||
225 | throw $e; |
||
226 | } catch (\JOSE_Exception_VerificationFailed|\Exception $e) { |
||
227 | throw new IdTokenValidationException($e->getMessage(), Response::HTTP_BAD_REQUEST, $e); |
||
228 | } |
||
229 | } |
||
230 | |||
231 | /** |
||
232 | * @param PersonInterface $person |
||
233 | * @param mixed $idToken |
||
234 | * @return bool |
||
235 | */ |
||
236 | private function checkIdTokenSub(PersonInterface $person = null, $idToken) |
||
237 | { |
||
238 | if (null === $person) { |
||
239 | // User is logged out |
||
240 | return true; |
||
241 | } |
||
242 | |||
243 | if (!($person instanceof PersonInterface)) { |
||
244 | return false; |
||
245 | } |
||
246 | |||
247 | $client = $this->getClient($idToken->claims['aud']); |
||
248 | |||
249 | $sub = $this->getSubjectIdentifier($person, $client); |
||
250 | |||
251 | return $idToken->claims['sub'] == $sub; |
||
252 | } |
||
253 | |||
254 | /** |
||
255 | * Enforces that the ID Token is a \JOSE_JWT object |
||
256 | * @param mixed $idToken |
||
257 | * @return \JOSE_JWE|\JOSE_JWT |
||
258 | */ |
||
259 | private function getIdToken($idToken) |
||
260 | { |
||
261 | if (!($idToken instanceof \JOSE_JWT)) { |
||
262 | try { |
||
263 | $idToken = \JOSE_JWT::decode($idToken); |
||
264 | } catch (\JOSE_Exception_InvalidFormat $e) { |
||
265 | throw new BadRequestHttpException($e->getMessage(), $e); |
||
266 | } |
||
267 | } |
||
268 | |||
269 | return $idToken; |
||
270 | } |
||
271 | |||
272 | private function validatePostLogoutUri($postLogoutUri, $idToken) |
||
273 | { |
||
274 | if ($postLogoutUri === null) { |
||
275 | return false; |
||
276 | } |
||
277 | |||
278 | $postLogoutUri = ClientMetadata::canonicalizeUri($postLogoutUri); |
||
279 | |||
280 | if (!$idToken) { |
||
281 | return count($this->findClientByPostLogoutRedirectUri($postLogoutUri)) > 0; |
||
282 | } |
||
283 | |||
284 | $idToken = $this->getIdToken($idToken); |
||
285 | $client = $this->getClient($idToken->claims['aud']); |
||
286 | |||
287 | return false !== array_search($postLogoutUri, $client->getMetadata()->getPostLogoutRedirectUris()); |
||
288 | } |
||
289 | |||
290 | private function addStateToUri($postLogoutUri, $state) |
||
305 | } |
||
306 | } |
||
307 | |||
308 | /** |
||
309 | * @return bool |
||
310 | */ |
||
311 | private function alwaysGetLogoutConsent() |
||
312 | { |
||
313 | return $this->getParameter('rp_initiated_logout.logout.always_get_consent'); |
||
314 | } |
||
315 | |||
316 | /** |
||
317 | * @return bool |
||
318 | */ |
||
319 | private function alwaysGetRedirectConsent() |
||
320 | { |
||
321 | return $this->getParameter('rp_initiated_logout.redirect.always_get_consent'); |
||
322 | } |
||
323 | |||
324 | /** |
||
325 | * @param string|\JOSE_JWT $idToken |
||
326 | * @return \LoginCidadao\OAuthBundle\Entity\Client|false |
||
327 | */ |
||
328 | private function getIdTokenClient($idToken) |
||
329 | { |
||
330 | if ($idToken === null) { |
||
331 | return false; |
||
332 | } |
||
333 | |||
334 | $idToken = $this->getIdToken($idToken); |
||
335 | $client = $this->getClient($idToken->claims['aud']); |
||
336 | |||
337 | return $client; |
||
338 | } |
||
339 | |||
340 | /** |
||
341 | * @return SecurityHelper |
||
342 | */ |
||
343 | private function getSecurityHelper() |
||
349 | } |
||
350 | |||
351 | private function getSubjectIdentifier(PersonInterface $person, ClientInterface $client) |
||
352 | { |
||
353 | /** @var SubjectIdentifierService $service */ |
||
354 | $service = $this->get('oidc.subject_identifier.service'); |
||
355 | |||
356 | return $service->getSubjectIdentifier($person, $client->getMetadata()); |
||
357 | } |
||
358 | |||
359 | private function findClientByPostLogoutRedirectUri($postLogoutUri) |
||
360 | { |
||
361 | /** @var ClientMetadataRepository $repo */ |
||
362 | $repo = $this->get('oidc.client_metadata.repository'); |
||
363 | |||
364 | return $repo->findByPostLogoutRedirectUri($postLogoutUri); |
||
365 | } |
||
366 | |||
367 | private function shouldGetLogoutConsent($idToken, $loggedOut) |
||
368 | { |
||
369 | $getLogoutConsent = $loggedOut ? false : $this->alwaysGetLogoutConsent(); |
||
370 | |||
371 | if ($idToken) { |
||
372 | if (false === $this->checkIdToken($idToken)) { |
||
373 | // We didn't receive a valid ID Token, therefore we should ask user for consent |
||
374 | $getLogoutConsent = true; |
||
375 | } |
||
376 | } |
||
377 | |||
378 | return $getLogoutConsent; |
||
379 | } |
||
380 | |||
381 | private function getLogoutClient($idToken) |
||
391 | } |
||
392 | |||
393 | private function getPostLogoutInfo(Request $request, $postLogoutUri, $idToken) |
||
394 | { |
||
395 | $postLogoutHost = null; |
||
396 | if ($this->validatePostLogoutUri($postLogoutUri, $idToken)) { |
||
397 | $postLogoutUri = $this->addStateToUri($postLogoutUri, $request->get('state', null)); |
||
398 | $postLogoutHost = parse_url($postLogoutUri)['host']; |
||
399 | } else { |
||
400 | $postLogoutUri = null; |
||
401 | } |
||
402 | |||
403 | return [$postLogoutUri, $postLogoutHost]; |
||
404 | } |
||
405 | } |
||
406 |
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.