استخدام nonce لمنع إعادة التشغيل
تعلّموا كيف تربط معلمة nonce في OpenID Connect رمز ID بطلب مصادقة محدد وتحمي من هجمات إعادة تشغيل الرمز.
استخدام nonce لمنع إعادة التشغيل درس مجاني في OAuth2 & OpenID Connect Deep Dive على CoddyKit. هذا هو الدرس 4 من أصل 4. يمكنك قراءة الدرس كاملاً أدناه مجاناً — ثم تمرن عليه مباشرة في المتصفح باستخدام محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7. هذا الدرس جزء من مسار التعلم في 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 لمنع إعادة التشغيل» كامل متاح مجاناً هنا على الويب. لتمرينه بشكل تفاعلي (محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7) وفتح باقي دورة OAuth2 & OpenID Connect Deep Dive، انتقل إلى CoddyKit PRO. تتضمن دورة OAuth2 & OpenID Connect Deep Dive 4 دروس في المجموع.
ماذا ستتعلم في «استخدام nonce لمنع إعادة التشغيل»؟
تعلّموا كيف تربط معلمة nonce في OpenID Connect رمز ID بطلب مصادقة محدد وتحمي من هجمات إعادة تشغيل الرمز. تتمرن على OAuth2 & OpenID Connect Deep Dive مع أكواد عملية تشغلها مباشرة في المتصفح، ومدرس ذكاء اصطناعي متاح 24/7 يجيب على أسئلتك أثناء عملك.
هل أحتاج إلى خبرة سابقة لأبدأ OAuth2 & OpenID Connect Deep Dive؟
لا تُشترط خبرة سابقة. OAuth2 & OpenID Connect Deep Dive على CoddyKit منظم للمبتدئين حتى المتقدمين، لذا يمكنك البدء من هنا أو من البداية والتقدم بسرعتك الخاصة. هذا هو الدرس 4 من أصل 4.
كم من الوقت يستغرق درس «استخدام nonce لمنع إعادة التشغيل»؟
معظم دروس CoddyKit تستغرق حوالي 5–10 دقائق. كل منها موجز وتفاعلي، لذا تحرز تقدماً مستمراً وتستأنف من حيث توقفت عبر الويب والتطبيق.
هل يمكنني كتابة وتشغيل أكواد في درس OAuth2 & OpenID Connect Deep Dive هذا؟
نعم. كل درس في OAuth2 & OpenID Connect Deep Dive يتضمن محرر أكواد مدمج، لذا تكتب وتشغل أكواداً حقيقية مباشرة في متصفحك وتحصل على تعليقات فورية من الذكاء الاصطناعي — بدون إعداد محلي.
جميع الدروس في هذه الدورة
- تدفق رمز التفويض مع OIDC
- التدفق الضمني مع OIDC
- التدفق المختلط مع OIDC
- استخدام nonce لمنع إعادة التشغيل