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

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\HttpKernel\Event\RequestEvent;
  13. use Symfony\Component\Security\Http\AccessMapInterface;
  14. use Symfony\Component\Security\Http\EntryPoint\AuthenticationEntryPointInterface;
  15. /**
  16.  * ChannelListener switches the HTTP protocol based on the access control
  17.  * configuration.
  18.  *
  19.  * @author Fabien Potencier <[email protected]>
  20.  *
  21.  * @final since Symfony 4.3
  22.  */
  23. class ChannelListener implements ListenerInterface
  24. {
  25.     use LegacyListenerTrait;
  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 __invoke(RequestEvent $event)
  39.     {
  40.         $request $event->getRequest();
  41.         list(, $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 (false !== strpos($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.             $response $this->authenticationEntryPoint->start($request);
  53.             $event->setResponse($response);
  54.             return;
  55.         }
  56.         if ('http' === $channel && $request->isSecure()) {
  57.             if (null !== $this->logger) {
  58.                 $this->logger->info('Redirecting to HTTP.');
  59.             }
  60.             $response $this->authenticationEntryPoint->start($request);
  61.             $event->setResponse($response);
  62.         }
  63.     }
  64. }