RHa*_*ris 2 node.js adal passport.js passport-azure-ad
我正在尝试使用 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);
})
Run Code Online (Sandbox Code Playgroud)
这是我的路线定义:
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")
})
Run Code Online (Sandbox Code Playgroud)
有几件事要提一下:
https://login.microsoft.com/<tenant>...IdentityMetadata 的版本,但收到一条有关 API 版本不支持应用程序的错误 - 使用 common 似乎已经解决了该问题。console.log()serializeUser、deserializeUser 和验证回调中的代码永远不会被调用。我的 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!
Run Code Online (Sandbox Code Playgroud)
您可以看到这种情况一直持续到我终止该会话或浏览到网站上的其他页面为止。还要注意,虽然生成了 POST 回调日志,但身份验证之后发生的日志却永远不会生成。
任何帮助将不胜感激。
在使用 Google 身份验证策略时,我使用 GET 进行回发;然而,当我关注 Azure 示例时,我们开始通过 POST 进行回发。
我从样本中删除了我认为不必要的所有内容。其中之一就是 bodyParser。不幸的是,这是一个必要的部分,以便 Passport 可以解析 POST 请求的正文以获取从身份验证服务器发回的信息。
因此,所需的位如下:
var parser = require("body-parser")
...
//before passport.initialize()
app.use(parser.urlencoded({extended: true}));
Run Code Online (Sandbox Code Playgroud)
就这样,一切都开始按预期进行。希望这可以避免其他人的头痛!