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.
Step 1: Client generates a random nonce, stores it in a secure session cookie, and includes it in /authorize with scope openid.
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.
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.
[ 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 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 1: Client generates a random nonce, stores it in a secure session cookie, and includes it in /authorize with scope openid.
Step 2: IdP authenticates user, bundles the nonce into the ID Token payload claims.
Step 3: IdP cryptographically signs the ID token using its private key (RS256/ES256).
Step 4: Client receives both access_token and id_token from the token endpoint.
Step 5: Client validates the ID Token signature against the IdP's published JWKS keys.
Step 6: Client validates standard claims: iss, aud, exp, and verifies that token.nonce matches the stored session nonce.
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,
};
}- ✕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.
Accepting an ID Token on the backend without checking the audience (aud) claim, allowing tokens issued for other applications to be accepted.
Strictly assert that payload.aud matches the specific client_id assigned to your application.
What is the purpose of the 'nonce' parameter in an OpenID Connect authentication flow?