RFC 7636 — Proof Key for Code Exchange (PKCE)

Implemented end-to-end (client + server, server enforcement landed in PR 297)

RFC 7636 — Proof Key for Code Exchange (PKCE)

Status: Standards Track
Published: 2015-09
L-tier / T-tier: L0 · T1 (prepare) + T5 (verify)
OneAuth coverage: Implemented end-to-end (client + server, server enforcement landed in PR 297)
See also: RFC_6749.md · RFC_oauth21.md · RFC_9700.md

In one sentence

PKCE proves that the client exchanging the authorization code is the same
client that requested it — closing the “what if someone steals my code
mid-flight” attack on public clients (and, in 2.1, all clients).

What problem does it solve?

Without PKCE With PKCE
A malicious mobile app registers the same redirect URI scheme as Spotify; Spotify’s code is delivered to the attacker; attacker swaps it for tokens at /token Attacker has the code but not the code_verifier; /token rejects with invalid_grant
Public clients (no secret) had no way to authenticate at /token — anyone with the code got the token Public clients prove possession via a fresh per-request secret (the verifier) that never crossed the wire
Confidential clients felt safe (they have a client secret) — but a leaked code + compromised redirect was still exploitable Defense-in-depth — even confidential clients use PKCE in OAuth 2.1 §4.1.1

PKCE is the cleanest piece of OAuth security work in the decade after 6749.
It’s one round of hashing and one extra parameter at each endpoint.

Relatable example

You install a third-party Reddit client. It uses Reddit’s OAuth. On Android,
any app can register itself as a handler for a custom URI scheme — so a
malicious app could in principle intercept the redirect_uri=myredditclient://callback
that Reddit redirects to.

Without PKCE: malicious app catches the code, runs to Reddit’s /token
with it. Reddit hands over tokens (no client secret needed for public mobile
clients). Account compromised.

With PKCE: the real Reddit client generated a random code_verifier at
launch, kept it in memory, and sent only code_challenge = SHA256(verifier)
to /authorize. When the malicious app reaches /token with the
intercepted code, it doesn’t know the verifier. Reddit’s /token computes
SHA256(provided_verifier) and compares against the stored challenge —
mismatch → invalid_grant → attacker gets nothing.

The defense costs the legitimate client a single SHA-256 hash; it costs the
attacker the whole account.

Key concepts

  1. One-time secret per auth request. Client picks a high-entropy random
    code_verifier (43–128 bytes, [A-Z][a-z][0-9]-._~). Throws it away
    after one use.
  2. Send the hash, not the secret. code_challenge = BASE64URL(SHA256(verifier)).
    This is sent on /authorize. The verifier itself only crosses the wire on
    the (TLS-encrypted) /token POST.
  3. Two transformation methods — only one survives. plain (challenge == verifier)
    and S256 (challenge == SHA256(verifier)). OAuth 2.1 §4.1.1 rejects
    plain and so does OneAuth’s /authorize.
  4. Verification is at /token. AS stores (code, code_challenge, method)
    when issuing the code. At exchange, AS computes transformation(provided_verifier)
    and compares to stored challenge. Mismatch → invalid_grant.
  5. PKCE replaces client authentication for public clients and defends
    confidential clients against code injection — that’s why 2.1 makes it
    universal, not “public-only” as 7636 originally suggested.

Wire example

T1 — client prepares. In memory:

verifier  = "dBjftJeZ4CVP-mB92K27uhbUJU1p1r_wW1gFWFOEjXk"   # 43 chars, random
challenge = BASE64URL(SHA256(verifier))
          = "E9Melhoa2OwvFrEMTJguCHaoeK1t8URWbuGJSstw-cM"

T2 — /authorize with the challenge:

GET /authorize?response_type=code
              &client_id=public-client-1
              &redirect_uri=https%3A%2F%2Fapp.example.com%2Fcb
              &code_challenge=E9Melhoa2OwvFrEMTJguCHaoeK1t8URWbuGJSstw-cM
              &code_challenge_method=S256
              &state=xyz HTTP/1.1
