Protecting Auth Endpoints: Rate Limiting and Brute-Force Defense
How to design rate limiting for login, OTP, and reset endpoints — algorithms, what to key on, lockout strategies, and avoiding the traps that let attackers through or lock out real users.
Every authentication endpoint is a guessing game an attacker would love to play at machine speed. Rate limiting is how you slow that game down to the point where it isn't worth playing. Done well, it stops brute-force and credential-stuffing attacks without ever inconveniencing a real user. Done badly, it either does nothing or locks out the very people you're trying to protect.
What you're defending against
- Password brute-forcing — trying many passwords against one account.
- Credential stuffing — trying many leaked email/password pairs across many accounts.
- OTP / code brute-forcing — a six-digit code is only a million possibilities; without limits it falls in seconds.
- Reset/enumeration abuse — hammering the "forgot password" or "resend code" endpoints to spam users or probe for valid accounts.
Notice these have different shapes. Per-account limits stop brute-forcing one account; per-IP limits stop one machine hitting many accounts. You need both.
Choosing a rate-limit algorithm
Fixed window
Count requests in each clock-aligned window (e.g. per minute). Simple, but vulnerable to bursts at the window boundary — an attacker can send a full quota at 0:59 and another at 1:00.
Sliding window
Smooths the boundary problem by weighting the previous window. More accurate, slightly more state.
Token bucket
A bucket refills at a steady rate up to a maximum. Each request spends a token; an empty bucket means rejection. This allows short legitimate bursts while enforcing a sustained average — usually the best fit for auth endpoints.
For most applications, a token-bucket or sliding-window limiter backed by a fast shared store (Redis or equivalent) is the right default. The store must be shared across all your servers, or attackers simply spread requests across instances to dodge per-node counters.
Here is a token bucket implemented as a single atomic Redis operation — atomicity matters, because a check-then-write split across two commands is itself a race an attacker can exploit under load:
-- KEYS[1] = bucket key ARGV = capacity, refillPerSec, now, cost
local capacity = tonumber(ARGV[1])
local refill = tonumber(ARGV[2])
local now = tonumber(ARGV[3])
local cost = tonumber(ARGV[4])
local data = redis.call('HMGET', KEYS[1], 'tokens', 'ts')
local tokens = tonumber(data[1]) or capacity
local ts = tonumber(data[2]) or now
tokens = math.min(capacity, tokens + (now - ts) * refill)
if tokens < cost then
return 0 -- rejected
end
tokens = tokens - cost
redis.call('HMSET', KEYS[1], 'tokens', tokens, 'ts', now)
redis.call('EXPIRE', KEYS[1], math.ceil(capacity / refill))
return 1 -- allowed
Call it once per request with a key derived from what you're limiting (login:acct:<id>, otp:ip:<ip>, and so on). Returning 0 maps to a 429.
What to key the limit on
Choosing the right key is more important than the algorithm:
- Per IP — catches a single source hammering you. But beware: many legitimate users share an IP (corporate NAT, mobile carriers), and attackers rotate IPs cheaply. IP alone is necessary but not sufficient.
- Per account / username — catches brute-forcing of a specific account regardless of source IP. Essential for login.
- Per IP + account combination — fine-grained and useful for distinguishing targeted attacks.
- Global / endpoint-wide — a circuit breaker for the whole endpoint under a distributed attack.
A layered approach — combining per-account, per-IP, and global limits — is far more robust than any single key.
The trap: trusting the client's IP
If your app sits behind a load balancer or CDN, the socket's remote address is your proxy, not the user — so you read the client IP from a forwarded header like X-Forwarded-For. But that header is client-supplied and trivially spoofed, and a naïve limiter that keys on the leftmost value it finds can be defeated by an attacker who simply sends a new fake IP on every request:
X-Forwarded-For: 203.0.113.9 <- real client (appended by your trusted proxy)
X-Forwarded-For: 1.2.3.4, 203.0.113.9 <- attacker prepended a fake; only the rightmost is trustworthy
The fix is to configure your framework's trusted proxy setting so it takes the client IP from the correct position in the chain — the entry your own infrastructure added — and never from an arbitrary attacker-controlled value. Getting this wrong silently disables your per-IP limits.
Distributed credential stuffing
Sophisticated attackers spread a stuffing campaign across thousands of residential proxies, so no single IP ever trips a limit. Per-IP keys can't catch this alone. This is exactly why per-account limits and global endpoint circuit breakers matter, and why device fingerprinting, phishing-resistant passkeys, and breach-password checks are worth layering on top for high-value accounts.
Lockout strategies
When a threshold is crossed, you have options, from gentle to strict:
- Throttling / exponential backoff — each failed attempt increases the required wait. Smooth and self-correcting; usually the best user experience.
- CAPTCHA challenge — after a few failures, require proof of humanity. Stops bots while letting real users continue.
- Temporary lockout — block further attempts for a fixed period. Effective, but be careful: a naive per-account lockout becomes a denial-of-service tool — an attacker can lock a victim out of their own account on purpose.
Prefer throttling and CAPTCHA over hard account lockouts. If you do lock accounts, lock the attempt source rather than the target account where possible, and always give the legitimate user a recovery path.
Don't forget the secondary endpoints
The single most common mistake: rate-limiting the login form and nothing else. Apply limits consistently to every auth-adjacent endpoint:
- Sign-in
- OTP verification (critical — this is the most brute-forceable)
- OTP / magic-link request (to prevent email/SMS flooding and cost abuse)
- Password reset request and submission
- Token refresh
- Sign-up (to limit automated account creation)
An unprotected OTP-verify endpoint quietly undoes all the work you did protecting the login form.
Make responses safe, too
Rate limiting interacts with information disclosure. Keep responses uniform so the limiter itself doesn't leak which accounts exist or which codes were "closer." Return a clear, generic error and the standard 429 Too Many Requests status with a Retry-After header so well-behaved clients can back off gracefully.
HTTP/1.1 429 Too Many Requests
Retry-After: 60
Cost limits are a separate control
Rate limits are usually framed as a security measure, but on any endpoint that sends an email or an SMS they are also a spending control, and the two need different thresholds.
A magic-link request endpoint with a generous limit — say twenty per hour per IP — is perfectly reasonable from a brute-force standpoint, because there is nothing to guess. It is also an invitation to burn several thousand emails a day from a handful of hosts, at your expense, with your sending domain's reputation as collateral damage. SMS is worse: international premium-rate destinations turn an unlimited OTP-send endpoint into a revenue stream for the attacker, a fraud pattern common enough to have a name — SMS pumping, or toll fraud.
The defenses are different in kind from a token bucket:
- A per-destination cooldown. The same address or number cannot be targeted again for sixty seconds, regardless of who asks. This is the single highest-value limit on a send endpoint, and it also happens to be what stops an attacker from mailbombing a specific user.
- A daily spend ceiling per tenant and globally. Not a rate — a hard count that trips an alert and stops sending. Rate limits shape traffic; a ceiling is what stops a slow, patient abuser who stays under every per-window threshold.
- Destination-country allowlists for SMS. If your product has no users in a given country, sending there should be impossible rather than merely rate-limited. Toll fraud depends on being able to reach specific expensive ranges.
- Reputation-weighted limits. A brand-new, unverified account should get a far smaller send budget than an account with a payment method and six months of history. Most abuse comes from accounts minutes old.
Where to enforce the limit
There is a layering question that determines whether your limits actually hold. Enforcement at the edge — a CDN or WAF rule — is cheap, absorbs volumetric attacks before they touch your infrastructure, and knows the true client IP. Enforcement in the application is expensive per request but is the only layer that knows which account is being targeted, whether the credential was correct, and what the tenant's plan allows.
You want both, and you want them to mean different things. The edge layer defends availability: coarse per-IP ceilings, obvious bot signatures, volumetric floods. The application layer defends accounts: per-account failure counts, per-destination cooldowns, escalating challenges. Trying to do account-aware limiting at the edge means shipping identity context to the edge; trying to do volumetric defense in the application means paying for every request an attacker sends.
One trap in the middle: if your application limiter runs after your session middleware and database lookups, an attacker still costs you a full request's worth of work for every rejected attempt. Put the limiter check as early in the pipeline as it can see the key it needs — usually immediately after parsing the body, before any authentication work. Rejecting at the limiter should be measurably cheaper than serving a real request, or the limiter itself becomes the amplification vector.
Observability
Rate limiting is also a detection surface. Spikes in 429s, a flood of failed logins across many accounts, or unusual geographic patterns are early warnings of an attack in progress. Log limiter decisions, alert on anomalies, and feed the data back into tuning your thresholds.
A sensible default configuration
- Login: per-account limit (e.g. 5 failures → exponential backoff, CAPTCHA after 10), plus a per-IP limit, plus a global circuit breaker.
- OTP verify: strict — a handful of attempts per code, then the code is burned and a new one required.
- OTP/magic-link/reset requests: a few per account and per IP per hour to prevent flooding.
- Refresh: modest per-token limits, with rotated-token-reuse detection layered on top.
- Shared store across all instances;
429withRetry-After; uniform error messages; full logging.
Rate limiting isn't a feature you bolt on after a breach — it's a structural property of a healthy auth system. The goal is asymmetry: make each guess so cheap for you to reject and so expensive for an attacker to attempt that brute force simply stops being viable.
Rate limiting is one layer of defense, not the whole wall. It pairs with the enumeration-safe, timing-uniform responses in Common Authentication Vulnerabilities and the rotating, revocable sessions that contain the blast radius when an account is compromised anyway. EmbedAuth applies per-IP limits to every hosted auth route by default — including the OTP-verify endpoint that's so easy to forget — plus per-address limits on the routes that send email and an account lockout after repeated wrong passwords.
Written by
Emilian GheoneaSenior Blockchain & Full-Stack Software Engineer. I build EmbedAuth — an embeddable authentication platform for SaaS — and write about the auth problems most teams hit too late.
Related articles
Security
Credential Stuffing: Why Your Login Page Is Someone Else's Business Model
How stuffing attacks are actually run in 2026 — combo lists, residential proxies, config files and solver services — and the layered defenses that work, from breached-password checks to risk scoring and the metrics that reveal an attack in progress.
Jul 31, 2026 8 minReadSecurity
Common Authentication Vulnerabilities and How to Prevent Them
Credential stuffing, broken session handling, JWT confusion attacks, account enumeration, and the other ways login systems get broken in production — plus the concrete fixes for each.
May 28, 2026 6 minReadSecurity
Webhook Signatures: Verifying What Your Server Is Told
A webhook endpoint is an unauthenticated API that changes your data. How to sign and verify properly — HMAC over the raw body, timestamps and replay windows, constant-time comparison, key rotation, and the idempotency and SSRF issues on both sides.
Aug 3, 2026 8 minReadSecurity
Audit Logs for Authentication: What to Record Before You Need It
The events every auth system should log, the schema that makes them queryable, why application logs are not an audit trail, tamper-evidence and retention, and how to expose logs to customers without leaking other tenants' data.
Aug 2, 2026 8 minRead