APPLICATION-SECURITY Contents

OAuth2 Flows in Practice (Auth Code + PKCE)

OAuth2 flows for production teams: which flow to use, how to avoid common misconfigs, and what to log and alert on.

On this page

Choose the Right Flow

  • Browser SPA: Authorization Code + PKCE
  • Server-side web app: Authorization Code
  • Service-to-service: Client Credentials
  • Devices: Device Code (when needed)

Production Checklist

  • Strict redirect URI allowlist (no wildcards).
  • Use PKCE for public clients.
  • Scope minimization (least privilege).
  • Short-lived access tokens + refresh token rotation.

Logging You Actually Need

Log fields:
- client_id
- grant_type
- user_id (if present)
- scopes granted
- redirect_uri (normalized)
- decision (allow/deny) + reason
- token_id / session_id hash

Failure Modes

  • Redirect URI abuse: token leakage → strict allowlist + normalize.
  • Over-scoped tokens: breach blast radius → per-client scopes.
  • No rotation: stolen refresh token persists → rotate + detect reuse.

Operational Test

Verify redirect rules:
- attempt auth with unregistered redirect_uri
- expect hard failure (no partial success)