BasicAuthenticationEntryPoint.php 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  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\Request;
  12. use Symfony\Component\HttpFoundation\Response;
  13. use Symfony\Component\Security\Core\Exception\AuthenticationException;
  14. /**
  15. * BasicAuthenticationEntryPoint starts an HTTP Basic authentication.
  16. *
  17. * @author Fabien Potencier <fabien@symfony.com>
  18. */
  19. class BasicAuthenticationEntryPoint implements AuthenticationEntryPointInterface
  20. {
  21. private $realmName;
  22. public function __construct(string $realmName)
  23. {
  24. $this->realmName = $realmName;
  25. }
  26. /**
  27. * {@inheritdoc}
  28. */
  29. public function start(Request $request, AuthenticationException $authException = null)
  30. {
  31. $response = new Response();
  32. $response->headers->set('WWW-Authenticate', sprintf('Basic realm="%s"', $this->realmName));
  33. $response->setStatusCode(401);
  34. return $response;
  35. }
  36. }