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)));
     }
     $this->userChecker->checkPreAuth($user);
     if (true !== $guardAuthenticator->checkCredentials($token->getCredentials(), $user)) {
         throw new BadCredentialsException(sprintf('Authentication failed because %s::checkCredentials() did not return true.', get_class($guardAuthenticator)));
     }
     $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
  * @param string                      $providerKey             The key of the firewall
  *
  * @return null|Response
  */
 public function handleAuthenticationFailure(AuthenticationException $authenticationException, Request $request, GuardAuthenticatorInterface $guardAuthenticator, $providerKey)
 {
     $token = $this->tokenStorage->getToken();
     if ($token instanceof PostAuthenticationGuardToken && $providerKey === $token->getProviderKey()) {
         $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)));
 }