การตรวจสอบ Claims มาตรฐานของโทเค็น ID
เรียนรู้ขั้นตอนบังคับในการตรวจสอบ Claims ได้แก่ iss, aud, exp, iat และ nonce ของโทเค็น ID ของ OpenID Connect
การตรวจสอบ Claims มาตรฐานของโทเค็น ID เป็นบทเรียน OAuth2 & OpenID Connect Deep Dive ฟรีบน CoddyKit นี่คือบทเรียนที่ 4 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน OAuth2 & OpenID Connect Deep Dive และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส OAuth2 & OpenID Connect Deep Dive มีบทเรียนทั้งหมด 4 บทเรียน
บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ
Signature Is Not Enough
Verifying an ID token's signature proves it came from the provider, but you must also validate its claims to ensure it was meant for you, right now, and is still valid. A valid signature on a token meant for another app is still dangerous.
Validate iss (Issuer)
The iss claim must exactly equal the issuer identifier of your trusted provider, as published in its discovery document. Reject anything else.
if (claims.iss !== 'https://op.example.com') reject();Validate aud (Audience)
The aud claim must contain your client_id. If aud is an array with multiple values, an azp (authorized party) claim must be present and equal your client_id.
const auds = Array.isArray(claims.aud) ? claims.aud : [claims.aud];
if (!auds.includes(MY_CLIENT_ID)) reject();Validate exp (Expiration)
The exp claim is a Unix timestamp. The current time must be before exp. Reject expired tokens; allow a small clock skew (a few minutes) at most.
const now = Math.floor(Date.now() / 1000);
if (now >= claims.exp) reject('expired');Validate iat (Issued At)
The iat claim says when the token was issued. You can reject tokens that are unreasonably old, and use iat to enforce freshness policies.
if (now - claims.iat > MAX_AGE_SECONDS) reject('too old');Validate nonce
If you sent a nonce in the authentication request, the token's nonce must equal the value you stored. This blocks replay.
if (claims.nonce !== session.nonce) reject('nonce mismatch');Check azp When Present
The azp (authorized party) claim identifies which client the token was issued to when there are multiple audiences. If present, it must match your client_id.
auth_time and max_age
If you requested max_age or auth_time is essential, verify the auth_time claim shows the user authenticated recently enough; otherwise force re-authentication.
Order of Operations
A safe sequence:
- Decode and verify the signature (correct alg + key).
- Validate iss, aud/azp.
- Validate exp, iat (and auth_time if needed).
- Validate nonce.
Only after all pass do you trust the identity.
A Combined Check
Bringing the claim validations together:
function validateClaims(c, cfg, now) {
if (c.iss !== cfg.issuer) throw 'bad iss';
const auds = [].concat(c.aud);
if (!auds.includes(cfg.clientId)) throw 'bad aud';
if (now >= c.exp) throw 'expired';
if (c.nonce !== cfg.nonce) throw 'bad nonce';
return true;
}Use a Vetted Library
Hand-rolling JWT validation invites subtle bugs (alg confusion, skew handling). Prefer a well-maintained OIDC/JWT library and only configure the policy; let it enforce signature and claim checks.
Quick Check
Check your claim-validation knowledge.
Recap
Validating ID token claims goes beyond the signature:
issmust match the trusted issuer;audmust include your client_id.exp/iatenforce validity and freshness (allow small skew).nonceblocks replay; checkazpwith multiple audiences.- Prefer a vetted library over hand-rolled checks.
คำถามที่พบบ่อย
บทเรียน “การตรวจสอบ Claims มาตรฐานของโทเค็น ID” ฟรีหรือไม่
ใช่ — ข้อความเต็มของ “การตรวจสอบ Claims มาตรฐานของโทเค็น ID” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส OAuth2 & OpenID Connect Deep Dive ให้อัปเกรดเป็น CoddyKit PRO คอร์ส OAuth2 & OpenID Connect Deep Dive มีบทเรียนทั้งหมด 4 บทเรียน
คุณจะเรียนรู้อะไรในบทเรียน “การตรวจสอบ Claims มาตรฐานของโทเค็น ID”
เรียนรู้ขั้นตอนบังคับในการตรวจสอบ Claims ได้แก่ iss, aud, exp, iat และ nonce ของโทเค็น ID ของ OpenID Connect คุณปฏิบัติ OAuth2 & OpenID Connect Deep Dive ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน
คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน OAuth2 & OpenID Connect Deep Dive หรือไม่
ไม่จำเป็นต้องมีประสบการณ์มาก่อน OAuth2 & OpenID Connect Deep Dive บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 4 จากทั้งหมด 4 บทเรียน
บทเรียน “การตรวจสอบ Claims มาตรฐานของโทเค็น ID” ใช้เวลานานแค่ไหน
บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย
ฉันเขียนและรันโค้ดในบทเรียน OAuth2 & OpenID Connect Deep Dive นี้ได้ไหม
ได้ บทเรียน OAuth2 & OpenID Connect Deep Dive ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ
บทเรียนทั้งหมดในหลักสูตรนี้
- โครงสร้างและลายเซ็นของโทเค็น ID
- JWS และชุดคีย์ JWK
- การเพิกถอนและตรวจสอบโทเค็น
- การตรวจสอบ Claims มาตรฐานของโทเค็น ID