jwt - 从 token 中检索电子邮件 | @auth0/auth0-spa-js

标签 jwt access-token auth0

我一直在尝试升级到 @auth0/auth0-spa-js 来自 auth0-js ,尽管我无法从一个 token 中阅读一封电子邮件,而该 token 又是从 await useAuth0().getTokenSilently() 获得的
我用 jwt.io解密 token ,这就是我在有效负载中得到的:

{
  "iss": "https://TENANT_NAME.auth0.com/",
  "sub": "auth0|SOME_HASH",
  "aud": [
    "https://API_IDENTIFIER",
    "https://TENANT_NAME.auth0.com/userinfo"
  ],
  "iat": 1563699940,
  "exp": 1563786340,
  "azp": "SOME_OTHER_HASH",
  "scope": "openid profile email"
}
当我使用 auth0-js 我可以添加 scope: "openid email"new auth0.WebAuth({...})瞧——我有 电子邮件 email_verified 在解密 token 的有效负载中。
我相信2 part series of tutorials无法回答我的问题,并从 Hook getTokenSilently() 中检索 token 在我的 Apollo 配置中本身也是一个挑战。但是,我喜欢 SPA 的重定向实现。请问,您能否建议一种在 token 的有效负载中包含 email 和 email_verified 的正确方法?
更新
通过阅读 this piece of documentation on api-tokens我了解我从 getTokenSilently() 获得的 token 被称为 访问 token .我一直在使用 ID token 直到今天,在我所有请求的标题中,这可能是一个不好的方法:

In the OIDC-conformant pipeline, ID Tokens should never be used as API tokens.


此外,文档说:

The token does not contain any information about the user except for the user ID (located in the sub claim).

In many cases, you may find it useful to retrieve additional user information. You can do this by calling the /userinfo API endpoint with the Access Token.


我通过关注 the shell example 完成的.我已经用我的访问 token 发送了请求,并神奇地从 Auth0 的自定义 API 中获得了一个包含用户个人资料信息的对象。
我们接近了,将“子”转换为驻留在 Auth0 的自定义 API 中的用户配置文件的算法是什么,我可以为我用 ruby​​ 编写的后端实现它?

最佳答案

是的,正如您提到的,ID token 不应用作 API token 。它们有不同的用法(您不希望 API token 太大,因为您在每个请求的 header 中发送它)。

要获取用户电子邮件,您只需获取 ID token 中给出的 UserProfile。为此,您只需调用getUser而不是 getTokenSilently .

如果您想了解有关用户的更多信息,您有两种获取用户信息的方法:

  • 您使用 Auth0 管理 API 来获取用户信息,基于用户 ID(在 ID token 的 sub 声明中)并使用 this API endpoint .在 Ruby 中,您可以只使用一个基本的 HTTP 请求。
  • 您使用规则始终在 ID token 中包含特定字段。例如,您可以包含 user_metadataapp_metadata在您的 ID token 中。这样,您无需额外的 API 调用即可使用它。为此,您需要 a specific Rule that will run when ID Tokens生成(more general API doc)。

  • 将添加所有 user_metadata 的示例规则和 app_metadata到 ID token 将是:

    function (user, context, callback) {
      const namespace = 'your_url_namespace_just_for_cosmetic_but_required/';
      context.idToken[namespace + 'user_metadata'] = user.user_metadata;
      context.idToken[namespace + 'app_metadata'] = user.app_metadata;
      callback(null, user, context);
    }
    

    您将在您的 ID token 中获得信息供您的前端使用。

    记录在案,更多规则示例见 this hard-to-find page .

    关于jwt - 从 token 中检索电子邮件 | @auth0/auth0-spa-js,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57131967/

    相关文章:

    php - Swagger Doctrine/zircote持有者授权码

    jwt - Azure AD jwks uri 配置

    ios - 为什么我的 Auth0 token 从 iOS 发送到 node.js 服务器时总是返回无效?

    Angular 2 UI-路由器身份验证

    node.js - auth0 回调在登录 angular2 后不设置配置文件日期

    Azure 身份 : Custom token validation to validate issuers in a multi tenant app

    jwt - 使用外部数据在 Keycloak 中扩展用户访问 token

    c# - 如何使用 unity 3d 获取谷歌访问 token ?

    azure - 在 Azure 数据工厂中完成事件后,如何将消息发送到 Azure 服务总线

    ios - 使用 .netrc 文件安装 Mapbox iOS SDK