RetryAuthenticationEntryPoint.php 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  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\EntryPoint;
  11. use Symfony\Component\HttpFoundation\RedirectResponse;
  12. use Symfony\Component\HttpFoundation\Request;
  13. use Symfony\Component\Security\Core\Exception\AuthenticationException;
  14. /**
  15. * RetryAuthenticationEntryPoint redirects URL based on the configured scheme.
  16. *
  17. * This entry point is not intended to work with HTTP post requests.
  18. *
  19. * @author Fabien Potencier <fabien@symfony.com>
  20. */
  21. class RetryAuthenticationEntryPoint implements AuthenticationEntryPointInterface
  22. {
  23. private $httpPort;
  24. private $httpsPort;
  25. public function __construct(int $httpPort = 80, int $httpsPort = 443)
  26. {
  27. $this->httpPort = $httpPort;
  28. $this->httpsPort = $httpsPort;
  29. }
  30. /**
  31. * {@inheritdoc}
  32. */
  33. public function start(Request $request, AuthenticationException $authException = null)
  34. {
  35. $scheme = $request->isSecure() ? 'http' : 'https';
  36. if ('http' === $scheme && 80 != $this->httpPort) {
  37. $port = ':'.$this->httpPort;
  38. } elseif ('https' === $scheme && 443 != $this->httpsPort) {
  39. $port = ':'.$this->httpsPort;
  40. } else {
  41. $port = '';
  42. }
  43. $qs = $request->getQueryString();
  44. if (null !== $qs) {
  45. $qs = '?'.$qs;
  46. }
  47. $url = $scheme.'://'.$request->getHost().$port.$request->getBaseUrl().$request->getPathInfo().$qs;
  48. return new RedirectResponse($url, 301);
  49. }
  50. }