vendor/scheb/two-factor-bundle/Security/TwoFactor/Trusted/TrustedCookieResponseListener.php line 72

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace Scheb\TwoFactorBundle\Security\TwoFactor\Trusted;
  4. use Symfony\Component\HttpFoundation\Cookie;
  5. use Symfony\Component\HttpKernel\Event\FilterResponseEvent;
  6. use Symfony\Component\HttpKernel\Event\ResponseEvent;
  7. class TrustedCookieResponseListener
  8. {
  9.     /**
  10.      * @var TrustedDeviceTokenStorage
  11.      */
  12.     private $trustedTokenStorage;
  13.     /**
  14.      * @var int
  15.      */
  16.     private $trustedTokenLifetime;
  17.     /**
  18.      * @var string
  19.      */
  20.     private $cookieName;
  21.     /**
  22.      * @var bool
  23.      */
  24.     private $cookieSecure;
  25.     /**
  26.      * @var string|null
  27.      */
  28.     private $cookieSameSite;
  29.     /**
  30.      * @var string|null
  31.      */
  32.     private $cookiePath;
  33.     /**
  34.      * @var string|null
  35.      */
  36.     private $cookieDomain;
  37.     public function __construct(
  38.         TrustedDeviceTokenStorage $trustedTokenStorage,
  39.         int $trustedTokenLifetime,
  40.         string $cookieName,
  41.         bool $cookieSecure,
  42.         ?string $cookieSameSite,
  43.         ?string $cookiePath,
  44.         ?string $cookieDomain
  45.     ) {
  46.         $this->trustedTokenStorage $trustedTokenStorage;
  47.         $this->trustedTokenLifetime $trustedTokenLifetime;
  48.         $this->cookieName $cookieName;
  49.         $this->cookieSecure $cookieSecure;
  50.         $this->cookieSameSite $cookieSameSite;
  51.         $this->cookiePath $cookiePath;
  52.         $this->cookieDomain $cookieDomain;
  53.     }
  54.     /**
  55.      * @psalm-suppress UndefinedDocblockClass
  56.      * @psalm-suppress UndefinedClass
  57.      *
  58.      * @param FilterResponseEvent|ResponseEvent $event
  59.      */
  60.     public function onKernelResponse($event): void
  61.     {
  62.         if ($this->trustedTokenStorage->hasUpdatedCookie()) {
  63.             $domain null;
  64.             if (null !== $this->cookieDomain) {
  65.                 $domain $this->cookieDomain;
  66.             } else {
  67.                 $requestHost $event->getRequest()->getHost();
  68.                 if ($this->shouldSetDomain($requestHost)) {
  69.                     $domain '.'.$requestHost;
  70.                 }
  71.             }
  72.             // Set the cookie
  73.             $cookie = new Cookie(
  74.                 $this->cookieName,
  75.                 $this->trustedTokenStorage->getCookieValue(),
  76.                 $this->getValidUntil(),
  77.                 $this->cookiePath,
  78.                 $domain,
  79.                 $this->cookieSecure,
  80.                 true,
  81.                 false,
  82.                 $this->cookieSameSite
  83.             );
  84.             $response $event->getResponse();
  85.             $response->headers->setCookie($cookie);
  86.         }
  87.     }
  88.     private function shouldSetDomain(string $requestHost): bool
  89.     {
  90.         return !(
  91.             'localhost' === $requestHost
  92.             || preg_match('#^(?:[0-9]{1,3}\.){3}[0-9]{1,3}$#'$requestHost// IPv4
  93.             || substr_count($requestHost':') > // IPv6
  94.         );
  95.     }
  96.     private function getValidUntil(): \DateTime
  97.     {
  98.         return $this->getDateTimeNow()->add(new \DateInterval('PT'.$this->trustedTokenLifetime.'S'));
  99.     }
  100.     protected function getDateTimeNow(): \DateTime
  101.     {
  102.         return new \DateTime();
  103.     }
  104. }