-
-
Save Carpetfizz/549bffbbc86332d70945 to your computer and use it in GitHub Desktop.
Attempting to access LDAP directory via HTTP
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var ldap = require('ldapjs'); | |
var ldapHelper = { | |
client: {}, | |
initClient: function(config){ | |
this.client = ldap.createClient({ | |
url: config.url | |
}) | |
}, | |
bindClient: function(credentials, callback){ | |
this.client.bind(credentials.bindDN, credentials.bindCredentials, function(err){ | |
if(err){ | |
callback({ | |
error: err, | |
auth: false | |
}); | |
}else{ | |
callback({ | |
auth: true | |
}); | |
} | |
}); | |
}, | |
searchDirectory: function(baseDN, options, callback){ | |
this.client.search(baseDN, options, function(err, res){ | |
var results = []; | |
if(err){ | |
console.log(err); | |
} | |
res.on('searchEntry', function(entry) { | |
results.push(entry.object); | |
}); | |
res.on('end', function(result){ | |
callback(results); | |
console.log(result.status); | |
}); | |
/* TODO: Add listeners for other events */ | |
}); | |
} | |
} | |
module.exports = ldapHelper; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var express = require('express'); | |
var bodyParser = require('body-parser'); | |
var ldapHelper = require('./ldapHelper'); | |
var fs = require('fs'); | |
var app = express(); | |
app.use(bodyParser.json()); | |
app.use(bodyParser.urlencoded({extended: true})); | |
var config = JSON.parse(fs.readFileSync('config.json', 'utf8')); | |
/* Create the LDAP Client. URL must be in ldap://address:port format */ | |
ldapHelper.initClient({url: config.url}); | |
/* HTTP POST | |
bindDN: your username, ex: 'cn=Directory Manager' | |
bindCredentials: your password, ex: 'admin123' | |
Returns {} auth status and or auth errors | |
*/ | |
app.post('/login', function(req,res){ | |
var credentials = { | |
bindDN: req.body.bindDN, | |
bindCredentials: req.body.bindCredentials, | |
} | |
ldapHelper.bindClient(credentials, function(status){ | |
res.json(status); | |
}); | |
}); | |
/* HTTP POST | |
dn: location of the root of the search, ex: 'ou=People, dc=example, dc=com' | |
filter: fully qualified parenthetical LDAP filter, ex: '(&(mail=*@domain.net)(l=California))' | |
scope: how deep to search, ex: 'base' , 'one', or 'sub' | |
Returns [] of search results | |
*/ | |
app.post('/search', function(req,res){ | |
var opts = { | |
filter: req.body.filter, | |
scope: req.body.scope | |
} | |
ldapHelper.searchDirectory(req.body.dn,opts, function(object){ | |
res.json(object); | |
}); | |
}); | |
/* Start Express Server */ | |
var server = app.listen(3000, function(){ | |
console.log('Server listening at http://%s:%s', server.address().address, server.address().port); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment