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 hashTurning data into a short, fixed-size value called a hash, in a way that cannot be reversed and where changing one character changes the result completely. Services store password hashes instead of passwords, and signatures are made over a 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 tokenA token that a client presents to an API to use access it has been granted, usually limited in what it allows and how long it lasts. It authorizes API calls; it does not tell the client who signed in.?
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 serverIn OAuth, the service that handles a person's sign-in and approval and decides which tokens to issue to a client. It may belong to the same company as the API, but its job is separate. 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 base64urlA way of encoding data as ordinary text characters so it can travel safely in headers, URLs, and JSON. Anyone can decode it, so it hides nothing; Base64url is a variant safe for URLs, used in JWTs. 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:
S256With 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 encryptionTransforming data so that only someone holding the right key can read it. It protects confidentiality and, unlike hashing, is designed to be reversed by the key holder.. 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 tokenA piece of data issued by a trusted service that a client presents as a credential, for example to call an API or to report a sign-in. Some tokens carry readable claims; others are random strings that only the issuer can interpret. 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 endpointThe authorization server address where clients exchange an authorization code, refresh token, or other grant for tokens, in a direct request rather than through a browser redirect., 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.
Understanding what the proof means
The verifier belongs to one attempt. It is not the printer's long-term client secretA shared secret that a confidential client uses to prove itself to the authorization server, much like a password for an application. It belongs on a backend, never in code sent to browsers or phones. and does not establish the client's registered identity. Our confidential backend authenticates itself separately when making the token request.
For a confidential clientAn OAuth client, typically running on a backend, that can keep a credential such as a client secret or private key protected, and so can authenticate itself to the authorization server. 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 authenticationChecking evidence that someone, or some application, controls an account, such as a password or a passkey. It establishes which account is being used, not what that account may do., 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 OAuthA framework that lets a person give an application limited access to their resources at another service, using tokens instead of sharing their password. OAuth is about access; OpenID Connect adds sign-in. 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 grantThe main OAuth flow: the browser visits the authorization server, where the person signs in and approves access, then carries an authorization code back to the client, which exchanges it for tokens directly. When the client runs on a server, the tokens never pass through the browser., 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.
Try it in the Lab
- Compute, send and prove a PKCE verifierReproduce the lesson's challenge calculation, generate a real verifier, and see Lab Photos' token endpoint refuse every verifier that does not match the challenge bound to Ava's code.
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.