-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathindex.js
More file actions
265 lines (251 loc) · 7.14 KB
/
index.js
File metadata and controls
265 lines (251 loc) · 7.14 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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
var crypto = require('crypto')
ldap = require('ldapjs'),
sync = require('synchronize')
;
var ldap_middleware = {
handleReq: function (req, res, next){
switch (req.method) {
case 'GET':
auth(req, res, next, get);
break;
case 'PUT':
auth(req, res, next, put);
break;
case 'POST':
post(req, res, next);
break;
case 'DELETE':
auth(req, res, next, del);
break;
}
},
passphrase: 'xxxxxxxxxxxxxxxxxxxxxxx',
algorithm: 'aes-256-ctr',
config: {
url: 'ldap://localhost/'
},
tokenTimeout: 30
};
function auth(req, res, next, func) {
var client = connect();
if(req.query.auth != undefined) {
if (req.query.auth != undefined) {
var auth = JSON.parse(decrypt(req.query.auth));
}
if (auth.expireTime < parseInt(new Date().getTime()/1000)) {
res.status(401).json({err: "Auth datas expired"});
} else if (auth.dn != undefined && auth.credential != undefined) {
client.bind(auth.dn, auth.credential, function (err) {
if (err) {
res.status(401).json({err: err});
} else {
func(client, req, res, next);
}
});
} else {
res.status(401).json({err: "Invalid auth datas"});
}
} else {
func(client, req, res, next);
}
}
function del(client, req, res, next) {
var parts = req._parsedUrl.pathname.substr(1).split('/');
var apiversion = parts.shift();
var dn = parts.shift();
client.del(dn, function(err) {
client.unbind(function(){});
if (err) {
res.status(400).json({err: err});
} else {
res.status(200).json({msg: "ok"});
}
});
}
function put(client, req, res, next) {
if (req.body.command == undefined) {
res.status(400).json({err: "No action found"});
return;
}
var parts = req._parsedUrl.pathname.substr(1).split('/');
var apiversion = parts.shift();
var dn = parts.shift();
switch(req.body.command) {
case 'modify':
if (req.body.actions == undefined || typeof req.body.actions != 'object' || Object.keys(req.body.actions).length < 1) {
res.status(400).json({err: "No actions provided "});
return;
}
sync.fiber(function() {
for (var action in req.body.actions) {
switch(action) {
case 'add':
case 'replace':
case 'delete':
var datas = req.body.actions[action];
if (req.body.actions[action]['jpegPhoto'] != undefined) {
datas['jpegPhoto'] = new Buffer(req.body.actions[action]['jpegPhoto'], 'base64');
}
var change = new ldap.Change({
operation: action,
modification: datas
});
try {
client.modify(dn, change, function(err) {
if (err) {
res.status(400).json({err: err});
client.unbind(function(){});
return;
} else if (!res._headerSent) {
res.status(200).json({msg: "ok"});
}
});
} catch (e) {
res.status(400).json({err: e.toString()});
client.unbind(function(){});
return;
}
break;
default:
res.status(400).json({err: "Unknown action " + action});
client.unbind(function(){});
return;
break;
}
}
});
break;
case 'add':
if (req.body.datas == undefined || typeof req.body.datas != 'object') {
res.status(400).json({err: "No datas provided "});
return;
}
try {
client.add(dn, req.body.datas, function(err) {
client.unbind(function(){});
if (err) {
res.status(400).json({err: err});
} else {
res.status(200).json({msg: "ok"});
}
});
} catch (e) {
res.status(400).json({err: e.toString()});
return;
}
break;
case 'modifydn':
if (req.body.newdn == undefined) {
res.status(400).json({err: "No new DN provided "});
return;
}
try {
client.modifyDN(dn, req.body.newdn, function(err) {
client.unbind(function(){});
if (err) {
res.status(400).json({err: err});
} else {
res.status(200).json({msg: "ok"});
}
});
} catch (e) {
res.status(400).json({err: e.toString()});
return;
}
break;
default:
res.status(400).json({err: "Unknown command " + req.body.command});
return;
break;
}
}
function get(client, req, res, next) {
var parts = req._parsedUrl.pathname.substr(1).split('/');
var entries = [];
try {
var opts = {};
var apiversion = parts.shift();
var dn = parts.shift();
while (part = parts.shift()) {
switch(part) {
case 'one':
case 'sub':
case 'base':
opts['scope'] = part;
break;
default:
if (part.indexOf('=') > -1) {
opts['filter'] = decodeURI(part);
} else {
opts['attributes'] = part.split(',');
}
break;
}
}
try {
client.search(dn, opts, function (err, lres) {
if (!err) {
lres.on('searchEntry', function(entry) {
var obj = entry.object;
var raw = entry.raw;
var jpegPhoto = raw.jpegPhoto;
if (jpegPhoto) obj.jpegPhoto = jpegPhoto.toString('base64');
entries.push(obj);
});
lres.on('error', function(err) {
res.status(400).json({err: err});
});
lres.on('end', function(result) {
client.unbind(function(){});
if(result.status == 0) {
res.json(entries);
} else {
res.status(400).json({err: result});
}
});
}
});
} catch (e) {
client.unbind(function(){});
res.status(400).json({err: e.toString()});
return;
}
} catch (e) {
client.unbind(function(){});
res.status(400).json({err: e.toString()});
return;
}
}
function connect() {
return ldap.createClient({
url: ldap_middleware.config['url'],
tlsOptions: ldap_middleware.config['options']
});
}
// res.send(req.originalUrl);
function post(req, res, next) {
if (req.body.dn != undefined && req.body.credential != undefined) {
var auth = {
expireTime:ldap_middleware.tokenTimeout + parseInt(new Date().getTime()/1000),
dn: req.body.dn,
credential: req.body.credential
};
var datas = encrypt(JSON.stringify(auth));
res.send(datas);
} else {
next();
}
}
function encrypt(text){
var cipher = crypto.createCipher(ldap_middleware.algorithm,ldap_middleware.passphrase)
var crypted = cipher.update(text,'utf8','hex')
crypted += cipher.final('hex');
return crypted;
}
function decrypt(text){
var decipher = crypto.createDecipher(ldap_middleware.algorithm,ldap_middleware.passphrase)
var dec = decipher.update(text,'hex','utf8')
dec += decipher.final('utf8');
return dec;
}
module.exports = ldap_middleware;