-
Notifications
You must be signed in to change notification settings - Fork 79
/
mock-ldap-server.js
89 lines (71 loc) · 2.28 KB
/
mock-ldap-server.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
const ldap = require('ldapjs');
const db = require('./test/resources/mock_ldap_data.json');
const nconf = require('nconf');
const BASE_DN = 'dc=example,dc=org';
const LDAP_SERVER_PORT = 4444;
nconf.set('LDAP_URL', `ldap://0.0.0.0:${LDAP_SERVER_PORT}`);
nconf.set('LDAP_BASE', 'dc=example,dc=org');
nconf.set('LDAP_BIND_USER', 'cn=admin,dc=example,dc=org');
nconf.set('LDAP_BIND_PASSWORD', 'admin');
nconf.set('LDAP_USER_BY_NAME', '(&(objectClass=inetOrgPerson)(uid={0}))');
nconf.set(
'LDAP_SEARCH_QUERY',
'(&(objectClass=inetOrgPerson)(|(cn={0})(givenName={0})(sn={0})(uid={0})))'
);
nconf.set('LDAP_SEARCH_ALL_QUERY', '(objectClass=inetOrgPerson)');
nconf.set('LDAP_SEARCH_GROUPS', '(member={0})');
// This is an in-memory LDAP server used to run unit/integration tests
// It is based on the example of the ldapjs library: http://ldapjs.org/examples.html
const server = ldap.createServer();
server.bind(BASE_DN, function (req, res, next) {
var dn = req.dn.format({ skipSpace: true });
if (!db[dn]) return next(new ldap.NoSuchObjectError(dn));
if (!db[dn].userPassword)
return next(new ldap.NoSuchAttributeError('userPassword'));
if (db[dn].userPassword.indexOf(req.credentials) === -1)
return next(new ldap.InvalidCredentialsError());
res.end();
return next();
});
server.search(BASE_DN, function (req, res, next) {
var dn = req.dn.format({ skipSpace: true });
if (!db[dn]) return next(new ldap.NoSuchObjectError(dn));
var scopeCheck;
switch (req.scope) {
case 'base':
if (req.filter.matches(db[dn])) {
res.send({
dn: dn,
attributes: db[dn],
});
}
res.end();
return next();
case 'one':
scopeCheck = function (k) {
if (req.dn.equals(k)) return true;
var parent = ldap.parseDN(k).parent();
return parent ? parent.equals(req.dn) : false;
};
break;
case 'sub':
scopeCheck = function (k) {
return req.dn.equals(k) || req.dn.parentOf(k);
};
break;
}
Object.keys(db).forEach(function (key) {
if (!scopeCheck(key)) return;
if (req.filter.matches(db[key])) {
res.send({
dn: key,
attributes: db[key],
});
}
});
res.end();
return next();
});
server.listen(LDAP_SERVER_PORT, () => {
console.log(`LDAP server running on ${LDAP_SERVER_PORT}`);
});