vendor/gesdinet/jwt-refresh-token-bundle/EventListener/AttachRefreshTokenOnSuccessListener.php line 103

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\EventListener;
  11. use Gesdinet\JWTRefreshTokenBundle\Generator\RefreshTokenGeneratorInterface;
  12. use Gesdinet\JWTRefreshTokenBundle\Model\RefreshTokenInterface;
  13. use Gesdinet\JWTRefreshTokenBundle\Model\RefreshTokenManagerInterface;
  14. use Gesdinet\JWTRefreshTokenBundle\Request\Extractor\ExtractorInterface;
  15. use Lexik\Bundle\JWTAuthenticationBundle\Event\AuthenticationSuccessEvent;
  16. use Symfony\Component\HttpFoundation\Cookie;
  17. use Symfony\Component\Security\Core\User\UserInterface;
  18. use Symfony\Component\HttpFoundation\RequestStack;
  19. class AttachRefreshTokenOnSuccessListener
  20. {
  21.     /**
  22.      * @var RefreshTokenManagerInterface
  23.      */
  24.     protected $refreshTokenManager;
  25.     /**
  26.      * @var int
  27.      */
  28.     protected $ttl;
  29.     /**
  30.      * @var RequestStack
  31.      */
  32.     protected $requestStack;
  33.     /**
  34.      * @var string
  35.      */
  36.     protected $tokenParameterName;
  37.     /**
  38.      * @var bool
  39.      */
  40.     protected $singleUse;
  41.     /**
  42.      * @var RefreshTokenGeneratorInterface
  43.      */
  44.     protected $refreshTokenGenerator;
  45.     /**
  46.      * @var ExtractorInterface
  47.      */
  48.     protected $extractor;
  49.     protected array $cookieSettings;
  50.     protected bool $returnExpiration;
  51.     protected string $returnExpirationParameterName;
  52.     /**
  53.      * @param int    $ttl
  54.      * @param string $tokenParameterName
  55.      * @param bool   $singleUse
  56.      */
  57.     public function __construct(
  58.         RefreshTokenManagerInterface $refreshTokenManager,
  59.         $ttl,
  60.         RequestStack $requestStack,
  61.         $tokenParameterName,
  62.         $singleUse,
  63.         RefreshTokenGeneratorInterface $refreshTokenGenerator,
  64.         ExtractorInterface $extractor,
  65.         array $cookieSettings,
  66.         bool $returnExpiration false,
  67.         string $returnExpirationParameterName 'refresh_token_expiration'
  68.     ) {
  69.         $this->refreshTokenManager $refreshTokenManager;
  70.         $this->ttl $ttl;
  71.         $this->requestStack $requestStack;
  72.         $this->tokenParameterName $tokenParameterName;
  73.         $this->singleUse $singleUse;
  74.         $this->refreshTokenGenerator $refreshTokenGenerator;
  75.         $this->extractor $extractor;
  76.         $this->cookieSettings array_merge([
  77.             'enabled' => false,
  78.             'same_site' => 'lax',
  79.             'path' => '/',
  80.             'domain' => null,
  81.             'http_only' => true,
  82.             'secure' => true,
  83.             'remove_token_from_body' => true,
  84.         ], $cookieSettings);
  85.         $this->returnExpiration $returnExpiration;
  86.         $this->returnExpirationParameterName $returnExpirationParameterName;
  87.     }
  88.     public function attachRefreshToken(AuthenticationSuccessEvent $event): void
  89.     {
  90.         $user $event->getUser();
  91.         if (!$user instanceof UserInterface) {
  92.             return;
  93.         }
  94.         $data $event->getData();
  95.         $request $this->requestStack->getCurrentRequest();
  96.         if (null === $request) {
  97.             return;
  98.         }
  99.         // Extract refreshToken from the request
  100.         $refreshTokenString $this->extractor->getRefreshToken($request$this->tokenParameterName);
  101.         // Remove the current refreshToken if it is single-use
  102.         if ($refreshTokenString && true === $this->singleUse) {
  103.             $refreshToken $this->refreshTokenManager->get($refreshTokenString);
  104.             $refreshTokenString null;
  105.             if ($refreshToken instanceof RefreshTokenInterface) {
  106.                 $this->refreshTokenManager->delete($refreshToken);
  107.             }
  108.         }
  109.         // Set or create the refreshTokenString
  110.         if ($refreshTokenString) {
  111.             $data[$this->tokenParameterName] = $refreshTokenString;
  112.         } else {
  113.             $refreshToken $this->refreshTokenGenerator->createForUserWithTtl($user$this->ttl);
  114.             $this->refreshTokenManager->save($refreshToken);
  115.             $refreshTokenString $refreshToken->getRefreshToken();
  116.             $data[$this->tokenParameterName] = $refreshTokenString;
  117.         }
  118.         if ($this->returnExpiration) {
  119.             $data[$this->returnExpirationParameterName] = time() + $this->ttl;
  120.         }
  121.         // Add a response cookie if enabled
  122.         if ($this->cookieSettings['enabled']) {
  123.             $event->getResponse()->headers->setCookie(
  124.                 new Cookie(
  125.                     $this->tokenParameterName,
  126.                     $refreshTokenString,
  127.                     time() + $this->ttl,
  128.                     $this->cookieSettings['path'],
  129.                     $this->cookieSettings['domain'],
  130.                     $this->cookieSettings['secure'],
  131.                     $this->cookieSettings['http_only'],
  132.                     false,
  133.                     $this->cookieSettings['same_site']
  134.                 )
  135.             );
  136.             // Remove the refreshTokenString from the response body
  137.             if (isset($this->cookieSettings['remove_token_from_body']) && $this->cookieSettings['remove_token_from_body']) {
  138.                 unset($data[$this->tokenParameterName]);
  139.             }
  140.         }
  141.         // Set response data
  142.         $event->setData($data);
  143.     }
  144. }