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

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