Skip to content
Back to CurriculumLesson 15 of 30
oauth-oidcAdvanced

OpenID Connect (OIDC): Identity Layer, ID Tokens, Claims & Nonce Replay Defense

OpenID Connect 1.0 is an identity layer built directly on top of OAuth 2.0. While OAuth 2.0 provides authorization (Access Tokens for APIs), OIDC provides authentication, delivering an ID Token (signed JWT) containing verifiable identity claims (sub, email, aud, iss) and a nonce to defeat replay attacks.

Interactive Visual Concept ExplainerStep-by-Step Educational Simulation
Visual Step-by-Step Concept Explaineroauth-oidc
OpenID Connect (OIDC): Identity Layer, ID Tokens, Claims & Nonce Replay Defense
Steps:
ID Token (OpenID Connect)For Client App
Format: Signed JWT
Contains: sub, email, name, nonce
Purpose: Proves WHO the authenticated user is
Access Token (OAuth 2.0)For Resource Server
Format: Opaque String or JWT
Contains: scope, aud, exp
Purpose: Proves WHAT API resources can be invoked
Step 1 of 6
Auto-stepping (1x)

Step 1: Client generates a random nonce, stores it in a secure session cookie, and includes it in /authorize with scope openid.

Security Guarantee: OIDC adds standardized authentication to OAuth 2.0 via signed ID Tokens (JWTs).
Why This Architectural Concept Exists

OAuth 2.0 alone does not tell the client who the user is; access tokens are opaque to the client and intended only for resource servers. Developers previously hacked OAuth by making ad-hoc /userinfo calls. OIDC standardized identity assertions, profile claims, and token formats.

Intuitive Mental Model

The Airline Ticket vs Driver's License: An OAuth access token is a boarding pass that lets you onto a specific airplane. An OIDC ID Token is a state-issued driver's license with your photograph, birthdate, and full name stamped with the state seal.

Architectural System TopologyProtocol Diagram
[ Client (CosmosApp) ]                                [ Identity Provider (Google OIDC) ]
         │                                                            │
         ├── (1) GET /authorize?scope=openid email&nonce=n_981 ─────►│
         │                                                            ├── Authenticates User
         │                                                            ├── Signs ID Token with RS256 Private Key
         │◄── (2) Redirect with Authorization Code ───────────────────┤
         │                                                            │
         ├── (3) POST /token (Exchange Code) ────────────────────────►│
         │◄── (4) 200 OK { access_token, id_token } ──────────────────┤
         │
         ├── 1. Decode & Verify ID Token Signature with IdP JWKS
         ├── 2. Verify: iss == 'https://accounts.google.com'
         ├── 3. Verify: aud == cosmos_client_id
         ├── 4. Verify: nonce == n_981 (Replay Protection!)
         ▼
[ User Identity Verified: sub='1083921', email='alice@domain.com' ]
CosmosApp Production Milestone

CosmosApp allows users to 'Sign in with Google'. CosmosApp initiates an OIDC flow with scope openid profile email and a cryptographic nonce stored in Alice's browser session. Google returns an ID token. CosmosApp verifies Google's signature via JWKS, checks that aud matches its client ID, and verifies that the nonce matches Alice's session, preventing replay attacks.

Step-by-Step Execution Sequence
01

Step 1: Client generates a random nonce, stores it in a secure session cookie, and includes it in /authorize with scope openid.

02

Step 2: IdP authenticates user, bundles the nonce into the ID Token payload claims.

03

Step 3: IdP cryptographically signs the ID token using its private key (RS256/ES256).

04

Step 4: Client receives both access_token and id_token from the token endpoint.

05

Step 5: Client validates the ID Token signature against the IdP's published JWKS keys.

06

Step 6: Client validates standard claims: iss, aud, exp, and verifies that token.nonce matches the stored session nonce.

Production Architecture Blueprint
import { jwtVerify, createRemoteJWKSet } from 'jose';

// CosmosApp OIDC ID Token Validation:
export async function verifyIdToken(idToken: string, expectedNonce: string) {
  const JWKS = createRemoteJWKSet(new URL('https://accounts.google.com/.well-known/openid-configuration/jwks'));

  const { payload } = await jwtVerify(idToken, JWKS, {
    issuer: 'https://accounts.google.com',
    audience: process.env.GOOGLE_CLIENT_ID,
  });

  // CRITICAL: Verify Nonce to prevent token replay
  if (payload.nonce !== expectedNonce) {
    throw new Error('ID Token nonce mismatch - possible replay attack!');
  }

  return {
    userId: payload.sub,
    email: payload.email,
  };
}
Common Architectural Misconceptions
  • ✕You can send the ID Token to APIs to access resources (ID Tokens are intended exclusively for the client app; APIs should receive Access Tokens).
  • ✕Using the userinfo endpoint replaces verifying the ID Token signature (Clients must always verify the ID Token digital signature).

Key Architectural Takeaways

  • ✓OIDC adds standardized authentication to OAuth 2.0 via signed ID Tokens (JWTs).
  • ✓Always validate the nonce claim in the ID token to ensure it was issued specifically for the current login attempt.
  • ✓Never pass ID Tokens as bearer authorization tokens to backend resource servers.
Common Architectural Pitfall

Accepting an ID Token on the backend without checking the audience (aud) claim, allowing tokens issued for other applications to be accepted.

Production Best Practice Fix

Strictly assert that payload.aud matches the specific client_id assigned to your application.

Interactive Knowledge Check

What is the purpose of the 'nonce' parameter in an OpenID Connect authentication flow?