vendor/gesdinet/jwt-refresh-token-bundle/Security/Provider/RefreshTokenProvider.php line 23

Open in your IDE?
  1. <?php
  2. /*
  3.  * This file is part of the GesdinetJWTRefreshTokenBundle package.
  4.  *
  5.  * (c) Gesdinet <http://www.gesdinet.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 Gesdinet\JWTRefreshTokenBundle\Security\Provider;
  11. use ReflectionClass;
  12. use Symfony\Component\Security\Core\User\InMemoryUser;
  13. use Symfony\Component\Security\Core\User\UserProviderInterface;
  14. use Symfony\Component\Security\Core\User\User;
  15. use Symfony\Component\Security\Core\User\UserInterface;
  16. use Symfony\Component\Security\Core\Exception\UnsupportedUserException;
  17. use Gesdinet\JWTRefreshTokenBundle\Model\RefreshTokenManagerInterface;
  18. use Gesdinet\JWTRefreshTokenBundle\Model\RefreshTokenInterface;
  19. trigger_deprecation('gesdinet/jwt-refresh-token-bundle''1.0''The "%s" class is deprecated, configure the user provider for the `refresh_jwt` authenticator instead.'RefreshTokenProvider::class);
  20. if ((new ReflectionClass(UserProviderInterface::class))->getMethod('supportsClass')->hasReturnType()) {
  21.     /**
  22.      * Compatibility layer for Symfony 7.0 and later, where {@see UserProviderInterface::supportsClass()} has a return type.
  23.      *
  24.      * @internal
  25.      */
  26.     abstract class CompatRefreshTokenProvider implements UserProviderInterface
  27.     {
  28.         /**
  29.          * @param class-string<UserInterface> $class
  30.          */
  31.         public function supportsClass(string $class): bool
  32.         {
  33.             return $this->doSupportsClass($class);
  34.         }
  35.         /**
  36.          * @param class-string<UserInterface> $class
  37.          */
  38.         abstract protected function doSupportsClass(string $class): bool;
  39.     }
  40. } else {
  41.     /**
  42.      * Compatibility layer for Symfony 6.4 and earlier, where {@see UserProviderInterface::supportsClass()} does not have a return type.
  43.      *
  44.      * @internal
  45.      */
  46.     abstract class CompatRefreshTokenProvider implements UserProviderInterface
  47.     {
  48.         /**
  49.          * @param class-string<UserInterface> $class
  50.          *
  51.          * @return bool
  52.          */
  53.         public function supportsClass($class)
  54.         {
  55.             return $this->doSupportsClass($class);
  56.         }
  57.         /**
  58.          * @param class-string<UserInterface> $class
  59.          */
  60.         abstract protected function doSupportsClass(string $class): bool;
  61.     }
  62. }
  63. /**
  64.  * @deprecated configure the user provider for the `refresh_jwt` authenticator instead
  65.  */
  66. class RefreshTokenProvider extends CompatRefreshTokenProvider
  67. {
  68.     /**
  69.      * @var RefreshTokenManagerInterface
  70.      */
  71.     protected $refreshTokenManager;
  72.     /**
  73.      * @var UserProviderInterface
  74.      */
  75.     protected $customUserProvider;
  76.     public function __construct(RefreshTokenManagerInterface $refreshTokenManager)
  77.     {
  78.         $this->refreshTokenManager $refreshTokenManager;
  79.     }
  80.     /**
  81.      * @return void
  82.      */
  83.     public function setCustomUserProvider(UserProviderInterface $customUserProvider)
  84.     {
  85.         $this->customUserProvider $customUserProvider;
  86.     }
  87.     /**
  88.      * @param string $token
  89.      *
  90.      * @return string|null
  91.      */
  92.     public function getUsernameForRefreshToken($token)
  93.     {
  94.         $refreshToken $this->refreshTokenManager->get($token);
  95.         if ($refreshToken instanceof RefreshTokenInterface) {
  96.             return $refreshToken->getUsername();
  97.         }
  98.         return null;
  99.     }
  100.     /**
  101.      * @param string $username
  102.      *
  103.      * @return UserInterface
  104.      *
  105.      * @deprecated use loadUserByIdentifier() instead
  106.      */
  107.     public function loadUserByUsername($username)
  108.     {
  109.         return $this->loadUserByIdentifier($username);
  110.     }
  111.     public function loadUserByIdentifier(string $identifier): UserInterface
  112.     {
  113.         if (null !== $this->customUserProvider) {
  114.             if (method_exists($this->customUserProvider'loadUserByIdentifier')) {
  115.                 return $this->customUserProvider->loadUserByIdentifier($identifier);
  116.             }
  117.             return $this->customUserProvider->loadUserByUsername($identifier);
  118.         }
  119.         if (class_exists(InMemoryUser::class)) {
  120.             return new InMemoryUser(
  121.                 $identifier,
  122.                 null,
  123.                 ['ROLE_USER']
  124.             );
  125.         }
  126.         return new User(
  127.             $identifier,
  128.             null,
  129.             ['ROLE_USER']
  130.         );
  131.     }
  132.     public function refreshUser(UserInterface $user): UserInterface
  133.     {
  134.         if (null !== $this->customUserProvider) {
  135.             return $this->customUserProvider->refreshUser($user);
  136.         }
  137.         throw new UnsupportedUserException();
  138.     }
  139.     /**
  140.      * @param class-string<UserInterface> $class
  141.      */
  142.     protected function doSupportsClass(string $class): bool
  143.     {
  144.         if (null !== $this->customUserProvider) {
  145.             return $this->customUserProvider->supportsClass($class);
  146.         }
  147.         if (class_exists(InMemoryUser::class) && InMemoryUser::class === $class) {
  148.             return true;
  149.         }
  150.         return class_exists(User::class) && User::class === $class;
  151.     }
  152. }