使用 nonce 防止重放
了解 OpenID Connect nonce 参数如何将 ID 令牌绑定到特定身份验证请求,并防御令牌重放攻击。
使用 nonce 防止重放 是 CoddyKit 上的免费 OAuth2 & OpenID Connect Deep Dive 课时。 这是第 4 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 OAuth2 & OpenID Connect Deep Dive 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 OAuth2 & OpenID Connect Deep Dive 课程共包含 4 节课。
本课时的部分内容尚未翻译,以英文显示。
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.
常见问题解答
「使用 nonce 防止重放」课时是免费的吗?
是的 — 「使用 nonce 防止重放」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 OAuth2 & OpenID Connect Deep Dive 课程的其余内容,请升级到 CoddyKit PRO。 OAuth2 & OpenID Connect Deep Dive 课程共包含 4 节课。
「使用 nonce 防止重放」这节课中我会学到什么?
了解 OpenID Connect nonce 参数如何将 ID 令牌绑定到特定身份验证请求,并防御令牌重放攻击。 你通过在浏览器中直接运行的动手代码来练习 OAuth2 & OpenID Connect Deep Dive,全天候 AI 导师会在你学习这节课的过程中回答你的问题。
学习 OAuth2 & OpenID Connect Deep Dive 需要有经验吗?
无需任何先前经验。CoddyKit 上的 OAuth2 & OpenID Connect Deep Dive 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 4 节课,共 4 节。
「使用 nonce 防止重放」课时需要多长时间?
大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。
我能在这节 OAuth2 & OpenID Connect Deep Dive 课中编写并运行代码吗?
能。每节 OAuth2 & OpenID Connect Deep Dive 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。
此课程中的所有课时
- 使用 OIDC 的授权码流程
- 使用 OIDC 的隐式流程
- 使用 OIDC 的混合流程
- 使用 nonce 防止重放