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

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.     private $tokenStorage;
  25.     private $secret;
  26.     private $authenticationManager;
  27.     private $logger;
  28.     public function __construct(TokenStorageInterface $tokenStoragestring $secretLoggerInterface $logger nullAuthenticationManagerInterface $authenticationManager null) {
  29.         $this->tokenStorage $tokenStorage;
  30.         $this->secret $secret;
  31.         $this->authenticationManager $authenticationManager;
  32.         $this->logger $logger;
  33.     }
  34.     /**
  35.      * Handles anonymous authentication.
  36.      */
  37.     public function handle(GetResponseEvent $event) {
  38.         if (null !== $this->tokenStorage->getToken()) {
  39.             return;
  40.         }
  41.         try {
  42.             $token = new AnonymousToken($this->secret'anon.', []);
  43.             if (null !== $this->authenticationManager) {
  44.                 $token $this->authenticationManager->authenticate($token);
  45.             }
  46.             $this->tokenStorage->setToken($token);
  47.             if (null !== $this->logger) {
  48.                 $this->logger->info('Populated the TokenStorage with an anonymous Token.');
  49.             }
  50.         } catch (AuthenticationException $failed) {
  51.             if (null !== $this->logger) {
  52.                 $this->logger->info('Anonymous authentication failed.', ['exception' => $failed]);
  53.             }
  54.         }
  55.     }
  56. }