PKCE in OAuth 2.0: the verifier, the S256 challenge and the exchange
PKCE, pronounced pixy, is an extension to the OAuth 2.0 authorization-code flow defined in RFC 7636. It protects against an attacker who intercepts an authorization code and tries to exchange it for tokens. The client creates a random secret called the code verifier, sends a derived challenge with the authorization request, and later proves possession of the verifier at the token endpoint. A stolen code is useless without the verifier.
The flow has four steps. The client generates the verifier and challenge and stores the verifier locally. It sends the challenge and the method S256 in the authorization request. The user authenticates and the authorization server redirects back with a code. The client posts the code and the original verifier to the token endpoint; the server hashes the verifier, compares it with the stored challenge, and issues tokens only if they match.
Building the verifier and challenge
A verifier is 43 to 128 characters from the unreserved set: uppercase and lowercase letters, digits, and the four characters -, ., _, ~. The simplest safe construction is 32 random bytes from a cryptographic generator, base64url encoded without padding, which yields 43 characters. Do not build it from a timestamp, a user ID or a counter; it must be unguessable.
The S256 challenge is BASE64URL(SHA256(ASCII(verifier))) with no padding. Hash the verifier as ASCII bytes, not as UTF-8 after some transformation, and encode the 32-byte digest with the URL-safe alphabet. The result is always 43 characters. Sending the verifier itself as the challenge is the plain method, which provides no protection if the authorization request leaks.
| Parameter | Where it goes | Value | Checked by |
|---|---|---|---|
| code_challenge | Authorization request | 43-char base64url SHA-256 | Authorization server |
| code_challenge_method | Authorization request | S256 | Authorization server |
| code_verifier | Token request | 43-128 unreserved chars | Authorization server |
| state | Both requests | Random, stored with the session | Client |
| nonce | Authorization request and ID token | Random, stored with the session | Client |
State and nonce are separate jobs
state protects the redirect against cross-site request forgery and against mixing up two flows. The client stores it with the session before redirecting and compares it exactly at the callback. nonce is an OpenID Connect parameter that binds an ID token to the authentication request; the client sends it in the request and checks that the returned ID token contains the same value. PKCE protects the code exchange; state and nonce protect the redirect and the identity response. They are not substitutes.
All three should be generated from a cryptographic random source and stored server-side or in a secure, HttpOnly session cookie. Putting the verifier in the URL, in localStorage readable by scripts, or in a cookie without protection weakens the binding. If an attacker who intercepts the code can also read the verifier from the same place, PKCE has done nothing.
The token exchange
The client posts to the token endpoint with grant_type=authorization_code, the code, the redirect URI, the client identifier, and code_verifier. A confidential client also authenticates with its secret or a private key JWT. The authorization server computes the challenge from the verifier, compares it with the value stored when the code was issued, checks that the code is unused and unexpired, and returns the token response. A mismatch is an invalid_grant error.
Codes must be single-use and short-lived. If a code is replayed, the authorization server should revoke the tokens issued from it, because replay suggests interception. The redirect URI must match exactly one registered value, not a prefix, or an attacker can redirect the code to their own host. These checks belong on the server; a client-side PKCE implementation cannot enforce them.
Errors that look like PKCE failures
The most common is a verifier that changed between the authorization request and the token request, for example because a new session generated a new value or because the request was retried on another instance without shared state. The second is an encoding difference: base64 with padding, standard base64 instead of base64url, or a hash of UTF-8 bytes that differ from ASCII. The third is a challenge computed over the wrong string, such as a verifier with a trailing newline.
A fourth is using plain on one side and S256 on the other, which the server should reject as a method mismatch. Log the method and a hash of the challenge, never the verifier itself, when diagnosing. The generator on this page computes S256 in the browser and shows both values, which is useful for checking an implementation against a known input.
PKCE and JWT
PKCE itself does not issue JWTs. It protects the code exchange; the tokens returned may be opaque or JWTs depending on the authorization server. When they are JWTs, the access token is for the resource server and the ID token is for the client, with different audiences and validation rules. PKCE is the part of the flow that makes the code safe to receive in a browser or a mobile app.
For a JavaScript single-page application, the authorization-code flow with PKCE is the recommended pattern. The older implicit flow returns tokens directly in the redirect fragment, where they are more exposed and cannot be bound to a verifier. If you maintain an implicit-flow client, migrating to code plus PKCE is one of the highest-value changes available.
The verifier is a secret for the duration of one flow. Generate it per authorization request, never reuse it, and discard it after the token response.
Sources
- RFC 7636 — Proof Key for Code Exchange by OAuth Public Clients
- RFC 9700 — Best Current Practice for OAuth 2.0 Security
- OpenID Connect Core — nonce
Questions
Is PKCE only for mobile apps?
No. RFC 7636 was written for public clients that cannot keep a secret, but the current guidance recommends PKCE for confidential clients too. It binds the authorization code to the client instance that started the flow, which limits code interception and injection even when a client secret exists.
What is the difference between S256 and plain?
S256 sends a SHA-256 hash of the verifier as the challenge; plain sends the verifier itself. Plain offers no protection if the authorization request is observed. S256 is the required method in current practice, and plain should be used only for a legacy server that cannot compute SHA-256.
How long is a code verifier?
Between 43 and 128 characters from the unreserved set A–Z, a–z, 0–9 and the characters - . _ ~. A 32-byte random value base64url encoded gives 43 characters, which is the common minimum. Shorter or longer values must be rejected by the authorization server.
Generate a verifier and S256 challenge, then copy them into your authorization request.
Open the verifierWritten against the JOSE specifications (RFC 7515–7519), RFC 7636 and RFC 8037; every example can be reproduced in the verifier linked above.
Back to the guides