vendor/symfony/security-http/Firewall/AnonymousAuthenticationListener.php line 22

Open in your IDE?
  1. <?php
  2. /*
  3.  * This file is part of the Symfony package.
  4.  *
  5.  * (c) Fabien Potencier <fabien@symfony.com>
  6.  *
  7.  * For the full copyright and license information, please view the LICENSE
  8.  * file that was distributed with this source code.
  9.  */
  10. namespace Symfony\Component\Security\Http\Firewall;
  11. use Psr\Log\LoggerInterface;
  12. use Symfony\Component\HttpFoundation\Request;
  13. use Symfony\Component\HttpKernel\Event\RequestEvent;
  14. use Symfony\Component\Security\Core\Authentication\AuthenticationManagerInterface;
  15. use Symfony\Component\Security\Core\Authentication\Token\AnonymousToken;
  16. use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
  17. use Symfony\Component\Security\Core\Exception\AuthenticationException;
  18. trigger_deprecation('symfony/security-http''5.3''The "%s" class is deprecated, use the new authenticator system instead.'AnonymousAuthenticationListener::class);
  19. // Help opcache.preload discover always-needed symbols
  20. class_exists(AnonymousToken::class);
  21. /**
  22.  * AnonymousAuthenticationListener automatically adds a Token if none is
  23.  * already present.
  24.  *
  25.  * @author Fabien Potencier <fabien@symfony.com>
  26.  *
  27.  * @deprecated since Symfony 5.3, use the new authenticator system instead
  28.  */
  29. class AnonymousAuthenticationListener extends AbstractListener
  30. {
  31.     private $tokenStorage;
  32.     private $secret;
  33.     private $authenticationManager;
  34.     private $logger;
  35.     public function __construct(TokenStorageInterface $tokenStoragestring $secretLoggerInterface $logger nullAuthenticationManagerInterface $authenticationManager null)
  36.     {
  37.         $this->tokenStorage $tokenStorage;
  38.         $this->secret $secret;
  39.         $this->authenticationManager $authenticationManager;
  40.         $this->logger $logger;
  41.     }
  42.     /**
  43.      * {@inheritdoc}
  44.      */
  45.     public function supports(Request $request): ?bool
  46.     {
  47.         return null// always run authenticate() lazily with lazy firewalls
  48.     }
  49.     /**
  50.      * Handles anonymous authentication.
  51.      */
  52.     public function authenticate(RequestEvent $event)
  53.     {
  54.         if (null !== $this->tokenStorage->getToken()) {
  55.             return;
  56.         }
  57.         try {
  58.             $token = new AnonymousToken($this->secret'anon.', []);
  59.             if (null !== $this->authenticationManager) {
  60.                 $token $this->authenticationManager->authenticate($token);
  61.             }
  62.             $this->tokenStorage->setToken($token);
  63.             if (null !== $this->logger) {
  64.                 $this->logger->info('Populated the TokenStorage with an anonymous Token.');
  65.             }
  66.         } catch (AuthenticationException $failed) {
  67.             if (null !== $this->logger) {
  68.                 $this->logger->info('Anonymous authentication failed.', ['exception' => $failed]);
  69.             }
  70.         }
  71.     }
  72. }