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

POST/v1/auth/refresh

Rotate a refresh token

Request body JSON
  • refresh_tokenstringrequired
Response 200 OK
  • access_tokenstringrequired

    A JWT signed with HS256 and JWT_SECRET, carrying exp. Its other claims are yours to choose.

  • refresh_tokenstringrequired

    32 random bytes as 64 lowercase hex characters.

  • token_type"Bearer"required
  • expires_in900required
Response 400 Bad Request

invalid_body

Response 401

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 with invalid_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 with invalid_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 cd repeated 32 times returns HTTP 401 with invalid_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:

bash
cd backend && make up

Run the W3O1 end-to-end tests:

bash
cd backend && make test W=3O1

Read 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.

Finished Exercise 3.22?

Implementation

Run locally

Write the code in your editor inside zomato-prep, then work through this list.

  1. Distinguish consumed refresh tokens from unknown tokens.
  2. Revoke the newest token when an old token is replayed.
  3. Detect replay after three successful rotations.
  4. Keep separate logins for the same user independent.
  5. Confirm unknown tokens do not revoke valid sessions.

Commands

terminal
cd backend && make up
terminal
cd backend && make test W=3O1

Tests

  1. Replaying a refresh token that has already been rotated revokes the replacement token from that login.

    POST/v1/auth/refresh
    Body raw JSON
    {
      "refresh_token": "{first refresh_token, already rotated}"
    }
    Response 401
    {
      "error": "invalid_refresh_token",
      "message": "…"
    }
    POST/v1/auth/refresh
    Body 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 with invalid_refresh_token.

    TestW3O1_ReplayingARotatedTokenRevokesThatLogin in backend/e2e/w3_o1_test.go