Example #1
0
 /**
  * Performs the logout if requested.
  *
  * If a CsrfTokenManagerInterface instance is available, it will be used to
  * validate the request.
  *
  * @param GetResponseEvent $event A GetResponseEvent instance
  *
  * @throws LogoutException   if the CSRF token is invalid
  * @throws \RuntimeException if the LogoutSuccessHandlerInterface instance does not return a response
  */
 public function handle(GetResponseEvent $event)
 {
     $request = $event->getRequest();
     if (!$this->requiresLogout($request)) {
         return;
     }
     if (null !== $this->csrfTokenManager) {
         $csrfToken = ParameterBagUtils::getRequestParameterValue($request, $this->options['csrf_parameter']);
         if (false === $this->csrfTokenManager->isTokenValid(new CsrfToken($this->options['csrf_token_id'], $csrfToken))) {
             throw new LogoutException('Invalid CSRF token.');
         }
     }
     $response = $this->successHandler->onLogoutSuccess($request);
     if (!$response instanceof Response) {
         throw new \RuntimeException('Logout Success Handler did not return a Response.');
     }
     // handle multiple logout attempts gracefully
     if ($token = $this->tokenStorage->getToken()) {
         foreach ($this->handlers as $handler) {
             $handler->logout($request, $response, $token);
         }
     }
     $this->tokenStorage->setToken(null);
     $event->setResponse($response);
 }
 /**
  * {@inheritdoc}
  */
 protected function attemptAuthentication(Request $request)
 {
     if (null !== $this->csrfTokenManager) {
         $csrfToken = ParameterBagUtils::getRequestParameterValue($request, $this->options['csrf_parameter']);
         if (false === $this->csrfTokenManager->isTokenValid(new CsrfToken($this->options['csrf_token_id'], $csrfToken))) {
             throw new InvalidCsrfTokenException('Invalid CSRF token.');
         }
     }
     if ($this->options['post_only']) {
         $username = trim(ParameterBagUtils::getParameterBagValue($request->request, $this->options['username_parameter']));
         $password = ParameterBagUtils::getParameterBagValue($request->request, $this->options['password_parameter']);
     } else {
         $username = trim(ParameterBagUtils::getRequestParameterValue($request, $this->options['username_parameter']));
         $password = ParameterBagUtils::getRequestParameterValue($request, $this->options['password_parameter']);
     }
     $request->getSession()->set(Security::LAST_USERNAME, $username);
     return $this->authenticationManager->authenticate(new UsernamePasswordToken($username, $password, $this->providerKey));
 }
 /**
  * {@inheritdoc}
  */
 public function onAuthenticationFailure(Request $request, AuthenticationException $exception)
 {
     if ($failureUrl = ParameterBagUtils::getRequestParameterValue($request, $this->options['failure_path_parameter'])) {
         $this->options['failure_path'] = $failureUrl;
     }
     if (null === $this->options['failure_path']) {
         $this->options['failure_path'] = $this->options['login_path'];
     }
     if ($this->options['failure_forward']) {
         if (null !== $this->logger) {
             $this->logger->debug('Authentication failure, forward triggered.', array('failure_path' => $this->options['failure_path']));
         }
         $subRequest = $this->httpUtils->createRequest($request, $this->options['failure_path']);
         $subRequest->attributes->set(Security::AUTHENTICATION_ERROR, $exception);
         return $this->httpKernel->handle($subRequest, HttpKernelInterface::SUB_REQUEST);
     }
     if (null !== $this->logger) {
         $this->logger->debug('Authentication failure, redirect triggered.', array('failure_path' => $this->options['failure_path']));
     }
     $request->getSession()->set(Security::AUTHENTICATION_ERROR, $exception);
     return $this->httpUtils->createRedirectResponse($request, $this->options['failure_path']);
 }
 /**
  * Checks whether remember-me capabilities were requested.
  *
  * @param Request $request
  *
  * @return bool
  */
 protected function isRememberMeRequested(Request $request)
 {
     if (true === $this->options['always_remember_me']) {
         return true;
     }
     $parameter = ParameterBagUtils::getRequestParameterValue($request, $this->options['remember_me_parameter']);
     if (null === $parameter && null !== $this->logger) {
         $this->logger->debug('Did not send remember-me cookie.', array('parameter' => $this->options['remember_me_parameter']));
     }
     return $parameter === 'true' || $parameter === 'on' || $parameter === '1' || $parameter === 'yes';
 }
 /**
  * Builds the target URL according to the defined options.
  *
  * @param Request $request
  *
  * @return string
  */
 protected function determineTargetUrl(Request $request)
 {
     if ($this->options['always_use_default_target_path']) {
         return $this->options['default_target_path'];
     }
     if ($targetUrl = ParameterBagUtils::getRequestParameterValue($request, $this->options['target_path_parameter'])) {
         return $targetUrl;
     }
     if (null !== $this->providerKey && ($targetUrl = $this->getTargetPath($request->getSession(), $this->providerKey))) {
         $this->removeTargetPath($request->getSession(), $this->providerKey);
         return $targetUrl;
     }
     if ($this->options['use_referer'] && ($targetUrl = $request->headers->get('Referer')) && $targetUrl !== $this->httpUtils->generateUri($request, $this->options['login_path'])) {
         return $targetUrl;
     }
     return $this->options['default_target_path'];
 }
 /**
  * Provide a BC wrapper for deep item finding deprecation.
  *
  * @param ParameterBag $bag
  * @param string $param
  * @return mixed
  */
 protected function getParameterFromBag($bag, $param)
 {
     if (!$this->useParameterBagUtils()) {
         return $bag->get($param, null, true);
     }
     return ParameterBagUtils::getParameterBagValue($bag, $param);
 }