# OAuth decision sheet: code plus PKCE, always

> If your redirect URL shows #access_token=, your app uses the implicit flow, and RFC 9700 says not to.

Source: https://fullbytedeveloper.com/blog/oauth-pkce
Published here: 2026-11-17, updated 2026-12-03

Log in and read the redirect URL. A fragment with access_token is the implicit flow, which RFC 9700 says not to use [1][3]. Prefer authorization code with PKCE for SPAs, mobile apps, and confidential servers. Build the verifier and S256 challenge with the standard library, then confirm the RFC 7636 appendix vector before you wire the authorize URL.

## Quick start: decision sheet

1. SPA or mobile app: authorization code plus PKCE. RFC 9700: public clients MUST use PKCE [1].
2. Server-side confidential app: authorization code plus PKCE too. RFC 9700: for confidential clients, PKCE is RECOMMENDED [1].
3. Implicit flow (`response_type=token`): SHOULD NOT use [1].
4. Resource-owner password grant (your app collects the user's password): MUST NOT use [1].
5. SPA or mobile refresh tokens: rotate them or bind them to the client [1].
6. Hosted auth SDK: check which `response_type` its login request actually sends.

That list is the whole product decision. The rest of this page is how PKCE blocks a stolen code and how to mint the pair correctly.

## Why a stolen code fails with PKCE

Without PKCE, an authorization code that leaks from a redirect can be exchanged at the token endpoint by whoever holds it first. With PKCE, the app creates a high-entropy `code_verifier`, stores it locally, and sends only `code_challenge` (the S256 hash) on the authorize request [2]. The token request must present the original verifier. The authorization server hashes it and compares. A thief who copied the code still lacks the verifier [1][2].

RFC 7636 defines the S256 transform: BASE64URL(SHA256(ASCII(code_verifier))) without padding [2]. Plain challenges exist in the RFC for legacy cases; new work should use S256 [2].

<figure>
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 720 280" role="img" aria-label="Authorize sends code_challenge; token exchange requires matching code_verifier">
  <defs>
    <marker id="pkceArrow" markerWidth="10" markerHeight="10" refX="8" refY="3" orient="auto">
      <path d="M0,0 L8,3 L0,6 Z" fill="#14b8a6"/>
    </marker>
  </defs>
  <rect x="0" y="0" width="720" height="280" rx="12" fill="#0b1f3a"/>
  <text x="24" y="36" fill="#f8fafc" font-family="ui-sans-serif, system-ui, sans-serif" font-size="16" font-weight="700">PKCE on authorization code</text>
  <rect x="24" y="56" width="168" height="88" rx="8" fill="#123052" stroke="#14b8a6" stroke-width="2"/>
  <text x="36" y="88" fill="#f8fafc" font-family="ui-sans-serif, system-ui, sans-serif" font-size="14">App</text>
  <text x="36" y="112" fill="#f59e0b" font-family="ui-monospace, monospace" font-size="14">verifier kept</text>
  <path d="M192 100 H240" stroke="#14b8a6" stroke-width="3" fill="none" marker-end="url(#pkceArrow)"/>
  <rect x="248" y="56" width="200" height="88" rx="8" fill="#123052" stroke="#14b8a6" stroke-width="2"/>
  <text x="260" y="84" fill="#f8fafc" font-family="ui-sans-serif, system-ui, sans-serif" font-size="14">/authorize</text>
  <text x="260" y="108" fill="#cbd5e1" font-family="ui-sans-serif, system-ui, sans-serif" font-size="14">code_challenge=S256</text>
  <path d="M448 100 H496" stroke="#14b8a6" stroke-width="3" fill="none" marker-end="url(#pkceArrow)"/>
  <rect x="504" y="56" width="192" height="88" rx="8" fill="#123052" stroke="#f59e0b" stroke-width="2"/>
  <text x="516" y="84" fill="#f8fafc" font-family="ui-sans-serif, system-ui, sans-serif" font-size="14">Auth server</text>
  <text x="516" y="108" fill="#f59e0b" font-family="ui-sans-serif, system-ui, sans-serif" font-size="14">returns code</text>
  <path d="M600 144 V168" stroke="#14b8a6" stroke-width="3" fill="none" marker-end="url(#pkceArrow)"/>
  <rect x="248" y="176" width="448" height="72" rx="8" fill="#123052" stroke="#14b8a6" stroke-width="2"/>
  <text x="260" y="208" fill="#f8fafc" font-family="ui-sans-serif, system-ui, sans-serif" font-size="14">/token needs code + code_verifier</text>
  <text x="260" y="232" fill="#cbd5e1" font-family="ui-sans-serif, system-ui, sans-serif" font-size="14">hash(verifier) must match stored challenge</text>
</svg>
</figure>

*The authorize redirect never carries the verifier; only the later token call does, so a stolen code alone is useless.*

## Make the pair (Python 3 standard library)

```python
import secrets, hashlib, base64

def pkce_pair():
    verifier = secrets.token_urlsafe(32)
    challenge = base64.urlsafe_b64encode(
        hashlib.sha256(verifier.encode("ascii")).digest()
    ).rstrip(b"=").decode("ascii")
    return verifier, challenge

v, c = pkce_pair()
print("verifier_len", len(v))
print("verifier", v)
print("challenge", c)

# RFC 7636 Appendix B fixed vector
v2 = "dBjftJeZ4CVP-mB92K27uhbUJU1p1r_wW1gFWFOEjXk"
c2 = base64.urlsafe_b64encode(hashlib.sha256(v2.encode("ascii")).digest()).rstrip(b"=").decode("ascii")
print("rfc_challenge", c2)
print("rfc_expected", "E9Melhoa2OwvFrEMTJguCHaoeK1t8URWbuGJSstw-cM")
print("match", c2 == "E9Melhoa2OwvFrEMTJguCHaoeK1t8URWbuGJSstw-cM")
```

Captured output on this machine:

```text
verifier_len 43
verifier 64DtnRorTTL1gxhg-pvwmyhwjHQmaL_YdkeHOSJoaXo
challenge YuwR_qYXRei0fSPPWKDokaqJGn8kKUVHmOQ2JTok1qM
rfc_challenge E9Melhoa2OwvFrEMTJguCHaoeK1t8URWbuGJSstw-cM
rfc_expected E9Melhoa2OwvFrEMTJguCHaoeK1t8URWbuGJSstw-cM
match True
```

A 43-character verifier fits RFC 7636's length window (43 to 128 characters) [2]. If your appendix vector fails, fix the base64url and padding strip before you debug the IdP.

Wire the authorize URL with `response_type=code`, `code_challenge`, `code_challenge_method=S256`, and your `client_id` / `redirect_uri`. On the callback, POST the code plus `code_verifier` to the token endpoint [2][3].

## Spot implicit flow in the wild

Open DevTools, clear the network log, sign in, and inspect the final redirect. Examples:

```text
# bad: implicit
https://app.example/callback#access_token=...&token_type=Bearer

# good: code
https://app.example/callback?code=SplxlOBeZQQYbYS6WxSbIA&state=af0ifjsldkj
```

Hash fragments never hit your server access logs, which is why implicit looked attractive for SPAs historically. It also parks long-lived tokens in browser history and referrer-adjacent surfaces. RFC 9700's guidance is to move those apps to code plus PKCE [1].

## Confidential clients still benefit

A server that holds a client secret can already authenticate at the token endpoint. PKCE still helps when the authorization code is intercepted on the redirect path, because the secret alone does not prove possession of the verifier that started the login [1][2]. Adding PKCE is cheap: one random string in the session store and two query fields.

If you issue refresh tokens to public clients, follow RFC 9700 on rotation or sender-constraining so a stolen refresh token is not an infinite session [1]. Pair that with short access-token lifetimes.

## Related token hygiene

Authorization codes are not JWTs, but the access tokens you eventually mint often are. [JWTs are signed, not encrypted](/blog/jwt-peek) shows why packing email and roles into a readable payload is a separate mistake. [Math.random is the wrong tool for reset tokens](/blog/math-random-not-secure-tokens) covers insecure generators for the non-OAuth secrets sitting next to your login flow.

## Mistakes that look like IdP bugs

Sending `code_challenge` but forgetting `code_challenge_method=S256`, so the server treats the challenge as plain and the verifier never matches [2].

Generating the verifier with `Math.random` or a short hex string. Use a CSPRNG sized for at least 32 bytes before base64url [2].

Storing the verifier in localStorage on a page that also runs third-party scripts, then blaming PKCE when XSS steals it. PKCE stops code interception, not XSS.

Keeping `response_type=token` in an old mobile WebView build while the new SPA uses code. Test both clients after the IdP change.

## State, redirect URI, and mix-ups

PKCE does not replace `state`. Keep a random `state` value tied to the browser session to block CSRF on the callback, and reject callbacks whose `state` does not match [1][3]. Register exact redirect URIs with the IdP; wildcards and custom URL schemes need the same care the security BCP calls out for public clients [1].

Mobile apps that claim HTTPS app-link redirects still need PKCE. Custom scheme redirects are easier to hijack on some platforms; pair them with claimed HTTPS links where the OS supports it, and keep the verifier in memory that other apps cannot read [1][2].

Server frameworks sometimes offer an "OAuth" button that still defaults to implicit for SPAs. Read the authorize request your template emits, not the marketing page. If you see `response_type=token`, change the template before you ship branding.

## Token endpoint checks

On the token request, send `grant_type=authorization_code`, the `code`, the `redirect_uri` that matches the authorize call, and `code_verifier` [2][3]. Confidential clients also send client authentication. If the IdP returns `invalid_grant`, check verifier storage first: a page reload that dropped memory-only state looks identical to a wrong hash.

Never log the verifier or the authorization code at info level. Treat both like passwords in your logging redaction list.

Clock skew rarely breaks PKCE itself, but it does break access-token expiry checks after a successful exchange. Keep NTP on the API hosts that validate JWTs you mint from the token response.

## Migration from implicit

Ship code-plus-PKCE behind a feature flag for one client at a time. Keep the IdP's implicit grant disabled only after every production client has moved, or older mobile builds will strand users. Measure authorize and token error rates for a week. When the hash-fragment traffic hits zero in access logs (remember fragments never hit the server; instrument the client), remove the old path.

Document the decision sheet in the repo so the next engineer does not re-enable implicit "because the SPA tutorial used it."

## When login is right

The redirect carries a `code` query param, never an `access_token` fragment. Token exchange succeeds only with the verifier your app stored for that `state`. The RFC 7636 appendix vector matches in your unit test. That is the bar: code plus PKCE, always [1][2]. Agents that call Stripe through MCP need a different key path than OAuth clients: [migrate off full-access Stripe keys before the MCP cut-over](/blog/stripekey).

Browse the [Security topic](https://fullbytedeveloper.com/blog/topic/security).

Send this to the friend who still ships login redirects with access_token in the hash.

Image: A three-step token dance showing a verifier cube, a hashed challenge shard, and an authorization bead passing through a teal redirect ring, while a dangerous implicit token on a fragment path is blocked by an amber barrier, with no typography or URLs.
https://fullbytedeveloper.com/media/oauth-pkce/oauth-pkce-venice.webp
Use the code flow with a proof of possession. Do not put access tokens in a fragment.

## Sources

1. [RFC 9700: OAuth 2.0 Security Best Current Practice](https://www.rfc-editor.org/rfc/rfc9700)
2. [RFC 7636: Proof Key for Code Exchange (PKCE)](https://www.rfc-editor.org/rfc/rfc7636)
3. [RFC 6749: The OAuth 2.0 Authorization Framework](https://www.rfc-editor.org/rfc/rfc6749)
