Using nonce to Prevent Replay
Learn how the OpenID Connect nonce parameter binds an ID token to a specific authentication request and protects against token replay attacks.
Using nonce to Prevent Replay is a free OAuth2 & OpenID Connect Deep Dive lesson on CoddyKit — lesson 4 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the OAuth2 & OpenID Connect Deep Dive learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
What Is the nonce?
The nonce is a random value the client generates and includes in the authentication request. The OpenID Provider echoes it back inside the issued ID token. Matching them proves the token belongs to this request.
The Replay Threat
Without a nonce, an attacker who captures a valid ID token (for example, in the Implicit or Hybrid flow where tokens travel via the browser) could replay it into another session. The nonce ties the token to one specific request, defeating replay.
nonce vs state
They are different tools:
- state — protects the OAuth2 authorization request/response against CSRF.
- nonce — protects the ID token against replay, validated inside the token itself.
Use both together in OIDC flows.
Generating a nonce
Create a high-entropy random value and store it bound to the user's session before redirecting.
import secrets
nonce = secrets.token_urlsafe(32)
session['oidc_nonce'] = nonce
print(nonce)Including It in the Request
Add the nonce to the authorization request alongside the usual parameters.
GET /authorize?
response_type=code
&client_id=app123
&scope=openid profile
&redirect_uri=https://app.example.com/cb
&state=xyz
&nonce=Tk9SQ0VfdmFsdWUIt Comes Back in the ID Token
The ID token's payload includes the exact nonce you sent.
{
"iss": "https://op.example.com",
"sub": "248289",
"aud": "app123",
"nonce": "Tk9SQ0VfdmFsdWU",
"exp": 1735689600
}Validating the nonce
After validating the ID token's signature and claims, compare its nonce with the value stored in the session.
if id_token['nonce'] != session.pop('oidc_nonce', None):
raise Exception('nonce mismatch - reject token')When nonce Is Required
The nonce is mandatory in the Implicit and Hybrid flows because ID tokens are returned through the browser front channel. In the Authorization Code flow it is recommended and strongly encouraged.
One-Time Use
Treat each nonce as single-use. Remove it from the session as soon as it is validated so the same value can never authorize a second token, closing replay windows.
Common Mistakes
Pitfalls to avoid:
- Using a predictable or reused nonce.
- Forgetting to compare it after validating the signature.
- Storing it client-side without integrity protection.
- Skipping it in front-channel flows.
Putting It Together
The full lifecycle: generate nonce, store in session, send in auth request, receive it in the ID token, verify signature and claims, then compare and discard the nonce. Only then trust the authentication.
Quick Check
Test your knowledge of the nonce.
Recap
The nonce protects ID tokens from replay.
- Generate a random nonce, store it in session, send it in the auth request.
- The OP echoes it inside the ID token.
- Validate by comparing token nonce to session nonce, then discard it.
- Required in Implicit/Hybrid flows; recommended everywhere.
Frequently asked questions
Is the “Using nonce to Prevent Replay” lesson free?
Yes — the full text of “Using nonce to Prevent Replay” is free to read here on the web, and the OAuth2 & OpenID Connect Deep Dive course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the OAuth2 & OpenID Connect Deep Dive course, upgrade to CoddyKit PRO.
What will I learn in “Using nonce to Prevent Replay”?
Learn how the OpenID Connect nonce parameter binds an ID token to a specific authentication request and protects against token replay attacks. You practise OAuth2 & OpenID Connect Deep Dive with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start OAuth2 & OpenID Connect Deep Dive?
No prior experience is required. OAuth2 & OpenID Connect Deep Dive on CoddyKit is structured for beginners through advanced learners; this is — lesson 4 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Using nonce to Prevent Replay” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this OAuth2 & OpenID Connect Deep Dive lesson?
Yes. Every OAuth2 & OpenID Connect Deep Dive lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Authorization Code Flow with OIDC
- Implicit Flow with OIDC
- Hybrid Flow with OIDC
- Using nonce to Prevent Replay