OAuth 2.0 PKCE explained, step by step
Doha I. · Published · Lire en français

PKCE (Proof Key for Code Exchange, RFC 7636) makes the OAuth authorization code flow safe even if the code is intercepted. The client creates a random code verifier, sends its SHA-256 hash (the S256 code challenge) when the user starts signing in, and must present the original verifier to exchange the code for tokens. An attacker holding only the code cannot complete the exchange.
OAuth 2.0's authorization code flow sends a short-lived code back to your application after the user signs in. Your application then trades that code for tokens. The weak point is the code itself: if someone intercepts it (a malicious app on the same phone, a leaked log, a hijacked redirect), they could trade it first.
PKCE closes that gap.
The idea in one sentence
The client proves, at the token exchange, that it is the same client that started the sign-in, by revealing a secret that only it knows and that was committed to at the start.
How it works
- Create a code verifier. A random string of 43 to 128 characters, generated fresh for every sign-in attempt.
- Derive the code challenge. With the S256 method, the challenge is
BASE64URL(SHA256(code_verifier)). - Start the sign-in with
code_challengeandcode_challenge_method=S256in the authorization request, together withstateand, for OpenID Connect,nonce. - The provider stores the challenge with the authorization code it issues.
- Exchange the code at the token endpoint and include the original
code_verifier. - The provider hashes the verifier and compares it with the stored challenge. Only a match returns tokens.
An attacker who intercepted the code never saw the verifier, only its hash, and a hash cannot be reversed.
Example
# 1. Generate a verifier and its S256 challenge
VERIFIER=$(openssl rand -base64 48 | tr -d '=+/' | cut -c1-64)
CHALLENGE=$(printf '%s' "$VERIFIER" | openssl dgst -sha256 -binary | openssl base64 | tr '+/' '-_' | tr -d '=')
echo "$CHALLENGE"
The authorization request then carries code_challenge=$CHALLENGE&code_challenge_method=S256, and the token request carries code_verifier=$VERIFIER.
Who should use PKCE?
Everyone using the authorization code flow. PKCE was designed for mobile apps and single-page apps, which cannot keep a client secret, but current best practice (the OAuth 2.0 Security Best Current Practice and OAuth 2.1 drafts) recommends it for confidential server-side clients too. It costs almost nothing and blocks a whole class of attacks.
Common mistakes
- Using
plaininstead ofS256. Withplain, the challenge equals the verifier, so intercepting the request reveals it. Always use S256. - Reusing a verifier. Generate a new one for every attempt.
- Storing the verifier where scripts can read it. Keep it in server-side session state or, in a browser-only app, in memory for the duration of the flow.
- Skipping
stateandnonce. PKCE protects the code exchange;stateprotects against cross-site request forgery andnoncebinds the ID token to the request. Use all three. - Hand-rolling token validation. Use a maintained OpenID Connect library to check the ID token signature, issuer, audience and expiry.
PKCE in SyAuth
SyAuth's interactive sign-in uses the authorization code flow with S256 PKCE. The complete starter for Node.js, Next.js and Cloudflare Workers generates the verifier and challenge for you and validates the ID token, state and nonce before creating a session. See also the glossary entries for PKCE and the authorization code flow.