private function authenticateViaGuard(GuardAuthenticatorInterface $guardAuthenticator, PreAuthenticationGuardToken $token)
 {
     // get the user from the GuardAuthenticator
     $user = $guardAuthenticator->getUser($token->getCredentials(), $this->userProvider);
     if (null === $user) {
         throw new UsernameNotFoundException(sprintf('Null returned from %s::getUser()', get_class($guardAuthenticator)));
     }
     if (!$user instanceof UserInterface) {
         throw new \UnexpectedValueException(sprintf('The %s::getUser method must return a UserInterface. You returned %s.', get_class($guardAuthenticator), is_object($user) ? get_class($user) : gettype($user)));
     }
     // check the preAuth UserChecker
     $this->userChecker->checkPreAuth($user);
     // check the credentials
     $guardAuthenticator->checkCredentials($token->getCredentials(), $user);
     // check the postAuth UserChecker
     $this->userChecker->checkPostAuth($user);
     // turn the UserInterface into a TokenInterface
     $authenticatedToken = $guardAuthenticator->createAuthenticatedToken($user, $this->providerKey);
     if (!$authenticatedToken instanceof TokenInterface) {
         throw new \UnexpectedValueException(sprintf('The %s::createAuthenticatedToken method must return a TokenInterface. You returned %s.', get_class($guardAuthenticator), is_object($authenticatedToken) ? get_class($authenticatedToken) : gettype($authenticatedToken)));
     }
     return $authenticatedToken;
 }
 /**
  * Handles an authentication failure and returns the Response for the
  * GuardAuthenticator.
  *
  * @param AuthenticationException     $authenticationException
  * @param Request                     $request
  * @param GuardAuthenticatorInterface $guardAuthenticator
  *
  * @return null|Response
  */
 public function handleAuthenticationFailure(AuthenticationException $authenticationException, Request $request, GuardAuthenticatorInterface $guardAuthenticator)
 {
     $this->tokenStorage->setToken(null);
     $response = $guardAuthenticator->onAuthenticationFailure($request, $authenticationException);
     if ($response instanceof Response || null === $response) {
         // returning null is ok, it means they want the request to continue
         return $response;
     }
     throw new \UnexpectedValueException(sprintf('The %s::onAuthenticationFailure method must return null or a Response object. You returned %s.', get_class($guardAuthenticator), is_object($response) ? get_class($response) : gettype($response)));
 }
 /**
  * Checks to see if remember me is supported in the authenticator and
  * on the firewall. If it is, the RememberMeServicesInterface is notified.
  *
  * @param GuardAuthenticatorInterface $guardAuthenticator
  * @param Request                     $request
  * @param TokenInterface              $token
  * @param Response                    $response
  */
 private function triggerRememberMe(GuardAuthenticatorInterface $guardAuthenticator, Request $request, TokenInterface $token, Response $response = null)
 {
     if (!$guardAuthenticator->supportsRememberMe()) {
         return;
     }
     if (null === $this->rememberMeServices) {
         if (null !== $this->logger) {
             $this->logger->info('Remember me skipped: it is not configured for the firewall.', array('authenticator' => get_class($guardAuthenticator)));
         }
         return;
     }
     if (!$response instanceof Response) {
         throw new \LogicException(sprintf('%s::onAuthenticationSuccess *must* return a Response if you want to use the remember me functionality. Return a Response, or set remember_me to false under the guard configuration.', get_class($guardAuthenticator)));
     }
     $this->rememberMeServices->loginSuccess($request, $response, $token);
 }