Contao\CoreBundle\Security\ContaoAuthenticator::authenticateToken PHP Method

authenticateToken() public method

Authenticates a token.
public authenticateToken ( Symfony\Component\Security\Core\Authentication\Token\TokenInterface $token, Symfony\Component\Security\Core\User\UserProviderInterface $userProvider, string $providerKey ) : Symfony\Component\Security\Core\Authentication\Token\TokenInterface | ContaoToken | AnonymousToken
$token Symfony\Component\Security\Core\Authentication\Token\TokenInterface
$userProvider Symfony\Component\Security\Core\User\UserProviderInterface
$providerKey string
return Symfony\Component\Security\Core\Authentication\Token\TokenInterface | Contao\CoreBundle\Security\Authentication\ContaoToken | Symfony\Component\Security\Core\Authentication\Token\AnonymousToken
    public function authenticateToken(TokenInterface $token, UserProviderInterface $userProvider, $providerKey)
    {
        if ($this->canSkipAuthentication($token)) {
            return $token;
        }
        if (!$token instanceof AnonymousToken) {
            throw new AuthenticationException('The ContaoAuthenticator can only handle AnonymousToken.');
        }
        try {
            $user = $userProvider->loadUserByUsername($token->getSecret());
            if ($user instanceof User) {
                return new ContaoToken($user);
            }
        } catch (UsernameNotFoundException $e) {
            // ignore and return the original token
        }
        return $token;
    }

Usage Example

 /**
  * Tests authenticating a token without the container being set.
  *
  * @expectedException \LogicException
  */
 public function testAuthenticateTokenWithoutContainer()
 {
     $authenticator = new ContaoAuthenticator();
     $authenticator->authenticateToken(new AnonymousToken('frontend', 'anon.'), $this->mockUserProvider(), 'frontend');
 }