简体   繁体   English

Auth0调用userinfo的正确方法

[英]Auth0 right way to call userinfo

I have created an API with RS256 signing algorithm and http://localhost:3000/api/v1 as the Identifier (audience) and I added openid, phone, profile as the scopes to the created API 我已经创建了一个具有RS256签名算法的API,并以http:// localhost:3000 / api / v1作为标识符(受众),并且将openid,phone,配置文件添加为所创建API的范围

Then created an application to invoke the above API, with RS256 signing and turned off OIDC Conformant since I'm using a customized login page. 然后创建一个使用RS256签名的调用上述API的应用程序,并关闭OIDC Conformant,因为我使用的是自定义登录页面。

I was able to invoke the following authorize request successfully : 我能够成功调用以下授权请求:

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

After getting the code I was able to execute the token call and received the access_token 获得代码后,我能够执行令牌调用并收到了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"}'

But after decoding the JWT token I couldn't see the userinfo endpoint in audience field 但是在解码了JWT令牌后,我在观众字段中看不到userinfo端点

So I'm getting unauthorized error in executing the following userinfo call, but I was able to call my other API (secured resources) using the given access token without any issue. 因此,在执行以下userinfo调用时遇到了未经授权的错误,但是我可以使用给定的访问令牌调用其他API(受保护的资源)而没有任何问题。

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

Unauthorized 未经授权

-Then I tried to invoke the token endpoint using userinfo url as the audience value: -然后,我尝试使用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"}'

Then I'm getting the following error: 然后我得到以下错误:

 {"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"}

When I tried to add userinfo url as an additional Identifier (audience) when creating an API, I'm getting an error saying 'provided identifier is reserved' 当我在创建API时尝试将userinfo url作为附加标识符(受众群体)添加时,出现了一条错误消息,提示“保留了提供的标识符”

Please let me know what I'm doing wrong here. 请让我知道我在做什么错。 Looking forward to your reply. 期待你的回复。

Thanks. 谢谢。

I see multiple issues in what you are doing. 我在您的工作中遇到多个问题。

If you are looking to get an access token for your API as well, you should specify that API's identifier as the audience in the initial /authorize call. 如果您还希望获取API的访问令牌,则应在初始/authorize调用中将该API的标识符指定为audience /userinfo audience is assumed, so you don't need to specifically mention it. /userinfo是假定的读者,因此您无需特别提及。 For example, if your API identifier is https://api.example.com : 例如,如果您的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

You may also want to specify some of the scopes defined in the API in the above call (apart from openid and profile ). 您可能还需要在上述调用中指定API中定义的某些范围(除了openidprofile )。

When you exchange the code to tokens, the grant_type should be authorization_code (not client_credentials ). 当您将代码交换为令牌时,grant_type应该为authorization_code (而不是client_credentials )。 Also, you don't need to specify the audience again during this code exchange. 同样,在此代码交换期间,您无需再次指定受众。 But make sure you specify the redirect_uri that you sent in initial /authorize request here as well. 但也请确保在此也指定在初始/authorize请求中发送的redirect_uri This is required to prevent some attack vectors. 这是防止某些攻击媒介所必需的。

Changing the API calls based on the above points should send you back the correct access token - which can be used to both call your API and the /userinfo endpoint. 根据以上几点更改API调用,应将正确的访问令牌发送回给您-该令牌可用于调用您的API和/userinfo端点。

More info about this flow can be found in the docs: https://auth0.com/docs/api-auth/tutorials/authorization-code-grant 有关此流程的更多信息,请参见docs: https : //auth0.com/docs/api-auth/tutorials/authorization-code-grant

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM