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

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