การเพิกถอนและตรวจสอบโทเค็น
ทำความเข้าใจวิธีเพิกถอนโทเค็นเข้าถึงและโทเค็นต่ออายุ รวมถึงการใช้ปลายทางตรวจสอบเพื่อเช็กสถานะการใช้งานของโทเค็น
การเพิกถอนและตรวจสอบโทเค็น เป็นบทเรียน OAuth2 & OpenID Connect Deep Dive ฟรีบน CoddyKit นี่คือบทเรียนที่ 3 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน OAuth2 & OpenID Connect Deep Dive และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส OAuth2 & OpenID Connect Deep Dive มีบทเรียนทั้งหมด 4 บทเรียน
บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ
Why Token Management Matters
Imagine you've logged into an app, and it gives you a digital key (an access token) to open certain doors (access resources). What if you want to change the lock, or lose the key?
This lesson explores how to manage these digital keys: revoking them to stop access, and introspecting them to check if they're still valid.
Stopping Access: Revocation
Token revocation is the process of explicitly invalidating an access or refresh token before its natural expiration time.
- Why revoke? User logs out, password reset, account compromise, or the client application is no longer trusted.
- It immediately cuts off access, enhancing security.
The Revocation Endpoint
OAuth2 and OpenID Connect providers offer a standard revocation endpoint. Clients send a POST request to this endpoint to invalidate a token.
This endpoint requires client authentication to ensure only authorized clients can revoke tokens.
Revoking an Access Token
To revoke an access token, the client sends the token to the revocation endpoint. The server then marks the token as invalid.
Here's a conceptual example using curl. Remember, this is an API call, not a runnable local program:
curl -X POST \
https://auth.example.com/oauth2/revoke \
-H "Content-Type: application/x-www-form-urlencoded" \
-u "client_id:client_secret" \
-d "token=YOUR_ACCESS_TOKEN" \
-d "token_type_hint=access_token"Revoking a Refresh Token
Revoking a refresh token is crucial! Since a refresh token can be used to obtain *new* access tokens, revoking it prevents future access.
Often, revoking a refresh token also implicitly revokes all access tokens that were issued using it.
curl -X POST \
https://auth.example.com/oauth2/revoke \
-H "Content-Type: application/x-www-form-urlencoded" \
-u "client_id:client_secret" \
-d "token=YOUR_REFRESH_TOKEN" \
-d "token_type_hint=refresh_token"Checking Status: Introspection
Token introspection is the process where a resource server can query the authorization server to determine the active status and metadata of a given token.
- This is especially useful for opaque tokens (tokens that don't reveal their contents), where the resource server can't validate them locally.
- It answers: "Is this token still good?"
The Introspection Endpoint
Similar to revocation, OAuth2 and OIDC providers expose an introspection endpoint. Resource servers (or other authorized parties) use this endpoint.
The endpoint typically requires authentication, ensuring only trusted entities can inspect tokens.
Using Introspection
When a resource server receives an opaque access token, it can send a POST request to the introspection endpoint. The response will be a JSON object containing details about the token.
Key fields in the response include active (boolean), scope, client_id, exp (expiration), and sub (subject/user ID).
curl -X POST \
https://auth.example.com/oauth2/introspect \
-H "Content-Type: application/x-www-form-urlencoded" \
-u "client_id:client_secret" \
-d "token=OPAQUE_ACCESS_TOKEN" \
-d "token_type_hint=access_token"Response from Introspection
A successful introspection response for an active token might look like this:
{"active": true, "scope": "profile email", "client_id": "my_app", "exp": 1678886400, "sub": "user123"}If the token is inactive (e.g., expired or revoked), the response would be: {"active": false}
Revocation vs. Introspection
It's important to differentiate these two concepts:
- Revocation: Changes the state of a token from active to inactive. It's an action performed by the client or authorization server.
- Introspection: Checks the current state of a token. It's a query performed by a resource server to verify a token's validity.
They both contribute to token security but serve distinct purposes.
Quick Check
You have an opaque access token that you suspect might have been compromised. Which action would you take to immediately prevent its further use?
Recap & Next Steps
In this lesson, we learned about two crucial aspects of token management: revocation and introspection.
- Revocation: Explicitly invalidates tokens via a dedicated endpoint, essential for security events like logout or compromise.
- Introspection: Allows resource servers to verify the active status and metadata of opaque tokens.
These mechanisms ensure that access granted by tokens remains secure and manageable throughout its lifecycle. Up next, we'll explore advanced OIDC features!
คำถามที่พบบ่อย
บทเรียน “การเพิกถอนและตรวจสอบโทเค็น” ฟรีหรือไม่
ใช่ — ข้อความเต็มของ “การเพิกถอนและตรวจสอบโทเค็น” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส OAuth2 & OpenID Connect Deep Dive ให้อัปเกรดเป็น CoddyKit PRO คอร์ส OAuth2 & OpenID Connect Deep Dive มีบทเรียนทั้งหมด 4 บทเรียน
คุณจะเรียนรู้อะไรในบทเรียน “การเพิกถอนและตรวจสอบโทเค็น”
ทำความเข้าใจวิธีเพิกถอนโทเค็นเข้าถึงและโทเค็นต่ออายุ รวมถึงการใช้ปลายทางตรวจสอบเพื่อเช็กสถานะการใช้งานของโทเค็น คุณปฏิบัติ OAuth2 & OpenID Connect Deep Dive ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน
คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน OAuth2 & OpenID Connect Deep Dive หรือไม่
ไม่จำเป็นต้องมีประสบการณ์มาก่อน OAuth2 & OpenID Connect Deep Dive บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 3 จากทั้งหมด 4 บทเรียน
บทเรียน “การเพิกถอนและตรวจสอบโทเค็น” ใช้เวลานานแค่ไหน
บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย
ฉันเขียนและรันโค้ดในบทเรียน OAuth2 & OpenID Connect Deep Dive นี้ได้ไหม
ได้ บทเรียน OAuth2 & OpenID Connect Deep Dive ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ
บทเรียนทั้งหมดในหลักสูตรนี้
- โครงสร้างและลายเซ็นของโทเค็น ID
- JWS และชุดคีย์ JWK
- การเพิกถอนและตรวจสอบโทเค็น
- การตรวจสอบ Claims มาตรฐานของโทเค็น ID