-
-
Notifications
You must be signed in to change notification settings - Fork 4.8k
/
MockLdapServer.js
48 lines (41 loc) · 1.16 KB
/
MockLdapServer.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
const ldapjs = require('ldapjs');
function newServer(port, dn, provokeSearchError = false) {
const server = ldapjs.createServer();
server.bind('o=example', function(req, res, next) {
if (req.dn.toString() !== dn || req.credentials !== 'secret')
return next(new ldapjs.InvalidCredentialsError());
res.end();
return next();
});
server.search('o=example', function(req, res, next) {
if (provokeSearchError) {
res.end(ldapjs.LDAP_SIZE_LIMIT_EXCEEDED);
return next();
}
const obj = {
dn: req.dn.toString(),
attributes: {
objectclass: ['organization', 'top'],
o: 'example',
},
};
const group = {
dn: req.dn.toString(),
attributes: {
objectClass: ['groupOfUniqueNames', 'top'],
uniqueMember: ['uid=testuser, o=example'],
cn: 'powerusers',
ou: 'powerusers',
},
};
if (req.filter.matches(obj.attributes)) {
res.send(obj);
}
if (req.filter.matches(group.attributes)) {
res.send(group);
}
res.end();
});
return new Promise(resolve => server.listen(port, () => resolve(server)));
}
module.exports = newServer;