Last active
October 23, 2015 09:49
-
-
Save JulienBlancher/2f4637971242b9c58050 to your computer and use it in GitHub Desktop.
Bind 42 LDAP in NodeJS
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
'use strict'; | |
/** | |
* Module dependencies. | |
*/ | |
var ldap = require('ldapjs'), | |
config = require('../../config/config'); | |
/** | |
* SList students | |
*/ | |
exports.list = function (req, res) { | |
// do not initialize this var in global scope or it won't work anymore | |
var client = ldap.createClient({ | |
url: 'ldaps://ldap.42.fr:636' | |
}); | |
var dn = 'uid=' + config.ldap.login + ',ou=' + config.ldap.month + ',ou=' + config.ldap.year + ',ou=paris,ou=people,dc=42,dc=fr'; | |
// Bind LDAP | |
client.bind(dn, config.ldap.password, function (err) { | |
if (err) | |
return res.send({error: err.message}); | |
else { | |
client.search('ou=paris,ou=people,dc=42,dc=fr', { | |
scope: 'sub', | |
attributes: ['uidNumber', 'uid', 'givenName', 'sn', 'mobile', 'alias'], | |
timeLimit: 600 | |
}, function (err, data) { | |
var entries = {}; | |
data.on('searchEntry', function (entry) { | |
var match = entry.object.dn.match(/uid=[a-z\-]{3,8},ou=(july|august|september),ou=([0-9]{4})/); | |
if (match) { | |
console.log('entry: ' + JSON.stringify(entry.object)); | |
var month = match[1]; | |
var year = match[2]; | |
if (!entries[year]) | |
entries[year] = {}; | |
if (!entries[year][month]) | |
entries[year][month] = []; | |
entries[year][month].push(entry.object); | |
} | |
}); | |
data.on('searchReference', function (referral) { | |
console.log('referral: ' + referral.uris.join()); | |
}); | |
data.on('error', function (err) { | |
console.error('error: ' + err.message); | |
client.unbind(); | |
res.status(400).send({error: err.message}); | |
}); | |
data.on('end', function (result) { | |
console.log('status: ' + result.status); | |
client.unbind(); | |
res.send(entries); | |
}); | |
}); | |
} | |
}); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment