PreAuthenticatedAuthenticationProvider.php 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. <?php
  2. namespace Symfony\Component\Security\Authentication\Provider;
  3. use Symfony\Component\Security\User\UserProviderInterface;
  4. use Symfony\Component\Security\User\AccountCheckerInterface;
  5. use Symfony\Component\Security\Exception\BadCredentialsException;
  6. use Symfony\Component\Security\Authentication\Token\PreAuthenticatedToken;
  7. use Symfony\Component\Security\Authentication\Token\TokenInterface;
  8. /*
  9. * This file is part of the Symfony package.
  10. *
  11. * (c) Fabien Potencier <fabien.potencier@symfony-project.com>
  12. *
  13. * For the full copyright and license information, please view the LICENSE
  14. * file that was distributed with this source code.
  15. */
  16. /**
  17. * Processes a pre-authenticated authentication request.
  18. *
  19. * This authentication provider will not perform any checks on authentication
  20. * requests, as they should already be pre-authenticated. However, the
  21. * UserProviderInterface implementation may still throw a
  22. * UsernameNotFoundException, for example.
  23. *
  24. * @author Fabien Potencier <fabien.potencier@symfony-project.com>
  25. */
  26. class PreAuthenticatedAuthenticationProvider implements AuthenticationProviderInterface
  27. {
  28. protected $userProvider;
  29. protected $accountChecker;
  30. /**
  31. * Constructor.
  32. *
  33. * @param UserProviderInterface $userProvider A UserProviderInterface instance
  34. * @param AccountCheckerInterface $accountChecker An AccountCheckerInterface instance
  35. */
  36. public function __construct(UserProviderInterface $userProvider, AccountCheckerInterface $accountChecker)
  37. {
  38. $this->userProvider = $userProvider;
  39. $this->accountChecker = $accountChecker;
  40. }
  41. /**
  42. * {@inheritdoc}
  43. */
  44. public function authenticate(TokenInterface $token)
  45. {
  46. if (!$this->supports($token)) {
  47. return null;
  48. }
  49. if (null === $token->getUser()) {
  50. throw new BadCredentialsException('No pre-authenticated principal found in request.');
  51. }
  52. /*
  53. if (null === $token->getCredentials()) {
  54. throw new BadCredentialsException('No pre-authenticated credentials found in request.');
  55. }
  56. */
  57. $user = $this->userProvider->loadUserByUsername($token->getUser());
  58. $this->accountChecker->checkPostAuth($user);
  59. return new PreAuthenticatedToken($user, $token->getCredentials(), $user->getRoles());
  60. }
  61. /**
  62. * {@inheritdoc}
  63. */
  64. public function supports(TokenInterface $token)
  65. {
  66. return $token instanceof PreAuthenticatedToken;
  67. }
  68. }