node.js - 重定向到redirectURL Passport-azure-ad OIDCStrategy

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

我正在尝试通过 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 策略(Google 和本地),我们需要将一些详细信息存储到 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>

最佳答案

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

关于node.js - 重定向到redirectURL Passport-azure-ad OIDCStrategy,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65134680/

相关文章:

node.js - 如何在node.js Passport 中通过twitter登录后使用访问 token 对用户进行身份验证

javascript - 如何从字符串中获取 JSON 元素(包含 JSON 对象)。

azure - 如何在azure逻辑应用程序中将 float 四舍五入为小数点后两位?

azure - 使用 PowerShell 隐式导入较旧的模块版本

node.js - Passport + Express V4 要求用户未定义

node.js - 使用 Passport.js 验证角色和身份验证

node.js - 如何将 HTTP 流转换为 HTTPS?

Javascript - 用数组替换字符串值中的相同匹配项

javascript - Meteor Angular & Request - 如何绑定(bind)?

iis - gif 文件在 Windows azure 中给出 404 错误