Host: as.example.com

T5 — /token with the verifier:

POST /api/token HTTP/1.1
Host: as.example.com
Content-Type: application/x-www-form-urlencoded

grant_type=authorization_code
&code=SplxlOBeZQQYbYS6WxSbIA
&redirect_uri=https%3A%2F%2Fapp.example.com%2Fcb
&client_id=public-client-1
&code_verifier=dBjftJeZ4CVP-mB92K27uhbUJU1p1r_wW1gFWFOEjXk

The AS pulls the stored code_challenge for this code, computes
SHA256(code_verifier), compares — pass → issue tokens; fail →
{"error":"invalid_grant"}.

Where it lands on the OAuth abstraction

  • L0 spine. Originally a 7636 add-on; OAuth 2.1 §4.1.1 makes it part of
    the spine.
  • T-tier: T1 (prepare) and T5 (verify). The challenge ships on the
    outbound /authorize request (T1); the verifier closes the loop on the
    inbound /token POST (T5). Nothing about T2–T4 changes.
  • PKCE is the cheapest of the L0 defenses: ~30 lines of code on each side.

OAuth 2.0 status

7636 was published 2015 as a SHOULD for public clients. Under bare 2.0

  • 7636, PKCE is recommended for public (browser, mobile) clients, not
    required for anyone
    , and both plain and S256 are conformant
    challenge methods
    . Confidential clients with a server-side secret were
    considered to not need PKCE under the original 7636 framing.

The wider 2.0 ecosystem has tightened this over time:

  • RFC 9700 (the BCP) recommends PKCE for all clients
  • Most major IdPs require PKCE for public clients today, with S256 only
  • Some require it for confidential clients too as part of their security
    profiles
  • But this is “vendor convention,” not “spec mandate” — under bare 2.0,
    a deployment that allows plain or skips PKCE for confidential clients
    is still 2.0-conformant

OAuth 2.1 status

OAuth 2.1 §4.1.1 promotes PKCE from SHOULD to MUST for every
authorization_code exchange — public and confidential clients alike
.
It also rejects the plain method outright; only S256 is conformant
under 2.1.

This is the cleanest 2.0 → 2.1 tightening: same protocol surface, stricter
defaults.

OneAuth status

/authorize requires an S256 code_challenge from every client by default
(apiauth/authorize.go validatePKCE), and the code grant verifies the
code_verifier (apiauth/authorize_granter.go checkPKCE).
OneAuthConfig.PKCEPolicy = PKCEOptionalForConfidential lets a client
registered with a confidential token_endpoint_auth_method omit PKCE, as
OpenID Connect Core and bare OAuth 2.0 allow. Its code then redeems only for
a client that authenticated, and a code_verifier sent for it is refused as
a downgrade attempt (RFC 9700 §2.1.1). FAPI2 refuses the setting.

Migration path

Most modern deployments are already most of the way there. The remaining
work, in order of difficulty:

  1. Client SDKs (easy): ship a code_verifier + code_challenge even
    if your AS doesn’t require it. ASes ignore unknown params. The cost is
    one SHA-256 per login. This is what OneAuth’s client SDK does
    (client/browser_login.go buildAuthorizationURL).
  2. AS (medium): add code_challenge storage to authorization-code
    records, require code_challenge_method=S256 at /authorize, verify
    code_verifier at /token. This is what OneAuth’s /authorize
    endpoint does (apiauth/authorize.go AuthorizationHandler.ValidateValues,
    apiauth/authorize_granter.go AuthorizationCodeGrant).
  3. Legacy client tail (hard): older clients that didn’t ship PKCE need
    to update. Run a deprecation window with logging — fail-soft (accept
    PKCE-absent flows with a warning) → fail-hard (reject) once usage
    drops. Most IdPs are running this exact window today.
  4. Confidential clients (controversial): some operators argue
    confidential clients with mTLS / private_key_jwt don’t need PKCE. 2.1
    disagrees; the BCP §2.1.1 disagrees. The pragmatic answer is “add it
    anyway, it’s one SHA-256.”

