Skip to Content
Mengty Learns
Security

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.

explanationintermediateTested with asvs 5.0.0 (V10), specs OAuth 2.1 draft, RFC 7636, OIDC Core 1.0Updated Nov 26, 2026

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

TL;DR

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.

OIDC adds an ID Token (audience = Client App) alongside the OAuth Access Token (audience = Resource API).

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:
    1. PKCE required (code_challenge_method=S256) for all authorization code flows, not just mobile/SPAs.
    2. Implicit (token) and Resource Owner Password (password) grants removed—never collect a third-party user's password or return tokens in URL fragments.
    3. Exact redirect_uri matching—no wildcards or open-redirect path prefixes.
    4. Refresh tokens for public clients must be sender-constrained or rotated on use.
  • OIDC identity layer:
    • ID Token: Always a JWT, always aud === client_id, verified by the client app (including nonce and iss). Never send the ID Token as a Bearer token to an API.
    • Access Token: Opaque or JWT with aud === api_identifier, sent to Resource APIs.

Real-world analogy

▶ Flow: watch it work

OAuth 2.1 Authorization Code + PKCE + OIDC

Overview of OAuth 2.1 Authorization Code + PKCE + OIDC. 8 steps.
Overview8 steps in this flow

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

When to use
  • 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.
When to avoid
  • 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

Pros
  • PKCE prevents authorization code interception attacks even for public clients without a client secret.
  • OIDC standardizes identity claims (sub, iss, aud, nonce) across providers.
Cons
  • 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