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 <[email protected]>
  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 <[email protected]>
  21.  *
  22.  * @final since Symfony 4.3
  23.  */
  24. class ChannelListener extends AbstractListener implements ListenerInterface
  25. {
  26.     use LegacyListenerTrait;
  27.     private $map;
  28.     private $authenticationEntryPoint;
  29.     private $logger;
  30.     public function __construct(AccessMapInterface $mapAuthenticationEntryPointInterface $authenticationEntryPointLoggerInterface $logger null)
  31.     {
  32.         $this->map $map;
  33.         $this->authenticationEntryPoint $authenticationEntryPoint;
  34.         $this->logger $logger;
  35.     }
  36.     /**
  37.      * Handles channel management.
  38.      */
  39.     public function supports(Request $request): ?bool
  40.     {
  41.         [, $channel] = $this->map->getPatterns($request);
  42.         if ('https' === $channel && !$request->isSecure()) {
  43.             if (null !== $this->logger) {
  44.                 if ('https' === $request->headers->get('X-Forwarded-Proto')) {
  45.                     $this->logger->info('Redirecting to HTTPS. ("X-Forwarded-Proto" header is set to "https" - did you set "trusted_proxies" correctly?)');
  46.                 } elseif (str_contains($request->headers->get('Forwarded'''), 'proto=https')) {
  47.                     $this->logger->info('Redirecting to HTTPS. ("Forwarded" header is set to "proto=https" - did you set "trusted_proxies" correctly?)');
  48.                 } else {
  49.                     $this->logger->info('Redirecting to HTTPS.');
  50.                 }
  51.             }
  52.             return true;
  53.         }
  54.         if ('http' === $channel && $request->isSecure()) {
  55.             if (null !== $this->logger) {
  56.                 $this->logger->info('Redirecting to HTTP.');
  57.             }
  58.             return true;
  59.         }
  60.         return false;
  61.     }
  62.     public function authenticate(RequestEvent $event)
  63.     {
  64.         $request $event->getRequest();
  65.         $response $this->authenticationEntryPoint->start($request);
  66.         $event->setResponse($response);
  67.     }
  68. }