Download the PHP package janakdom/jwt-refresh-token-bundle without Composer
On this page you can find all versions of the php package janakdom/jwt-refresh-token-bundle. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download janakdom/jwt-refresh-token-bundle
More information about janakdom/jwt-refresh-token-bundle
Files in janakdom/jwt-refresh-token-bundle
Package jwt-refresh-token-bundle
Short Description Implements a refresh token system over Json Web Tokens in Symfony
License MIT
Informations about the package jwt-refresh-token-bundle
JWTRefreshTokenBundle
The purpose of this bundle is manage refresh tokens with JWT (Json Web Tokens) in an easy way. This bundles uses LexikJWTAuthenticationBundle. Supports Doctrine ORM/ODM.
Prerequisites
This bundle requires Symfony 3.4+, 4.0+ or 5.0+.
If you want to use this bundle with previous Symfony versions, please use 0.2.x releases.
Protip: Though the bundle doesn't enforce you to do so, it is highly recommended to use HTTPS.
Installation
Step 1: Download the Bundle
It's important you manually require either Doctrine's ORM or MongoDB ODM as well, these packages are not required automatically now as you can choose between them. Failing to do so may trigger errors on installation
With Doctrine's ORM
With Doctrine's MongoDB ODM
or edit composer.json:
// ...
"gesdinet/jwt-refresh-token-bundle": "~0.1",
"doctrine/orm": "^2.4.8",
"doctrine/doctrine-bundle": "~1.4",
"doctrine/mongodb-odm-bundle": "^3.4"
// ...
Step 2: Enable the Bundle
Symfony 3 Version:
Register bundle into app/AppKernel.php
:
Symfony 4 Version:
Register bundle into config/bundles.php
(Flex did it automatically):
Step 3: Configure your own routing to refresh token
Open your main routing configuration file and copy the following four lines at the very beginning of it.
Symfony 3 Version:
Symfony 4 Version:
Step 4: Allow anonymous access to refresh token
Add next lines on security.yml file:
Step 5: Update your schema
With the next command you will create a new table to handle your refresh tokens
Symfony 3 Version:
Symfony 4 Version:
USAGE
The configurations can be put in:
Symfony 3 Version: app/config
Symfony 4 Version: config/packages/gesdinet_jwt_refresh_token.yaml
Config TTL
You can define Refresh Token TTL. Default value is 1 month. You can change this value adding this line to your config:
Config User identity field
You can change user identity field. Make sure that your model user has getter
for this field. Default value is username
. You can change this value by adding this line to your config:
Config TTL update
You can expand Refresh Token TTL on refresh. Default value is false. You can change this value adding this line to your config:
This will reset the token TTL each time you ask a refresh.
Config Firewall Name
You can define Firewall name. Default value is api. You can change this value adding this line to your config:
Config Refresh token parameter Name
You can define refresh token parameter name. Default value is refresh_token. You can change this value adding this line to your config file:
Config Refresh token expiration
You can configure the expiration parameter in Response. If set to true
, token expiration timestamp will be provided.
To enable this behavior add this line to your config:
Config Refresh token expiration parameter Name
You can define refresh token expiration parameter name. Default value is token_expiration. You can change this value adding this line to your config file:
Config UserProvider
You can define your own UserProvider. By default we use our custom UserProvider. You can change this value by adding this line to your config:
For example, if you are using FOSUserBundle, user_provider_service_id must be set to fos_user.user_provider.username_email
.
For Doctrine ORM UserProvider, user_provider_service_id must be set to security.user.provider.concrete.<your_user_provider_name_in_security_yaml>
.
For example, in your app/config/security.yml
or config/packages/security.yaml
:
then your user_provider_service_id is security.user.provider.concrete.app_user_provider
.
Select Manager Type
By default manager type is set to use Doctrine's ORM, if you want to use Doctrine's MongoDB ODM you have to change this value:
Config UserChecker
You can define your own UserChecker. By default the Symfony UserChecker will be used. You can change this value by adding this line to your config:
You will probably want to use a custom UserProvider along with your UserChecker to ensure that the checker recieves the right type of user.
Config Single Use
You can configure the refresh token so it can only be consumed once. If set to true
and the refresh token is consumed, a new refresh token will be provided.
To enable this behavior add this line to your config:
Use another entity for refresh tokens
You can define your own refresh token class on your project.
When using default ORM create the entity class extending Gesdinet\JWTRefreshTokenBundle\Entity\AbstractRefreshToken
in your own bundle:
When using MongoBD ODM create the document class extending Gesdinet\JWTRefreshTokenBundle\Document\AbstractRefreshToken
in you own bundle:
Then declare this class adding this line to your config.yml file:
Use another object manager
You can tell JWTRefreshTokenBundle to use another object manager than default one (if using ORM it is doctrine.orm.entity_manager, when using MongoDB ODM it is doctrine_mongodb.odm.document_manager).
Just add this line to your config.yml file:
Generating Tokens
When you authenticate through /api/login_check with user/password credentials, LexikJWTAuthenticationBundle now returns a JWT Token and a Refresh Token data.
This refresh token is persisted in RefreshToken entity. After that, when your JWT valid token expires, if you want to get a new one you can proceed in two ways:
-
Send you user credentials again to /api/login_check. This generates another JWT with another Refresh Token.
- Ask to renew valid JWT with our refresh token. Make a POST call to /api/token/refresh url with refresh token as payload. In this way, you can always get a valid JWT without asking for user credentials. But you must notice if refresh token is still valid. Your refresh token do not change but valid datetime will increase.
Note that when a refresh token is consumed and the config option single_use
is set to true
the token will no longer be valid.
This call returns a new valid JWT token renewing valid datetime of your refresh token.
Useful Commands
We give you two commands to manage tokens.
Revoke all invalid tokens
If you want to revoke all invalid (datetime expired) refresh tokens you can execute:
Symfony 3 Version:
Symfony 4 Version:
Optional argument is datetime, it deletes all tokens smaller than this datetime:
Symfony 3 Version:
Symfony 4 Version:
We recommend to execute this command with a cronjob to remove invalid refresh tokens every certain time.
Revoke a token
If you want to revoke a single token you can use this:
Symfony 3 Version:
Symfony 4 Version:
Events
If you want to do something when token is refreshed you can listen for gesdinet.refresh_token
event.
For example:
All versions of jwt-refresh-token-bundle with dependencies
symfony/framework-bundle Version ~3.4|~4.0|~5.0
symfony/validator Version ~3.4|~4.0|~5.0
lexik/jwt-authentication-bundle Version ^1.1|^2.0@dev