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

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\HttpKernel\Event\GetResponseEvent;
  13. use Symfony\Component\Security\Core\Authentication\AuthenticationManagerInterface;
  14. use Symfony\Component\Security\Core\Authentication\Token\AnonymousToken;
  15. use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
  16. use Symfony\Component\Security\Core\Exception\AuthenticationException;
  17. /**
  18.  * AnonymousAuthenticationListener automatically adds a Token if none is
  19.  * already present.
  20.  *
  21.  * @author Fabien Potencier <fabien@symfony.com>
  22.  */
  23. class AnonymousAuthenticationListener implements ListenerInterface
  24. {
  25.     private $tokenStorage;
  26.     private $secret;
  27.     private $authenticationManager;
  28.     private $logger;
  29.     public function __construct(TokenStorageInterface $tokenStoragestring $secretLoggerInterface $logger nullAuthenticationManagerInterface $authenticationManager null)
  30.     {
  31.         $this->tokenStorage $tokenStorage;
  32.         $this->secret $secret;
  33.         $this->authenticationManager $authenticationManager;
  34.         $this->logger $logger;
  35.     }
  36.     /**
  37.      * Handles anonymous authentication.
  38.      */
  39.     public function handle(GetResponseEvent $event)
  40.     {
  41.         if (null !== $this->tokenStorage->getToken()) {
  42.             return;
  43.         }
  44.         try {
  45.             $token = new AnonymousToken($this->secret'anon.', []);
  46.             if (null !== $this->authenticationManager) {
  47.                 $token $this->authenticationManager->authenticate($token);
  48.             }
  49.             $this->tokenStorage->setToken($token);
  50.             if (null !== $this->logger) {
  51.                 $this->logger->info('Populated the TokenStorage with an anonymous Token.');
  52.             }
  53.         } catch (AuthenticationException $failed) {
  54.             if (null !== $this->logger) {
  55.                 $this->logger->info('Anonymous authentication failed.', ['exception' => $failed]);
  56.             }
  57.         }
  58.     }
  59. }