OAuth2 PKCE Flow
Add a code challenge to the OAuth2 authorization code flow so a public client like a SPA cannot have its code intercepted, without a client secret.
On this page
Introduction
This guide covers OAuth2 PKCE for public clients, a recurring concern in the auth-jwt scenario. The plain authorization code flow assumes a client secret, which a single-page app cannot keep. PKCE adds a code challenge derived from a random verifier, so even if the authorization code is intercepted it cannot be redeemed without the verifier, and no secret is needed on the client.
Generate a verifier
Create a cryptographically random verifier of 43 to 128 chars on the client, and keep it in memory for the redeem step.
The trade-off worth naming is that create a cryptographically random verifier of 43 to 128 chars on the client, and keep it in memory for the redeem step adds a measurable step to the hot path, so measure it before assuming it is free. A reasonable approach is to validate once at the edge, fail closed with a structured error, and log the rejection reason so a spike in bad input is visible on a dashboard rather than silent. Pair the implementation with a test that asserts a valid payload passes and at least one representative violation is rejected with the expected message; this is the cheapest way to keep the guarantee honest as the schema evolves.
const verifier = crypto.randomUUID() + crypto.randomUUID();
sessionStorage.setItem('pkce_verifier', verifier);
Derive the challenge
Hash the verifier with SHA256 and base64url encode it, then send that challenge with method S256, so the verifier never leaves the client until redeem.
The trade-off worth naming is that hash the verifier with sha256 and base64url encode it, then send that challenge with method s256, so the verifier never leaves the client until redeem adds a measurable step to the hot path, so measure it before assuming it is free. A reasonable approach is to validate once at the edge, fail closed with a structured error, and log the rejection reason so a spike in bad input is visible on a dashboard rather than silent. Pair the implementation with a test that asserts a valid payload passes and at least one representative violation is rejected with the expected message; this is the cheapest way to keep the guarantee honest as the schema evolves.
const challenge = b64url(crypto.createHash('sha256').update(verifier).digest());
const authUrl = '/authorize?response_type=code&client_id=...&code_challenge=' + challenge + '&code_challenge_method=S256';
Redeem with verifier
Send the verifier along with the code when redeeming, so the server re-derives the challenge and compares, and an intercepted code without the verifier is useless.
The trade-off worth naming is that send the verifier along with the code when redeeming, so the server re-derives the challenge and compares, and an intercepted code without the verifier is useless adds a measurable step to the hot path, so measure it before assuming it is free. A reasonable approach is to validate once at the edge, fail closed with a structured error, and log the rejection reason so a spike in bad input is visible on a dashboard rather than silent. Pair the implementation with a test that asserts a valid payload passes and at least one representative violation is rejected with the expected message; this is the cheapest way to keep the guarantee honest as the schema evolves.
const r = await fetch('/token', {
method: 'POST',
body: new URLSearchParams({ grant_type: 'authorization_code', code, code_verifier: verifier, client_id }),
});
No client secret
Because the verifier binds the redeem to the original request, a public client needs no secret, which is the whole point for a SPA or mobile app.
The trade-off worth naming is that because the verifier binds the redeem to the original request, a public client needs no secret, which is the whole point for a spa or mobile app adds a measurable step to the hot path, so measure it before assuming it is free. A reasonable approach is to validate once at the edge, fail closed with a structured error, and log the rejection reason so a spike in bad input is visible on a dashboard rather than silent. Pair the implementation with a test that asserts a valid payload passes and at least one representative violation is rejected with the expected message; this is the cheapest way to keep the guarantee honest as the schema evolves.
Use S256
Always use the S256 method; plain is only for clients that cannot hash and protects less, so a modern client has no reason to pick it.
The trade-off worth naming is that always use the s256 method; plain is only for clients that cannot hash and protects less, so a modern client has no reason to pick it adds a measurable step to the hot path, so measure it before assuming it is free. A reasonable approach is to validate once at the edge, fail closed with a structured error, and log the rejection reason so a spike in bad input is visible on a dashboard rather than silent. Pair the implementation with a test that asserts a valid payload passes and at least one representative violation is rejected with the expected message; this is the cheapest way to keep the guarantee honest as the schema evolves.
Test the intercept
Redeem with the right verifier and assert success, then redeem with a wrong verifier and assert failure, so the binding is enforced.
The trade-off worth naming is that redeem with the right verifier and assert success, then redeem with a wrong verifier and assert failure, so the binding is enforced adds a measurable step to the hot path, so measure it before assuming it is free. A reasonable approach is to validate once at the edge, fail closed with a structured error, and log the rejection reason so a spike in bad input is visible on a dashboard rather than silent. Pair the implementation with a test that asserts a valid payload passes and at least one representative violation is rejected with the expected message; this is the cheapest way to keep the guarantee honest as the schema evolves.
Sources
Sources: RFC 7636 PKCE, OAuth 2.1 draft.
FAQ
- Is PKCE only for SPAs?
- It started there, but it is now recommended for all clients including confidential ones, because it adds a per-request binding that protects the code even if the redirect is intercepted.
- Why SHA256 and not plain challenge?
- S256 is the standard method; plain sends the verifier as the challenge, which only protects against passive eavesdropping, so use S256 unless the client cannot hash.
Report a problem with this articleOpens a pre-filled issue on GitHub.