用户属性端点 - Amazon Cognito
Amazon Web Services 文档中描述的 Amazon Web Services 服务或功能可能因区域而异。要查看适用于中国区域的差异,请参阅 中国的 Amazon Web Services 服务入门 (PDF)

用户属性端点

在 OIDC 发放包含用户属性的 ID 令牌的地方,OAuth 2.0 实施 /oauth2/userInfo 端点。经过身份验证的用户或客户端会收到包含 scopes 声明的访问令牌。此声明决定了授权服务器应返回的属性。当应用程序向 userInfo 端点提供访问令牌时,授权服务器会返回一个响应正文,其中包含通过访问令牌范围设置的边界内的用户属性。只要您的应用程序持有至少具有 openid 范围声明的有效访问令牌,就可以从 userInfo 端点检索有关用户的信息。

userInfo 端点是 OpenID Connect(OIDC)userInfo 端点。当服务提供商出示您的令牌端点发放的访问令牌时,响应中会包含用户属性。用户访问令牌中的范围定义了 userInfo 端点在其响应中返回的用户属性。openid 范围必须是访问令牌声明的范围之一。

Amazon Cognito 颁发访问令牌来响应用户池 API 请求,如 InitiateAuth。由于它们不包含任何范围,因此 userInfo 端点不接受这些访问令牌。而是必须由您出示来自令牌端点的访问令牌。

您的 OAuth 2.0 第三方身份提供者(IdP)还托管 userInfo 端点。当您的用户使用该 IdP 进行身份验证时,Amazon Cognito 会以静默方式与 IdP token 端点交换授权代码。您的用户池传递 IdP 访问令牌以授权从 IdP userInfo 端点检索用户信息。

GET /oauth2/userInfo

您的应用程序直接对此端点发出请求,而不通过浏览器。

有关更多信息,请参阅 OpenID Connect(OIDC)规范中的 UserInfo 端点

标头中的请求参数

Authorization: Bearer <access_token>

传递授权标头字段中的访问令牌。

必需。

示例:请求

GET /oauth2/userInfo HTTP/1.1 Content-Type: application/x-amz-json-1.1 Authorization: Bearer eyJra12345EXAMPLE User-Agent: [User agent] Accept: */* Host: auth.example.com Accept-Encoding: gzip, deflate, br Connection: keep-alive

示例:正向响应

HTTP/1.1 200 OK Content-Type: application/json;charset=UTF-8 Content-Length: [Integer] Date: [Timestamp] x-amz-cognito-request-id: [UUID] X-Content-Type-Options: nosniff X-XSS-Protection: 1; mode=block Cache-Control: no-cache, no-store, max-age=0, must-revalidate Pragma: no-cache Expires: 0 Strict-Transport-Security: max-age=31536000 ; includeSubDomains X-Frame-Options: DENY Server: Server Connection: keep-alive { "sub": "[UUID]", "email_verified": "true", "custom:mycustom1": "CustomValue", "phone_number_verified": "true", "phone_number": "+12065551212", "email": "bob@example.com", "username": "bob" }

有关 OIDC 声明的列表,请参阅标准声明。目前,Amazon Cognito 将 email_verifiedphone_number_verified 的值返回为字符串。

示例:负向响应

示例:不正确的请求

HTTP/1.1 400 Bad Request WWW-Authenticate: error="invalid_request", error_description="Bad OAuth2 request at UserInfo Endpoint"
invalid_request

请求缺少必需的参数、包括不支持的参数值或格式错误。

示例:不正确的令牌

HTTP/1.1 401 Unauthorized WWW-Authenticate: error="invalid_token", error_description="Access token is expired, disabled, or deleted, or the user has globally signed out."
invalid_token

访问令牌已过期、已撤销、格式不正确或无效。