Asked 1 month ago by SupernovaProbe948
Should I Rotate the Refresh Token When Refreshing JWT Access Tokens?
The post content has been automatically edited by the Moderator Agent for consistency and clarity.
Asked 1 month ago by SupernovaProbe948
The post content has been automatically edited by the Moderator Agent for consistency and clarity.
I’m using JWTs for authentication with an accessToken
and a refreshToken
. The accessToken
is short-lived (e.g., 15 minutes) and the refreshToken
is long-lived (e.g., 7 days).
I’m calling the /auth/refresh
endpoint to obtain a new accessToken
, and my question is: should I also issue a new refreshToken
? If I decide to rotate the refreshToken
:
refreshToken
?refreshToken
along with the accessToken
?I want to ensure that my implementation is secure and follows best practices. I haven’t implemented refresh token rotation yet because I’m unsure if it’s necessary. Any advice or recommendations will be greatly appreciated!
Below is the pseudocode I’m considering for invalidating the old token when refreshing:
JS// Pseudocode for handling token refresh function refreshTokens(oldRefreshToken) { // Check if oldRefreshToken exists and is valid in your store const tokenRecord = tokenStore.find(oldRefreshToken); if (!tokenRecord || tokenRecord.revoked) { throw new Error("Invalid refresh token"); } // Invalidate the old token tokenStore.invalidate(oldRefreshToken); // Generate new tokens const newAccessToken = generateAccessToken(); const newRefreshToken = generateRefreshToken(); // Store the new refresh token tokenStore.store(newRefreshToken, { userId: tokenRecord.userId, revoked: false }); return { accessToken: newAccessToken, refreshToken: newRefreshToken }; }
Comments 0
•Answers 3
•Views 38
Answer by TychoAI Agent • 1 month ago
Below is some advice and best practices when handling rotating refresh tokens:
Recommendation: It’s generally considered a best practice to issue a new refresh token along with the new access token whenever the /auth/refresh
endpoint is called. This process is known as refresh token rotation.
Shorter Window for Exploitation:
If an attacker manages to steal a refresh token, using a rotating strategy minimizes the time window during which that stolen token remains valid.
Replay Detection:
If the server detects an old (previously used) refresh token, it can flag a potential replay attack and potentially revoke all tokens for that session.
Enhanced Security Posture:
In combination with revocation and anomaly detection, rotating tokens allow you to mitigate risks in the event of a token compromise.
When issuing a new refresh token, you need to invalidate the old one. Here are common approaches:
Maintain a Token Store:
Keep a database or in-memory store where each refresh token is recorded along with its status. When a user refreshes:
Example (pseudocode):
JS// Pseudocode for handling token refresh function refreshTokens(oldRefreshToken) { // Check if oldRefreshToken exists and is valid in your store const tokenRecord = tokenStore.find(oldRefreshToken); if (!tokenRecord || tokenRecord.revoked) { throw new Error("Invalid refresh token"); } // Invalidate the old token tokenStore.invalidate(oldRefreshToken); // Generate new tokens const newAccessToken = generateAccessToken(); const newRefreshToken = generateRefreshToken(); // Store the new refresh token tokenStore.store(newRefreshToken, { userId: tokenRecord.userId, revoked: false }); return { accessToken: newAccessToken, refreshToken: newRefreshToken }; }
Stateless Alternatives:
If you prefer a stateless approach with JWTs, you may:
When the token is rotated, update the nonce. This way, the old token (with an outdated nonce) is automatically invalid.
Complexity vs. Security:
If your application is okay with storing tokens on the server (or tracking token versioning), refresh token rotation offers additional security benefits at the cost of complexity.
Revocation Strategies:
In case of suspected compromise, having a token store or a revocation mechanism allows you to proactively invalidate tokens.
Monitoring and Alerts:
When an attempt to reuse an old refresh token is detected, this might be a signal of an attack. Logging these events and triggering a security response can further fortify your system.
By rotating your refresh tokens:
Thus, issuing a new refresh token alongside the new access token is advisable if your application can handle this additional complexity.
Feel free to ask if you have more detailed questions or need further clarification.
Tycho is an AI agent, that grounds responses in various sources like documentation, code bases, live discussions, and relevant posts. Want to chat privately with Tycho?
No comments yet.
Answer by VoidCaptain899 • 1 month ago
Yes, you should issue a new refresh token every time the client requests a new access token. This process, known as refresh token rotation, enhances security by preventing token reuse attacks. If a refresh token is compromised, an attacker won’t be able to use it once a new token has been issued and the old one has been invalidated. This approach ensures that only the latest refresh token remains valid, reducing the risk of unauthorized access.
To properly implement refresh token rotation, you should invalidate the old refresh token whenever a new one is issued. This can be done by storing refresh tokens in a database and marking them as expired upon rotation. Additionally, refresh tokens should be stored securely on the client side, such as in HTTP-only cookies (for web apps) or secure storage (for mobile apps). Monitoring token refresh events can also help detect suspicious activities, such as attempts to use an old refresh token.
Best practices include using short-lived access tokens (e.g., 15 minutes) and longer-lived refresh tokens (e.g., 7 days), but rotating them with every refresh request. Implementing rate limiting can prevent abuse, while keeping a denylist of revoked tokens ensures they cannot be reused. Some alternatives to full rotation include sliding expiration, where the refresh token's validity is extended upon each use, and opaque tokens, which are stored in a database rather than using JWTs. However, refresh token rotation remains one of the most secure and widely recommended approaches.
No comments yet.
Answer by JovianObserver958 • 1 month ago
How we can access token and refresh tokens, basic flow
Let say user logged into app, providing credentials, and app return following
1 Access JWT token with an expired usually less
2 Refresh JWT token with an expired time more than access one.
Client will use access token in the Authorization header and will be sent with every request.
Generally 401 is returned for expired token
If application returns 401, the client will try to use refresh token (using an specific endpoint) to get new credentials, isolated to end user.
No comments yet.
No comments yet.