When NOT to use it

In 2025, you shouldn’t not use it on a new deployment. The classic
exception — first-party desktop clients with hardcoded redirects on a
trusted OS — has gotten brittle as native-app guidance (RFC 8252) has
hardened. And under 2.1 the exception goes away entirely. The only
reason to skip PKCE today is interop with an AS that doesn’t support it
(and most do).

OneAuth status

7636 surface Status Where (grep-verified) Notes
Client-side: compute challenge from verifier (§4.2) Implemented client/browser_login.go buildAuthorizationURL sets code_challenge + code_challenge_method=S256 Uses a local computeCodeChallenge (client/browser_login.go, called from LoginWithBrowser), not the core helper
Client-side: send verifier on token exchange (§4.5) Implemented client/browser_login.go exchangeCode sets code_verifier in POST Round-trip exercised by apiauth/authorize_grant_test.go
Client-side: refuse AS that doesn’t advertise S256 (§4.2) Implemented client/browser_login.go LoginWithBrowser reads code_challenge_methods_supported from discovery Hard-fails on missing S256
Server-side: require code_challenge on /authorize Implemented apiauth/authorize.go AuthorizationHandler.ValidateValues rejects missing invalid_request returned
Server-side: require explicit code_challenge_method Implemented apiauth/authorize.go AuthorizationHandler.ValidateValues rejects missing Explicit “no plain default”. plain would otherwise be 7636 §4.3’s default
Server-side: reject plain Implemented apiauth/authorize.go AuthorizationHandler.ValidateValues accepts only S256 by default[^plain] 2.1 §4.1.1 conformant
Server-side: PKCE value shape (§4.1, §4.2) Implemented apiauth/authorize.go s256Challenge / pkceVerifierShape, checked in AuthorizationHandler.ValidateValues and in AuthorizationCodeGrant (apiauth/authorize_granter.go) Since 0.1.49. An S256 code_challenge is exactly 43 base64url characters. A plain challenge and every code_verifier are 43 to 128 unreserved characters. A bad challenge is invalid_request at /authorize; a bad verifier is invalid_grant at /token
Server-side: verify at /token (§4.6) Implemented apiauth/authorize_granter.go AuthorizationCodeGrant calls core.VerifyPKCE; mismatch → invalid_grant Uses constant-time compare (subtle.ConstantTimeCompare) in core/pkce.go VerifyPKCE
Primitives shared across modules Implemented core/pkce.go (ComputeCodeChallenge, VerifyPKCE, CodeChallengeMethodS256) Moved to core/ in PR 300 so sub-modules can import without circular deps
AS metadata advertises S256 only Implemented cmd/oneauth-server config; code_challenge_methods_supported=["S256"] Discovery-aware clients (incl. OneAuth’s own SDK) refuse to start without it

PKCE is the cleanest “fully implemented” row in the OAuth 2.1 audit table.
Both sides are exercised by integration tests; the primitives live in core/
where every sub-module can pull them.

[^plain]: AuthorizationHandler now carries an AllowPlainPKCE opt-in
(AuthorizationHandler.AllowPlainPKCE in apiauth/authorize.go, default off, wired from
OneAuthConfig.AllowPlainPKCE). With it left unset, /authorize
rejects code_challenge_method=plain, so “S256-only” holds by
default. Operators running OAuth 2.0 fleets can flip it on to accept
plain; the redemption-time core.VerifyPKCE handles either method.

  • RFC 6749 — the auth-code grant 7636 patches
  • OAuth 2.1 draft — promotes 7636 from “for public clients” to “for all clients”
  • RFC 9700 — §2.1.1 names 7636 as the mandatory code-injection defense
  • RFC 8252 — Native Apps; 7636 is the security cornerstone of the native-app flow