vendor/gesdinet/jwt-refresh-token-bundle/Service/RefreshToken.php line 28

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\Service;
  11. use DateTime;
  12. use Gesdinet\JWTRefreshTokenBundle\Event\RefreshEvent;
  13. use Gesdinet\JWTRefreshTokenBundle\Security\Authenticator\RefreshTokenAuthenticator;
  14. use Gesdinet\JWTRefreshTokenBundle\Exception\InvalidRefreshTokenException;
  15. use Symfony\Component\HttpFoundation\Response;
  16. use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
  17. use InvalidArgumentException;
  18. use Symfony\Component\HttpFoundation\Request;
  19. use Symfony\Component\Security\Core\Exception\AuthenticationException;
  20. use Gesdinet\JWTRefreshTokenBundle\Model\RefreshTokenManagerInterface;
  21. use Gesdinet\JWTRefreshTokenBundle\Security\Provider\RefreshTokenProvider;
  22. use Symfony\Component\Security\Http\Authentication\AuthenticationFailureHandlerInterface;
  23. use Symfony\Component\Security\Http\Authentication\AuthenticationSuccessHandlerInterface;
  24. trigger_deprecation('gesdinet/jwt-refresh-token-bundle''1.0''The "%s" class is deprecated, use the `refresh_jwt` authenticator instead.'RefreshToken::class);
  25. /**
  26.  * @deprecated use the `refresh_jwt` authenticator instead
  27.  */
  28. class RefreshToken
  29. {
  30.     private RefreshTokenAuthenticator $authenticator;
  31.     private RefreshTokenProvider $provider;
  32.     private AuthenticationSuccessHandlerInterface $successHandler;
  33.     private AuthenticationFailureHandlerInterface $failureHandler;
  34.     private RefreshTokenManagerInterface $refreshTokenManager;
  35.     private int $ttl;
  36.     private string $providerKey;
  37.     private bool $ttlUpdate;
  38.     private EventDispatcherInterface $eventDispatcher;
  39.     /**
  40.      * @param int    $ttl
  41.      * @param string $providerKey
  42.      * @param bool   $ttlUpdate
  43.      */
  44.     public function __construct(
  45.         RefreshTokenAuthenticator $authenticator,
  46.         RefreshTokenProvider $provider,
  47.         AuthenticationSuccessHandlerInterface $successHandler,
  48.         AuthenticationFailureHandlerInterface $failureHandler,
  49.         RefreshTokenManagerInterface $refreshTokenManager,
  50.         $ttl,
  51.         $providerKey,
  52.         $ttlUpdate,
  53.         EventDispatcherInterface $eventDispatcher
  54.     ) {
  55.         $this->authenticator $authenticator;
  56.         $this->provider $provider;
  57.         $this->successHandler $successHandler;
  58.         $this->failureHandler $failureHandler;
  59.         $this->refreshTokenManager $refreshTokenManager;
  60.         $this->ttl $ttl;
  61.         $this->providerKey $providerKey;
  62.         $this->ttlUpdate $ttlUpdate;
  63.         $this->eventDispatcher $eventDispatcher;
  64.     }
  65.     /**
  66.      * @return Response
  67.      *
  68.      * @throws InvalidArgumentException
  69.      * @throws AuthenticationException
  70.      */
  71.     public function refresh(Request $request)
  72.     {
  73.         $credentials $this->authenticator->getCredentials($request);
  74.         try {
  75.             $user $this->authenticator->getUser($credentials$this->provider);
  76.             $postAuthenticationToken $this->authenticator->createAuthenticatedToken($user$this->providerKey);
  77.         } catch (AuthenticationException $e) {
  78.             return $this->failureHandler->onAuthenticationFailure($request$e);
  79.         }
  80.         $refreshToken $this->refreshTokenManager->get($credentials['token']);
  81.         if (null === $refreshToken || !$refreshToken->isValid()) {
  82.             return $this->failureHandler->onAuthenticationFailure(
  83.                 $request,
  84.                 new InvalidRefreshTokenException(
  85.                     sprintf('Refresh token "%s" is invalid.', (string) $refreshToken)
  86.                 )
  87.             );
  88.         }
  89.         if ($this->ttlUpdate) {
  90.             $expirationDate = new DateTime();
  91.             $expirationDate->modify(sprintf('+%d seconds'$this->ttl));
  92.             $refreshToken->setValid($expirationDate);
  93.             $this->refreshTokenManager->save($refreshToken);
  94.         }
  95.         $event = new RefreshEvent($refreshToken$postAuthenticationToken);
  96.         $this->eventDispatcher->dispatch($event'gesdinet.refresh_token');
  97.         return $this->successHandler->onAuthenticationSuccess($request$postAuthenticationToken);
  98.     }
  99. }