# Proof Key for Code Exchange (PKCE) explained: a stolen login code is useless

> PKCE, or Proof Key for Code Exchange, makes a stolen login code useless: the app proves it started the login with a secret only it holds. How it works.

- URL: https://iamops.dev/pkce-explained/
- Language: en
- Author: Argo
- Published: 2026-12-04
- Category: Knowledge
- Depth: Explainer
- Audience: Security leaders and architects
- Time: 5 min
- Topics: PKCE, OAuth, Basics, Login

## TL;DR

- A [login code that someone intercepts](https://iamops.dev/pkce-explained/#the-problem-a-code-on-the-road) is useless without the secret the app kept back.
- The app sends a hash of its secret when the login starts, and [the secret itself](https://iamops.dev/pkce-explained/#the-two-requests) only when it asks for tokens.
- The server [compares the two](https://iamops.dev/pkce-explained/#the-check-at-the-token-endpoint) and issues tokens only when they match.
- The check suits apps that cannot keep a secret, such as [phone apps and tools on a laptop](https://iamops.dev/pkce-explained/#where-this-is-used).

**Audience: Beginners.** This is for anyone new to login protocols who wonders why a phone app or a command-line tool takes an extra step. Security leads can skip to the last section.

When you sign in through a website, the site does not give the app your password. It sends the app a short code, and the app trades that code for a token. The code travels through your browser, so anything on that path can read it. <abbr title="Proof Key for Code Exchange">PKCE</abbr> is the extra check that makes a copied code worthless. It is defined by the <abbr title="Internet Engineering Task Force">IETF</abbr>, and it sits inside the <abbr title="Open Authorization">OAuth</abbr> login flow.

## The problem: a code on the road

A web server can keep a secret, because only the server reads its configuration. A phone app or a desktop tool cannot. Anyone who installs the app can take it apart and read whatever is inside. So the usual secret check does not protect these apps.

The attack is simple. A second app on the same phone registers for the same redirect address, catches the code as it goes past, and trades it for tokens itself. The authorization server sees a valid code and a valid request, so it issues the tokens.

## Two strings: the verifier and the challenge

PKCE adds two values that the app makes up for each login. The first is the code verifier, a random string of 43 to 128 characters that the app keeps in memory and never sends at the start. The second is the code challenge, which is the verifier run through a <abbr title="Secure Hash Algorithm 256-bit">SHA-256</abbr> hash and encoded in a <abbr title="web address">URL</abbr>-safe form.

Because the hash cannot be reversed, the challenge can travel in the open. Anyone who sees it still does not know the verifier.

## The two requests

The login request carries the challenge. The token request carries the verifier. Between them, the code comes back through the browser, where an attacker could grab it.

![Three actors: a mobile app, the authorization server and an attacker. The app sends a code challenge, gets a code back, and gets tokens only with the verifier. The attacker's stolen code is refused.](https://iamops.dev/img/pkce-explained.svg)

*A stolen code meets a missing verifier. The attacker has the code but not the secret the app kept back.*

## A stolen code meets a missing verifier

The attacker takes the code and sends it to the token endpoint. The attacker has no verifier, so the request has nothing to match. The server refuses it, and the attacker ends with no tokens.

The app itself does not change how it works. It simply adds one value to each of its two requests.

## The check at the token endpoint

When the server issues the code, it stores the challenge next to it. When the app sends the code and the verifier, the server hashes the verifier and compares the result with the stored challenge. Only a match gets tokens. A failed match returns an error, not a token.

The <abbr title="Request for Comments">RFC</abbr> also allows a method called plain, where the challenge is the verifier itself. It exists for clients that cannot compute a hash. Use S256, the hashed method, whenever the app can, because plain sends the secret in the open on the first request.

## Where this is used

PKCE is for public clients: single-page web apps, phone apps, desktop apps and command-line tools. A tool that an agent runs on a laptop falls in this group, because the laptop belongs to someone who can read the tool's files.

A server that can keep a secret still uses a client secret. Many of those servers also add PKCE, because it costs little and closes the code-theft path.

For the flows that PKCE belongs to, read [OAuth 2.0 flows explained](https://iamops.dev/oauth-2-flows-explained/). For what the code turns into, read [access, refresh and <abbr title="identifier">ID</abbr> tokens](https://iamops.dev/access-refresh-id-tokens-explained/). A device with no browser uses a different route, covered in [the OAuth device flow](https://iamops.dev/oauth-device-flow-explained/).

## What to check

- The app uses S256, not plain.
- The verifier is random, long enough and never written to a log.
- The server rejects a token request whose verifier does not match the stored challenge.
- The redirect address is fixed, so no other app can register the same one.

## Sources

- [IETF, RFC 7636: Proof Key for Code Exchange by OAuth Public Clients (September 2015)](https://www.rfc-editor.org/rfc/rfc7636)
- [IETF, RFC 6749: The OAuth 2.0 Authorization Framework](https://www.rfc-editor.org/rfc/rfc6749)

Discuss: [X](https://x.com/iamopsdev) | [LinkedIn](https://www.linkedin.com/company/iamops-dev/)
