UserAuthenticator.php 2.0 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\Bundle\SecurityBundle\Security;
  11. use Psr\Container\ContainerInterface;
  12. use Symfony\Component\HttpFoundation\Request;
  13. use Symfony\Component\HttpFoundation\RequestStack;
  14. use Symfony\Component\HttpFoundation\Response;
  15. use Symfony\Component\Security\Core\Exception\LogicException;
  16. use Symfony\Component\Security\Core\User\UserInterface;
  17. use Symfony\Component\Security\Http\Authentication\UserAuthenticatorInterface;
  18. use Symfony\Component\Security\Http\Authenticator\AuthenticatorInterface;
  19. /**
  20. * A decorator that delegates all method calls to the authenticator
  21. * manager of the current firewall.
  22. *
  23. * @author Wouter de Jong <wouter@wouterj.nl>
  24. *
  25. * @final
  26. * @experimental in 5.2
  27. */
  28. class UserAuthenticator implements UserAuthenticatorInterface
  29. {
  30. private $firewallMap;
  31. private $userAuthenticators;
  32. private $requestStack;
  33. public function __construct(FirewallMap $firewallMap, ContainerInterface $userAuthenticators, RequestStack $requestStack)
  34. {
  35. $this->firewallMap = $firewallMap;
  36. $this->userAuthenticators = $userAuthenticators;
  37. $this->requestStack = $requestStack;
  38. }
  39. public function authenticateUser(UserInterface $user, AuthenticatorInterface $authenticator, Request $request): ?Response
  40. {
  41. return $this->getUserAuthenticator()->authenticateUser($user, $authenticator, $request);
  42. }
  43. private function getUserAuthenticator(): UserAuthenticatorInterface
  44. {
  45. $firewallConfig = $this->firewallMap->getFirewallConfig($this->requestStack->getMasterRequest());
  46. if (null === $firewallConfig) {
  47. throw new LogicException('Cannot call authenticate on this request, as it is not behind a firewall.');
  48. }
  49. return $this->userAuthenticators->get($firewallConfig->getName());
  50. }
  51. }