Invalidating client side JWT session
Solution 1:
There are several reason to invalidate a JWT token before its expiration time: account deleted/blocked/suspended, password changed, permissions changed, user logged out by admin. So your question is on topic
There are several techniques to apply or combine depending on your use case
1) Remove the client token from local storage
2) Token blacklist: Store tokens that were between logout & expiry time, mark expired and check it in every request. Use a unique identifier jti
or include last login date and issued at iat
to remove old tokens
It is needed server storage. If you do not expect too many tokens to revoke, you also could use an in-memory blacklist. You only need to set an entry after updating critical data on user and currentTime - maxExpiryTime < lastLoginDate (iat)
. The entry can be discarded when currentTime - maxExpiryTime > lastModified
(no more non-expired tokens sent). In this case is not needed to store the entire token. Just sub
, iat
and maybe jti
3) Expiry times short and rotate them. Issue a new access token every few request. Use refresh tokens to allow your application to obtain new access tokens without needing to re-authenticate and combine with sliding-sessions
Sliding-sessions are sessions that expire after a period of inactivity. When a user performs an action, a new access token is issued. If the user uses an expired access token, the session is considered inactive and a new access token is required. This new token can be obtained with a refresh token or requiring credentials
Other common techniques
Allow change user unique ID if account is compromised with a new user&password login
To invalidate tokens when user changes their password, sign the token with a hash of their password. If the password changes, any previous tokens automatically fail to verify. Extend this mechanism with other field of interest to sign. The downside is that it requires access to the database
Change signature algorithm to revoke all current tokens in major security issues
Take a look at Invalidating JSON Web Tokens
Solution 2:
I've done some homework and seems that the better approach to implement the revocation is to use jti (id on Jtw) and a blacklist of revoked id (wich will be cleared when the token is expired). This make the JTW stateful for the only the blacklist part.