【问题标题】:Redirect to redirectURL passport-azure-ad OIDCStrategy重定向到redirectURL passport-azure-ad OIDCStrategy
【发布时间】:2021-03-15 23:34:36
【问题描述】:

我正在尝试通过 Azure AD Passport.js OIDCStrategy 进行登录。 到目前为止,我使用接下来的两页(除了 Google 和在 stackoverflow 上查看问题)作为资源最多:

我有以下问题,因为我还想扩展关于 passport.js 和 Azure AD 策略的知识:

  1. 当我转到 /auth/AzureAD 时,我被重定向到 https://login.microsoftonline.com/ 登录表单。但是,当我输入有效的登录凭据时,我会被重定向到 redirectUrl。我做错了什么?
  2. 是否还应该为 /auth/AzureAD/callback 提供 router.post,如果是,为什么?
  3. 对于其他 passport.js 策略(谷歌和本地),我们需要将一些详细信息存储到 MongoDB 中。 Azure AD 策略不应该也这样做吗?如果需要,有哪些细节?
  4. 什么时候使用 BearerStrategy 而不是 OIDCStrategy?

我很确定我以正确的方式配置了 identityMetadata 和 ClientID。我不会分享这些信息,因为我不确定我这样做是否正确。

如果你能帮助我朝着正确的方向前进,已经非常感谢了!!!

passport.js

const passport = require('passport');
const OIDCStrategy = require('passport-azure-ad').OIDCStrategy
const mongoose = require('mongoose');
const User = require('../Models/User');

  passport.use(
    new OIDCStrategy(
      {
        identityMetadata: 'https://login.microsoftonline.com/XXX.onmicrosoft.com/v2.0/.well-known/openid-configuration',
        clientID: 'XXX',
        responseType: 'code id_token',
        responseMode: 'form_post',
        redirectUrl: 'http://localhost:3000/auth/AzureAD/callback',
        allowHttpForRedirectUrl: true,
        clientSecret: 'XXX',
        validateIssuer: false,
        isB2C: false,
        issuer: null,
        passReqToCallback: false,
        scope: ['profile', 'offline_access'],
        loggingLevel: 'info',
        nonceLifetime: null,
        nonceMaxAmount: 6,
        clockSkew: 300,
          },
  function(iss, sub, profile, accessToken, refreshToken, done) {
    if (!profile.oid) {
      return done(new Error("No oid found"), null);
    }
    // asynchronous verification, for effect...
    process.nextTick(function () {
      findByOid(profile.oid, function(err, user) {
        if (err) {
          return done(err);
        }
        if (!user) {
          // "Auto-registration"
          users.push(profile);
          return done(null, profile);
        }
        return done(null, user);
      });
    });
  }
));

app.js

//Passport middleware - Express Session
  session({
    secret: process.env.SESSION_SECRET,
    resave: false,
    saveUninitialized: false,
    store: new MongoStore({ mongooseConnection: mongoose.connection, collection: 'sessions' }),
  })

app.use(passport.initialize());
app.use(passport.session());

auth.js

const express = require('express')
const passport = require('passport')
const router = express.Router()

// @desc    Auth with Google
// @route   GET /auth/AzureAD
router.get('/AzureAD', passport.authenticate("azuread-openidconnect", { scope: ['profile'] }))

// @desc    AzureAD auth callback
// @route   GET /auth/AzureAD/callback
router.get(
  '/AzureAD/callback',
  passport.authenticate("azuread-openidconnect", { failureRedirect: '/login' }),
  (req, res) => {
    res.redirect('/')
  }
)

login.ejs

<div class="section">
    <a href="/auth/AzureAD" class="btn red darken-1">
        <i class="fab fa-microsoft"></i> Log In With Azure
    </a>
</div>

【问题讨论】:

    标签: node.js azure passport.js passport-azure-ad


    【解决方案1】:
    1. 这是预期行为。
    2. 是的,因为您将 responseMode 设置为 form_post
    3. 没有必要。
    4. 用户 OIDCStrategy 将用户重定向到 Azure AD 登录表单,并在成功登录时获取 id 令牌。使用 BearerStrategy 验证附加到 HTTP 请求的访问令牌。

    【讨论】:

      猜你喜欢
      • 2017-03-04
      • 2017-10-08
      • 1970-01-01
      • 2019-03-15
      • 2023-01-15
      • 2021-07-28
      • 1970-01-01
      • 2015-10-13
      • 2018-05-15
      相关资源
      最近更新 更多