1---2name: oauth3description: Implement OAuth 2.0 and OpenID Connect flows securely.4---5
6## Flow Selection
7
8- Authorization Code + PKCE: use for all clients—web apps, mobile, SPAs
9- Client Credentials: service-to-service only—no user context
10- Implicit flow: deprecated—don't use; was for SPAs before PKCE existed
11- Device Code: for devices without browsers (TVs, CLIs)—user authorizes on separate device
12
13## PKCE (Proof Key for Code Exchange)
14
15- Required for public clients (SPAs, mobile), recommended for all
16- Generate `code_verifier`: 43-128 char random string, stored client-side
17- Send `code_challenge`: SHA256 hash of verifier, sent with auth request
18- Token exchange includes `code_verifier`—server verifies against stored challenge
19- Prevents authorization code interception—attacker can't use stolen code without verifier
20
21## State Parameter
22
23- Always include `state` in authorization request—prevents CSRF attacks
24- Generate random, unguessable value; store in session before redirect
25- Verify returned `state` matches stored value before processing callback
26- Can also encode return URL or other context (encrypted or signed)
27
28## Redirect URI Security
29
30- Register exact redirect URIs—no wildcards, no open redirects
31- Validate redirect_uri on both authorize and token endpoints
32- Use HTTPS always—except localhost for development
33- Path matching is exact—`/callback` ≠ `/callback/`
34
35## Tokens
36
37- Access token: short-lived (minutes to hour), used for API access
38- Refresh token: longer-lived, used only at token endpoint for new access tokens
39- ID token (OIDC): JWT with user identity claims—don't use for API authorization
40- Don't send refresh tokens to resource servers—only to authorization server
41
42## Scopes
43
44- Request minimum scopes needed—users trust granular requests more
45- Scope format varies: `openid profile email` (OIDC), `repo:read` (GitHub-style)
46- Server may grant fewer scopes than requested—check token response
47- `openid` scope required for OIDC—triggers ID token issuance
48
49## OpenID Connect
50
51- OIDC = OAuth 2.0 + identity layer—adds ID token and UserInfo endpoint
52- ID token is JWT with `sub`, `iss`, `aud`, `exp` + profile claims
53- Verify ID token signature before trusting claims
54- `nonce` parameter prevents replay attacks—include in auth request, verify in ID token
55
56## Security Checklist
57
58- HTTPS everywhere—tokens in URLs must be protected in transit
59- Validate `iss` and `aud` in tokens—prevents token confusion across services
60- Bind authorization code to client—code usable only by requesting client
61- Short authorization code lifetime (10 min max)—single use
62- Implement token revocation for logout/security events
63
64## Common Mistakes
65
66- Using access token as identity proof—use ID token for authentication
67- Storing tokens in localStorage—vulnerable to XSS; prefer httpOnly cookies or memory
68- Not validating redirect_uri—allows open redirect attacks
69- Accepting tokens from URL fragment in backend—fragment never reaches server
70- Long-lived access tokens—use short access + refresh pattern
71
72## Token Endpoints
73
74- `/authorize`: user-facing, returns code via redirect
75- `/token`: backend-to-backend, exchanges code for tokens; requires client auth for confidential clients
76- `/userinfo` (OIDC): returns user profile claims; requires access token
77- `/revoke`: invalidates tokens; accepts access or refresh token
78
79## Client Types
80
81- Confidential: can store secrets (backend apps)—uses client_secret
82- Public: cannot store secrets (SPAs, mobile)—uses PKCE only
83- Never embed client_secret in mobile apps or SPAs—it will be extracted