client.js
* @module greppy/helper/ldap/client
* @author Ralf Grawunder <r.grawunder@googlemail.com>
* @author Hermann Mayer <hermann.mayer92@gmail.com>
* @param {Object} options - Options of the ldap client helper
* ldap : // Object - Options of the ldap client according to "ldapjs"
var ldapClientHelper = function(options)
this.options = options || null;
* Connect to the configured LDAP server.
* @param {Function} callback - Function to call when finished
ldapClientHelper.prototype.connect = function(callback)
var connectionInfo = this.options.ldap.bindDN.green.bold + ' @ ' + this.options.ldap.url.green.bold;
this.client = require('ldapjs').createClient(this.options.ldap);
this.client.bind(this.options.ldap.bindDN, this.options.ldap.bindCredentials, function(err) {
logger.error('LDAP server connection failed for ' + connectionInfo);
return callback && callback(err);
logger.info('LDAP server connection suceeded for ' + connectionInfo);
* @param {Function} callback - Function to call when finished
ldapClientHelper.prototype.close = function(callback)
return this.client.unbind(callback);
* @param {String} base - Search DN
* @param {Object} options - Options for the search according to "ldapjs"
* @param {Function} callback - Function to call when finished
ldapClientHelper.prototype.search = function(base, options, callback)
return callback && callback('LDAP server connection not established');
this.client.search(base, options, function(err, res) {
return callback && callback(err);
res.on('error', function(err) {
logger.error('LDAP request failed. Details: ' + JSON.stringify(err));
res.on('searchEntry', function(entry) {
res.on('end', function(result) {
callback && callback(null, results);
* Strip results to their attributes.
* @param {Array} results - Results array
ldapClientHelper.prototype.resultsToJSON = function(results)
results.forEach(function(result) {
result.attributes.forEach(function(attr) {
res[attr.type] = attr.vals[0];