OAuth2 & OpenID Connect Deep Dive · 课时

UserInfo 端点

了解 OpenID Connect UserInfo 端点如何让客户端使用访问令牌获取经过验证的已认证用户附加声明。

第 4 / 4 课13 个步骤

UserInfo 端点 是 CoddyKit 上的免费 OAuth2 & OpenID Connect Deep Dive 课时。 这是第 4 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 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 sub matches the ID token's sub.
  • Responses can be plain JSON or signed/encrypted JWTs.
免费开始

用 AI 导师学习 OAuth2 & OpenID Connect Deep Dive — 免费

在浏览器中编写并运行真实代码,获得全天候 AI 导师的即时帮助,并在网页或应用中继续学习。

课程
12
课程
48

常见问题解答

「UserInfo 端点」课时是免费的吗?

是的 — 「UserInfo 端点」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 OAuth2 & OpenID Connect Deep Dive 课程的其余内容,请升级到 CoddyKit PRO。 OAuth2 & OpenID Connect Deep Dive 课程共包含 4 节课。

「UserInfo 端点」这节课中我会学到什么?

了解 OpenID Connect UserInfo 端点如何让客户端使用访问令牌获取经过验证的已认证用户附加声明。 你通过在浏览器中直接运行的动手代码来练习 OAuth2 & OpenID Connect Deep Dive,全天候 AI 导师会在你学习这节课的过程中回答你的问题。

学习 OAuth2 & OpenID Connect Deep Dive 需要有经验吗?

无需任何先前经验。CoddyKit 上的 OAuth2 & OpenID Connect Deep Dive 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 4 节课,共 4 节。

「UserInfo 端点」课时需要多长时间?

大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。

我能在这节 OAuth2 & OpenID Connect Deep Dive 课中编写并运行代码吗?

能。每节 OAuth2 & OpenID Connect Deep Dive 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。

此课程中的所有课时

  1. OIDC:OAuth2 上的身份层
  2. ID 令牌与声明
  3. OIDC 流程概览
  4. UserInfo 端点
← 返回 OAuth2 & OpenID Connect Deep Dive