PKCE (Proof Key for Code Exchange)
What is PKCE (Proof Key for Code Exchange)?
PKCE (Proof Key for Code Exchange)An OAuth 2.0 extension (RFC 7636) that binds an authorization-code redemption to a one-time secret created by the client, neutralizing authorization-code interception attacks on public and confidential clients alike.
PKCE (Proof Key for Code Exchange, RFC 7636, pronounced "pixy") is an OAuth 2.0 extension originally introduced to protect public clients — mobile apps, single-page apps, native CLIs — from authorization-code interception, and now mandated for every OAuth client by the OAuth 2.1 draft. The flow adds two parameters. At the authorization request the client generates a high-entropy random code_verifier (43–128 characters), hashes it with SHA-256 into a code_challenge, and sends only the challenge plus code_challenge_method=S256. When the client later redeems the returned authorization code, it presents the original code_verifier; the server re-hashes it and rejects the exchange unless the digest matches the challenge it stored. Because the verifier never travels over the front channel, an attacker who captures the redirect — via a malicious app claiming the same custom URI scheme, a Referer leak, or a logging proxy — holds a code they cannot redeem.
The original attack PKCE defeats is the authorization-code injection / interception attack on mobile OS custom-scheme handlers, which any sibling app could register. Weaknesses remain: the plain challenge method offers no protection and should be disabled, and a PKCE downgrade attack is possible if the server lets a client omit the challenge or silently falls back to plain, so servers must bind the method per request. Modern profiles (FAPI 2.0, Microsoft Entra ID, Okta, Auth0) now require S256 for confidential clients too.
flowchart TD
A[Client: generate random code_verifier] --> B[Derive code_challenge = SHA-256 and base64url]
B --> C[Authorize request with code_challenge + S256]
C --> D[Auth server stores challenge, user authenticates]
D --> E[Redirect back with authorization code]
E -. intercepted by malicious app .-> X[Attacker holds code only]
E --> F[Client: token request with code + code_verifier]
X --> Y[Token request WITHOUT verifier]
F --> G{SHA-256 of verifier == stored challenge?}
Y --> G
G -- match --> H[Issue access + ID tokens]
G -- mismatch/missing --> I[Reject: invalid_grant]● Examples
- 01
A React SPA initiates the OAuth code flow with a SHA-256 `code_challenge`, then exchanges the returned code plus `code_verifier` for tokens from the IdP.
- 02
An IdP rejects an authorization-code grant from a public client that arrived without a matching PKCE verifier, treating it as a sign of interception.
● Frequently asked questions
What is PKCE (Proof Key for Code Exchange)?
An OAuth 2.0 extension (RFC 7636) that binds an authorization-code redemption to a one-time secret created by the client, neutralizing authorization-code interception attacks on public and confidential clients alike. It belongs to the Identity & Access category of cybersecurity.
What does PKCE (Proof Key for Code Exchange) mean?
An OAuth 2.0 extension (RFC 7636) that binds an authorization-code redemption to a one-time secret created by the client, neutralizing authorization-code interception attacks on public and confidential clients alike.
How do you defend against PKCE (Proof Key for Code Exchange)?
Defences for PKCE (Proof Key for Code Exchange) typically combine technical controls and operational practices, as detailed in the full definition above.
What are other names for PKCE (Proof Key for Code Exchange)?
Common alternative names include: RFC 7636, Proof Key for Code Exchange.