จุดปลายทาง UserInfo
เรียนรู้วิธีที่จุดปลายทาง UserInfo ของ OpenID Connect ช่วยให้ไคลเอนต์ดึงข้อมูลยืนยันเพิ่มเติมเกี่ยวกับผู้ใช้ที่ยืนยันตัวตนแล้วด้วยโทเค็นเข้าถึง
จุดปลายทาง UserInfo เป็นบทเรียน OAuth2 & OpenID Connect Deep Dive ฟรีบน CoddyKit นี่คือบทเรียนที่ 4 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน OAuth2 & OpenID Connect Deep Dive และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส OAuth2 & OpenID Connect Deep Dive มีบทเรียนทั้งหมด 4 บทเรียน
บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ
Beyond the ID Token
The ID token proves who the user is, but to keep it small it may carry only a few claims. The UserInfo endpoint is an OAuth2-protected resource that returns additional claims about the currently authenticated user.
It Is a Protected Resource
UserInfo is not part of the token endpoint — it is a normal protected API. You call it with the access token obtained during the OIDC flow, presented as a Bearer token.
Discovering the Endpoint
Its URL is published in the provider's discovery document under userinfo_endpoint.
GET /.well-known/openid-configuration
{
"userinfo_endpoint": "https://op.example.com/userinfo"
}Making the Request
Send a GET (or POST) with the access token in the Authorization header.
GET /userinfo HTTP/1.1
Host: op.example.com
Authorization: Bearer eyJhbGciOiJSUzI1NiIs...The Response
The response is a JSON object of claims. It must include the sub claim, which must equal the sub in the ID token to prevent token substitution.
{
"sub": "248289761001",
"name": "Jane Doe",
"email": "jane@example.com",
"email_verified": true,
"picture": "https://example.com/jane.jpg"
}Scopes Control Claims
Which claims are returned depends on the scopes granted during authorization:
profile— name, picture, locale, etc.email— email, email_verified.address— postal address.phone— phone_number, phone_number_verified.
Verifying the sub
Always confirm the sub from UserInfo matches the sub in the validated ID token. Otherwise an attacker could swap an access token issued for a different user.
if (userInfo.sub !== idTokenClaims.sub) {
throw new Error('sub mismatch - possible token substitution');
}Signed and Encrypted Responses
By default UserInfo returns plain JSON. Providers can also return a signed JWT (set via userinfo_signed_response_alg) so the client can verify integrity, and even an encrypted JWT for confidentiality.
ID Token vs UserInfo
When to use which?
- Put stable, identity-critical claims in the ID token (sub, auth_time).
- Fetch large or changeable profile data from UserInfo as needed.
This keeps the ID token compact while still giving rich profile access.
Caching Considerations
UserInfo data can change (a user updates their name). Avoid caching it indefinitely; refresh it on login or when you need current values, balancing freshness against extra network calls.
Error Handling
If the access token is expired or invalid, UserInfo returns a 401 with a WWW-Authenticate: Bearer error="invalid_token" header. Handle this by refreshing the token or re-authenticating.
Quick Check
Check your understanding of the UserInfo endpoint.
Recap
The UserInfo endpoint returns additional user claims using the access token.
- It is a protected resource, discovered via
userinfo_endpoint. - Returned claims depend on granted scopes.
- Always verify its
submatches the ID token'ssub. - Responses can be plain JSON or signed/encrypted JWTs.
คำถามที่พบบ่อย
บทเรียน “จุดปลายทาง UserInfo” ฟรีหรือไม่
ใช่ — ข้อความเต็มของ “จุดปลายทาง UserInfo” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส OAuth2 & OpenID Connect Deep Dive ให้อัปเกรดเป็น CoddyKit PRO คอร์ส OAuth2 & OpenID Connect Deep Dive มีบทเรียนทั้งหมด 4 บทเรียน
คุณจะเรียนรู้อะไรในบทเรียน “จุดปลายทาง UserInfo”
เรียนรู้วิธีที่จุดปลายทาง UserInfo ของ OpenID Connect ช่วยให้ไคลเอนต์ดึงข้อมูลยืนยันเพิ่มเติมเกี่ยวกับผู้ใช้ที่ยืนยันตัวตนแล้วด้วยโทเค็นเข้าถึง คุณปฏิบัติ OAuth2 & OpenID Connect Deep Dive ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน
คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน OAuth2 & OpenID Connect Deep Dive หรือไม่
ไม่จำเป็นต้องมีประสบการณ์มาก่อน OAuth2 & OpenID Connect Deep Dive บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 4 จากทั้งหมด 4 บทเรียน
บทเรียน “จุดปลายทาง UserInfo” ใช้เวลานานแค่ไหน
บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย
ฉันเขียนและรันโค้ดในบทเรียน OAuth2 & OpenID Connect Deep Dive นี้ได้ไหม
ได้ บทเรียน OAuth2 & OpenID Connect Deep Dive ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ
บทเรียนทั้งหมดในหลักสูตรนี้
- OIDC: ชั้นอัตลักษณ์บน OAuth2
- โทเค็น ID และข้ออ้างสิทธิ์
- ภาพรวมโฟลว์ OIDC
- จุดปลายทาง UserInfo