Skip to content
Back to CurriculumLesson 12 of 30
oauth-oidcAdvanced

OAuth 2.0 PKCE & RFC 9700 Security Best Current Practice for Public SPAs & Mobile Apps

PKCE (RFC 7636, mandated by RFC 9700 OAuth Security BCP) eliminates client secrets for public clients (SPAs, mobile apps). The client creates a high-entropy Code Verifier, sends its SHA-256 Code Challenge on authorization, and proves ownership by submitting the verifier on token exchange.

Interactive Visual Concept ExplainerStep-by-Step Educational Simulation
Visual Step-by-Step Concept Explaineroauth-oidc
OAuth 2.0 PKCE & RFC 9700 Security Best Current Practice for Public SPAs & Mobile Apps
Steps:
Client Browser Memory (Private)RFC 7636
Code Verifier: dBjftJeZ4CVP-mB92K27uhbUJU1p1r_wW1gFWFOEjXk
Code Challenge (S256): E9Melhoa2OwvFrGMTJguCH5rtG6479bPWSMN
State (CSRF Defense): st_9f2c1a8e
Attacker Interception AttemptMitigated
Intercepted: Auth Code (?code=auth_123)
Attacker Exchanging: POST /oauth/token (Missing Verifier!)
Server Rejection: 400 Invalid Grant!
Step 1 of 6
Auto-stepping (1x)

Step 1: Client creates a cryptographic random string between 43 and 128 characters (code_verifier).

Security Guarantee: RFC 9700 mandates PKCE for all OAuth 2.0 clients, including confidential clients (defense-in-depth).
Why This Architectural Concept Exists

Single-Page Apps (React) and Mobile Apps cannot securely hide a client secret; anyone can decompile the JavaScript bundle or mobile APK. Without PKCE, malicious apps registered to the same custom URI scheme or intercepting redirects could steal authorization codes and exchange them for tokens.

Intuitive Mental Model

The Torn Half-Ticket: Before sending a courier to pick up concert passes, you rip a ticket in half (Code Verifier) and email a photo of the jagged rip line (Code Challenge) to the box office. When the courier delivers the receipt (authorization code), only someone holding the physical matching half can claim the wristbands.

Architectural System TopologyProtocol Diagram
[ React SPA / Mobile Client ]                       [ Authorization Server ]
              │                                                     │
   1. Generate: code_verifier = random(64)                          │
   2. Hash: code_challenge = S256(verifier)                        │
              │                                                     │
              ├── (1) GET /authorize?code_challenge=...&method=S256 ►│
              │                                                     │
              │◄── (2) 302 Redirect to callback with ?code=auth123 ─┤
              │                                                     │
              ├── (3) POST /token { code: auth123,                  │
              │                     code_verifier: ... } ──────────►│
              │                                                     ├── Verify: S256(verifier) == stored_challenge
              │◄── (4) 200 OK { access_token, refresh_token } ──────┤
CosmosApp Production Milestone

CosmosApp builds a Next.js Single Page App. In compliance with RFC 9700, CosmosApp implements PKCE with the S256 method. It generates a 43-to-128 character cryptographically random verifier, computes its base64url(sha256()) challenge, and passes it to the IdP. The server validates that only the originating browser tab can trade the authorization code.

Step-by-Step Execution Sequence
01

Step 1: Client creates a cryptographic random string between 43 and 128 characters (code_verifier).

02

Step 2: Client computes code_challenge = BASE64URL(SHA256(code_verifier)).

03

Step 3: Client redirects user to /authorize with code_challenge and code_challenge_method=S256.

04

Step 4: Server authenticates user, stores the challenge linked to the generated authorization code.

05

Step 5: Client receives authorization code and sends POST /token with the original code_verifier in plaintext.

06

Step 6: Server computes SHA-256 of the submitted verifier and confirms it matches the stored challenge before issuing tokens.

Production Architecture Blueprint
import crypto from 'node:crypto';

// CosmosApp PKCE Generation:
export function generatePkcePair() {
  // 1. High-entropy random verifier (43-128 chars):
  const codeVerifier = crypto.randomBytes(32).toString('base64url');

  // 2. Compute SHA-256 Code Challenge:
  const codeChallenge = crypto
    .createHash('sha256')
    .update(codeVerifier)
    .digest('base64url');

  return { codeVerifier, codeChallenge };
}
Common Architectural Misconceptions
  • ✕PKCE replaces the need for the state parameter (PKCE prevents authorization code injection/theft, but state is still required to prevent Login CSRF).
  • ✕Using code_challenge_method=plain is acceptable (RFC 9700 strictly forbids the 'plain' method; S256 must always be used).

Key Architectural Takeaways

  • ✓RFC 9700 mandates PKCE for all OAuth 2.0 clients, including confidential clients (defense-in-depth).
  • ✓Always use code_challenge_method=S256; never use 'plain'.
  • ✓PKCE protects the token exchange, while a cryptographic state parameter protects against Login CSRF.
Common Architectural Pitfall

Storing client secrets in frontend JavaScript or React environment variables (.env.production).

Production Best Practice Fix

Treat frontend clients as public clients; use PKCE without client secrets, or use a Backend-For-Frontend (BFF) proxy.

Interactive Knowledge Check

Why is code_challenge_method=plain forbidden by modern OAuth 2.0 security specifications (RFC 9700)?