|
| 1 | +Invalidate token |
| 2 | +================ |
| 3 | + |
| 4 | +The blocklist token relies on the ``jti`` claim, a standard claim designed for tracking and revoking JWTs. `"jti" (JWT ID) Claim <https://datatracker.ietf.org/doc/html/rfc7519#section-4.1.7>`_ |
| 5 | + |
| 6 | +The blocklist storage utilizes a cache implementing ``Psr\Cache\CacheItemPoolInterface``. The cache stores the ``jti`` of the blocked token to the cache, and the cache item expires after the "exp" (expiration time) claim of the token |
| 7 | + |
| 8 | +Configuration |
| 9 | +~~~~~~~~~~~~~ |
| 10 | + |
| 11 | +To configure token blocklist, update your `lexik_jwt_authentication.yaml` file: |
| 12 | + |
| 13 | +.. code-block:: yaml |
| 14 | +
|
| 15 | + # config/packages/lexik_jwt_authentication.yaml |
| 16 | + # ... |
| 17 | + lexik_jwt_authentication: |
| 18 | + # ... |
| 19 | + # invalidate the token on logout by storing it in the cache |
| 20 | + blocklist_token: |
| 21 | + enabled: true |
| 22 | + cache: cache.app |
| 23 | +
|
| 24 | +
|
| 25 | +Enabling ``blocklist_token`` causes the activation of listeners: |
| 26 | + |
| 27 | +* an event listener ``Lexik\Bundle\JWTAuthenticationBundle\EventListenerAddClaimsToJWTListener`` which adds a ``jti`` claim if not present when the token is created |
| 28 | + |
| 29 | +* an event listener ``Lexik\Bundle\JWTAuthenticationBundle\BlockJWTListener`` which blocks JWTs on logout (``Symfony\Component\Security\Http\Event\LogoutEvent``) |
| 30 | +or on login failure due to the user not being enabled (``Symfony\Component\Security\Core\Exception\DisabledException``) |
| 31 | + |
| 32 | +* an event listener ``Lexik\Bundle\JWTAuthenticationBundle\RejectBlockedTokenListener`` which rejects blocked tokens during authentication |
| 33 | + |
| 34 | +To block JWTs on logout, you must either activate logout in the firewall configuration or do it programmatically |
| 35 | + |
| 36 | +* by firewall configuration |
| 37 | + |
| 38 | + .. code-block:: yaml |
| 39 | + # config/packages/security.yaml |
| 40 | + security: |
| 41 | + enable_authenticator_manager: true |
| 42 | + firewalls: |
| 43 | + api: |
| 44 | + ... |
| 45 | + jwt: ~ |
| 46 | + logout: |
| 47 | + path: app_logout |
| 48 | +
|
| 49 | +- programmatically in an controller action |
| 50 | + |
| 51 | + .. code-block:: php |
| 52 | + use Symfony\Component\EventDispatcher\EventDispatcherInterface; |
| 53 | + use Symfony\Component\HttpFoundation\JsonResponse; |
| 54 | + use Symfony\Component\HttpFoundation\Request; |
| 55 | + use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface; |
| 56 | + use Symfony\Component\Security\Http\Event\LogoutEvent; |
| 57 | + //... |
| 58 | + class SecurityController |
| 59 | + { |
| 60 | + //... |
| 61 | + public function logoutAction(Request $request, EventDispatcherInterface $eventDispatcher, TokenStorageInterface $tokenStorage) |
| 62 | + { |
| 63 | + $eventDispatcher->dispatch(new LogoutEvent($request, $tokenStorage->getToken())); |
| 64 | +
|
| 65 | + return new JsonResponse(); |
| 66 | + } |
| 67 | + ] |
| 68 | +
|
| 69 | +Refer to `Symfony logging out <https://symfony.com/doc/current/security.html#logging-out>`_ for more details. |
| 70 | + |
| 71 | +Changing blocklist storage |
| 72 | +~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 73 | + |
| 74 | +To change the blocklist storage, refer to `Configuring Cache with FrameworkBundle <https://symfony.com/doc/current/cache.html#configuring-cache-with-frameworkbundle>`_ |
| 75 | + |
| 76 | +.. code-block:: yaml |
| 77 | +
|
| 78 | + # config/packages/framework.yaml |
| 79 | + framework: |
| 80 | + # ... |
| 81 | + cache: |
| 82 | + default_redis_provider: 'redis://localhost' |
| 83 | + pools: |
| 84 | + block_list_token_cache_pool: |
| 85 | + adapter: cache.adapter.redis |
| 86 | + # ... |
| 87 | + blocklist_token: |
| 88 | + enabled: true |
| 89 | + cache: block_list_token_cache_pool |
0 commit comments