/** * Handles a request to either validate a user login or log a user out. * * The path that this controller/action handle are always set to the "service" * when authenticating with the CAS server, so CAS server communicates back to * the Drupal site using this controller. */ public function handle() { $request = $this->requestStack->getCurrentRequest(); // First, check if this is a single-log-out (SLO) request from the server. if ($request->request->has('logoutRequest')) { $this->casHelper->log("Logout request: passing to casLogout::handleSlo"); $this->casLogout->handleSlo($request->request->get('logoutRequest')); // Always return a 200 code. CAS Server doesn’t care either way what // happens here, since it is a fire-and-forget approach taken. return Response::create('', 200); } // Our CAS Subscriber, which implements forced redirect and gateway, will // set this query string param which indicates we should disable the // subscriber on the next redirect. This prevents an infinite redirect loop. if ($request->query->has('cas_temp_disable')) { $this->casHelper->log("Temp disable flag set, set session flag."); $_SESSION['cas_temp_disable'] = TRUE; } // Check if there is a ticket parameter. If there isn't, we could be // returning from a gateway request and the user may not be logged into CAS. // Just redirect away from here. if (!$request->query->has('ticket')) { $this->casHelper->log("No ticket detected, move along."); $this->handleReturnToParameter($request); return RedirectResponse::create($this->urlGenerator->generate('<front>')); } $ticket = $request->query->get('ticket'); // Our CAS service will need to reconstruct the original service URL // when validating the ticket. We always know what the base URL for // the service URL (it's this page), but there may be some query params // attached as well (like a destination param) that we need to pass in // as well. So, detach the ticket param, and pass the rest off. $service_params = $request->query->all(); unset($service_params['ticket']); $cas_version = $this->casHelper->getCasProtocolVersion(); $this->casHelper->log("Configured to use CAS protocol version: {$cas_version}"); try { $cas_validation_info = $this->casValidator->validateTicket($cas_version, $ticket, $service_params); } catch (CasValidateException $e) { // Validation failed, redirect to homepage and set message. $this->setMessage(t('There was a problem validating your login, please contact a site administrator.'), 'error'); $this->handleReturnToParameter($request); return RedirectResponse::create($this->urlGenerator->generate('<front>')); } try { $this->casLogin->loginToDrupal($cas_validation_info, $ticket); if ($this->casHelper->isProxy() && $cas_validation_info->getPgt()) { $this->casHelper->log("Storing PGT information for this session."); $this->casHelper->storePGTSession($cas_validation_info->getPgt()); } $this->setMessage(t('You have been logged in.')); } catch (CasLoginException $e) { $this->setMessage(t('There was a problem logging in, please contact a site administrator.'), 'error'); } $this->handleReturnToParameter($request); return RedirectResponse::create($this->urlGenerator->generate('<front>')); }