首页 > 解决方案 > 如何使用 lock v11 从 auth0 无密码获取刷新令牌?

问题描述

我有一个老式的 angularJs 应用程序,它有两个页面。在这两个页面上,我都包含了 auth0 锁定脚本。

<script src="https://cdn.auth0.com/js/lock/11.9.0/lock.min.js"></script>

在这两个页面中,一个具有以下 js,该 js 指定了一个 auth0 锁以允许用户登录:

new Auth0LockPasswordless(configuration.id,configuration.domain,
{
    allowedConnections: ['email'],
    passwordlessMethod: "link",
    auth: {
        redirectUrl: configuration.redirectUrl,
        responseType: 'token id_token',
        params: {
            scope: 'openid profile email offline_access'
        }
    }
}).show();

另一个页面负责在他们单击电子邮件中的链接后进行回叫。

var lock = new Auth0LockPasswordless(configuration.id, configuration.domain);

lock.on('authorization_error',
    function(authResult) {
        console.log("DEBUG::AUTHRESULT::", authResult);
});

lock.on('authenticated',
    function(authResult) {
        console.log("DEBUG::AUTHRESULT::", authResult);
});

现在我已经设置offline_access了请求的范围,并且在我的本地环境中,在进行身份验证时被提示提供额外的权限(所以它正在通过)。但是,当我从lock.on('authenticated', function(authResult)..refreshToken 检查日志时,它始终为空。

网络上有一些相互矛盾的文档,两种建议都锁定将返回刷新令牌,也不会返回刷新令牌。是否有人能够确认此代码是否应产生有效的 refreshToken?

标签: javascriptauth0auth0-lock

解决方案


正如@user44 在上面的评论中所说,您不应该在 SPA(单页应用程序)中使用刷新令牌,因为它不是一个安全的客户端和安全存储它的方式。相反,使用静默身份验证方法来请求新的访问令牌。

https://auth0.com/docs/api-auth/tutorials/silent-authentication

根据您使用的 SDK,auth0-spa-js 或 auth0.js:

(免责声明:我在 Auth0 担任高级解决方案工程师)


更新(2020 年 5 月 7 日):

需要注意的是Auth0最近引入了Refresh Token Rotation https://auth0.com/docs/tokens/concepts/refresh-token-rotation,Auth0 SPA SDK也支持


推荐阅读