OpenAPI as the source of truth; verifying tokens safely. ~25 min
Lesson notes
The typical FDE stack: Next.js/React front end on Vercel ↔ FastAPI back end (container on Fly/Render/Cloud Run/AWS) ↔ Postgres + a vector store + LLM providers. FastAPI emits openapi.json; generate a TypeScript client from it (openapi-typescript, orval) so the front end and back end can't drift.
Verifying a bearer token in FastAPI with a dependency
from fastapi import Depends, HTTPException
from fastapi.security import HTTPBearer, HTTPAuthorizationCredentials
bearer = HTTPBearer()
def current_user(creds: HTTPAuthorizationCredentials = Depends(bearer)) -> str:
user_id = verify(creds.credentials) # e.g. Supabase / Auth0 JWT via PyJWT
if user_id is None:
raise HTTPException(401, "invalid token")
return user_id
Practice
1. Sign & verify a token
hard50 XP
Implement sign(user_id, expires_at, secret) -> str producing "<user_id>.<expires_at>.<hex_sig>" where sig is HMAC-SHA256 of "<user_id>.<expires_at>". Implement verify(token, secret, now) -> str | None returning user_id only if the format is valid, the signature matches (constant-time), and now < expires_at. Never raise on malformed input.
Ctrl/⌘ + Enter runs the tests · Esc then Tab leaves the editor