AnonymousAuthenticationProvider.php 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  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\Core\Authentication\Provider;
  11. use Symfony\Component\Security\Core\Authentication\Token\AnonymousToken;
  12. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  13. use Symfony\Component\Security\Core\Exception\AuthenticationException;
  14. use Symfony\Component\Security\Core\Exception\BadCredentialsException;
  15. /**
  16. * AnonymousAuthenticationProvider validates AnonymousToken instances.
  17. *
  18. * @author Fabien Potencier <fabien@symfony.com>
  19. */
  20. class AnonymousAuthenticationProvider implements AuthenticationProviderInterface
  21. {
  22. /**
  23. * Used to determine if the token is created by the application
  24. * instead of a malicious client.
  25. *
  26. * @var string
  27. */
  28. private $secret;
  29. /**
  30. * @param string $secret The secret shared with the AnonymousToken
  31. */
  32. public function __construct(string $secret)
  33. {
  34. $this->secret = $secret;
  35. }
  36. /**
  37. * {@inheritdoc}
  38. */
  39. public function authenticate(TokenInterface $token)
  40. {
  41. if (!$this->supports($token)) {
  42. throw new AuthenticationException('The token is not supported by this authentication provider.');
  43. }
  44. if ($this->secret !== $token->getSecret()) {
  45. throw new BadCredentialsException('The Token does not contain the expected key.');
  46. }
  47. return $token;
  48. }
  49. /**
  50. * {@inheritdoc}
  51. */
  52. public function supports(TokenInterface $token)
  53. {
  54. return $token instanceof AnonymousToken;
  55. }
  56. }