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

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. // Help opcache.preload discover always-needed symbols
  19. class_exists(AnonymousToken::class);
  20. /**
  21.  * AnonymousAuthenticationListener automatically adds a Token if none is
  22.  * already present.
  23.  *
  24.  * @author Fabien Potencier <fabien@symfony.com>
  25.  *
  26.  * @final
  27.  */
  28. class AnonymousAuthenticationListener extends AbstractListener
  29. {
  30.     private $tokenStorage;
  31.     private $secret;
  32.     private $authenticationManager;
  33.     private $logger;
  34.     public function __construct(TokenStorageInterface $tokenStoragestring $secretLoggerInterface $logger nullAuthenticationManagerInterface $authenticationManager null)
  35.     {
  36.         $this->tokenStorage $tokenStorage;
  37.         $this->secret $secret;
  38.         $this->authenticationManager $authenticationManager;
  39.         $this->logger $logger;
  40.     }
  41.     /**
  42.      * {@inheritdoc}
  43.      */
  44.     public function supports(Request $request): ?bool
  45.     {
  46.         return null// always run authenticate() lazily with lazy firewalls
  47.     }
  48.     /**
  49.      * Handles anonymous authentication.
  50.      */
  51.     public function authenticate(RequestEvent $event)
  52.     {
  53.         if (null !== $this->tokenStorage->getToken()) {
  54.             return;
  55.         }
  56.         try {
  57.             $token = new AnonymousToken($this->secret'anon.', []);
  58.             if (null !== $this->authenticationManager) {
  59.                 $token $this->authenticationManager->authenticate($token);
  60.             }
  61.             $this->tokenStorage->setToken($token);
  62.             if (null !== $this->logger) {
  63.                 $this->logger->info('Populated the TokenStorage with an anonymous Token.');
  64.             }
  65.         } catch (AuthenticationException $failed) {
  66.             if (null !== $this->logger) {
  67.                 $this->logger->info('Anonymous authentication failed.', ['exception' => $failed]);
  68.             }
  69.         }
  70.     }
  71. }