PKCE: bind the OAuth request to token exchange to stop code theft
Proof Key for Code Exchange (PKCE) ties an OAuth authorization-code request to a one-time secret created by the client. An intercepted code alone then cannot be exchanged for an access token. Public clients MUST use PKCE; confidential clients are RECOMMENDED to use it. New implementations should use S256, not plain, which exposes the verifier.
Proof Key for Code Exchange (PKCE) ties an OAuth authorization-code request to a one-time secret created by the client. An intercepted code alone then cannot be exchanged for an access token. Public clients MUST use PKCE; confidential clients are RECOMMENDED to use it. New implementations should use S256, not plain, which exposes the verifier.
What PKCE prevents
In the authorization-code flow, the authorization server returns a code to the client after user authorization. A malicious app may intercept a code delivered through a redirect path that multiple apps can handle, such as a mobile custom URI scheme, and try to redeem it. A fixed client secret embedded in a public app cannot be treated as confidential from its users.
For each authorization transaction, PKCE creates a fresh code_verifier and sends a derived code_challenge in the authorization request. When exchanging the code, the client sends the original verifier. The authorization server compares it with the earlier challenge. An interceptor with only the code cannot complete the exchange.
Implement the flow safely
1. Generate a cryptographically random code_verifier for this transaction only.
2. Compute code_challenge = BASE64URL(SHA256(ASCII(code_verifier))).
3. Send code_challenge and code_challenge_method=S256 in the authorization request.
4. Keep the returned authorization code; send it with the original code_verifier to the token endpoint.
5. The authorization server derives the challenge from the verifier and compares it before issuing tokens.Use a maintained OAuth library and follow RFC 7636’s verifier length, character, and Base64url rules. Do not use a constant, timestamp, user identifier, or weak random value. Store the verifier securely with its authorization transaction, discard it when no longer needed, and keep it out of logs, URLs, analytics, and error reports.
The authorization and token requests must belong to the same transaction. Apps may support concurrent sign-ins or tabs; a single global verifier can be overwritten or paired with the wrong code. Keep each verifier bound to its own state and redirect validation.
Choose S256 and prevent downgrade
With plain, the challenge equals the verifier, so an attacker who can observe the authorization request may learn the verifier. RFC 9700 recommends a method that does not expose it; S256 is currently the standardized method meeting that condition. Send code_challenge_method=S256 and do not silently fall back to plain.
Authorization servers MUST support PKCE and, when a code_challenge was sent, enforce the matching code_verifier at the token endpoint. They must also prevent downgrade by rejecting a token request with a verifier if the corresponding authorization request had no challenge. Where available, check code_challenge_methods_supported in authorization-server metadata and follow documented deployment checks.
PKCE does not replace exact redirect-URI validation, TLS, state or applicable CSRF protection, or secure token storage. These safeguards address different risks and should be used together.
Troubleshoot failures
**Verifier mismatch at token exchange:** confirm the original transaction’s verifier was sent and not changed by encoding, whitespace, or storage. Do not retry the same code with a new verifier; start a new authorization flow.
**Server only advertises `plain`:** verify support and policy with the provider and request a secure update. Do not weaken protection by blindly downgrading.
**Wrong failure after concurrent sign-ins:** verify that each request stores its own verifier and state.
**Library and server disagree:** review standards-compliant library settings rather than adding ad hoc transformations. Never log raw verifiers or tokens.
Before release
Is a fresh, unpredictable verifier generated for every authorization transaction?
Is only an `S256` challenge sent, keeping the original verifier out of the authorization request?
Is the verifier bound to the same code and state, without mixing concurrent flows?
Does the token endpoint validate the pair and reject PKCE downgrade?
Do public clients always use PKCE while redirect validation, TLS, and CSRF protections remain separately in place?
PKCE security depends on linking the request and token exchange with the same one-time verifier. Use strong randomness for each flow, choose S256, and ensure the server enforces the check.
Sources
[RFC 7636: Proof Key for Code Exchange by OAuth Public Clients](https://www.rfc-editor.org/rfc/rfc7636.html)
[RFC 9700 §2.1.1: Authorization Code Grant](https://www.rfc-editor.org/rfc/rfc9700.html#section-2.1.1)
[RFC 9700 §4.8.2: PKCE Downgrade Attack Countermeasures](https://www.rfc-editor.org/rfc/rfc9700.html#section-4.8.2)