JWT客户凭证授予的存储位置

Tim*_*nen 5 javascript node.js express jwt openid-connect

我有一个NodeJS Express应用程序,该应用程序通过客户端凭据授予对Auth Server进行身份验证。我收到的令牌用于从API加载数据。

在整个应用程序中存储令牌的最佳实践是什么?

请注意,JWT不是特定于用户的,因为我的Express App是客户端。

Dat*_*ran 4

我会将它储存在记忆中。通常我会编写一个单例模块来处理它。

auth.js:

class Auth {
    getToken() {
        // check if we has token already and that token isn't expired
        if (this.token && !isExpired(this.token)) {
            return Promise.resolve(this.token);
        }
        // if not we call API for the new token then return the new token
        return asyncCallApiForToken();
    }
}
module.exports = new Auth();
Run Code Online (Sandbox Code Playgroud)

main.js

const auth = require('./auth.js)

auth.getToken()
    .then(token => {
        // we got token here
    }
Run Code Online (Sandbox Code Playgroud)