-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathldap.js
More file actions
60 lines (56 loc) · 1.42 KB
/
ldap.js
File metadata and controls
60 lines (56 loc) · 1.42 KB
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
import dns from "dns";
import { promisify } from "util";
import ldapjs from "ldapjs";
const resolve = promisify(dns.resolveSrv);
// TODO: This is a race condition!
export let client;
export function searchOne(base, filter, attributes) {
return new Promise((resolve, reject) => {
client.search(
base,
{
filter,
scope: "one",
paged: false,
sizeLimit: 1,
...(attributes && { attributes }),
},
(err, res) => {
if (err) {
reject(err);
} else {
// Don't leak memory:
function onSearchEntry(entry) {
resolve(entry);
res.removeListener("end", onEnd);
}
function onEnd() {
res.removeListener("searchEntry", onSearchEntry);
reject(new Error("User not found!"));
}
res.once("searchEntry", onSearchEntry);
res.once("end", onEnd);
}
}
);
});
}
resolve("_ldap._tcp.csh.rit.edu").then((records) => {
client = ldapjs.createClient({
url: records.map((record) => `ldap://${record.name}:${record.port}`),
reconnect: true,
});
client.on("connect", () => {
console.log("Client connected. Binding...");
client.bind(
process.env.GK_LDAP_BIND_DN,
process.env.GK_LDAP_PASSWORD,
(err) => {
if (err) {
throw err;
}
console.log("LDAP is bound!");
}
);
});
});