JWTRefreshTokenBundle
repository·master·Indexed 20 days ago
https://github.com/markitosgv/jwtrefreshtokenbundleA Symfony bundle for managing refresh tokens for JWT-based authentication. It integrates with LexikJWTAuthenticationBundle and supports persistence via Doctrine ORM, Doctrine MongoDB ODM, or Doctrine DBAL. Features include single-use tokens, HttpOnly cookie support, token revocation, and customizable TTL settings. Compatible with PHP 8.2+ and Symfony 6.4, 7.2+, or 8.0+.
What's inside JWTRefreshTokenBundle
- JWTRefreshTokenBundle is designed to manage refresh tokens with JSON Web Tokens (JWT) in an easy way. It is built on top of LexikJWTAuthenticationBundle and supports both Doctrine ORM and MongoDB ODM for persistence. It allows developers to implement a secure token refresh mechanism in Symfony applications.
Listen to JWTRefreshTokenBundle events
masterThe bundle dispatches several events that you can listen to for custom logic during the token lifecycle:
gesdinet.refresh_token: Dispatched when a token is successfully refreshed. Carries aGesdinet\JWTRefreshTokenBundle\Event\RefreshEventobject.gesdinet.refresh_token_failure: Dispatched when refresh token authentication fails. Carries aGesdinet\JWTRefreshTokenBundle\Event\RefreshAuthenticationFailureEventobject.gesdinet.refresh_token_not_found: Dispatched when a refresh token cannot be found. Carries aGesdinet\JWTRefreshTokenBundle\Event\RefreshTokenNotFoundEventobject.
Enable the Bundle in Symfony
masterIf you are using Symfony Flex, the bundle should be registered automatically. If not, manually add the bundle to your
config/bundles.phpfile.<?php return [ //... Gesdinet\JWTRefreshTokenBundle\GesdinetJWTRefreshTokenBundle::class => ['all' => true], ];Install JWTRefreshTokenBundle with Doctrine ORM
masterTo install the bundle using Doctrine ORM, you must also install the Doctrine ORM and Doctrine Bundle packages, as they are not automatically included.
composer require doctrine/orm doctrine/doctrine-bundle gesdinet/jwt-refresh-token-bundleDefine the Refresh Token Route
masterAdd a route for the refresh token endpoint in your routing configuration (e.g.,
config/routes.yaml).# config/routes.yaml api_refresh_token: path: /api/token/refreshInstall JWTRefreshTokenBundle with Doctrine MongoDB ODM
masterTo install the bundle using Doctrine MongoDB ODM, you must also install the MongoDB ODM and its bundle packages.
composer require doctrine/mongodb-odm doctrine/mongodb-odm-bundle gesdinet/jwt-refresh-token-bundleUpgrade from 1.x to 2.0
masterTo upgrade
JWTRefreshTokenBundlefrom version 1.x to 2.0, ensure your environment meets the new requirements and update your configuration to replace deprecated nodes.Bundle Requirements
- Symfony: 6.4 or 7.2+
- PHP: 8.2 or later
Update Database Schema for Refresh Tokens
masterYou must add the refresh token table/collection to your database. It is highly recommended to use migrations.
Using MakerBundle:
php bin/console make:migrationWithout MakerBundle:
php bin/console doctrine:migrations:diff php bin/console doctrine:migrations:migrateNot Recommended (Direct Schema Update):
php bin/console doctrine:schema:update --forcePrerequisites for JWTRefreshTokenBundle
masterBefore installing, ensure your environment meets the following requirements:
- PHP: 8.2 or later
- Symfony: 6.4, 7.2+, or 8.0+
Note: For older Symfony versions, use the 1.x release of the bundle. It is highly recommended to use HTTPS in production environments.
Invalidate refresh token on logout
masterThe bundle listens for
LogoutEventto automatically invalidate the current refresh token and unset its cookie.To implement this:
- Define a logout path in your firewall in
security.yaml. - Define a corresponding route in
routes.yaml. - (Optional) If using a non-default firewall, specify
logout_firewallin the bundle configuration.
Example Setup:
# config/packages/security.yaml security: firewalls: api: logout: path: api_token_invalidate # config/routes.yaml api_token_invalidate: path: /api/token/invalidate- Define a logout path in your firewall in
Prioritize custom Token Extractors
masterWhen using multiple extractors, you can control the order in which they are executed by assigning a
priorityto thegesdinet_jwt_refresh_token.request_extractortag. A higher number means the extractor will be run sooner.services: App\Request\Extractor\HeaderExtractor: tags: - { name: gesdinet_jwt_refresh_token.request_extractor, priority: 25 }Configure the Refresh Token Class
masterCreate a configuration file at
config/packages/gesdinet_jwt_refresh_token.yamlto specify the class name of your refresh token entity/document. You must adjust therefresh_token_classvalue to match the actual class used in your application.gesdinet_jwt_refresh_token: refresh_token_class: App\Entity\RefreshToken