IAM glossary
What is PKCE?
Proof Key for Code Exchange
The app creates a secret (code_verifier), sends only its hash (code_challenge) at the start, and the secret when it redeems the code. A stolen code is useless without it. Use it in every Authorization Code flow.
How it works
PKCE protects the Authorization Code flow when the app cannot keep a secret, like mobile and single-page apps. Before the login the app creates a random string, the code_verifier, and sends only its SHA-256 hash, the code_challenge, in the authorization request.
When the app exchanges the code for tokens, it sends the original code_verifier. The server hashes it and compares it with the challenge it received earlier. An attacker who steals the code in the redirect does not have the verifier, so the code is useless to them. Today PKCE is recommended for every client, confidential ones included.
A simple example
A banking app generates code_verifier = dBjftJeZ4CVP... and sends code_challenge = E9Melhoa2Owv.... A malicious app on the same phone intercepts the redirect with the code and tries to exchange it, but cannot provide the verifier. The bank’s server refuses; only the real app, which kept the verifier in memory, gets the tokens.
Common mistakes
- Using the
plainmethod instead ofS256. - Reusing the same verifier for every login.
- Servers that accept the exchange without a verifier when a challenge was sent.
See it in action: steal the code
Its hash