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.
Step 1: Client creates a cryptographic random string between 43 and 128 characters (code_verifier).
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.
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.
[ 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 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 1: Client creates a cryptographic random string between 43 and 128 characters (code_verifier).
Step 2: Client computes code_challenge = BASE64URL(SHA256(code_verifier)).
Step 3: Client redirects user to /authorize with code_challenge and code_challenge_method=S256.
Step 4: Server authenticates user, stores the challenge linked to the generated authorization code.
Step 5: Client receives authorization code and sends POST /token with the original code_verifier in plaintext.
Step 6: Server computes SHA-256 of the submitted verifier and confirms it matches the stored challenge before issuing tokens.
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 };
}- ✕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.
Storing client secrets in frontend JavaScript or React environment variables (.env.production).
Treat frontend clients as public clients; use PKCE without client secrets, or use a Backend-For-Frontend (BFF) proxy.
Why is code_challenge_method=plain forbidden by modern OAuth 2.0 security specifications (RFC 9700)?