1

我创建了一个使用 RS256 签名算法和http://localhost:3000/api/v1作为标识符(受众)的 API,并将 openid、phone、profile 作为范围添加到创建的 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 添加为附加标识符(受众)时,我收到一条错误消息,提示“已保留提供的标识符”

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

谢谢。

4

1 回答 1

2

我在您所做的事情中看到了多个问题。

如果您还想为您的 API 获取访问令牌,则应audience在初始/authorize调用中指定该 API 的标识符。/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 中定义的一些范围(除了openidand profile)。

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

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

有关此流程的更多信息,请参阅文档:https ://auth0.com/docs/api-auth/tutorials/authorization-code-grant

于 2018-01-09T12:12:46.060 回答