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.
Published here , updated · 3 sources · Resume reading
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
- SPA or mobile app: authorization code plus PKCE. RFC 9700: public clients MUST use PKCE [1].
- Server-side confidential app: authorization code plus PKCE too. RFC 9700: for confidential clients, PKCE is RECOMMENDED [1].
- Implicit flow (
response_type=token): SHOULD NOT use [1]. - Resource-owner password grant (your app collects the user's password): MUST NOT use [1].
- SPA or mobile refresh tokens: rotate them or bind them to the client [1].
- Hosted auth SDK: check which
response_typeits 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].
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)
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:
verifier_len 43
verifier 64DtnRorTTL1gxhg-pvwmyhwjHQmaL_YdkeHOSJoaXo
challenge YuwR_qYXRei0fSPPWKDokaqJGn8kKUVHmOQ2JTok1qM
rfc_challenge E9Melhoa2OwvFrEMTJguCHaoeK1t8URWbuGJSstw-cM
rfc_expected E9Melhoa2OwvFrEMTJguCHaoeK1t8URWbuGJSstw-cM
match TrueA 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:
# bad: implicit
https://app.example/callback#access_token=...&token_type=Bearer
# good: code
https://app.example/callback?code=SplxlOBeZQQYbYS6WxSbIA&state=af0ifjsldkjHash 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.
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.
Browse the Security topic.
Send this to the friend who still ships login redirects with access_token in the hash.

Keep this guide
Filed under Security
Questions about this guide
Why PKCE?
Your app keeps a random code_verifier and sends only its S256 hash as code_challenge at login. A stolen authorization code cannot be swapped for tokens without the verifier.
Do server apps need PKCE?
Yes. RFC 9700 says public clients MUST use PKCE, and for confidential clients the use of PKCE is RECOMMENDED.
How do I spot the implicit flow?
Log in and watch the redirect. A fragment with access_token= means response_type=token (implicit). Prefer response_type=code with PKCE instead.
Sources
- RFC 9700: OAuth 2.0 Security Best Current Practicerfc-editor.org
- RFC 7636: Proof Key for Code Exchange (PKCE)rfc-editor.org
- RFC 6749: The OAuth 2.0 Authorization Frameworkrfc-editor.org