/**
  * Tries to authenticate the tokens in the security context (in the given order)
  * with the available authentication providers, if needed.
  * If the authentication strategy is set to "allTokens", all tokens have to be authenticated.
  * If the strategy is set to "oneToken", only one token needs to be authenticated, but the
  * authentication will stop after the first authenticated token. The strategy
  * "atLeastOne" will try to authenticate at least one and as many tokens as possible.
  *
  * @return void
  * @throws \TYPO3\FLOW3\Security\Exception
  * @throws \TYPO3\FLOW3\Security\Exception\AuthenticationRequiredException
  */
 public function authenticate()
 {
     $anyTokenAuthenticated = FALSE;
     if ($this->securityContext === NULL) {
         throw new \TYPO3\FLOW3\Security\Exception('Cannot authenticate because no security context has been set.', 1232978667);
     }
     $tokens = $this->securityContext->getAuthenticationTokens();
     if (count($tokens) === 0) {
         throw new \TYPO3\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() === \TYPO3\FLOW3\Security\Authentication\TokenInterface::AUTHENTICATION_NEEDED) {
                 $provider->authenticate($token);
                 if ($token->isAuthenticated()) {
                     $this->emitAuthenticatedToken($token);
                 }
                 break;
             }
         }
         if ($token->isAuthenticated()) {
             $anyTokenAuthenticated = TRUE;
             if ($this->securityContext->getAuthenticationStrategy() === \TYPO3\FLOW3\Security\Context::AUTHENTICATE_ONE_TOKEN) {
                 return;
             }
         } else {
             if ($this->securityContext->getAuthenticationStrategy() === \TYPO3\FLOW3\Security\Context::AUTHENTICATE_ALL_TOKENS) {
                 throw new \TYPO3\FLOW3\Security\Exception\AuthenticationRequiredException('Could not authenticate all tokens, but authenticationStrategy was set to "all".', 1222203912);
             }
         }
     }
     if (!$anyTokenAuthenticated && $this->securityContext->getAuthenticationStrategy() !== \TYPO3\FLOW3\Security\Context::AUTHENTICATE_ANY_TOKEN) {
         throw new \TYPO3\FLOW3\Security\Exception\AuthenticationRequiredException('Could not authenticate any token. Might be missing or wrong credentials or no authentication provider matched.', 1222204027);
     }
 }