Skip to content

Instantly share code, notes, and snippets.

@elliotf
Last active January 4, 2016 16:59
Show Gist options
  • Save elliotf/8650620 to your computer and use it in GitHub Desktop.
Save elliotf/8650620 to your computer and use it in GitHub Desktop.
reuse of an ldap client
var ldap = require('ldapjs')
, port = 1389
, host = '127.0.0.1'
;
var server = ldap.createServer();
var client;
var showFailure = process.argv.indexOf('fail') > -1;
var serverDelay = 100;
var clientDelay = showFailure ? serverDelay/2 : serverDelay*6;
console.log("SHOWING FAILURE: ", showFailure);
function userAuth(client, id){
client.bind('cn=admin,o=example', 'password', function(err){
if (err) return console.log(id, ": admin bind failed: ", err);
console.log(id, ": admin bind successful, unbinding");
client.unbind();
client.bind('cn=someuser,o=example', 'apassword', function(err){
if (err) return console.log(id, ": user bind failed: ", err);
console.log(id, ": user auth successful, unbinding");
client.unbind();
});
});
}
server.listen(port, '127.0.0.1', function() {
console.log("LDAP server listening on port " + port + " of " + host)
client = ldap.createClient({
url: ['ldap://', host, ':', port].join('')
});
// connect to ldap server and make parallel overlapping requests, with each one unbinding
userAuth(client, 'client1');
setTimeout(function() {
userAuth(client, 'client2');
},clientDelay);
});
server.bind('o=example', function(req, res, next){
//console.log("Bind DN: ", req.dn.toString(), " binding!");
setTimeout(function(){
//console.log("Bind DN: ", req.dn.toString(), " bound!");
//console.log("unbinding!");
res.end();
},serverDelay);
});
{
"dependencies": {
"ldapjs": "~0.7.0"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment