PassPort JS + OIDC具有Azure Redis会话的策略

时间:2020-08-02 11:26:24

标签: node.js azure passport.js openid-connect

我正在使用https://github.com/AzureAD/passport-azure-ad插件与Azure AD Graph API配合使用。

我引用了此处提供的示例:https://github.com/AzureADQuickStarts/WebApp-OpenIDConnect-NodeJS

尽管我的代码在开发中运行良好。我想为生产使用实现相同的功能。现在,我想使用Azure Redis缓存来实现相同目的。我一直在尝试做不同的事情,但是不知何故,请在代码下面查看到目前为止的内容

 passport.use('oidc',new OIDCStrategy(
    {
      identityMetadata: `${process.env.OAUTH_AUTHORITY}${process.env.OAUTH_ID_METADATA}`,
      clientID: process.env.OAUTH_APP_ID,
      responseType: 'code id_token',
      responseMode: 'form_post',
      redirectUrl: process.env.OAUTH_REDIRECT_URI,
      allowHttpForRedirectUrl: true,
      clientSecret: process.env.OAUTH_APP_PASSWORD,
      validateIssuer: false,
      passReqToCallback: false,
      scope: process.env.OAUTH_SCOPES.split(' ')
    },
    signInComplete
  ));

async function signInComplete(iss, sub, profile, accessToken, refreshToken, params, done) {
    if (!profile.oid) {
      return done(new Error("No OID found in user profile."));
    }
  //
    try{
      const user = await graph.getUserDetails(accessToken);
      if (user) {
        // Add properties to profile
        profile['email'] = user.mail ? user.mail : user.userPrincipalName;
        console.log(profile['email']);
      }
    } catch (err) {
      return done(err);
    }
  
    // Create a simple-oauth2 token from raw tokens
    let oauthToken = oauth2.accessToken.create(params);
  
    // Save the profile and tokens in user storage
    users[profile.oid] = { profile, oauthToken };
    return done(null, users[profile.oid]);
  }

我知道redisClient中有get和set选项可以使用它,但是我真的不确定在哪里以及如何实现相同的功能。到目前为止,我知道我可以使用

 passport.authenticate('oidc');

并在其中使用

router.post('/checkAuth',passport.authenticate('oidc'), (req, res,next)=> {

任何建议或指示,将不胜感激。

0 个答案:

没有答案