是否有用于node.js的现有用户身份验证库?特别是,我正在寻找可以为用户(使用自定义后端auth DB)执行密码身份验证,并将该用户与会话相关联的东西。
在我写一个auth库之前,我想我应该看看人们是否知道现有的库。通过谷歌搜索找不到任何明显的东西。
-Shreyas
发布于 2010-08-22 14:44:01
看起来连接中间件的connect-auth插件正是我所需要的
我使用的是express http://expressjs.com,所以连接插件非常适合,因为express是connect的子类(ok -prototyped
发布于 2011-10-25 01:34:18
如果您正在寻找用于连接或快速的身份验证框架,那么Passport是值得研究的:https://github.com/jaredhanson/passport
(披露:我是Passport的开发者)
在研究connect-auth和both auth之后,我开发了Passport。虽然它们都是很棒的模块,但它们并不适合我的需求。我想要的是更轻、更不显眼的东西。
Passport被分成不同的模块,所以你可以选择只使用你需要的模块(OAuth,只有在必要的时候)。Passport也不会在您的应用程序中挂载任何路由,使您可以灵活地决定何时何地进行身份验证,并通过钩子控制身份验证成功或失败时发生的情况。
例如,下面是设置基于表单(用户名和密码)身份验证的两步过程:
passport.use(new LocalStrategy(
function(username, password, done) {
// Find the user from your DB (MongoDB, CouchDB, other...)
User.findOne({ username: username, password: password }, function (err, user) {
done(err, user);
});
}
));
app.post('/login',
passport.authenticate('local', { failureRedirect: '/login' }),
function(req, res) {
// Authentication successful. Redirect home.
res.redirect('/');
});其他策略可用于通过Facebook、Twitter等进行身份验证。如果需要,可以插入自定义策略。
发布于 2011-02-12 09:38:23
会话+ If
我猜您没有找到很多好的库的原因是,使用库进行身份验证大部分都是过度设计的。
您正在寻找的只是一个会话绑定器:)具有以下内容的会话:
if login and user == xxx and pwd == xxx
then store an authenticated=true into the session
if logout destroy session就是这样。
我不同意你的结论,即connect-auth插件是可行的。
我也在使用connect,但我不使用connect-auth有两个原因:
之类的产品
使用connect进行非常简单的身份验证
(它是完整的。执行它只是为了测试,但如果您想在生产中使用它,请确保使用https) (并且为了符合REST原则,您应该使用POST-Request而不是GET-Request b/c更改状态:)
var connect = require('connect');
var urlparser = require('url');
var authCheck = function (req, res, next) {
url = req.urlp = urlparser.parse(req.url, true);
// ####
// Logout
if ( url.pathname == "/logout" ) {
req.session.destroy();
}
// ####
// Is User already validated?
if (req.session && req.session.auth == true) {
next(); // stop here and pass to the next onion ring of connect
return;
}
// ########
// Auth - Replace this example with your Database, Auth-File or other things
// If Database, you need a Async callback...
if ( url.pathname == "/login" &&
url.query.name == "max" &&
url.query.pwd == "herewego" ) {
req.session.auth = true;
next();
return;
}
// ####
// This user is not authorized. Stop talking to him.
res.writeHead(403);
res.end('Sorry you are not authorized.\n\nFor a login use: /login?name=max&pwd=herewego');
return;
}
var helloWorldContent = function (req, res, next) {
res.writeHead(200, { 'Content-Type': 'text/plain' });
res.end('authorized. Walk around :) or use /logout to leave\n\nYou are currently at '+req.urlp.pathname);
}
var server = connect.createServer(
connect.logger({ format: ':method :url' }),
connect.cookieParser(),
connect.session({ secret: 'foobar' }),
connect.bodyParser(),
authCheck,
helloWorldContent
);
server.listen(3000);备注
我在一年多前写了这篇文章,目前还没有活动的节点项目。所以在Express中可能会有API的变化。如果我需要更改任何内容,请添加评论。
https://stackoverflow.com/questions/3498005
复制相似问题