RetryAuthenticationEntryPoint.php 1.9 KB

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