/**
  * Tries to authenticate the tokens in the security context (in the given order)
  * with the available authentication providers, if needed.
  * If securityContext->authenticateAllTokens() returns TRUE all tokens have to be authenticated,
  * otherwise there has to be at least one authenticated token to have a valid authentication.
  *
  * Note: This method sets the 'authenticationPerformed' flag in the security context. You have to
  * set it back to FALSE if you need reauthentication (usually the tokens should do it as soon as they
  * received new credentials).
  *
  * @return void
  * @throws \F3\FLOW3\Security\Exception\AuthenticationRequiredException
  * @author Andreas Förthner <*****@*****.**>
  */
 public function authenticate()
 {
     $allTokensAreAuthenticated = TRUE;
     if ($this->securityContext === NULL) {
         throw new \F3\FLOW3\Security\Exception('Cannot authenticate because no security context has been set.', 1232978667);
     }
     $tokens = $this->securityContext->getAuthenticationTokens();
     if (count($tokens) === 0) {
         throw new \F3\FLOW3\Security\Exception\AuthenticationRequiredException('The security context contained no tokens which could be authenticated.', 1258721059);
     }
     foreach ($tokens as $token) {
         foreach ($this->providers as $provider) {
             if ($provider->canAuthenticate($token) && $token->getAuthenticationStatus() === \F3\FLOW3\Security\Authentication\TokenInterface::AUTHENTICATION_NEEDED) {
                 $provider->authenticate($token);
                 break;
             }
         }
         if ($token->isAuthenticated() && !$this->securityContext->authenticateAllTokens()) {
             return;
         }
         if (!$token->isAuthenticated() && $this->securityContext->authenticateAllTokens()) {
             throw new \F3\FLOW3\Security\Exception\AuthenticationRequiredException('Could not authenticate all tokens, but authenticateAllTokens was set to TRUE.', 1222203912);
         }
         $allTokensAreAuthenticated &= $token->isAuthenticated();
     }
     if (!$allTokensAreAuthenticated) {
         throw new \F3\FLOW3\Security\Exception\AuthenticationRequiredException('Could not authenticate any token. Might be missing or wrong credentials or no authentication provider matched.', 1222204027);
     }
 }