security · intermediate
OAuth 2.0, OIDC, and JWT — Delegation Without Sharing Passwords
Start here
Three related pieces that people often mash together:
- OAuth 2.0 — authorization framework: an app gets limited access to a user’s resources on another system without the user’s password.
- OpenID Connect (OIDC) — identity layer on OAuth: the app learns who logged in (ID token).
- JWT (JSON Web Token) — a compact signed token format often used for access/ID tokens.
What you will learn
- Separate authentication vs authorization language.
- Map roles: resource owner, client, authorization server, resource server.
- Choose authorization code + PKCE for public clients.
- Understand access tokens vs refresh tokens vs ID tokens.
- Validate JWTs correctly.
- Avoid common anti-patterns.
Words you need before we begin
| Term | Plain English |
|---|---|
| Resource owner | Usually the end user. |
| Client | App requesting access (SPA, mobile, backend). |
| Authorization server (AS) | Issues tokens after login/consent (IdP). |
| Resource server (RS) | API that accepts access tokens. |
| Access token | Credential for calling APIs. |
| Refresh token | Long-lived credential to get new access tokens. |
| ID token | OIDC token asserting user identity to the client. |
| PKCE | Proof Key for Code Exchange—protects public clients. |
| Scope | Limited permissions requested/granted. |
| JWT | Signed (optionally encrypted) JSON token structure. |
Simple story: hotel key card
You (resource owner) check in at the front desk (authorization server). You get a key card (access token) that opens gym and room (scopes), not the vault. You do not give the gym your passport every time—only the card. OIDC is the desk also giving your app a badge that says your name (ID token).
The problem OAuth solves
Before OAuth, apps asked for your password to another site (“give us your Gmail password to import contacts”). That is catastrophic: apps see passwords; revocation is hard; least privilege is impossible.
OAuth lets you delegate with scopes and revocable tokens.
Step-by-step: authorization code + PKCE (modern default)
Step 1 — Client prepares PKCE
Generate code_verifier; derive code_challenge.
Step 2 — Redirect user to AS
Browser goes to authorize URL with client_id, redirect_uri, scope, state, code_challenge.
Step 3 — User authenticates and consents
AS authenticates user; user approves scopes.
Step 4 — Redirect back with code
Only if redirect_uri exactly matches allowlist. Validate state (CSRF).
Step 5 — Client exchanges code for tokens
Back-channel (or app) POST to token endpoint with code + code_verifier. Receives access token (+ refresh, + id_token for OIDC).
Step 6 — Call APIs with access token
Authorization: Bearer … to resource server. RS validates token (JWT locally or introspection).
Step 7 — Refresh carefully
Store refresh tokens securely (httpOnly secure cookies or secure mobile storage—not localStorage if avoidable). Rotate when supported.
JWT validation essentials
For signed JWT access/ID tokens:
- Validate signature with the right keys (JWKS).
- Validate iss (issuer) and aud (audience).
- Validate exp (expiry) with small clock skew.
- Do not trust unsigned
alg=none. - Remember JWT payload is readable if only signed—not secret.
Visual mental model
sequenceDiagram
participant U as User
participant C as Client app
participant AS as Auth server
participant API as Resource API
U->>C: Login
C->>AS: redirect authorize + PKCE
U->>AS: authenticate + consent
AS->>C: redirect code
C->>AS: token exchange
AS-->>C: access + id tokens
C->>API: API call + access token
API-->>C: data
Learning question: Why is the authorization code redirected to the browser instead of sending tokens in the first redirect for public clients?
Caption: Code + PKCE reduces token exposure; tokens obtained via direct token request with verifier.
Complete worked example: “Login with IdP” for a SaaS API
SPA or mobile app uses AS. After login, SPA calls api.myapp.com with access token scoped orders:read. API validates JWT aud=api.myapp.com, iss=https://idp.example/, signature via JWKS, and enforces user permissions server-side (token is not the only authz).
Failure modes / attacks (intro)
| Issue | Risk | Mitigation |
|---|---|---|
| Open redirect_uri | Token/code theft | Exact allowlist |
| Missing state | CSRF on login | Required state |
| Tokens in URL fragments logged | Leakage | Prefer code flow; careful storage |
| Accepting any JWT signing key | Forgery | Pin iss/jwks carefully |
| Long-lived access tokens in LS | XSS theft | Short TTL; better storage |
| Confused deputy | Wrong audience | Validate aud |
Trade-offs
| Choice | Benefit | Cost |
|---|---|---|
| Opaque tokens + introspection | Revocation easy | AS dependency on each call |
| JWT access tokens | Local validate | Revocation harder |
| Session cookies for first party | Simple CSRF model | Cross-site patterns differ |
| OAuth for first-party login only | Sometimes overkill | Complexity |
Common misunderstandings
- “OAuth is authentication.” OAuth is authorization; OIDC adds authN.
- “JWT is always encrypted.” Often only signed.
- “Put roles only in JWT forever.” Authorization can change; keep TTLs short or check server.
- “Implicit flow is fine.” Considered obsolete for SPAs—use code+PKCE.
- “HTTPS optional in dev excuses production mistakes.” Redirect URIs and cookies need real hygiene.
Check your understanding
- Name the four OAuth roles.
- What problem does PKCE solve?
- Access token vs ID token?
- List three JWT checks.
- Why allowlist redirect URIs?
Practice
- Draw code+PKCE for a mobile app.
- Write pseudocode for API JWT middleware.
- Compare first-party session login vs OIDC.
- Draft scopes for a photo app.
- Find one historical OAuth vulnerability class and map to a row above.
Deeper production notes
Service-to-service
Client credentials flow for machine clients—no user—still needs secret management and least privilege scopes.
Key rotation
JWKS rotation must be supported; cache keys with kid.
Specialist boundary
Standards and browser rules evolve (BCP for browser apps). Re-read current OAuth/OIDC BCPs before production freeze.
Additional teaching scenarios
Scenario A — 10× peak
What breaks first? Mitigation?Scenario B — dependency outage
What still works?Scenario C — teach-back
Five sentences: problem, mechanism, example, failure, trade-off.Revision summary
- OAuth delegates access; OIDC identifies users; JWT is a token format.
- Prefer authorization code + PKCE for public clients.
- Validate tokens strictly; store secrets carefully.
- Server-side authorization still required.
Glossary
| Term | Definition |
|---|---|
| OAuth 2.0 | Delegation framework for limited access. |
| OIDC | Identity layer on OAuth. |
| JWT | JSON-based token format, often signed. |
| PKCE | Extension protecting auth code flows for public clients. |
Abbreviations and terminology
- AS/RS — Authorization / resource server
- JWKS — JSON Web Key Set
- SPA — Single-page application
- BCP — Best current practice
What to learn next
Extra teaching notes
When you apply this lesson, write the user-visible success metric first, then the failure mode you fear most. Design the smallest mechanism that protects that metric under partial failure. Prefer explicit timeouts, idempotency, and ownership over adding more infrastructure boxes.
In interviews or design reviews, narrate assumptions, request paths, and trade-offs out loud. A correct-enough design with clear failure handling beats a buzzword diagram without numbers. Revisit the worked example and restate it for a different domain to prove you own the ideas, not the template wording.
Extra teaching notes
When you apply this lesson, write the user-visible success metric first, then the failure mode you fear most. Design the smallest mechanism that protects that metric under partial failure. Prefer explicit timeouts, idempotency, and ownership over adding more infrastructure boxes.
In interviews or design reviews, narrate assumptions, request paths, and trade-offs out loud. A correct-enough design with clear failure handling beats a buzzword diagram without numbers. Revisit the worked example and restate it for a different domain to prove you own the ideas, not the template wording.
Extra teaching notes
When you apply this lesson, write the user-visible success metric first, then the failure mode you fear most. Design the smallest mechanism that protects that metric under partial failure. Prefer explicit timeouts, idempotency, and ownership over adding more infrastructure boxes.
In interviews or design reviews, narrate assumptions, request paths, and trade-offs out loud. A correct-enough design with clear failure handling beats a buzzword diagram without numbers. Revisit the worked example and restate it for a different domain to prove you own the ideas, not the template wording.
Extra teaching notes
When you apply this lesson, write the user-visible success metric first, then the failure mode you fear most. Design the smallest mechanism that protects that metric under partial failure. Prefer explicit timeouts, idempotency, and ownership over adding more infrastructure boxes.
In interviews or design reviews, narrate assumptions, request paths, and trade-offs out loud. A correct-enough design with clear failure handling beats a buzzword diagram without numbers. Revisit the worked example and restate it for a different domain to prove you own the ideas, not the template wording.
Extra teaching notes
When you apply this lesson, write the user-visible success metric first, then the failure mode you fear most. Design the smallest mechanism that protects that metric under partial failure. Prefer explicit timeouts, idempotency, and ownership over adding more infrastructure boxes.
In interviews or design reviews, narrate assumptions, request paths, and trade-offs out loud. A correct-enough design with clear failure handling beats a buzzword diagram without numbers. Revisit the worked example and restate it for a different domain to prove you own the ideas, not the template wording.
Interview and production field guide for this topic
Use this section as deliberate practice, not filler. Rewrite the worked example for a second domain you know well—fintech, education, logistics, or media. Keep the same skeleton: requirements, estimates, high-level diagram, request path, data model, scale lever, failure modes, and trade-offs. If you cannot fill every section without copying buzzwords, you do not yet own the design.
Numbers to force yourself to state
Always speak order-of-magnitude figures: peak QPS, storage growth per day, fan-out factor, connection counts, or queue depth. Wrong numbers that are explicit beat silent hand-waving. Correct the numbers when the interviewer or teammate challenges them; that is collaboration, not failure.
Failure minute
Set a timer for sixty seconds and list only failures: timeouts, duplicates, hot keys, dependency outages, bad deploys, and data corruption paths. For each, name detection and first mitigation. Designs that only describe the happy path are incomplete for production and weak in interviews.
Ownership and operability
Name the dashboard, the alert, the runbook section, and the team that pages. If any are blank, the system will train you during an incident. Prefer progressive delivery: canaries, flags, and rollback notes written before the change lands.
Consistency and retries
State whether the design assumes at-least-once delivery, whether handlers are idempotent, and where unique constraints live. Retries without idempotency are how double charges, double messages, and duplicate fan-out jobs appear. Timeouts without bounds are how thread pools die.
What good looks like in a review
A strong design review or interview answer clarifies scope, makes assumptions audible, draws a minimal path, deepens one or two bottlenecks, and closes with trade-offs and evolution. Use that bar on oauth2-oidc-jwt-deep-dive round 0 every time you revisit it.
FAQ from first-time learners
Q: Is Auth0/Cognito required?
A: No—concepts are the same whether you build or buy the AS.
Q: Can access tokens be JWTs?
A: Yes, commonly; opaque tokens are also valid.
Track: Security and Identity
Previous: Threat Modelling for Backend Services
Next: Secrets and Key Management
By Shubham Jain