vendor/gesdinet/jwt-refresh-token-bundle/Security/Authenticator/RefreshTokenAuthenticator.php line 31

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\Authenticator;
  11. use Gesdinet\JWTRefreshTokenBundle\Request\Extractor\ExtractorInterface;
  12. use Gesdinet\JWTRefreshTokenBundle\Exception\UnknownRefreshTokenException;
  13. use Gesdinet\JWTRefreshTokenBundle\Exception\UnknownUserFromRefreshTokenException;
  14. use Gesdinet\JWTRefreshTokenBundle\Security\Exception\MissingTokenException;
  15. use Symfony\Component\HttpFoundation\JsonResponse;
  16. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  17. use Symfony\Component\Security\Core\Exception\AuthenticationException;
  18. use Symfony\Component\HttpFoundation\Request;
  19. use Symfony\Component\Security\Core\Exception\UsernameNotFoundException;
  20. use Symfony\Component\Security\Core\Exception\UserNotFoundException;
  21. use Symfony\Component\Security\Core\User\UserCheckerInterface;
  22. use Symfony\Component\Security\Core\User\UserInterface;
  23. use Symfony\Component\Security\Core\User\UserProviderInterface;
  24. use Symfony\Component\Security\Guard\AbstractGuardAuthenticator;
  25. use Symfony\Component\HttpFoundation\Response;
  26. use Gesdinet\JWTRefreshTokenBundle\Security\Provider\RefreshTokenProvider;
  27. trigger_deprecation('gesdinet/jwt-refresh-token-bundle''1.0''The "%s" class is deprecated, use the `refresh_jwt` authenticator instead.'RefreshTokenAuthenticator::class);
  28. /**
  29.  * @deprecated use the `refresh_jwt` authenticator instead
  30.  */
  31. class RefreshTokenAuthenticator extends AbstractGuardAuthenticator
  32. {
  33.     private UserCheckerInterface $userChecker;
  34.     /**
  35.      * @var string
  36.      */
  37.     protected $tokenParameterName;
  38.     /**
  39.      * @var ExtractorInterface
  40.      */
  41.     protected $extractor;
  42.     /**
  43.      * @param string $tokenParameterName
  44.      */
  45.     public function __construct(UserCheckerInterface $userChecker$tokenParameterNameExtractorInterface $extractor)
  46.     {
  47.         $this->userChecker $userChecker;
  48.         $this->tokenParameterName $tokenParameterName;
  49.         $this->extractor $extractor;
  50.     }
  51.     /**
  52.      * @return bool
  53.      */
  54.     public function supports(Request $request)
  55.     {
  56.         return null !== $this->extractor->getRefreshToken($request$this->tokenParameterName);
  57.     }
  58.     /**
  59.      * @return array{token: string|null}
  60.      */
  61.     public function getCredentials(Request $request)
  62.     {
  63.         return [
  64.             'token' => $this->extractor->getRefreshToken($request$this->tokenParameterName),
  65.         ];
  66.     }
  67.     /**
  68.      * @param array{token: string|null} $credentials
  69.      *
  70.      * @return UserInterface
  71.      */
  72.     public function getUser($credentialsUserProviderInterface $userProvider)
  73.     {
  74.         if (!$userProvider instanceof RefreshTokenProvider) {
  75.             throw new \InvalidArgumentException(sprintf('The user provider must be an instance of RefreshTokenProvider (%s was given).'get_class($userProvider)));
  76.         }
  77.         $refreshToken $credentials['token'] ?? null;
  78.         if (null === $refreshToken) {
  79.             throw new MissingTokenException('The refresh token could not be read from the request.');
  80.         }
  81.         $username $userProvider->getUsernameForRefreshToken($refreshToken);
  82.         if (null === $username) {
  83.             throw new UnknownRefreshTokenException(sprintf('Refresh token "%s" does not exist.'$refreshToken));
  84.         }
  85.         try {
  86.             $user $userProvider->loadUserByUsername($username);
  87.         } catch (UsernameNotFoundException|UserNotFoundException $exception) {
  88.             throw new UnknownUserFromRefreshTokenException(sprintf('User with refresh token "%s" does not exist.'$refreshToken), $exception->getCode(), $exception);
  89.         }
  90.         $this->userChecker->checkPreAuth($user);
  91.         $this->userChecker->checkPostAuth($user);
  92.         return $user;
  93.     }
  94.     /**
  95.      * @param array{token: string|null} $credentials
  96.      *
  97.      * @return bool
  98.      */
  99.     public function checkCredentials($credentialsUserInterface $user)
  100.     {
  101.         // check credentials - e.g. make sure the password is valid
  102.         // no credential check is needed in this case
  103.         // return true to cause authentication success
  104.         return true;
  105.     }
  106.     /**
  107.      * @param string $providerKey
  108.      *
  109.      * @return null
  110.      */
  111.     public function onAuthenticationSuccess(Request $requestTokenInterface $token$providerKey)
  112.     {
  113.         // on success, let the request continue
  114.         return null;
  115.     }
  116.     /**
  117.      * @return Response
  118.      */
  119.     public function onAuthenticationFailure(Request $requestAuthenticationException $exception)
  120.     {
  121.         return new Response('Refresh token authentication failed.'403);
  122.     }
  123.     /**
  124.      * @return Response
  125.      */
  126.     public function start(Request $requestAuthenticationException $authException null)
  127.     {
  128.         $data = [
  129.             // you might translate this message
  130.             'message' => 'Authentication Required',
  131.         ];
  132.         return new JsonResponse($dataResponse::HTTP_UNAUTHORIZED);
  133.     }
  134.     /**
  135.      * @return bool
  136.      */
  137.     public function supportsRememberMe()
  138.     {
  139.         return false;
  140.     }
  141. }