OAuth2 & OpenID Connect Deep Dive · Pelajaran

Menggunakan nonce untuk Mencegah Pemutaran Ulang

Pelajari cara parameter nonce OpenID Connect mengikat token ID ke permintaan autentikasi tertentu dan melindungi dari serangan pemutaran ulang token.

Pelajaran 4 dari 413 langkah

Menggunakan nonce untuk Mencegah Pemutaran Ulang adalah pelajaran OAuth2 & OpenID Connect Deep Dive gratis di CoddyKit. Ini adalah pelajaran 4 dari 4. Kamu bisa membaca pelajaran lengkapnya di bawah secara gratis — lalu praktikkan langsung di browser dengan editor kode bawaan dan tutor AI 24/7. Ini adalah bagian dari jalur belajar OAuth2 & OpenID Connect Deep Dive, dan progresmu tersinkronisasi di web dan aplikasi CoddyKit. Kursus OAuth2 & OpenID Connect Deep Dive mencakup 4 pelajaran total.

Bagian dari pelajaran ini belum diterjemahkan dan ditampilkan dalam bahasa Inggris.

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.
Gratis untuk memulai

Belajar OAuth2 & OpenID Connect Deep Dive dengan tutor AI — gratis

Tulis dan jalankan kode asli di browser kamu, dapatkan bantuan instan dari tutor AI 24/7, dan lanjutkan di mana kamu tinggalkan di web atau aplikasi.

Kursus
12
Pelajaran
48

Pertanyaan yang Sering Diajukan

Apakah pelajaran “Menggunakan nonce untuk Mencegah Pemutaran Ulang” gratis?

Ya — teks lengkap “Menggunakan nonce untuk Mencegah Pemutaran Ulang” gratis dibaca di sini di web. Untuk praktiknya secara interaktif (editor kode bawaan dan tutor AI 24/7) dan buka sisa kursus OAuth2 & OpenID Connect Deep Dive, upgrade ke CoddyKit PRO. Kursus OAuth2 & OpenID Connect Deep Dive mencakup 4 pelajaran total.

Apa yang akan aku pelajari di “Menggunakan nonce untuk Mencegah Pemutaran Ulang”?

Pelajari cara parameter nonce OpenID Connect mengikat token ID ke permintaan autentikasi tertentu dan melindungi dari serangan pemutaran ulang token. Kamu berlatih OAuth2 & OpenID Connect Deep Dive dengan kode praktik yang langsung kamu jalankan di browser, dan tutor AI 24/7 menjawab pertanyaanmu saat kamu mengerjakan pelajaran ini.

Apakah aku perlu pengalaman untuk memulai OAuth2 & OpenID Connect Deep Dive?

Tidak diperlukan pengalaman sebelumnya. OAuth2 & OpenID Connect Deep Dive di CoddyKit dirancang untuk pemula hingga pelajar tingkat lanjut, jadi kamu bisa memulai di sini atau dari awal dan belajar sesuai kecepatan kamu sendiri. Ini adalah pelajaran 4 dari 4.

Berapa lama pelajaran “Menggunakan nonce untuk Mencegah Pemutaran Ulang” memakan waktu?

Sebagian besar pelajaran CoddyKit memakan waktu sekitar 5–10 menit. Setiap pelajaran ringkas dan interaktif, jadi kamu membuat kemajuan stabil dan melanjutkan dari tempat kamu tinggalkan di web dan aplikasi.

Bisakah aku menulis dan menjalankan kode dalam pelajaran OAuth2 & OpenID Connect Deep Dive ini?

Ya. Setiap pelajaran OAuth2 & OpenID Connect Deep Dive menyertakan editor kode bawaan, jadi kamu menulis dan menjalankan kode nyata langsung di browser dan mendapatkan umpan balik AI instan — tidak diperlukan penyiapan lokal.

Semua pelajaran dalam kursus ini

  1. Alur Kode Otorisasi dengan OIDC
  2. Alur Implisit dengan OIDC
  3. Alur Hibrida dengan OIDC
  4. Menggunakan nonce untuk Mencegah Pemutaran Ulang
← Kembali ke OAuth2 & OpenID Connect Deep Dive