UNPKG

1.69 kBJavaScriptView Raw
1// Copyright 2011 Mark Cavage, Inc. All rights reserved.
2
3var querystring = require('querystring');
4var url = require('url');
5var util = require('util');
6
7var dn = require('./dn');
8var filter = require('./filters/index');
9
10
11module.exports = {
12
13 parse: function (urlStr, parseDN) {
14 var u = url.parse(urlStr);
15 if (!u.protocol || !(u.protocol === 'ldap:' || u.protocol === 'ldaps:'))
16 throw new TypeError(urlStr + ' is an invalid LDAP url (protocol)');
17
18 u.secure = (u.protocol === 'ldaps:');
19
20 if (!u.hostname)
21 u.hostname = 'localhost';
22
23 if (!u.port) {
24 u.port = (u.secure ? 636 : 389);
25 } else {
26 u.port = parseInt(u.port, 10);
27 }
28
29 if (u.pathname) {
30 u.pathname = querystring.unescape(u.pathname.substr(1));
31 u.DN = parseDN ? dn.parse(u.pathname) : u.pathname;
32 }
33
34 if (u.search) {
35 u.attributes = [];
36 var tmp = u.search.substr(1).split('?');
37 if (tmp && tmp.length) {
38 if (tmp[0]) {
39 tmp[0].split(',').forEach(function (a) {
40 u.attributes.push(querystring.unescape(a.trim()));
41 });
42 }
43 }
44 if (tmp[1]) {
45 if (tmp[1] !== 'base' && tmp[1] !== 'one' && tmp[1] !== 'sub')
46 throw new TypeError(urlStr + ' is an invalid LDAP url (scope)');
47 u.scope = tmp[1];
48 }
49 if (tmp[2]) {
50 u.filter = querystring.unescape(tmp[2]);
51 }
52 if (tmp[3]) {
53 u.extensions = querystring.unescape(tmp[3]);
54 }
55
56 if (!u.scope)
57 u.scope = 'base';
58 if (!u.filter)
59 u.filter = filter.parseString('(objectclass=*)');
60 else
61 u.filter = filter.parseString(u.filter);
62 }
63
64 return u;
65 }
66
67};