OAuth 2.1, OIDC & PKCE
What OAuth 2.1 changes (mandatory PKCE, no implicit grant, exact redirect matching), how OIDC adds identity via ID tokens, and the full Authorization Code + PKCE flow.
TL;DR
OAuth 2.1 consolidates modern OAuth 2.0 security best practices: PKCE is mandatory, the Implicit and Password grants are removed, and exact redirect_uri matching is required. OpenID Connect (OIDC) layers authentication on top of OAuth by issuing a signed ID Token for the client app alongside the Access Token for APIs.
Intent
Understand why OAuth 2.1 drops legacy grants in favor of Authorization Code + PKCE, and keep OIDC ID Tokens (for your app's login) strictly separate from Access Tokens (for downstream APIs).
Problem
OAuth 2.0 originally offered the Implicit grant (response_type=token) for SPAs, which leaked access tokens in browser URL fragments and history. Worse, wildcard redirect_uri matching let attackers bounce authorization codes to open redirectors, and public clients without PKCE could have intercepted codes exchanged by a malicious app.
Solution
Use Authorization Code + PKCE (S256) for every OAuth 2.1 client, and add scope=openid when you need user identity via OIDC:
- OAuth 2.1 key changes:
- PKCE required (
code_challenge_method=S256) for all authorization code flows, not just mobile/SPAs. - Implicit (
token) and Resource Owner Password (password) grants removed—never collect a third-party user's password or return tokens in URL fragments. - Exact
redirect_urimatching—no wildcards or open-redirect path prefixes. - Refresh tokens for public clients must be sender-constrained or rotated on use.
- PKCE required (
- OIDC identity layer:
- ID Token: Always a JWT, always
aud === client_id, verified by the client app (includingnonceandiss). Never send the ID Token as aBearertoken to an API. - Access Token: Opaque or JWT with
aud === api_identifier, sent to Resource APIs.
- ID Token: Always a JWT, always
Real-world analogy
▶ Flow: watch it work
OAuth 2.1 Authorization Code + PKCE + OIDC
Press Start or tap any step dot to trace the path.
▸All steps (8)
Code
Generating a PKCE code_verifier and S256 code_challenge with Web Crypto:
// UNSAFE: Implicit flow (response_type=token) leaks access tokens in URL fragments
const unsafeAuthUrl =
'https://auth.example.com/authorize?response_type=token&client_id=web&redirect_uri=https://app.example.com/cb';
// SAFE: OAuth 2.1 Authorization Code + PKCE (S256) + state + OIDC nonce (RFC 7636)
const verifierBytes = crypto.getRandomValues(new Uint8Array(32));
const codeVerifier = Buffer.from(verifierBytes).toString('base64url');
const digest = await crypto.subtle.digest('SHA-256', new TextEncoder().encode(codeVerifier));
const codeChallenge = Buffer.from(digest).toString('base64url');
const params = new URLSearchParams({
response_type: 'code',
client_id: 'web',
redirect_uri: 'https://app.example.com/callback',
scope: 'openid profile email',
code_challenge: codeChallenge,
code_challenge_method: 'S256',
state: crypto.randomUUID(),
nonce: crypto.randomUUID(),
});When to use / when to avoid
- Use OAuth 2.1 Authorization Code + PKCE for every web, mobile, and CLI login flow.
- Use OIDC (scope=openid) when your app needs to authenticate the user, not just call an API.
- Use a Backend-for-Frontend (BFF) when you want tokens kept out of browser memory entirely.
- Never use response_type=token (Implicit) or grant_type=password (ROPC)—both are removed in OAuth 2.1.
- Never accept an ID Token as an API Bearer credential, or skip validating aud and iss.
How to implement
Register exact redirect URIs
Configure the Authorization Server with exact https:// callback URLs (no wildcards).
Send S256 PKCE + state + nonce
Bind the request to the browser session (state) and the upcoming ID Token (nonce).
Exchange on the server when possible
In confidential/BFF apps, exchange the code + code_verifier + client credentials from the backend and set an HttpOnly session cookie.
Pros / Cons
- PKCE prevents authorization code interception attacks even for public clients without a client secret.
- OIDC standardizes identity claims (sub, iss, aud, nonce) across providers.
- More moving parts than simple username/password forms (redirects, state storage, JWKS caching).
- Misconfigured redirect URIs or missing audience checks can turn into cross-app token confusion.
Relations
Check yourself
Check Yourself
1. Which grant types are removed in OAuth 2.1?
2. In OpenID Connect, what is the primary purpose of the ID Token?
Sources
Related Notes
Server Sessions vs JWTs (Rotation, Storage & Pitfalls)
Trade-offs between stateful server sessions and stateless JWTs, refresh-token rotation with reuse detection, cookie storage, and JWT alg/kid pitfalls.
XSS & Content Security Policy (CSP)
Reflected, stored, and DOM XSS, context-aware output encoding, React's default escaping vs escape hatches, and CSP with nonces and strict-dynamic.