node.js - ldapjs authentication error -


my company has ldap server, try test ldap connection using nodejs ldapjs module, want test connection @ first place, no search function included.

here code:

exports.authenticate = function(req, res){   var ldap = require('ldapjs');   var username = req.body.username;   var password = req.body.password;    var client = ldap.createclient({     url: 'ldap://192.168.3.220/'   });    client.bind(username, password, function (err) {     if(err){       res.send(err);     }else{       res.send('login');           }); }; 

when input correct username , password, sends "login", expected. when input correct username wrong password, sends err object, expected.

here problem: when input valid username or invalid username (such "fjdkfjdklsjfsjd") without password, sends "login", abnormal.

i new ldap , ldapjs, might simple mistake not figure out. please help....

for binding, need pass dn , password associated entry in ldap, not directly username / password.

so make basic anonymous search, grab dn result , try bind dn , password user entered.

var ldapres = null  var opts = {   filter: '(username='+ username +')',   scope: 'sub', }  client.search('ou=people,dc=company,dc=com', opts, function (err, result) {    result.on('searchentry', function (entry) {     ldapres = entry.raw   })    result.on('end', function (result) {     if (!ldapres) { return res.send('invalid username') }      client.bind(ldapres.dn, password, function (err) {       if (err) { return res.send('wrong password') }       res.send('you logged')     })    }) }) 

i assume want target attribute username in ldap, can change it, same ou , dc fields of search.


Comments

Popular posts from this blog

Android layout hidden on keyboard show -

google app engine - 403 Forbidden POST - Flask WTForms -

c - Why would PK11_GenerateRandom() return an error -8023? -