Skip to content

Instantly share code, notes, and snippets.

@reinvented
Created November 24, 2011 15:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save reinvented/1391547 to your computer and use it in GitHub Desktop.
Save reinvented/1391547 to your computer and use it in GitHub Desktop.
Beginnings of a node.js script to act as a web search to LDAP shim
// https://github.com/mattt/vcard.js
require('./vcard.js');
// https://github.com/silentrob/Apricot
var Apricot = require('apricot').Apricot;
// http://nodejs.org/docs/latest/api/http.html
var http = require('http');
// http://nodejs.org/docs/latest/api/util.html
var util = require('util');
// http://ldapjs.org/
var ldap = require('ldapjs');
var server = ldap.createServer();
var ldap_port = 389;
var basedn = "dc=gov, dc=pe, dc=ca";
var company = "Province of PEI";
var users = new Array();
var resultssent = 0;
var resultsreturned = 0;
// Grab and parse the telephone directory, and find vCards links, if any
function govWebSearch(searchString,res) {
console.log("Initiated the search...");
Apricot.open("http://www.gov.pe.ca/phone/index.php3?lname=" + searchString, function(err, doc) {
doc.find('a');
doc.each(function(el){
if (el.href.substring(0,5) == "vcard") {
getvCard(el.href,res);
resultssent++;
console.log("Fired off a lookup for " + el.href);
}
});
});
}
server.listen(1389, function() {
console.log('LDAP server up at: %s', server.url);
});
server.bind('cn=root', function(req, res, next) {
if (req.dn.toString() !== 'cn=root' || req.credentials !== 'secret')
return next(new ldap.InvalidCredentialsError());
res.end();
return next();
});
function authorize(req, res, next) {
if (!req.connection.ldap.bindDN.equals('cn=root'))
return next(new ldap.InsufficientAccessRightsError());
return next();
}
var pre = [authorize];
server.search('o=ca', pre, function(req, res, next) {
console.log("For now I just want to pull a simple string out of req.filter but this is alluding me...");
console.log("So I am just hard-coding the search string here for how as 'croken'...");
govWebSearch("croken",res);
});
function sendLDAP(res) {
console.log("Sending results...");
users.forEach(function(user) {
res.send(user);
});
res.end();
}
// Given a path to a vCard link, grab the vCard and transform into an LDAP DN
function getvCard(path,ldapres) {
var options = {
host: 'www.gov.pe.ca',
port: 80,
path: '/phone/' + path
};
http.get(options, function(res) {
res.on('end', function() {
resultsreturned++;
if (resultsreturned == resultssent) {
sendLDAP(ldapres);
}
}),
res.on('data', function (chunk) {
var vcardObject = vCard.initialize(String(chunk));
var p = vcardObject.fn.indexOf(" ");
if (p != -1)
firstname = vcardObject.fn.substr(0, p);
p = vcardObject.fn.lastIndexOf(" ");
if (p != -1)
surname = vcardObject.fn.substr(p + 1);
email = '';
if (vcardObject.hasOwnProperty('email')) {
email = vcardObject.email.internet;
}
telephone = '';
fax = '';
if (vcardObject.hasOwnProperty('tel')) {
telephone = vcardObject.tel.work;
fax = vcardObject.tel.fax;
}
var dn = ({
dn: "cn=" + vcardObject.fn + ", " + basedn,
attributes: {
objectclass: [ "top" ],
cn: vcardObject.fn,
mail: email,
telephoneNumber: telephone,
facsimileTelephoneNumber: fax,
givenname: firstname,
title: vcardObject.title,
sn: surname,
organizationUnitName: vcardObject.org,
ou: company
}
});
console.log("Pushed a DN...");
users.push(dn);
});
});
}
@olleolleolle
Copy link

Very nice!

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