Refresh Token Lifecycle: Refresh Token Rotation, Replay Detection & Token Family Revocation
Refresh tokens allow clients to obtain new access tokens without requiring the user to re-authenticate. Refresh Token Rotation (RTR, mandated by RFC 9700 for public clients) issues a brand-new refresh token on every exchange. If an old token is reused, the server detects token theft and revokes the entire token family.
RT_1, the server detects token reuse, sounds an alarm, and immediately revokes the ENTIRE token family (RT_1, RT_2, RT_3)!Step 1: Client sends POST /oauth/token with grant_type=refresh_token and current refresh token (RT_1).
Access tokens are short-lived (15 minutes) to limit exposure if leaked. However, forcing users to type passwords every 15 minutes ruins user experience. Refresh tokens bridge this gap. But if a refresh token is stolen, an attacker could maintain permanent access unless rotation and replay detection are enforced.
The Self-Shredding Relay Baton: When you finish your running shift, you turn in your baton to get a fresh 15-minute runner pass and a new sequentially numbered baton. The old baton is shredded. If someone shows up 10 minutes later holding the old shredded baton, the guards know someone stole it and immediately disqualify the entire relay team.
[ Client ] [ Authorization Server ]
│ │
├── (1) POST /token { grant_type: 'refresh_token', RT_1 } ────────►│
│ ├── Check: Is RT_1 active? Yes.
│ ├── Invalidate RT_1 (mark consumed)
│ ├── Generate RT_2 (Family: Fam_A)
│◄── (2) 200 OK { access_token, refresh_token: RT_2 } ──────────────┤
│ │
[ Attacker (Has Stolen RT_1) ] │
│ │
├── (3) POST /token { grant_type: 'refresh_token', RT_1 } ────────►│
│ ├── ALARM: RT_1 was already consumed!
│ ├── REPLAY ATTACK DETECTED!
│ ├── REVOKE ENTIRE FAMILY (Fam_A)!
│◄── (4) 400 Bad Request { error: 'invalid_grant' } ────────────────┤CosmosApp issues 15-minute access tokens and 30-day rotating refresh tokens. Alice's laptop exchanges RT_1 for RT_2. Later, an attacker who intercepted RT_1 attempts to use it. CosmosApp detects that RT_1 was already consumed, flags a replay attack, and immediately revokes RT_2 and all sessions associated with Alice's token family, forcing re-authentication.
Step 1: Client sends POST /oauth/token with grant_type=refresh_token and current refresh token (RT_1).
Step 2: Server checks if RT_1 exists in database and is marked as 'active'.
Step 3: If RT_1 is found and active, server marks RT_1 as 'consumed', generates new refresh token (RT_2) in the same token family.
Step 4: Server returns new access token and RT_2 to the client.
Step 5: If RT_1 is presented again (reuse attempt), server identifies that a previously consumed token was submitted.
Step 6: Server immediately revokes all refresh tokens and access tokens in that family, logging out both the attacker and the victim.
// CosmosApp Refresh Token Rotation with Replay Detection:
export async function rotateRefreshToken(submittedToken: string) {
const tokenRecord = await db.query('SELECT * FROM refresh_tokens WHERE token = $1', [submittedToken]);
if (!tokenRecord) throw new Error('Invalid token');
// REPLAY ATTACK DETECTION:
if (tokenRecord.is_revoked || tokenRecord.is_consumed) {
// Revoke all tokens in this family immediately!
await db.query('UPDATE refresh_tokens SET is_revoked = true WHERE family_id = $1', [tokenRecord.family_id]);
throw new Error('Security Alert: Refresh token reuse detected! All sessions revoked.');
}
// Mark current token consumed:
await db.query('UPDATE refresh_tokens SET is_consumed = true WHERE id = $1', [tokenRecord.id]);
// Issue new pair:
const newRefreshToken = crypto.randomBytes(32).toString('hex');
await db.query('INSERT INTO refresh_tokens (token, family_id) VALUES ($1, $2)', [newRefreshToken, tokenRecord.family_id]);
return { accessToken: generateJwt(tokenRecord.user_id), refreshToken: newRefreshToken };
}- ✕Refresh tokens never expire (Refresh tokens should have both an idle timeout and an absolute maximum lifetime).
- ✕Rotating refresh tokens without replay detection is sufficient (Without invalidating the active token when a used token is replayed, the attacker retains valid access).
Key Architectural Takeaways
- ✓Refresh Token Rotation (RTR) ensures every refresh token can be used exactly once.
- ✓Replay detection must immediately invalidate the entire token family upon detecting reuse of an old token.
- ✓Combine RTR with short-lived access tokens to limit vulnerability windows.
Issuing non-rotating, perpetual refresh tokens to browser SPAs or mobile clients.
Implement Refresh Token Rotation with token family tracking as mandated by RFC 9700.
What security action must an Authorization Server take when a previously consumed refresh token is presented for exchange?