Beta

Create a tenant

A new tenant starts with its own users, OAuth settings, audit history and logs. You are its first Tenant Admin.

Proof Key for Code Exchange (PKCE)

PKCE ties an authorization code to the attempt that asked for it. The printer makes a secret for Ava's attempt, sends only its hash through the browser, and shows the secret itself when it redeems the code, so a code that turns up anywhere else cannot be redeemed.

Preparing a verifier and challenge

Suppose someone obtains the authorization code while it is returning to the printer. Should possessing that code be enough to exchange it for an access token?

It should not be. Public and confidential clients introduced PKCE, a secret the client creates for one authorization attempt so that only the client instance holding it can redeem the code. The printer prepares that secret before sending Ava's browser away, and the authorization server checks it when the code comes back.

The secret is called the code_verifier. A real verifier is made from cryptographically secure randomness and is between 43 and 128 characters long, using letters, digits and the characters -, ., _ and ~. Thirty-two random bytes in base64url make 43 characters, which is how most libraries produce one.

For teaching only, this lesson uses a predictable value so the calculation can be repeated:

code_verifier:
btl_training_verifier_for_one_attempt_only_2026

code_challenge:
qd-75t-gnweZAhIl6REjxxgFnPXGwvqYcxq3vlsaJYQ

code_challenge_method:
S256

With S256, the client takes the verifier's characters as bytes, hashes them with SHA-256 and writes the 32-byte digest in base64url without the trailing = padding, the steps from Encoding and hashing:

challenge = BASE64URL_WITHOUT_PADDING(SHA256(ASCII(verifier)))

This is a hash transformation, not encryption. The server never recovers the verifier from the challenge; it can only hash a verifier it is given and compare the results.

Hashing does not rescue a predictable verifier. If an application uses a short counter, a timestamp or the same value for every request, anyone who sees the challenge can guess candidate verifiers, hash each one and compare. Generate a new cryptographically random value for each attempt, keep it with that attempt's state and deadline in the pending transaction, and send the original value only in the token request.

Proving possession later

The client sends the challenge and method in the authorization request, where they sit in plain view in the address bar. It keeps the verifier with the pending transaction.

When the authorization server issues a code, it records the challenge with that code. Later, the client sends the code and the verifier to the token endpoint, in the direct back-channel request from Why use the authorization code flow?. The server applies the same transformation to the verifier it received and compares the result with the challenge it recorded for the code.

A wrong verifier, or none at all, makes the exchange fail. Copying the visible challenge does not help anyone who wants to redeem the code, because the token endpoint asks for the verifier that produces it.

The client sends the challenge first and keeps the verifier private. At the token endpoint, the server transforms the supplied verifier in the same way and compares it with the challenge bound to the code.

Understanding what the proof means

The verifier belongs to one attempt. It is not the printer's long-term client secret and does not establish the client's registered identity. Our confidential backend authenticates itself separately when making the token request.

For a confidential client like the printer, the benefit is easy to miss. A stolen code is not much use at the token endpoint without the printer's client authentication, but someone with Ava's code can start a connection of their own on the printer's website, plant her code in their own callback, and let the genuine printer redeem it for them. The printer would then attach Ava's photos to their account. This is authorization code injection. PKCE stops it because the printer redeems the code with the verifier saved for the attempt that received the callback, their attempt, while Ava's code is bound to the challenge from her attempt, so the exchange fails. Code interception and injection follows that attack, and the interception of codes on the way back, step by step.

Use PKCE with S256 in the flow taught here. Current OAuth security guidance, RFC 9700, requires PKCE for public clients, recommends it for confidential clients as well, and requires every authorization server to support it. OAuth 2.1 goes further and requires PKCE for every client that uses the authorization code grant, with only a narrow exception for clients the server knows use another protection with the same effect.

PKCE is not a complete defense against every form of credential theft. If an attacker compromises the client and obtains both the code and the verifier, this check cannot distinguish their request from the client's. It also does not protect an access token after that token has been issued. Token theft and replay covers access tokens that escape after they are issued.

PKCE binds code redemption, and nothing more. It does not grant the printer permission, check a token or encrypt anything. state and the pending attempt stored for this browser decide whether the callback is accepted, the expected issuer keeps a code from being sent to the wrong authorization server, and token and session checks have their own jobs.

Check your understanding

Try these questions before moving on. If an answer isn't right, use the feedback and try again.

Someone starts their own connection on the printer's website and plants a code stolen from Ava in their callback. The printer's backend authenticates correctly at the token endpoint. What makes the exchange fail? 1 of 3

Choose one answer.

Does using PKCE remove this confidential client's need to authenticate at the token endpoint? 2 of 3

Choose one answer.

A developer makes each PKCE verifier from the current time in seconds, reasoning that only its SHA-256 hash appears in the address. Is that safe? 3 of 3

Choose one answer.

Try it in the Lab

Try a Capture the Flag challenge

Apply this lesson against an isolated, intentionally vulnerable application. Capture a flag, explain the mistake, then repair and retest it.

  1. A Code Without Its Verifier Difficulty 3 of 5 · About 35 minutes · API target

We value your privacy

We use cookies and similar technologies to enhance your browsing experience, and analytics to understand our traffic. By clicking "Allow all", you consent to optional analytics. Cookie Policy

Learn identity