使用passport-ldapauth npm进行LDAPauthentication

我正在尝试使用passport-ldapauth npm来validationopenLDAP用户名和密码。 在执行下面的代码时,我总是得到错误{ message: 'Missing credentials' } 。 请帮助我,我的代码有什么问题。

 var connect = require('connect'), app = connect(), passport = require('passport'), LdapStrategy = require('passport-ldapauth'); // Credentials from the free LDAP test server by forumsys // More info at: http://www.forumsys.com/tutorials/integration-how-to/ldap/online-ldap-test-server/ var OPTS = { server: { url: 'ldap://<ip>', bindDn: '<admin username>', bindCredentials: '<admin password>', usernameField: "<passing actual username>", passwordField: "<password>" } }; passport.use(new LdapStrategy(OPTS)); app.use(passport.initialize()); app.use(connectRoute(function (router) { router.post('/login', function (req, res, next) { passport.authenticate('ldapauth', {session: false}, function (err, user, info) { console.log(info); if (err) { return next(err); // will generate a 500 error } // Generate a JSON response reflecting authentication status if (!user) { return res.send({success: false, message: 'authentication failed'}); } return res.send({success: true, message: 'authentication succeeded'}); })(req, res, next); }); })) app.listen(8080); 

有关更多详细信息,请参阅此badRequestMessage快速消息以查找缺less的用户名/密码(默认值:“缺less凭据”)

这是我的configuration:

 var passport = require('passport'); var LdapStrategy = require('passport-ldapauth').Strategy; var OPTS = { server: { url: '<ldap server>', bindDn: '<admin username>', bindCredentials: '<admin password>', searchBase: '<base dn>', searchFilter: '(sAMAccountName={{username}})' } }; passport.use(new LdapStrategy(OPTS)); app.use(passport.initialize()); app.use(passport.session()); passport.serializeUser(function(user, done) { done(null, user); }); passport.deserializeUser(function(user, done) { done(null, user); }); 

OPTS中的usernameFieldpasswordField是可选的。 我的程序正在使用默认值的usernamepassword 。 如果设置了usernameField则需要将searchFilter修改为(sAMAccountName={{<usernameField value>}})(uid={{<usernameField value>}})

此外,为了在会话中存储login状态,需要使用express-session模块。 会话configuration如下所示:

 var session = require('express-session'); app.use(session({ secret: 'ldap secret', resave: false, saveUninitialized: true, cookie : { httpOnly: true, maxAge: 2419200000 } /// maxAge in milliseconds })); 

然后,您可以使用LDAP身份validation:

 app.post('/login', passport.authenticate('ldapauth', { successRedirect: '/users/profile', failureRedirect: '/login' })); 

根据passport-ldapauth提供的文档,作为服务器值的对象不包含usernameFieldpasswordField 。 你的策略应该是这样的:

 var OPTS = { server: { url: 'ldap://<ip>', bindDn: '<admin username>', bindCredentials: '<admin password>' }, usernameField: "<field containing username>", passwordField: "<field containing password>" }; 

但正如G陈在他的回答中提到的,usernameField和passwordField是可选的。

请检查下面的代码,用户必须进行基本authentication。 此代码在用户login凭据必须validation时才起作用。

我们需要使用3个字段即 usernameField,passwordField和credentialsLookup

 `var basicAuth = require('basic-auth'); var OPTS = { server: { url: Constants.LDAP_SERVER_URL_STRING, bindDn: Constants.LDAP_ADMIN_STRING, bindCredentials: Constants.LDAP_PASSWORD_STRING, // searchBase: Constants.LDAP_SEARCHBASE_STRING, // searchFilter: Constants.LDAP_SEARCHFILTER_STRING // reconnect: true }, usernameField: username, passwordField: password, credentialsLookup: basicAuth }; 

LDAPpipe理工具(如Userbooster light)对于了解authentication过程如何发生非常有用。

searchBase,searchFilter等字段没有用处。 然而,免责声明是您需要testing以确定它是否属实

也许这个问题不是由passport-ldapauth造成的。 您的post请求可能存在一些问题。 在使用passport.authenticate之前,请检查您的请求中是否有[usernameField][passwordField]

请用“LDAPauthentication”检查NPM

你会遇到一个名为ldapauth-fork的包。 这个包似乎工作正常。

请检查以下链接https://www.npmjs.com/package/ldapauth-fork

 var basicAuth = require('basic-auth'); var LdapAuth = require('ldapauth-fork'); var username: string = req.body.username; var password: string = req.body.password; var ldap = new LdapAuth({ url: Constants.LDAP_SERVER_URL_STRING, bindDN: Constants.LDAP_BIND_DN_STRING, bindCredentials: Constants.LDAP_PASSWORD_STRING, searchBase: 'uid=' + username + ',' + Constants.LDAP_SEARCHBASE_STRING, searchFilter: Constants.LDAP_SEARCHFILTER_STRING // reconnect: true }); ldap.authenticate(username, password, function(err, user) { if (err) { console.log("login Error"); res.send({ success : false, message : 'authentication failed' }); } else if(!user.uid) { console.log("user not found Error"); res.send({ success : false, message : 'authentication failed' }); } else if(user.uid) { console.log("success : user "+ user.uid +" found "); } });