Nonce zur Verhinderung von Replay-Angriffen
Lernen Sie, wie der OpenID-Connect-Nonce-Parameter ein ID-Token an eine bestimmte Authentifizierungsanfrage bindet und vor Token-Replay-Angriffen schützt.
Nonce zur Verhinderung von Replay-Angriffen ist eine kostenlose OAuth2 & OpenID Connect Deep Dive-Lektion auf CoddyKit. Dies ist Lektion 4 von 4. Du kannst die komplette Lektion unten kostenlos lesen – dann übst du sie direkt im Browser mit einem integrierten Code-Editor und einem KI-Tutor rund um die Uhr. Sie ist Teil des OAuth2 & OpenID Connect Deep Dive-Lernpfads, und dein Fortschritt wird über Web und CoddyKit-App synchronisiert. Der OAuth2 & OpenID Connect Deep Dive-Kurs umfasst insgesamt 4 Lektionen.
Teile dieser Lektion wurden noch nicht übersetzt und werden auf Englisch angezeigt.
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.
Häufig gestellte Fragen
Ist die Lektion „Nonce zur Verhinderung von Replay-Angriffen“ kostenlos?
Ja — der vollständige Text von „Nonce zur Verhinderung von Replay-Angriffen“ ist hier im Web kostenlos zu lesen. Um sie interaktiv zu üben (integrierter Code-Editor und 24/7 KI-Tutor) und den Rest des OAuth2 & OpenID Connect Deep Dive-Kurses freizuschalten, upgrade auf CoddyKit PRO. Der OAuth2 & OpenID Connect Deep Dive-Kurs umfasst insgesamt 4 Lektionen.
Was lerne ich in „Nonce zur Verhinderung von Replay-Angriffen“?
Lernen Sie, wie der OpenID-Connect-Nonce-Parameter ein ID-Token an eine bestimmte Authentifizierungsanfrage bindet und vor Token-Replay-Angriffen schützt. Du übst OAuth2 & OpenID Connect Deep Dive mit praktischem Code, den du direkt im Browser ausführst, und ein 24/7 KI-Tutor beantwortet deine Fragen während du die Lektion bearbeitest.
Brauche ich Erfahrung, um OAuth2 & OpenID Connect Deep Dive zu starten?
Keine Vorkenntnisse erforderlich. OAuth2 & OpenID Connect Deep Dive auf CoddyKit ist für Anfänger bis fortgeschrittene Lernende strukturiert, sodass du hier starten oder von Anfang an beginnen und in deinem eigenen Tempo voranschreiten kannst. Dies ist Lektion 4 von 4.
Wie lange dauert die Lektion „Nonce zur Verhinderung von Replay-Angriffen“?
Die meisten CoddyKit-Lektionen dauern etwa 5–10 Minuten. Jede ist kompakt und interaktiv, sodass du stetig Fortschritte machst und genau dort weitermachst, wo du aufgehört hast – im Web und in der App.
Kann ich in dieser OAuth2 & OpenID Connect Deep Dive-Lektion Code schreiben und ausführen?
Ja. Jede OAuth2 & OpenID Connect Deep Dive-Lektion enthält einen integrierten Code-Editor, sodass du echten Code direkt in deinem Browser schreibst und ausführst und sofort KI-Feedback erhältst — ohne lokale Einrichtung erforderlich.
Alle Lektionen in diesem Kurs
- Authorization-Code-Flow mit OIDC
- Implicit Flow mit OIDC
- Hybrid Flow mit OIDC
- Nonce zur Verhinderung von Replay-Angriffen