What PKCE is and why it exists
PKCE stands for Proof Key for Code Exchange. It is defined in RFC 7636 and is pronounced "pixy". It adds one secret to the authorization code flow: a random string that only the app knows, called the code verifier.
The problem it solves is a stolen authorization code. In the code flow, the code travels through the browser in the redirect URL. On a phone, another app can register the same custom URL scheme and receive it. On the web, a code can leak through logs, a referrer header or a malicious script. Without PKCE, whoever holds the code can redeem it at the token endpoint, as long as the client has no secret.
With PKCE, the code alone is useless. The token endpoint only hands out tokens to the party that can show the original verifier, and the verifier never left the app.
How PKCE works, step by step
- The app makes a random
code_verifierof 43 to 128 characters and keeps it. - It hashes the verifier with SHA-256 and encodes the hash as Base64url without padding. That is the
code_challenge. - The authorization request carries
code_challengeandcode_challenge_method=S256. - The server stores the challenge with the authorization code it issues.
- The app sends the code and the original
code_verifierto the token endpoint. - The server hashes the verifier, compares it with the stored challenge, and issues tokens only if they match.
The formula from RFC 7636 section 4.2 is code_challenge = BASE64URL-ENCODE(SHA256(ASCII(code_verifier))). The test vector in the RFC’s Appendix B is a good way to check any implementation.
code_verifier = dBjftJeZ4CVP-mB92K27uhbUJU1p1r_wW1gFWFOEjXk
code_challenge = E9Melhoa2OwvFrEMTJguCHaoeK1t8URWbuGJSstw-cMGET /realms/acme/protocol/openid-connect/auth
?response_type=code&client_id=web-app
&code_challenge=E9Melhoa2OwvFrEMTJguCHaoeK1t8URWbuGJSstw-cM
&code_challenge_method=S256&...
POST /realms/acme/protocol/openid-connect/token
grant_type=authorization_code&code=SAMPLE_CODE
&code_verifier=dBjftJeZ4CVP-mB92K27uhbUJU1p1r_wW1gFWFOEjXk&...What this tool does
The generator makes a code verifier in your browser with Web Crypto and computes its challenge. Nothing is sent to a server.
- It picks characters from the unreserved set RFC 7636 allows: letters, digits and
-,.,_,~. - It avoids modulo bias, so every character is equally likely.
- It lets you choose a length of 43, 64, 96 or 128 characters.
- It computes the S256 challenge, or shows the plain method with a warning.
- It accepts a verifier you paste and checks its length and characters.
- It loads the RFC 7636 example and confirms the result matches Appendix B.
It also prints the two snippets you need: the parameters for the authorization request and the one for the token request.
What it does not do
It does not store the verifier, send a request, or talk to your server. Your app must keep the verifier between the redirect and the callback. For a full authorization URL with PKCE already built in, use the authorization URL builder.
How to use the generator
- Open the page. A new 64-character verifier is ready.
- Pick a different length if your library expects one, or press Generate new.
- Leave the method on S256.
- Copy the authorization request parameters into your test URL.
- Keep the verifier and send it with the code to the token endpoint.
To test your own code, paste the verifier your app made. If the challenge here differs from the one your app sent, your encoding is wrong. The usual causes are standard Base64 instead of Base64url, or padding left on the end.
Where PKCE is used
RFC 9700, the OAuth 2.0 security best current practice, recommends PKCE for every client that uses the authorization code flow.
Single-page apps
A browser app cannot keep a client secret, so it is a public client. PKCE is what ties the code to the app that started the sign-in. Keep the verifier in memory or session storage for the length of the redirect only.
Mobile and desktop apps
RFC 8252 asks native apps to sign in through the system browser and to use PKCE. Custom URL schemes can be claimed by other apps, so the verifier is the real protection.
Server-side web apps
A confidential client has a secret, but PKCE still helps. It stops an attacker from injecting a stolen code into a victim’s session, which a client secret does not prevent.
Command-line tools
A CLI that opens the browser and listens on a loopback port uses the same pattern as a desktop app. Where no browser is available, the device flow fits better.
How Keycloak handles PKCE
Keycloak supports PKCE on every OpenID Connect client. If the authorization request carries a code_challenge, Keycloak checks the code_verifier when the code is redeemed.
A client can also require PKCE. Set its PKCE method to S256 in the client settings. Keycloak then rejects authorization requests without a challenge, with the error description Missing parameter: code_challenge_method.
The endpoints are https://<host>/realms/<realm>/protocol/openid-connect/auth for the challenge and https://<host>/realms/<realm>/protocol/openid-connect/token for the verifier.
Common PKCE errors and fixes
| Symptom | Likely cause | Fix |
|---|---|---|
invalid_grant at the token endpoint | The verifier does not hash to the stored challenge | Send the same verifier you hashed; check Base64url and no padding |
invalid_request on the authorization request | The client requires PKCE and no challenge was sent | Add code_challenge and code_challenge_method=S256 |
| Code works once, then fails | Codes are single-use | Start a new authorization request |
| Verifier rejected as invalid | Too short, too long, or a forbidden character | Use 43 to 128 unreserved characters |
A frequent bug is a new verifier on every page load. The app makes a verifier, redirects, and on return makes another one. Store the verifier before the redirect and read it back on the callback.
S256 or plain
RFC 7636 defines two methods. With S256 the challenge is a hash. With plain the challenge is the verifier itself.
Plain gives no protection when the authorization request can be seen, which is exactly the case PKCE is meant for. The RFC allows plain only when a client cannot compute SHA-256. Every current browser and platform can, so use S256.
Making PKCE values in your own code
Most OpenID Connect libraries make the verifier and challenge for you. If you write it yourself, the browser and Node.js both have what you need. The sketch below is a sample, not a drop-in library.
const bytes = crypto.getRandomValues(new Uint8Array(32));
const verifier = base64url(bytes); // 43 characters
const hash = await crypto.subtle.digest('SHA-256', new TextEncoder().encode(verifier));
const challenge = base64url(new Uint8Array(hash));The base64url helper must replace + with - and / with _, and drop any = padding. Thirty-two random bytes give a 43-character verifier, the shortest RFC 7636 allows, with 256 bits of randomness.
Test the helper against the Appendix B example before you ship it. If your result for the example verifier is not E9Melhoa2OwvFrEMTJguCHaoeK1t8URWbuGJSstw-cM, the encoding is off. A wrong helper fails only at the token endpoint, where the error says little more than invalid_grant.
Security notes
- Make the verifier with a cryptographic random source, such as
crypto.getRandomValues, neverMath.random. - Use a new verifier for every authorization request.
- Never put the verifier in the authorization URL or a log.
- PKCE does not replace
statefor your own session logic, ornoncefor ID tokens. See the state and nonce generator. - PKCE protects the code, not the tokens. Treat tokens as secrets once issued.