Usare nonce per prevenire i replay
Impari come il parametro nonce di OpenID Connect associ un ID token a una specifica richiesta di autenticazione e protegga dagli attacchi di token replay.
Usare nonce per prevenire i replay è una lezione OAuth2 & OpenID Connect Deep Dive gratuita su CoddyKit. Questa è la lezione 4 di 4. Puoi leggere la lezione completa qui gratuitamente — poi esercitati direttamente nel browser con un editor di codice integrato e un tutor IA disponibile 24/7. Fa parte del percorso di apprendimento OAuth2 & OpenID Connect Deep Dive, e i tuoi progressi si sincronizzano tra il web e l'app CoddyKit. Il corso OAuth2 & OpenID Connect Deep Dive include 4 lezioni in totale.
Parti di questa lezione non sono ancora state tradotte e vengono mostrate in inglese.
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.
Domande Frequenti
La lezione «Usare nonce per prevenire i replay» è gratuita?
Sì — il testo completo di «Usare nonce per prevenire i replay» è gratuito qui sul web. Per esercitarvi in modo interattivo (un editor di codice integrato e un tutor IA 24/7) e sbloccare il resto del corso OAuth2 & OpenID Connect Deep Dive, passa a CoddyKit PRO. Il corso OAuth2 & OpenID Connect Deep Dive include 4 lezioni in totale.
Cosa imparerò in «Usare nonce per prevenire i replay»?
Impari come il parametro nonce di OpenID Connect associ un ID token a una specifica richiesta di autenticazione e protegga dagli attacchi di token replay. Eserciti OAuth2 & OpenID Connect Deep Dive con codice pratico che esegui direttamente nel browser, e un tutor IA 24/7 risponde alle tue domande mentre lavori sulla lezione.
Ho bisogno di esperienza per iniziare OAuth2 & OpenID Connect Deep Dive?
Non è richiesta alcuna esperienza precedente. OAuth2 & OpenID Connect Deep Dive su CoddyKit è strutturato per principianti e studenti avanzati, quindi puoi iniziare da qui o dall'inizio e procedere al tuo ritmo. Questa è la lezione 4 di 4.
Quanto tempo richiede la lezione «Usare nonce per prevenire i replay»?
La maggior parte delle lezioni CoddyKit richiede circa 5–10 minuti. Ogni lezione è breve e interattiva, quindi fai progressi costanti e riprendi esattamente da dove hai lasciato su web e app.
Posso scrivere ed eseguire codice in questa lezione OAuth2 & OpenID Connect Deep Dive?
Sì. Ogni lezione OAuth2 & OpenID Connect Deep Dive include un editor di codice integrato, quindi scrivi ed esegui codice reale direttamente nel tuo browser e ricevi feedback istantaneo dall'IA — nessuna configurazione locale necessaria.
Tutte le lezioni di questo corso
- Authorization Code Flow con OIDC
- Implicit Flow con OIDC
- Hybrid Flow con OIDC
- Usare nonce per prevenire i replay