AnonymousAuthenticationProvider.php 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. <?php
  2. namespace Symfony\Component\Security\Authentication\Provider;
  3. use Symfony\Component\Security\Authentication\Token\TokenInterface;
  4. use Symfony\Component\Security\Exception\BadCredentialsException;
  5. use Symfony\Component\Security\Authentication\Token\AnonymousToken;
  6. /*
  7. * This file is part of the Symfony framework.
  8. *
  9. * (c) Fabien Potencier <fabien.potencier@symfony-project.com>
  10. *
  11. * This source file is subject to the MIT license that is bundled
  12. * with this source code in the file LICENSE.
  13. */
  14. /**
  15. * AnonymousAuthenticationProvider validates AnonymousToken instances.
  16. *
  17. * @author Fabien Potencier <fabien.potencier@symfony-project.com>
  18. */
  19. class AnonymousAuthenticationProvider implements AuthenticationProviderInterface
  20. {
  21. protected $key;
  22. /**
  23. * Constructor.
  24. *
  25. * @param string $key The key shared with the authentication token
  26. */
  27. public function __construct($key)
  28. {
  29. $this->key = $key;
  30. }
  31. /**
  32. * {@inheritdoc}
  33. */
  34. public function authenticate(TokenInterface $token)
  35. {
  36. if (!$this->supports($token)) {
  37. return null;
  38. }
  39. if ($this->key != $token->getKey()) {
  40. throw new BadCredentialsException('The Token does not contain the expected key.');
  41. }
  42. return $token;
  43. }
  44. /**
  45. * {@inheritdoc}
  46. */
  47. public function supports(TokenInterface $token)
  48. {
  49. return $token instanceof AnonymousToken;
  50. }
  51. }