0Pricing
OAuth2 & OpenID Connect Deep Dive · Lekcja

Używanie nonce do zapobiegania powtórzeniom

Dowiedz się, jak parametr nonce OpenID Connect wiąże token ID z konkretnym żądaniem uwierzytelnienia i chroni przed atakami polegającymi na ponownym użyciu tokenu.

Używanie nonce do zapobiegania powtórzeniom to bezpłatna lekcja OAuth2 & OpenID Connect Deep Dive na CoddyKit. To lekcja 4 z 4. Możesz przeczytać całą lekcję poniżej za darmo — a potem ćwiczyć ją interaktywnie w przeglądarce z wbudowanym edytorem kodu i tutorem AI dostępnym 24/7. To część ścieżki edukacyjnej OAuth2 & OpenID Connect Deep Dive, a Twój postęp synchronizuje się między webem a aplikacją CoddyKit. Kurs OAuth2 & OpenID Connect Deep Dive zawiera 4 lekcji w sumie.

Części tej lekcji nie zostały jeszcze przetłumaczone i są wyświetlane po angielsku.

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=Tk9SQ0VfdmFsdWU

It 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.

Często zadawane pytania

Czy lekcja „Używanie nonce do zapobiegania powtórzeniom” jest bezpłatna?

Tak — pełny tekst „Używanie nonce do zapobiegania powtórzeniom” jest dostępny za darmo tutaj w sieci. Aby ćwiczyć ją interaktywnie (wbudowany edytor kodu i tutor AI dostępny 24/7) i odblokować resztę kursu OAuth2 & OpenID Connect Deep Dive, przejdź na CoddyKit PRO. Kurs OAuth2 & OpenID Connect Deep Dive zawiera 4 lekcji w sumie.

Co nauczysz się w „Używanie nonce do zapobiegania powtórzeniom”?

Dowiedz się, jak parametr nonce OpenID Connect wiąże token ID z konkretnym żądaniem uwierzytelnienia i chroni przed atakami polegającymi na ponownym użyciu tokenu. Ćwiczysz OAuth2 & OpenID Connect Deep Dive z praktycznym kodem, który uruchamiasz bezpośrednio w przeglądarce, a tutor AI dostępny 24/7 odpowiada na Twoje pytania podczas pracy nad lekcją.

Czy potrzebuję doświadczenia, aby zacząć OAuth2 & OpenID Connect Deep Dive?

Nie wymagamy żadnego doświadczenia. OAuth2 & OpenID Connect Deep Dive w CoddyKit jest strukturyzowany dla początkujących i zaawansowanych użytkowników, więc możesz zacząć tutaj lub od początku i uczyć się w swoim tempie. To lekcja 4 z 4.

Ile czasu zajmuje lekcja „Używanie nonce do zapobiegania powtórzeniom”?

Większość lekcji CoddyKit trwa około 5–10 minut. Każda lekcja to mały, interaktywny krok, dzięki czemu robisz systematyczne postępy i zawsze wracasz dokładnie do tego samego miejsca — na webie i w aplikacji.

Czy mogę pisać i uruchamiać kod w tej lekcji OAuth2 & OpenID Connect Deep Dive?

Tak. Każda lekcja OAuth2 & OpenID Connect Deep Dive zawiera wbudowany edytor kodu, więc piszesz i uruchamiasz prawdziwy kod bezpośrednio w przeglądarce i od razu otrzymujesz sprzężenie zwrotne od AI — bez konfiguracji na komputerze.

Wszystkie lekcje w tym kursie

  1. Przepływ Authorization Code z OIDC
  2. Przepływ Implicit z OIDC
  3. Przepływ Hybrid z OIDC
  4. Używanie nonce do zapobiegania powtórzeniom
← Powrót do OAuth2 & OpenID Connect Deep Dive