vendor/kunstmaan/admin-bundle/EventListener/SessionSecurityListener.php line 50

Open in your IDE?
  1. <?php
  2. namespace Kunstmaan\AdminBundle\EventListener;
  3. use Psr\Log\LoggerInterface;
  4. use Symfony\Component\HttpFoundation\Request;
  5. use Symfony\Component\HttpFoundation\Session\SessionInterface;
  6. use Symfony\Component\HttpKernel\Event\RequestEvent;
  7. use Symfony\Component\HttpKernel\Event\ResponseEvent;
  8. use Symfony\Component\HttpKernel\HttpKernelInterface;
  9. class SessionSecurityListener
  10. {
  11.     /**
  12.      * @var LoggerInterface
  13.      */
  14.     private $logger;
  15.     /**
  16.      * @var bool
  17.      */
  18.     private $ipCheck;
  19.     /**
  20.      * @var bool
  21.      */
  22.     private $userAgentCheck;
  23.     /**
  24.      * @var string
  25.      */
  26.     private $ip;
  27.     /**
  28.      * @var string
  29.      */
  30.     private $userAgent;
  31.     /**
  32.      * @param bool $ipCheck
  33.      * @param bool $userAgentCheck
  34.      */
  35.     public function __construct($ipCheck$userAgentCheckLoggerInterface $logger)
  36.     {
  37.         $this->ipCheck $ipCheck;
  38.         $this->userAgentCheck $userAgentCheck;
  39.         $this->logger $logger;
  40.     }
  41.     public function onKernelResponse(ResponseEvent $event)
  42.     {
  43.         if (HttpKernelInterface::MAIN_REQUEST !== $event->getRequestType()) {
  44.             return;
  45.         }
  46.         // Make sure the ip and user agent is stored in the session
  47.         $request $event->getRequest();
  48.         if ($request->hasSession() && $request->getSession()->isStarted()) {
  49.             $session $request->getSession();
  50.             if ($this->ipCheck && !$session->has('kuma_ip')) {
  51.                 $session->set('kuma_ip'$this->getIp($request));
  52.             }
  53.             if ($this->userAgentCheck && !$session->has('kuma_ua')) {
  54.                 $session->set('kuma_ua'$this->getUserAgent($request));
  55.             }
  56.         }
  57.     }
  58.     public function onKernelRequest(RequestEvent $event)
  59.     {
  60.         if (HttpKernelInterface::MAIN_REQUEST !== $event->getRequestType()) {
  61.             return;
  62.         }
  63.         $request $event->getRequest();
  64.         if ($request->hasSession() && $request->getSession()->isStarted()) {
  65.             $session $request->getSession();
  66.             // Check that the ip matches
  67.             if ($this->ipCheck && $session->has('kuma_ip') && $session->get('kuma_ip') != $this->getIp($request)) {
  68.                 $this->logger->error(sprintf(
  69.                     "Session ip '%s' does not match with request ip '%s', invalidating the current session",
  70.                     $session->get('kuma_ip'),
  71.                     $this->getIp($request)
  72.                 ));
  73.                 $this->invalidateSession($session$request);
  74.             }
  75.             // Check that the user agent matches
  76.             if ($this->userAgentCheck && $session->has('kuma_ua') && $session->get('kuma_ua') != $this->getUserAgent($request)) {
  77.                 $this->logger->error(sprintf(
  78.                     "Session user agent '%s' does not match with request user agent '%s', invalidating the current session",
  79.                     $session->get('kuma_ua'),
  80.                     $this->getUserAgent($request)
  81.                 ));
  82.                 $this->invalidateSession($session$request);
  83.             }
  84.         }
  85.     }
  86.     private function invalidateSession(SessionInterface $sessionRequest $request)
  87.     {
  88.         $session->invalidate();
  89.         $session->set('kuma_ip'$this->getIp($request));
  90.         $session->set('kuma_ua'$this->getUserAgent($request));
  91.     }
  92.     private function getIp(Request $request): string
  93.     {
  94.         if (!$this->ip) {
  95.             $forwarded $request->server->get('HTTP_X_FORWARDED_FOR');
  96.             if (\strlen($forwarded) > 0) {
  97.                 $parts explode(','$forwarded);
  98.                 $parts array_map('trim'$parts);
  99.                 $parts array_filter($parts);
  100.                 if (\count($parts) > 0) {
  101.                     $ip $parts[0];
  102.                 }
  103.             }
  104.             if (empty($ip)) {
  105.                 $ip $request->getClientIp();
  106.             }
  107.             $this->ip $ip;
  108.         }
  109.         return $this->ip;
  110.     }
  111.     private function getUserAgent(Request $request): array|string
  112.     {
  113.         if (!$this->userAgent) {
  114.             $this->userAgent $request->headers->get('User-Agent');
  115.         }
  116.         return $this->userAgent;
  117.     }
  118. }