PKCE: protect OAuth code exchange with a verifier and S256
Learn how PKCE binds an authorization code to its client, how to create the verifier and S256 challenge, and what to verify in implementation.
PKCE: protect OAuth code exchange with a verifier and S256
PKCE (Proof Key for Code Exchange) binds an authorization code in the OAuth Authorization Code flow to the client that started the request. For each sign-in request, the client creates a secret code_verifier and sends a derived code_challenge in the authorization request. It presents the original verifier when exchanging the code, making it difficult for another app or attacker with only an intercepted code to obtain a token. RFC 9700 requires PKCE for public clients and recommends it for confidential clients when possible.
What problem does PKCE prevent?
When an authorization code returns to an app through a browser and operating system—for example, using a mobile app custom redirect URI—another app may register the same URI and intercept the code. PKCE adds a check so the code alone cannot be exchanged for a token. A fixed client_secret embedded in an app binary is not a secret: users can extract it.
PKCE is not a universal defense for user passwords or malicious code inside an app. Theft of a token after it is issued requires separate token protection and safe storage.
Create a verifier and challenge for each request
A code_verifier is an unpredictable random string used for one authorization request only. RFC 7636 requires a length of 43–128 permitted characters. With S256, calculate the challenge as follows:
code_challenge = BASE64URL-ENCODE(SHA256(ASCII(code_verifier)))For Base64url encoding, replace + with -, / with _, and remove trailing = padding. Even when using the standard browser crypto.subtle.digest API, it is safer to rely on a reviewed OAuth library for secure random generation and string conversion. Do not reuse a simple example string as a verifier or print it to logs.
Appendix B of RFC 7636 provides a public example for checking the calculation. These values are a public test vector and must not be used for a real sign-in:
code_verifier = dBjftJeZ4CVP-mB92K27uhbUJU1p1r_wW1gFWFOEjXk
code_challenge = E9Melhoa2OwvFrEMTJguCHaoeK1t8URWbuGJSstw-cM
method = S256Authorization and token exchange
The app creates a new verifier with a secure random generator. Do not share it with another sign-in request, even in the same browser or app.
Calculate an `S256` challenge from the verifier.
Send the browser to the authorization endpoint with values such as `response_type=code`, `code_challenge`, and `code_challenge_method=S256`. Keep the verifier only in the app while the user authenticates and consents.
When the authorization server returns a code to the callback, send that code to the token endpoint with the verifier saved for that specific request.
The authorization server recalculates the challenge from the verifier and compares it with the original. It issues tokens if they match and must reject the exchange if they do not.
RFC 9700 requires authorization servers to support PKCE and public clients to use it. S256 is a better default than plain because it does not reveal the verifier in the authorization request. A server must not allow a client to bypass PKCE by accepting a verifier in the token request when the corresponding authorization request had no challenge.
Implementation checklist
| Check | Expected behavior |
|---|---|
| Verifier generation | Generate a new value with a secure random generator for each request and meet the required format and length. |
| Challenge method | Use code_challenge_method=S256. |
| Verifier storage | Associate it only with the current sign-in transaction; do not mix it with values from another tab or request. |
| Callback handling | Validate state and the redirect, then exchange only the code and verifier for that transaction. |
| Server support | Confirm the authorization server supports PKCE and enforces the S256 challenge and verifier. |
| Error handling | If the verifier is missing or mismatched, no token is issued; do not log sensitive values. |
In a test environment, deliberately send an incorrect verifier and confirm the server rejects it. Do not use real account passwords or production tokens in tests. Do not break a production authorization flow just to check PKCE support; use the provider's official documentation or sandbox.
Key takeaway
PKCE requires proof of the verifier during authorization-code exchange, making a stolen code alone insufficient to obtain a token. Create a fresh verifier for every request, derive the challenge with S256, and use the verifier only in the callback for its matching request. PKCE is one layer of OAuth protection; also validate redirect URIs and state, and secure the tokens.
References
[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 — PKCE Downgrade Attack](https://www.rfc-editor.org/rfc/rfc9700.html#section-4.8)