在哪里存储 JWT Client Credentials Grant

Where to store the JWT Client Credentials Grant

我有一个 NodeJS Express 应用程序,它通过客户端凭据授权向 Auth Server 进行身份验证。我收到的令牌用于从 API.

加载数据

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

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

我会尽量避免保留返回的令牌,只将其保存在内存中,因为客户端凭据授予使您能够相对轻松地获取新令牌,并且无需用户交互。

但如果这有问题,那么我会说:在客户端凭据旁边,因为客户端凭据至少与 JWT 令牌一样敏感。

我会记在心里。一般我会写一个单例模块来处理。

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();

main.js

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

auth.getToken()
    .then(token => {
        // we got token here
    }