【问题标题】:Trouble using passport-azure-ad OIDCStrategy with Passportjs在 Passportjs 中使用 passport-azure-ad OIDCStrategy 时遇到问题
【发布时间】:2017-03-04 07:19:27
【问题描述】:

我正在尝试使用 passport-azure-ad 来验证用户身份。该应用程序显示通用登录屏幕,但当我点击登录时,浏览器窗口变为空白并显示一个微调器,指示其工作。与此同时,我的服务器正在接收到我的回调端点的连续 POST 请求流。

在此期间,我的任何护照功能(验证、序列化用户和反序列化用户)都没有被调用过。

这是我的策略定义:

    passport.use("azure", new azureStrategy({
    identityMetadata: 'https://login.microsoftonline.com/common/.well-known/openid-configuration',
    clientID: "*************************",
    responseType: 'code id_token',
    issuer: "https://sts.windows.net/********************/",
    responseMode: 'form_post',
    redirectUrl: "http://localhost:5055/auth/azure/callback",
    allowHttpForRedirectUrl: true,
    clientSecret: "**********************************"
}, function(iss, sub, profile, accessToken, refreshToken, done) {
    console.log("ID TOken: ", profile.oid); //Never gets called
    console.log("User" ,profile) //Never gets called
    done(null, profile);
}));

passport.serializeUser(function(user, done){
    console.log("serialize: ", user)  //Never gets called
    done(null, user);
})

passport.deserializeUser((user, done) => {
    console.log("deserialize: ", user)  //never gets called
    done(null, user);
})   

这是我的路线定义:

app.get("/auth/azure", passport.authenticate('azure', {failureRedirect: '/'}))

app.post("/auth/azure/callback",
  (req, res, next) => {
    console.log("POST Callback Received!");
    next();
  },
  passport.authenticate("azure", {
    failureRedirect: "/error.html"
  }),
  (req, res) => {
    console.log("Recieved POST callback")
    res.redirect("/user")
  })

有几点要提:

  1. 我正在为我的组织开发应用程序,因此它应该只对我们 AD 中的用户进行身份验证。
  2. 我最初尝试使用https://login.microsoft.com/<tenant>... 版本的identityMetadata,但我收到了一个关于API 版本不支持应用程序的错误——使用common 似乎已经解决了这个问题。
  3. 如上所述,serializeUser、deserializeUser 和验证回调中的 console.log() 代码永远不会被调用。

我的 nodejs 服务器上的控制台窗口简单地显示了这个:

Request at:  1477083649230 GET / {}          
Request at:  1477083649235 GET /login.html {}
Request at:  1477084498737 GET /auth/azure {}
Request at:  1477085275630 POST /auth/azure/callback {}
POST Callback Received!
Request at:  1477085275980 POST /auth/azure/callback {}
POST Callback Received!
Request at:  1477085276335 POST /auth/azure/callback {}
POST Callback Received!
Request at:  1477085276679 POST /auth/azure/callback {}
POST Callback Received!
Request at:  1477085277042 POST /auth/azure/callback {}
POST Callback Received!

您可以看到这种情况一直持续到我终止该会话或浏览到网站上的其他页面。还要注意,在生成 POST 回调日志时,在身份验证之后发生的日志永远不会生成。

任何帮助将不胜感激。

【问题讨论】:

  • 你的第二个中间件调用不应该是passport.authenticate("azure", {...})
  • 是的,对不起,那是其他东西的剩菜。我已经按照应有的方式清理了代码示例。
  • 好的。 azureStragegy 是什么?我在文档中找不到 (github.com/AzureAD/passport-azure-ad)
  • 这是我分配我的需求的 var -- var azureStrategy = require("passport-azure-ad").OIDCStrategy
  • 酷。那么您是否需要包含策略定义的文件?

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


【解决方案1】:

在使用 Google 身份验证策略时,我使用 GET 回帖;但是,当我按照 Azure 示例进行操作时,我们开始使用 POST 回帖。

我从样本中删除了我认为不必要的所有内容。其中之一就是bodyParser。不幸的是,这是一个必要的部分,以便 Passport 可以解析 POST 请求的主体以获取从身份验证服务器发回的信息。

因此,所需的位如下:

var parser = require("body-parser")

... 
//before passport.initialize()
app.use(parser.urlencoded({extended: true}));

仅此而已,一切都开始按应有的方式进行。希望这可以避免其他人的所有头痛!

【讨论】:

    猜你喜欢
    • 2021-03-15
    • 2017-10-08
    • 2021-07-28
    • 1970-01-01
    • 2017-11-19
    • 1970-01-01
    • 1970-01-01
    • 2018-05-27
    • 2017-02-01
    相关资源
    最近更新 更多