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

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