POST /v1/auth/refresh: detect token reuse
You add refresh-token family tracking to POST /v1/auth/refresh so replaying a consumed token revokes the newest token from that login. The same user's other login sessions must remain valid.
Ticket
Add refresh-token reuse detection to POST /v1/auth/refresh, extending the rotation you built in W3B1. Each login must have its own refresh-token family. When a client presents a previously consumed refresh token, reject the request and revoke the newest refresh token belonging to that family.
A stolen refresh token can be replayed after its legitimate owner has rotated it. Without reuse detection, the attacker and the legitimate client can continue using different tokens from the same session. Your implementation must detect this replay without invalidating the user's other login sessions. Week 4's Expo app depends on refresh tokens continuing to work correctly across login and refresh requests.
Endpoint
Rotate a refresh token
refresh_tokenstringrequired
access_tokenstringrequiredA JWT signed with HS256 and JWT_SECRET, carrying exp. Its other claims are yours to choose.
refresh_tokenstringrequired32 random bytes as 64 lowercase hex characters.
token_type"Bearer"requiredexpires_in900required
invalid_body
invalid_refresh_token
Acceptance criteria
- After signup, refreshing the issued refresh token succeeds with HTTP 200 and returns a new token pair. Replaying the original refresh token returns HTTP 401 with
invalid_refresh_token. Refreshing the replacement token also returns HTTP 401 withinvalid_refresh_token. - After three successful refresh-token rotations, replaying the original signup refresh token returns HTTP 401 with
invalid_refresh_token. Refreshing the newest token from the third rotation also returns HTTP 401 withinvalid_refresh_token. - Two logins belonging to the same user remain independent. After refreshing the signup token, replaying that consumed token returns HTTP 401. Refreshing the token issued by the second login still succeeds with HTTP 200 and returns a token pair.
- Presenting an unknown refresh token consisting of
cdrepeated 32 times returns HTTP 401 withinvalid_refresh_token. The valid refresh token issued by signup must still succeed with HTTP 200 and return a token pair.
Constraints
- A consumed refresh token must remain identifiable throughout its original refresh-token lifetime so that replay detection still works after several rotations.
- Revocation must affect only the family associated with the replayed token.
- An unknown refresh token must not revoke any existing login session.
Design direction
Keep refresh-token storage and reuse detection together. Let the authentication handlers create a new family during signup or login, then preserve that family during every rotation.
Associate each family with its currently active refresh token. Retain a record of each consumed token so you can distinguish a replay from an unknown token. Redis TTLs can bound how long these records remain stored. See Redis TTL.
Consider using a Redis Lua script for the consume operation. A single atomic operation can check the token, consume it, record its previous use and revoke the family's current token when necessary.
Think about these trade-offs:
- Atomicity: Separate Redis reads and writes can race. Two concurrent refresh requests must not both consume the same token successfully.
- Retention: Deleting consumed-token records immediately makes replay indistinguishable from an unknown token. Keep the records for the original token's remaining lifetime.
- Session isolation: A family represents one login, not one user. Revoking a family must leave other login sessions usable.
Any package layout that meets the contract passes.
How to check your work
Start the local services:
cd backend && make upRun the W3O1 end-to-end tests:
cd backend && make test W=3O1Read the first failing assertion to identify whether the problem is token consumption, replay detection, family revocation or session isolation. Fix that behaviour before investigating later failures.
The reference solution passes every check for this exercise. Try the hints first; reading the answer before you have struggled with the problem teaches much less.
Implementation
Run locally
Write the code in your editor inside zomato-prep, then work through this list.
- Distinguish consumed refresh tokens from unknown tokens.
- Revoke the newest token when an old token is replayed.
- Detect replay after three successful rotations.
- Keep separate logins for the same user independent.
- Confirm unknown tokens do not revoke valid sessions.
Commands
cd backend && make upcd backend && make test W=3O1Tests
Replaying a refresh token that has already been rotated revokes the replacement token from that login.
POST/v1/auth/refreshBody raw JSON{ "refresh_token": "{first refresh_token, already rotated}" }Response 401{ "error": "invalid_refresh_token", "message": "…" }POST/v1/auth/refreshBody raw JSON{ "refresh_token": "{newest refresh_token}" }Response 401{ "error": "invalid_refresh_token", "message": "…" }Input Sign up, refresh the signup token once, then present the original token again.
Expected The replay returns HTTP 401 with
invalid_refresh_token; refreshing the replacement token also returns HTTP 401 withinvalid_refresh_token.TestW3O1_ReplayingARotatedTokenRevokesThatLogininbackend/e2e/w3_o1_test.go