2011-08-04 20:32:01 +00:00
|
|
|
// Copyright 2011 Mark Cavage, Inc. All rights reserved.
|
|
|
|
|
|
|
|
var querystring = require('querystring');
|
|
|
|
var url = require('url');
|
|
|
|
var util = require('util');
|
|
|
|
|
|
|
|
var dn = require('./dn');
|
2012-02-21 01:21:53 +00:00
|
|
|
var filter = require('./filters/index');
|
2011-08-04 20:32:01 +00:00
|
|
|
|
|
|
|
|
|
|
|
module.exports = {
|
|
|
|
|
2012-02-18 08:15:52 +00:00
|
|
|
parse: function (urlStr, parseDN) {
|
2011-08-04 20:32:01 +00:00
|
|
|
var u = url.parse(urlStr);
|
|
|
|
if (!u.protocol || !(u.protocol === 'ldap:' || u.protocol === 'ldaps:'))
|
|
|
|
throw new TypeError(urlStr + ' is an invalid LDAP url (protocol)');
|
|
|
|
|
2011-08-26 22:07:10 +00:00
|
|
|
u.secure = (u.protocol === 'ldaps:');
|
2011-08-04 20:32:01 +00:00
|
|
|
|
|
|
|
if (!u.hostname)
|
|
|
|
u.hostname = 'localhost';
|
|
|
|
|
2011-08-26 22:07:10 +00:00
|
|
|
if (!u.port) {
|
|
|
|
u.port = (u.secure ? 636 : 389);
|
|
|
|
} else {
|
|
|
|
u.port = parseInt(u.port, 10);
|
|
|
|
}
|
2011-08-04 20:32:01 +00:00
|
|
|
|
|
|
|
if (u.pathname) {
|
|
|
|
u.pathname = querystring.unescape(u.pathname.substr(1));
|
|
|
|
u.DN = parseDN ? dn.parse(u.pathname) : u.pathname;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (u.search) {
|
|
|
|
u.attributes = [];
|
|
|
|
var tmp = u.search.substr(1).split('?');
|
|
|
|
if (tmp && tmp.length) {
|
|
|
|
if (tmp[0]) {
|
2012-02-18 08:15:52 +00:00
|
|
|
tmp[0].split(',').forEach(function (a) {
|
2011-08-04 20:32:01 +00:00
|
|
|
u.attributes.push(querystring.unescape(a.trim()));
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (tmp[1]) {
|
|
|
|
if (tmp[1] !== 'base' && tmp[1] !== 'one' && tmp[1] !== 'sub')
|
|
|
|
throw new TypeError(urlStr + ' is an invalid LDAP url (scope)');
|
|
|
|
u.scope = tmp[1];
|
|
|
|
}
|
|
|
|
if (tmp[2]) {
|
|
|
|
u.filter = querystring.unescape(tmp[2]);
|
|
|
|
}
|
|
|
|
if (tmp[3]) {
|
|
|
|
u.extensions = querystring.unescape(tmp[3]);
|
|
|
|
}
|
2013-10-28 21:12:12 +00:00
|
|
|
|
2011-08-04 20:32:01 +00:00
|
|
|
if (!u.scope)
|
|
|
|
u.scope = 'base';
|
|
|
|
if (!u.filter)
|
2012-02-21 01:21:53 +00:00
|
|
|
u.filter = filter.parseString('(objectclass=*)');
|
|
|
|
else
|
|
|
|
u.filter = filter.parseString(u.filter);
|
2011-08-04 20:32:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return u;
|
|
|
|
}
|
|
|
|
|
|
|
|
};
|