简体   繁体   English

使用谷歌身份验证从 Auth0 AccessToken 获取用户电子邮件

[英]Get user email from Auth0 AccessToken with google authentication

I am using Auth0 for Google Authentication for my React App.我正在为我的 React 应用程序使用 Auth0 进行 Google 身份验证。 Login is working successfully and I am getting access token using the getTokenSilently of the auth0-spa-js .登录成功地工作,我得到令牌使用接入getTokenSilently中的auth0-SPA-JS But this token do not have user email or name.但是此令牌没有用户电子邮件或姓名。

const { getTokenSilently } = useAuth0();
getTokenSilently().then((t:any) => {
    //t is the token
});

This has following claims:这有以下主张:

{
  "iss": "https://testauth0.auth0.com/",
  "sub": "google-oauth2|<id>",
  "aud": [
    "test1",
    "https://testauth0.auth0.com/userinfo"
  ],
  "iat": 1567615944,
  "exp": 1567702344,
  "azp": "<>",
  "scope": "openid profile email"
}

How can I request email and name to be part of the token?如何请求电子邮件和姓名成为令牌的一部分? Do I need to pass any parameters to getTokenSilently ?我需要将任何参数传递给getTokenSilently吗?

I will be using this token to call an API and I need the email address.我将使用此令牌来调用 API,我需要电子邮件地址。 An alternative I see is to use the id that is part of the "sub" claim but email is much easier.我看到的另一种方法是使用作为“子”声明一部分的 ID,但电子邮件要容易得多。

Thank you for your help.感谢您的帮助。

Update I am able to get user info in the API using the userinfo endpoint (part of the aud claim).更新我能够使用 userinfo 端点(aud 声明的一部分)在 API 中获取用户信息。 I would love to avoid this extra call.我很想避免这个额外的电话。

You should be able to get the id token via auth0.getIdTokenClaims() .您应该能够通过auth0.getIdTokenClaims()获取 id 令牌。 This will have the user profile.这将具有用户配置文件。

From Google's OpenId Connect documentation ( https://developers.google.com/identity/protocols/OpenIDConnect )来自 Google 的 OpenId Connect 文档 ( https://developers.google.com/identity/protocols/OpenIDConnect )

Obtaining user profile information获取用户个人资料信息

To obtain additional profile information about the user, you can use the access token (which your application receives during the authentication flow) and the OpenID Connect standard:要获取有关用户的其他配置文件信息,您可以使用访问令牌(您的应用程序在身份验证流程中收到)和 OpenID Connect 标准:

To be OpenID-compliant, you must include the openid profile scope in your authentication request.要符合 OpenID,您必须在身份验证请求中包含 openid 配置文件范围。

If you want the user's email address to be included, you can optionally request the openid email scope.如果您希望包含用户的电子邮件地址,您可以选择请求 openid 电子邮件范围。 To specify both profile and email, you can include the following parameter in your authentication request URI:要同时指定个人资料和电子邮件,您可以在身份验证请求 URI 中包含以下参数:

scope=openid%20email%20profile范围=openid%20email%20profile

Add your access token to the authorization header and make an HTTPS GET request to the userinfo endpoint, which you should retrieve from the Discovery document using the key userinfo_endpoint.将您的访问令牌添加到授权标头,并向 userinfo 端点发出 HTTPS GET 请求,您应该使用密钥 userinfo_endpoint 从 Discovery 文档中检索该请求。 The response includes information about the user, as described in OpenID Connect Standard Claims.响应包括有关用户的信息,如 OpenID Connect 标准声明中所述。 Users may choose to supply or withhold certain fields, so you might not get information for every field to which your scopes request access.用户可以选择提供或保留某些字段,因此您可能无法获得范围请求访问的每个字段的信息。

There's no way to avoid this extra call as you name it.正如你所说,没有办法避免这个额外的调用。

Adding to dan-woda 's answer we need to first add the required information in the claims, here in this case to the accesstoken .添加到dan-woda的答案中,我们需要首先在声明中添加所需的信息,在本例中添加到accesstoken This can be done using a rule.这可以使用规则来完成。

eg例如

function (user, context, callback) {
  context.accessToken["http://mynamespace/user_email"] = user.email;
  callback(null, user, context);
}

Check out the samples given example of adding to idtoken查看给定添加到 idtoken 示例的示例

Adding 'email' to the scopes would do the trick将“电子邮件”添加到范围会起作用

AuthorizationTokenRequest(
          AUTH0_CLIENT_ID,
          AUTH0_REDIRECT_URI,
          issuer: 'https://$AUTH0_DOMAIN',
          scopes: <String>['openid', 'email', 'profile'],
        ),

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

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