RemoteUserAuthenticator.php 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  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\Authenticator;
  11. use Psr\Log\LoggerInterface;
  12. use Symfony\Component\HttpFoundation\Request;
  13. use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
  14. use Symfony\Component\Security\Core\Exception\BadCredentialsException;
  15. use Symfony\Component\Security\Core\User\UserProviderInterface;
  16. /**
  17. * This authenticator authenticates a remote user.
  18. *
  19. * @author Wouter de Jong <wouter@wouterj.nl>
  20. * @author Fabien Potencier <fabien@symfony.com>
  21. * @author Maxime Douailin <maxime.douailin@gmail.com>
  22. *
  23. * @final
  24. *
  25. * @internal in Symfony 5.1
  26. */
  27. class RemoteUserAuthenticator extends AbstractPreAuthenticatedAuthenticator
  28. {
  29. private $userKey;
  30. public function __construct(UserProviderInterface $userProvider, TokenStorageInterface $tokenStorage, string $firewallName, string $userKey = 'REMOTE_USER', ?LoggerInterface $logger = null)
  31. {
  32. parent::__construct($userProvider, $tokenStorage, $firewallName, $logger);
  33. $this->userKey = $userKey;
  34. }
  35. protected function extractUsername(Request $request): ?string
  36. {
  37. if (!$request->server->has($this->userKey)) {
  38. throw new BadCredentialsException(sprintf('User key was not found: "%s".', $this->userKey));
  39. }
  40. return $request->server->get($this->userKey);
  41. }
  42. }