ldap.js
3.7 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
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
'use strict';
const assert = require('assert');
const Promise = require('any-promise');
const ldap = require('ldapjs');
let count = 0;
module.exports = app => {
app.ldapjs = createClient(app.config.ldapjs, app);
};
function createClient(config, app) {
assert(config.url && config.dn && config.password,
`[egg-ldapjs] 'url: ${config.url}', 'dn: ${config.dn}', 'password: ${config.password}' are required on config`);
app.coreLogger.info('[egg-ldapjs] connecting %s@%s:%s/%s',
config.dn, config.url);
const LdapClient = function(options) {
this.client = ldap.createClient(options);
};
function promisify(fn) {
return function() {
const client = this.client;
const args = Array.prototype.slice.call(arguments);
return new Promise(function(resolve, reject) {
args.push(function(err, result) {
if (err) reject(err);
else resolve(result);
});
client[fn].apply(client, args);
});
};
}
[ 'bind', 'add', 'compare', 'del', 'exop', 'modify', 'modifyDN', 'unbind' ].forEach(function(fn) {
LdapClient.prototype[fn] = promisify(fn);
});
LdapClient.prototype.destroy = function() { this.client.destroy(); };
LdapClient.prototype._search = promisify('search');
LdapClient.prototype.search = function(base, options, controls) {
const client = this.client;
return new Promise(function(resolve, reject) {
const searchCallback = function(err, result) {
const r = {
entries: [],
objEntries: [],
references: [],
};
result.on('searchEntry', function(entry) {
r.entries.push(entry);
r.objEntries.push(entry.object);
});
result.on('searchReference', function(reference) {
r.references.push(reference);
});
result.on('error', function(err) {
reject(err);
});
result.on('end', function(result) {
if (result.status === 0) {
resolve(r);
} else {
reject(new Error('non-zero status code: ' + result.status));
}
});
};
const args = ([ base, options, controls, searchCallback ])
.filter(function(x) { return typeof x !== 'undefined'; });
client.search.apply(client, args);
});
};
LdapClient.prototype.authenticate = function(base, cn, password) {
const _this = this;
return _this.bind('CN=' + cn + ',' + base, password).then(
function() {
return _this.search(base, { scope: 'sub', filter: '(cn=' + cn + ')' }).then(function(result) {
return result.entries[0].object;
});
},
function(err) {
if (err.name === 'InvalidCredentialsError') {
return null;
}
throw err;
}
);
};
LdapClient.prototype.authenticateUser = function(base, cn, password) {
const dnRegex = new RegExp('^CN=([^,]+),' + base + '$');
return this.authenticate(base, cn, password).then(function(result) {
if (result) {
let groups = [];
if (result.memberOf) {
groups = result.memberOf
.map(function(x) { return (x.match(dnRegex) || [])[1]; })
.filter(function(x) { return typeof x !== 'undefined'; });
}
return {
email: result.userPrincipalName,
name: result.displayName,
groups,
};
}
return null;
});
};
const client = new LdapClient({ url: config.url });
app.beforeStart(async () => {
const result = await client.bind(config.dn, config.password);
const index = count++;
app.coreLogger.info(`[egg-ldapjs] instance[${index}] status OK, client ready ${result}`);
});
return client;
}