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

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