问题描述
我正在开发一个 API,当从 Web 前端使用时,它应该可用于 Steam Open-ID 登录,当从其他应用程序无状态使用时,应可用于摘要式身份验证。
OpenID 版本运行良好,当我设置一个专门用于摘要式身份验证的路由时,passport.authenticate 作为中间件也能正常工作。
现在我尝试使用自己的中间件来检查我是否拥有 OpenID 的授权,如果没有,请尝试摘要授权。
这是我目前所拥有的:
app.use(cors({
credentials: true,allowedHeaders: ['Authorization'],exposedHeaders: ['Authorization']
}));
passport.use( 'steam',new SteamStrategy({
returnURL: `${cfg.scheme}://${cfg.host}:${cfg.apiPort}/login/return`,realm: `${cfg.scheme}://${cfg.host}:${cfg.apiPort}/`,profile: false
},(identifier,profile,done) => {
process.nextTick(function () {
// Cut the SteamID64 from the returned User-URI
let steamID64 = identifier.split('/')[5];
profile.identifier = steamID64;
logger.http({
'user': `${steamID64}`,'message': 'logged in'
});
return done(null,profile);
});
})
);
passport.use( 'digest',new DigestStrategy({ qop: 'auth' },(username,cb) => {
users.findByUsername(username,(err,user) => {
if (err) { return cb(err); }
if (!user) { return cb(null,false); }
return cb(null,user,user.password);
})
})
);
function ensureAuthenticated(req,res,next) {
if (req.isAuthenticated()) {
if (cfg.admins.includes(req.user.identifier)) {
return next();
} else {
return res.status(401).send('User not in Admin list.');
}
} else {
passport.authenticate('digest',{ session: false },info) => {
if (err) {
return res.status(401).send('Error in digest authentication');
}
if (!user) {
return res.status(401).send('Not logged in.');
}
req.logIn(user,function(err) {
if (err) {
return res.status(401).send('Error in digest authentication');
}
return next();
});
})(req,next);
}
}
根据 Passport 文档,在函数 1 中应该使用“authenticate”。 但是认证失败,提供的秘密函数甚至没有被调用。
调试显示缺少授权标头。当我使用passport.authenticate 作为中间件时,我会在调试器中再次调用授权,并且在调试器中存在授权标头。
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)