使用 Passport 检索和存储 access_token

acc*_*ate 5 openid express passport.js

我使用PassportJS一起openid-clientcookie-session我的Express服务器进行身份验证。我正在使用openid-client“Passport 使用”作为起点,并且我目前能够成功进行身份验证。由于我需要通过我的服务器代理的 API 的访问令牌,我选择使用以下代码将访问令牌保存在我的用户会话中:

passport.use(
  'oidc',
  new Strategy(
    { client, params, passReqToCallback, sessionKey, usePKCE },
    (req, tokenset, userinfo, done) => {
      logger.info('Retrieved tokenset & userinfo');
      // Attach tokens to the stored userinfo.
      userinfo.tokenset = tokenset;
      return done(null, userinfo);
    }
  )
);
Run Code Online (Sandbox Code Playgroud)

我的下一步是获取并存储一个新的访问令牌。附带的部分信息tokensetexpires_at密钥,并且设置为一小时后过期。所以当然,如果它即将到期或已经到期,我想获得一个新令牌。

文档说要使用:

client.refresh(refreshToken) // => Promise
  .then(function (tokenSet) {
    console.log('refreshed and validated tokens %j', tokenSet);
    console.log('refreshed id_token claims %j', tokenSet.claims);
});
Run Code Online (Sandbox Code Playgroud)

好的,我明白了,我也有一些代码可以工作。但我不知道如何将它保存回我的用户会话。如果我通过我的服务器进行后续 API 调用,我仍然会在req.session.passport.user.tokenset.access_token. 那么我该如何更新呢?

(我可能应该警告一下,我对 OpenID / Oauth 身份验证以及 Passport 本身仍然很陌生,所以我正在做的一些事情可能是完全显而易见的。)