Skip to content

Instantly share code, notes, and snippets.

@jfromaniello
Created September 12, 2013 13:21
Show Gist options
  • Save jfromaniello/6537176 to your computer and use it in GitHub Desktop.
Save jfromaniello/6537176 to your computer and use it in GitHub Desktop.
Fetch all the groups a user is member of with ldapjs
var Users = require('./Users');
var users = new Users();
passport.use(new WindowsStrategy({
ldap: {
url: process.env["LDAP_URL"],
base: process.env["LDAP_BASE"],
bindDN: process.env["LDAP_BIND_USER"],
bindCredentials: process.env["LDAP_BIND_PASSWORD"]
},
integrated: false
}, function(profile, done){
if (!profile) return done(null, false);
users.getAllGroups(profile._json, function (err, groups) {
if (err) return done (err);
profile.groups = groups.map(function (g) {
return g.cn;
});
done(null, profile);
});
}));
var nconf = require('nconf');
var ldap = require('ldapjs');
var async = require('async');
ldap.Attribute.settings.guid_format = ldap.GUID_FORMAT_D;
var Users = module.exports = function(){
var options = this._options = {
url: process.env["LDAP_URL"],
base: process.env["LDAP_BASE"],
bindDN: process.env["LDAP_BIND_USER"],
bindCredentials: process.env["LDAP_BIND_PASSWORD"]
};
this._client = ldap.createClient({
url: options.url,
maxConnections: 10,
bindDN: options.bindDN,
credentials: options.bindCredentials
});
this._client.on('error', function(e){
console.log('LDAP connection error:', e);
});
this._queue = [];
var self = this;
this._client.bind(options.bindDN, options.bindCredentials, function(err) {
if(err){
return console.log("Error binding to LDAP", 'dn: ' + err.dn + '\n code: ' + err.code + '\n message: ' + err.message);
}
self.clientConnected = true;
self._queue.forEach(function (cb) { cb(); });
});
};
Users.prototype.getAllGroups = function (obj, callback) {
var self = this;
self.getGroups(obj, function (err, groups) {
if (err) return callback(err);
async.map(groups, self.getAllGroups.bind(self), function (err, res) {
return callback(err, groups.concat.apply(groups, res));
});
});
};
Users.prototype.getGroups = function (obj, callback) {
var self = this;
var opts = {
scope: 'sub',
filter: '(&(objectclass=group)(member=' + obj.dn + '))'
};
self._client.search(self._options.base, opts, function(err, res){
if (err) {
console.log('List groups error:', err);
return callback(err);
}
var entries = [];
res.on('searchEntry', function(entry) {
entries.push(entry);
});
function done () {
if(entries.length === 0) return callback(null, []);
var result = entries.map(function (e) { return e.object; });
callback(null, result);
}
res.on('error', function(err) {
if (err.message === 'Size Limit Exceeded') return done();
callback(err);
});
res.on('end', done);
});
};
@AlistairHardy
Copy link

As someone who's a novice at JS and has never used ldapjs before, this is really hard to understand. Can you comment or explain this please?
Titled, this should in theory do exactly what I'm after, but damn if I can work out how to implement it.

@EmilianoGaytan
Copy link

where do you declare "passport" variable?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment