-
-
Notifications
You must be signed in to change notification settings - Fork 90
/
Copy pathLdapHelper.js
75 lines (55 loc) · 1.97 KB
/
LdapHelper.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
const BaseHelper = require('../BaseHelper.js');
export class LdapHelper {
loginLdap (url, domain, username, password) {
const Ldap = require('ldapjs');
return new Promise((resolve, reject) => {
const client = Ldap.createClient({
url: url,
timeout: 600000,
connectTimeout: 1000,
});
client.once('error', () => {
reject(false);
});
client.bind(`${domain}\\${username}`, (password ? password : ''), (err) => {
if (err) {
console.log(err);
resolve(false);
}
resolve(client);
});
});
}
async searchLdapUser (key) {
const client = await this.loginLdap(['ldap://yourldapserver.om:9999'], 'yourdomain', 'yourusername', 'thepasswordissecret');
let exist = {};
const opts = {
filter: `(${key})`,
scope: 'sub',
attributes: []
};
let done = await new Promise((resolve, reject) => {
client.search('DC=yourdomain,DC=com', opts, (err, res) => {
res.on('searchEntry', async (entry) => {
let ldapEntry = entry.object;
exist[ldapEntry.dn.split(',')] = ldapEntry.dn.split(',')[0];
resolve(ldapEntry);
});
res.on('resultError', (err) => {
console.error('error:' + err);
resolve(false);
});
res.on('error', (err) => {
console.error('error: ' + err.message);
resolve(false);
});
res.on('end', (result) => {
console.log('on: --------end: ' + result);
resolve(result);
});
});
});
return done;
}
}
module.exports = LdapHelper;