If the refresh token is leaked or compromised, how would you handle it? Considering that JWT is a self-sufficient token.
sobes.tech AI
Answer from AI
If the refresh token is compromised, despite JWT being a self-contained token, measures should be taken to minimize damage:
-
Store refresh tokens in a database with the ability to revoke. Unlike access tokens (JWT), the refresh token should be linked to a database record where it can be marked as revoked.
-
Upon detection of compromise — revoke the compromised refresh tokens. This is done by deleting or marking them in the database.
-
Use a short lifespan for access tokens and a longer one for refresh tokens. This limits the validity period of a compromised access token.
-
Implement mechanisms to detect suspicious activity. For example, monitoring IPs, devices, request frequency.
-
Ensure the ability to force user logout from all sessions. This allows quick revocation of all tokens.
-
Implement refresh token rotation. Issue a new refresh token on each use and revoke the old one.
Example processing scheme:
- The client sends a refresh token to obtain a new access token.
- The server checks the refresh token in the database: whether it is valid and not revoked.
- If valid — issues a new access token and a new refresh token, and revokes the old refresh token.
- If the refresh token is compromised — the server rejects the request and requires re-authentication.
Thus, despite JWT being self-contained, server-side verification and state management are necessary for refresh tokens to ensure security and the ability to revoke.