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
Exchange email and password for a token pair
emailstringrequiredpasswordstringrequired
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_credentials
rate_limited
Acceptance criteria
- With
LOGIN_LIMIT_PER_IP=5andLOGIN_LIMIT_PER_EMAIL=1000, the first five login attempts from one IP return HTTP 401. Each attempt uses a different forgedX-User-IDand a different unique email. The sixth attempt returns HTTP 429 with{"error":"rate_limited"}and a non-emptyRetry-Afterheader. - With
LOGIN_LIMIT_PER_IP=100000andLOGIN_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=100000andLOGIN_LIMIT_PER_EMAIL=3, a valid login using a signed-up account's email and password returns HTTP 200. A login request with the bodynot jsonreturns HTTP 400 with{"error":"invalid_body"}.
Constraints
- Configure the independent limits through
LOGIN_LIMIT_PER_IPandLOGIN_LIMIT_PER_EMAIL. - Apply the per-IP limit regardless of the client's
X-User-IDvalue. - Normalise email identity by trimming surrounding whitespace and ignoring letter case.
- A rejected request returns HTTP 429 with error code
rate_limitedand a non-emptyRetry-Afterheader. - 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_bodyresponse.
How to check your work
From the project root, run:
cd backend && make up
cd backend && make test W=3O3Read the first failing assertion to identify whether the IP bucket, email normalisation, rate-limit response, or request-body preservation is incorrect.
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.
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 3 done · Up next · 8 modules
Implementation
Run locally
Write the code in your editor inside zomato-prep, then work through this list.
- Apply an independent limit to login attempts from each client IP.
- Apply an independent limit to each normalised email address.
- Ignore forged
X-User-IDvalues when identifying the client IP. - Return HTTP 429 with
rate_limitedand aRetry-Afterheader when a limit is exceeded. - Preserve the request body for the login handler.
- Allow the handler to return its own response for valid and malformed login requests.
Commands
cd backend && make upcd backend && make test W=3O3Tests
The per-IP login limit rejects attempts after five requests from the same IP, regardless of the supplied
X-User-ID.POST/v1/auth/loginHeadersX-User-ID forged-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=5andLOGIN_LIMIT_PER_EMAIL=1000. Send six login requests from the same IP, each with a differentX-User-IDvalue 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-emptyRetry-Afterheader.TestW3O3_TooManyLoginsFromOneIPGet429WhateverTheirXUserIDinbackend/e2e/w3_o3_test.go