AbstractAuthenticator.php 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  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 Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  12. use Symfony\Component\Security\Core\Exception\LogicException;
  13. use Symfony\Component\Security\Http\Authenticator\Passport\PassportInterface;
  14. use Symfony\Component\Security\Http\Authenticator\Passport\UserPassportInterface;
  15. use Symfony\Component\Security\Http\Authenticator\Token\PostAuthenticationToken;
  16. /**
  17. * An optional base class that creates the necessary tokens for you.
  18. *
  19. * @author Ryan Weaver <ryan@symfonycasts.com>
  20. *
  21. * @experimental in 5.2
  22. */
  23. abstract class AbstractAuthenticator implements AuthenticatorInterface
  24. {
  25. /**
  26. * Shortcut to create a PostAuthenticationToken for you, if you don't really
  27. * care about which authenticated token you're using.
  28. *
  29. * @return PostAuthenticationToken
  30. */
  31. public function createAuthenticatedToken(PassportInterface $passport, string $firewallName): TokenInterface
  32. {
  33. if (!$passport instanceof UserPassportInterface) {
  34. throw new LogicException(sprintf('Passport does not contain a user, overwrite "createAuthenticatedToken()" in "%s" to create a custom authenticated token.', static::class));
  35. }
  36. return new PostAuthenticationToken($passport->getUser(), $firewallName, $passport->getUser()->getRoles());
  37. }
  38. }