我一直在尝试升级到 @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
.
如果您想了解有关用户的更多信息,您有两种获取用户信息的方法:
sub
声明中)并使用 this API endpoint .在 Ruby 中,您可以只使用一个基本的 HTTP 请求。 user_metadata
和 app_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/