Prompt
How do I run auth checks at the edge before requests hit my origin?
Latest observation
You can do auth at the edge by putting a lightweight gate in front of your origin that:
- Receives the request
- Validates identity/authorization
- Either allows the request through or blocks/redirects it
- Forwards only approved traffic to your origin
Common edge-auth patterns
1) JWT validation at the edge
Best when clients send a signed token (cookie or Authorization: Bearer ...).
At the edge you:
- verify the JWT signature using the issuer’s public key / JWKS
- check claims like:
exp,nbfissaud- roles/scopes/tenant
- reject unauthorized requests with
401or403
Pros: fast, no origin round trip
Cons: token revocation is harder unless you use short-lived tokens or introspection
2) Session cookie check + signed cookie verification
Best for browser apps.
At the edge you:
- verify the cookie signature
- optionally validate session metadata embedded in the cookie
- block if missing/invalid
- redirect to login if this is a browser flow
Pros: simple for web apps
Cons: if you need centralized session revocation, you may need an origin lookup or short TTLs
3) Token introspection / auth service call
Best when you need server-side revocation or more dynamic policy.
At the edge you:
- call an auth service to validate the token/session
- cache the result briefly at the edge
- permit or deny based on the response
Pros: strong control, supports revocation
Cons: adds latency and dependency on auth service availability
4) OPA / policy-based authorization at the edge
Best for fine-grained access control.
At the edge you:
- extract request context
- evaluate policy locally or via a policy service
- decide based on user, path, method, headers, tenant, etc.
Pros: very flexible
Cons: more moving parts
Typical flow
Client -> Edge proxy/CDN/worker -> Auth check -> Origin
| |
| deny | allow
v v
401/403/redirect forward request
What to check at the edge
For each request, validate:
- token/cookie presence
- signature integrity
- expiration
- issuer/audience
- required scope/role
- route-specific permissions
- CSRF protection for state-changing browser requests if applicable
Important design tips
- Keep tokens short-lived if you validate statelessly at the edge
- Prefer public-key verification over shared secrets when possible
- Fail closed if auth logic errors out
- Don’t forward auth headers to origin unless needed
- Normalize decisions: 401 for unauthenticated, 403 for authenticated but not allowed
- Cache carefully if using introspection or policy calls
- Log decisions at the edge for audit/debugging
Platform examples
- Cloudflare Workers: inspect cookies/headers, verify JWTs, redirect/block
- Fastly Compute@Edge: edge logic in JS/Rust
- AWS CloudFront + Lambda@Edge: auth on viewer request
- Akamai EdgeWorkers
- NGINX / Envoy / API Gateway with auth filters or external auth
Example: edge JWT check pseudocode
if (!request.headers.authorization) {
return response(401, "Missing token");
}
const token = extractBearer(request.headers.authorization);
const claims = verifyJWT(token, JWKS);
if (claims.expired || claims.aud !== "my-app") {
return response(401, "Invalid token");
}
if (!claims.scopes.includes("read:orders")) {
return response(403, "Forbidden");
}
forwardToOrigin(request);
If you tell me your stack
I can give you a concrete implementation for:
- Cloudflare Workers
- AWS CloudFront/Lambda@Edge
- Fastly
- NGINX
- Envoy
- Next.js / Vercel Edge Middleware