目前,我的 Express 3.0 应用程序的身份验证中间件如下所示:
return function (req, res, next) {
if (!isProtected(req.path)) {
debug('Allowed path "' + req.path + '" --> calling next()');
next();
} else if (req.session.user != undefined) {
debug('User session detected --> calling next()');
next();
} else if (req.cookies.user != undefined && req.cookies.pass != undefined) {
var username = req.cookies.user;
var hash = req.cookies.pass;
debug('Cookies detected, authorizing...');
dbman.auth(username, hash, function (err, record) {
if (err) debug('Authorization error --> ' + err);
else if (!record) res.redirect(redirectPath);
else {
debug('Autologin successful, storing session data');
req.session.user = record;
next();
}
});
} else {
debug('Protected path -- No valid session or cookies were detected');
debug('redirecting to "' + redirectPath + '"');
res.redirect(redirectPath);
}
};
我的工作假设是用户永远无法设置req.session
自己,并且req.session.user
只有在成功验证后才会设置。因此,如果会话存储中存在用户记录,我会让请求通过,甚至无需验证记录中存在的用户详细信息。但是,当有 cookie 时,我会验证 cookie 中存在的哈希值。我是否需要对会话记录进行身份验证,以免用户以某种方式操纵会话?