【问题标题】:Auth0 right way to call userinfoAuth0调用userinfo的正确方法
【发布时间】:2018-06-10 14:17:46
【问题描述】:

我创建了一个使用 RS256 签名算法和 http://localhost:3000/api/v1 作为标识符(受众)的 API,我添加了 openid、电话、配置文件作为创建 API 的范围

然后创建一个应用程序来调用上述 API,使用 RS256 签名并关闭 OIDC Conformant,因为我使用的是自定义登录页面。

我能够成功调用以下授权请求:

https://hostname.auth0.com/authorize?client_id=CLIENT_ID&redirect_uri=http://localhost:4200/dashboard&response_type=code&scope=openid%20profile&state=state&nonce=nonce&audience=https://hostname.auth0.com/userinfo

获取代码后,我能够执行令牌调用并收到 access_token

curl --request POST \ --url https://hostname.auth0.com/oauth/token \ --header 'content-type: application/json' \ --data '{"client_id":"CLIENT_ID","client_secret":"CLIENT_SECRET","audience":"localhost:3000/api/v1","grant_type":"client_credentials","code": "CODE"}'

但解码 JWT 令牌后,我在受众字段中看不到 userinfo 端点

所以我在执行以下 userinfo 调用时遇到未经授权的错误,但我能够使用给定的访问令牌调用我的其他 API(安全资源)而没有任何问题。

 curl --request GET \
 --url 'https://hostname.auth0.com/userinfo' \
 --header 'authorization: Bearer {ACCESS_TOKEN}' \
 --header 'content-type: application/json'

未经授权

-然后我尝试使用 userinfo url 作为受众值来调用令牌端点:

 curl --request POST \
 --url https://hostname.auth0.com/oauth/token \
 --header 'content-type: application/json' \
 --data '{"client_id":"CLIENT_ID","client_secret":"CLIENT_SECRET","audience":"https://hostname.auth0.com/userinfo","grant_type":"client_credentials","code": "CODE"}'

然后我收到以下错误:

 {"error":"access_denied","error_description":"Client is not authorized to access \"https://hostname.auth0.com/userinfo\". You might probably want to create a \"client-grant\" associated to this API. See: https://auth0.com/docs/api/v2#!/Client_Grants/post_client_grants"}

当我在创建 API 时尝试将 userinfo url 添加为附加标识符(受众)时,我收到一条错误消息,提示“已保留提供的标识符”

请让我知道我在这里做错了什么。期待您的回复。

谢谢。

【问题讨论】:

    标签: api authentication scope auth0 userinfo


    【解决方案1】:

    我发现您的操作存在多个问题。

    如果您还想为您的 API 获取访问令牌,则应在初始 /authorize 调用中将该 API 的标识符指定为 audience/userinfo 观众是假设的,所以你不需要特别提到它。例如,如果您的 API 标识符是 https://api.example.com

    https://hostname.auth0.com/authorize?client_id=CLIENT_ID&redirect_uri=http://localhost:4200/dashboard&response_type=code&scope=openid%20profile&state=state&nonce=nonce&audience=https://api.example.com
    

    您可能还想在上述调用中指定 API 中定义的一些范围(openidprofile 除外)。

    当您将代码交换为令牌时,grant_type 应为authorization_code(而不是client_credentials)。此外,您无需在此代码交换期间再次指定受众。但请确保您在此处也指定了您在初始/authorize 请求中发送的redirect_uri。这是防止某些攻击媒介所必需的。

    根据上述几点更改 API 调用应该会向您发送正确的访问令牌 - 可用于调用您的 API 和 /userinfo 端点。

    有关此流程的更多信息可以在文档中找到:https://auth0.com/docs/api-auth/tutorials/authorization-code-grant

    【讨论】:

      猜你喜欢
      • 2018-07-22
      • 2020-04-26
      • 2017-09-29
      • 2018-02-20
      • 2020-03-03
      • 1970-01-01
      • 2015-03-04
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多