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

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 Symfony\Component\EventDispatcher\EventDispatcherInterface;
  14. use Symfony\Contracts\EventDispatcher\EventDispatcherInterface as ContractsEventDispatcherInterface;
  15. use InvalidArgumentException;
  16. use Symfony\Component\HttpFoundation\Request;
  17. use Symfony\Component\Security\Core\Exception\AuthenticationException;
  18. use Gesdinet\JWTRefreshTokenBundle\Model\RefreshTokenManagerInterface;
  19. use Gesdinet\JWTRefreshTokenBundle\Security\Provider\RefreshTokenProvider;
  20. use Symfony\Component\Security\Http\Authentication\AuthenticationFailureHandlerInterface;
  21. use Symfony\Component\Security\Http\Authentication\AuthenticationSuccessHandlerInterface;
  22. /**
  23.  * Class RefreshToken.
  24.  */
  25. class RefreshToken
  26. {
  27.     /**
  28.      * @var RefreshTokenAuthenticator
  29.      */
  30.     private $authenticator;
  31.     /**
  32.      * @var RefreshTokenProvider
  33.      */
  34.     private $provider;
  35.     /**
  36.      * @var AuthenticationSuccessHandlerInterface
  37.      */
  38.     private $successHandler;
  39.     /**
  40.      * @var AuthenticationFailureHandlerInterface
  41.      */
  42.     private $failureHandler;
  43.     /**
  44.      * @var RefreshTokenManagerInterface
  45.      */
  46.     private $refreshTokenManager;
  47.     /**
  48.      * @var int
  49.      */
  50.     private $ttl;
  51.     /**
  52.      * @var string
  53.      */
  54.     private $providerKey;
  55.     /**
  56.      * @var bool
  57.      */
  58.     private $ttlUpdate;
  59.     /**
  60.      * @var EventDispatcherInterface
  61.      */
  62.     private $eventDispatcher;
  63.     /**
  64.      * RefreshToken constructor.
  65.      *
  66.      * @param RefreshTokenAuthenticator             $authenticator
  67.      * @param RefreshTokenProvider                  $provider
  68.      * @param AuthenticationSuccessHandlerInterface $successHandler
  69.      * @param AuthenticationFailureHandlerInterface $failureHandler
  70.      * @param RefreshTokenManagerInterface          $refreshTokenManager
  71.      * @param int                                   $ttl
  72.      * @param string                                $providerKey
  73.      * @param bool                                  $ttlUpdate
  74.      * @param EventDispatcherInterface              $eventDispatcher
  75.      */
  76.     public function __construct(
  77.         RefreshTokenAuthenticator $authenticator,
  78.         RefreshTokenProvider $provider,
  79.         AuthenticationSuccessHandlerInterface $successHandler,
  80.         AuthenticationFailureHandlerInterface $failureHandler,
  81.         RefreshTokenManagerInterface $refreshTokenManager,
  82.         $ttl,
  83.         $providerKey,
  84.         $ttlUpdate,
  85.         EventDispatcherInterface $eventDispatcher
  86.     ) {
  87.         $this->authenticator $authenticator;
  88.         $this->provider $provider;
  89.         $this->successHandler $successHandler;
  90.         $this->failureHandler $failureHandler;
  91.         $this->refreshTokenManager $refreshTokenManager;
  92.         $this->ttl $ttl;
  93.         $this->providerKey $providerKey;
  94.         $this->ttlUpdate $ttlUpdate;
  95.         $this->eventDispatcher $eventDispatcher;
  96.     }
  97.     /**
  98.      * Refresh token.
  99.      *
  100.      * @param Request $request
  101.      *
  102.      * @return mixed
  103.      *
  104.      * @throws InvalidArgumentException
  105.      * @throws AuthenticationException
  106.      */
  107.     public function refresh(Request $request)
  108.     {
  109.         try {
  110.             $user $this->authenticator->getUser(
  111.                 $this->authenticator->getCredentials($request),
  112.                 $this->provider
  113.             );
  114.             $postAuthenticationToken $this->authenticator->createAuthenticatedToken($user$this->providerKey);
  115.         } catch (AuthenticationException $e) {
  116.             return $this->failureHandler->onAuthenticationFailure($request$e);
  117.         }
  118.         $credentials $this->authenticator->getCredentials($request);
  119.         $refreshToken $this->refreshTokenManager->get($credentials['token']);
  120.         if (null === $refreshToken || !$refreshToken->isValid()) {
  121.             return $this->failureHandler->onAuthenticationFailure($request, new AuthenticationException(
  122.                     sprintf('Refresh token "%s" is invalid.'$refreshToken)
  123.                 )
  124.             );
  125.         }
  126.         if ($this->ttlUpdate) {
  127.             $expirationDate = new \DateTime();
  128.             $expirationDate->modify(sprintf('+%d seconds'$this->ttl));
  129.             $refreshToken->setValid($expirationDate);
  130.             $this->refreshTokenManager->save($refreshToken);
  131.         }
  132.         if ($this->eventDispatcher instanceof ContractsEventDispatcherInterface) {
  133.             $this->eventDispatcher->dispatch(new RefreshEvent($refreshToken$postAuthenticationToken), 'gesdinet.refresh_token');
  134.         } else {
  135.             $this->eventDispatcher->dispatch('gesdinet.refresh_token', new RefreshEvent($refreshToken$postAuthenticationToken));
  136.         }
  137.         return $this->successHandler->onAuthenticationSuccess($request$postAuthenticationToken);
  138.     }
  139. }