vendor/symfony/security-http/Session/SessionAuthenticationStrategy.php line 37

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\Session;
  11. use Symfony\Component\HttpFoundation\Request;
  12. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  13. use Symfony\Component\Security\Csrf\TokenStorage\ClearableTokenStorageInterface;
  14. /**
  15.  * The default session strategy implementation.
  16.  *
  17.  * Supports the following strategies:
  18.  * NONE: the session is not changed
  19.  * MIGRATE: the session id is updated, attributes are kept
  20.  * INVALIDATE: the session id is updated, attributes are lost
  21.  *
  22.  * @author Johannes M. Schmitt <schmittjoh@gmail.com>
  23.  */
  24. class SessionAuthenticationStrategy implements SessionAuthenticationStrategyInterface
  25. {
  26.     public const NONE 'none';
  27.     public const MIGRATE 'migrate';
  28.     public const INVALIDATE 'invalidate';
  29.     private string $strategy;
  30.     private ?ClearableTokenStorageInterface $csrfTokenStorage null;
  31.     public function __construct(string $strategyClearableTokenStorageInterface $csrfTokenStorage null)
  32.     {
  33.         $this->strategy $strategy;
  34.         if (self::MIGRATE === $strategy) {
  35.             $this->csrfTokenStorage $csrfTokenStorage;
  36.         }
  37.     }
  38.     /**
  39.      * {@inheritdoc}
  40.      */
  41.     public function onAuthentication(Request $requestTokenInterface $token)
  42.     {
  43.         switch ($this->strategy) {
  44.             case self::NONE:
  45.                 return;
  46.             case self::MIGRATE:
  47.                 $request->getSession()->migrate(true);
  48.                 if ($this->csrfTokenStorage) {
  49.                     $this->csrfTokenStorage->clear();
  50.                 }
  51.                 return;
  52.             case self::INVALIDATE:
  53.                 $request->getSession()->invalidate();
  54.                 return;
  55.             default:
  56.                 throw new \RuntimeException(sprintf('Invalid session authentication strategy "%s".'$this->strategy));
  57.         }
  58.     }
  59. }