This repository has been archived by the owner on Mar 8, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 41
/
ldapserver.js
76 lines (64 loc) · 1.56 KB
/
ldapserver.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
//
// Copyright IBM Corp. All Rights Reserved.
//
// SPDX-License-Identifier: Apache-2.0
//
'use strict';
const ldap = require('ldapjs');
const authorize = function (req, res, next) {
return next();
};
const SUFFIX = 'dc=example, dc=org';
let server = null;
const db = {
alice: {
dn: 'cn=alice, dc=example, dc=org',
attributes: {
uid: 'alice',
name: 'Alice',
mail: 'alice@example.org'
}
}
};
exports.start = function (port, password) {
if (server) {
return Promise.resolve();
}
server = ldap.createServer();
server.bind('cn=root, dc=example, dc=org', function(req, res, next) {
if (req.dn.toString() !== 'cn=root, dc=example, dc=org' || req.credentials !== password) {
return next(new ldap.InvalidCredentialsError());
}
res.end();
return next();
});
server.bind(SUFFIX, authorize, function(req, res, next) {
let dn = req.dn.toString();
if (dn !== 'cn=alice, dc=example, dc=org' || req.credentials !== password) {
return next(new ldap.InvalidCredentialsError());
}
res.end();
return next();
});
server.search(SUFFIX, authorize, function(req, res, next) {
if (req.filter.attribute === 'uid' && req.filter.value === 'alice') {
res.send(db.alice);
}
res.end();
return next();
});
return new Promise((resolve, reject) => {
server.listen(port, (error) => {
if (error) {
return reject(error);
}
resolve(server.address().port);
});
});
};
exports.close = function () {
if (server) {
server.close();
server = null;
}
};