PKCE for Public Clients
You'll learn how PKCE secures OAuth flows in browser-based apps where you can't safely store client secrets.
Why PKCE Exists
In OAuth, public clients (like single-page apps) can't securely store a client_secret because JavaScript code is fully visible to users. Without PKCE, an attacker could intercept your authorization code and exchange it for tokens. PKCE (Proof Key for Code Exchange, pronounced "pixy") solves this by creating a dynamic secret per authorization request that can't be stolen in advance.
How PKCE Works
Think of PKCE like a secret handshake only you know:
- Generate a code verifier: Your app creates a random string (43-128 characters) kept in memory
- Create a code challenge: Hash the verifier with SHA-256 and base64-encode it
- Send challenge with authorization request: Include
code_challengeandcode_challenge_method=S256alongside yourstateparameter - Exchange code with verifier: When trading the authorization code for tokens, send the original
code_verifier
The authorization server verifies the verifier matches the challenge. An attacker who intercepts the authorization code can't use it without the original verifier string (which never left your app).
The Security Model
PKCE turns the authorization code into a two-factor token: you need both the code (from the redirect) and the verifier (from your app's memory). Even if someone steals the redirect URL, they can't complete the token exchange.
This is crucial for SPAs because unlike server-side apps with secure client_secret storage, browsers have no truly private storage—everything is accessible to JavaScript running on the page.
Key Takeaway: PKCE protects public clients by binding each authorization request to a cryptographic secret that never leaves the client, making intercepted authorization codes useless to attackers.