POST /v1/auth/login: rate limit attempts

You add independent per-IP and per-email rate limits to POST /v1/auth/login. The middleware preserves the request body and returns HTTP 429 with rate_limited and Retry-After when either limit is exceeded.

Ticket

Add login-specific rate limiting to POST /v1/auth/login to protect accounts against credential stuffing. Apply one limit to the client IP and another to the normalised email address. For example, with LOGIN_LIMIT_PER_IP=5, the sixth attempt from the same IP must return HTTP 429 with {"error":"rate_limited"} and a non-empty Retry-After header.

A limit based only on IP can miss attacks spread across many addresses. A limit based only on email can let an attacker exhaust one account's allowance. Build on the existing rate limiter and keep the two checks independent.

Endpoint

POST/v1/auth/login

Exchange email and password for a token pair

Request body JSON
  • emailstringrequired
  • passwordstringrequired
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_credentials

Response 429 Too Many Requests

rate_limited

Acceptance criteria

  • With LOGIN_LIMIT_PER_IP=5 and LOGIN_LIMIT_PER_EMAIL=1000, the first five login attempts from one IP return HTTP 401. Each attempt uses a different forged X-User-ID and a different unique email. The sixth attempt returns HTTP 429 with {"error":"rate_limited"} and a non-empty Retry-After header.
  • With LOGIN_LIMIT_PER_IP=100000 and LOGIN_LIMIT_PER_EMAIL=3, attempts using the original email, its uppercase form, and the original email surrounded by two spaces each return HTTP 401. A fourth attempt using the uppercase email returns HTTP 429 with {"error":"rate_limited"}. A different unique email from the same IP returns HTTP 401 with {"error":"invalid_credentials"}.
  • With LOGIN_LIMIT_PER_IP=100000 and LOGIN_LIMIT_PER_EMAIL=3, a valid login using a signed-up account's email and password returns HTTP 200. A login request with the body not json returns HTTP 400 with {"error":"invalid_body"}.

Constraints

  • Configure the independent limits through LOGIN_LIMIT_PER_IP and LOGIN_LIMIT_PER_EMAIL.
  • Apply the per-IP limit regardless of the client's X-User-ID value.
  • Normalise email identity by trimming surrounding whitespace and ignoring letter case.
  • A rejected request returns HTTP 429 with error code rate_limited and a non-empty Retry-After header.
  • Preserve the request body for the login handler, including when the body is malformed or the middleware cannot extract a usable email.

Design direction

Add login-only HTTP middleware in front of the existing login handler. Derive the IP identity from the client connection, not from a client-supplied header. Use a separate HTTP identity key for each IP and normalised email bucket.

Apply the IP check before reading the request body. Then read the body once, restore it for the handler, and decode the email when possible. Use the existing HTTP 429 response and Retry-After header behaviour so rejected requests follow the API's established error format. Keep both login limits separate from the global limiter. Any layout that meets the contract passes.

Consider these trade-offs:

  • Account protection: An email limit protects one account against distributed attempts, but an attacker can exhaust that account's allowance. An IP limit controls concentrated attempts, but shared networks can contain multiple legitimate users.
  • Identity trust: A client can change X-User-ID. Use the connection address for the IP bucket and the normalised email from the request body for the email bucket.
  • Body handling: Reading a request body consumes it. Restore the original bytes so the login handler can authenticate valid credentials or return its own invalid_body response.

How to check your work

From the project root, run:

bash
cd backend && make up
cd backend && make test W=3O3

Read the first failing assertion to identify whether the IP bucket, email normalisation, rate-limit response, or request-body preservation is incorrect.

Finished Exercise 3.24?

Account and outbox extensions

4 items

  • 3.21Refresh families, relay locking, login throttlingnot completed
  • 3.22POST /v1/auth/refresh: detect token reusenot completed
  • 3.23Concurrent outbox relaynot completed
  • 3.24POST /v1/auth/login: rate limit attemptsnot completed

Week 4 · Customer application with Expo

Week 3 done · Up next · 8 modules

Open week 4

Implementation

Run locally

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

  1. Apply an independent limit to login attempts from each client IP.
  2. Apply an independent limit to each normalised email address.
  3. Ignore forged X-User-ID values when identifying the client IP.
  4. Return HTTP 429 with rate_limited and a Retry-After header when a limit is exceeded.
  5. Preserve the request body for the login handler.
  6. Allow the handler to return its own response for valid and malformed login requests.

Commands

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

Tests

  1. The per-IP login limit rejects attempts after five requests from the same IP, regardless of the supplied X-User-ID.

    POST/v1/auth/login
    Headers
    X-User-IDforged-6
    Body raw JSON
    {
      "email": "o3ip@test.local",
      "password": "wrong"
    }
    Response 429 Too Many Requests
    {
      "error": "rate_limited",
      "message": "…"
    }

    Input Start the API with LOGIN_LIMIT_PER_IP=5 and LOGIN_LIMIT_PER_EMAIL=1000. Send six login requests from the same IP, each with a different X-User-ID value and a different unique email.

    Expected The first five attempts return HTTP 401. An attempt past the limit returns HTTP 429 with {"error":"rate_limited"} and a non-empty Retry-After header.

    TestW3O3_TooManyLoginsFromOneIPGet429WhateverTheirXUserID in backend/e2e/w3_o3_test.go