2016-10-21 97 views
2

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

在此期间,我的护照功能(验证,serializeUser和deserializeUser)都不会被调用。

这里是我的策略定义:

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. 我最初尝试使用identityMetadata的https://login.microsoft.com/<tenant>...版本,但收到关于应用程序不支持API版本的错误 - 使用通用似乎已解决该问题。
  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回调日志被创建时,发生在认证之后的日志永远不会发生。

任何帮助将不胜感激。

+0

您的第二个中间件调用不应该是'passport.authenticate(“azure”,{...})' –

+0

是的,对不起,那是剩下的东西。我已经按照应该的方式清理了代码示例。 – RHarris

+0

好的。什么是'azureStragegy'?我无法在文档中找到(https://github.com/AzureAD/passport-azure-ad) –

回答

1

使用Google身份验证策略时,我使用GET发回;但是,当我遵循Azure示例时,我们开始使用POST进行回发。

我从样本中剥去了一切,我认为这是不必要的。其中一件事是bodyParser。不幸的是,这是一个必不可少的部分,因此Passport可以解析POST请求的正文以获取从auth服务器发回的信息。

因此,所需的位如下:

var parser = require("body-parser") 

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

这是故事的全部,一切都开始工作,因为它应该。希望这节省了别人所有的头痛!