Authentication & Authorization
Authentication vs authorization
- Authentication answers “who are you?” (identity proof: password, token, mTLS).
- Authorization answers “what may you do?” (roles, scopes, policies on resources).
- Mixing the two in error messages confuses clients; return 401 vs 403 consistently with your policy.
API keys
- Simple shared secrets in headers or query params; easy for scripts and integrations.
- Weaknesses: broad access if leaked, rotation pain; prefer scoped keys and rate limits.
- Often combined with IP allowlists for server-to-server calls.
JWT (JSON Web Tokens)
- Self-contained signed tokens; can carry claims (sub, exp, scopes) without hitting a session store every time.
- Risks: stolen tokens, difficulty revoking without a denylist, algorithm confusion if misconfigured (
alg=none).
- Prefer short lifetimes, refresh tokens with rotation, and explicit audience/issuer checks.
OAuth2 overview
- Delegated authorization framework: resource owner, client, authorization server, resource server.
- Common flows: Authorization Code (+ PKCE for public clients), Client Credentials for machine-to-machine.
- OAuth solves consent and token issuance, not your full domain authorization model.
Session-based auth vs token-based auth
- Sessions: server stores session id; client holds cookie; simple revocation; needs sticky sessions or shared store at scale.
- Tokens: client presents bearer token each call; scales horizontally; plan revocation and clock skew.
- Hybrid patterns (session + API tokens) are common; document cookie attributes (
Secure, HttpOnly, SameSite).