vendor/symfony/security-http/Firewall/ChannelListener.php line 28

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\HttpFoundation\Request;
  13. use Symfony\Component\HttpKernel\Event\RequestEvent;
  14. use Symfony\Component\Security\Http\AccessMapInterface;
  15. use Symfony\Component\Security\Http\EntryPoint\AuthenticationEntryPointInterface;
  16. /**
  17.  * ChannelListener switches the HTTP protocol based on the access control
  18.  * configuration.
  19.  *
  20.  * @author Fabien Potencier <fabien@symfony.com>
  21.  *
  22.  * @final
  23.  */
  24. class ChannelListener extends AbstractListener
  25. {
  26.     private $map;
  27.     private $authenticationEntryPoint;
  28.     private $logger;
  29.     public function __construct(AccessMapInterface $mapAuthenticationEntryPointInterface $authenticationEntryPointLoggerInterface $logger null)
  30.     {
  31.         $this->map $map;
  32.         $this->authenticationEntryPoint $authenticationEntryPoint;
  33.         $this->logger $logger;
  34.     }
  35.     /**
  36.      * Handles channel management.
  37.      */
  38.     public function supports(Request $request): ?bool
  39.     {
  40.         [, $channel] = $this->map->getPatterns($request);
  41.         if ('https' === $channel && !$request->isSecure()) {
  42.             if (null !== $this->logger) {
  43.                 if ('https' === $request->headers->get('X-Forwarded-Proto')) {
  44.                     $this->logger->info('Redirecting to HTTPS. ("X-Forwarded-Proto" header is set to "https" - did you set "trusted_proxies" correctly?)');
  45.                 } elseif (str_contains($request->headers->get('Forwarded'''), 'proto=https')) {
  46.                     $this->logger->info('Redirecting to HTTPS. ("Forwarded" header is set to "proto=https" - did you set "trusted_proxies" correctly?)');
  47.                 } else {
  48.                     $this->logger->info('Redirecting to HTTPS.');
  49.                 }
  50.             }
  51.             return true;
  52.         }
  53.         if ('http' === $channel && $request->isSecure()) {
  54.             if (null !== $this->logger) {
  55.                 $this->logger->info('Redirecting to HTTP.');
  56.             }
  57.             return true;
  58.         }
  59.         return false;
  60.     }
  61.     public function authenticate(RequestEvent $event)
  62.     {
  63.         $request $event->getRequest();
  64.         $response $this->authenticationEntryPoint->start($request);
  65.         $event->setResponse($response);
  66.     }
  67. }