This repository has been archived by the owner on May 19, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathldap.js
176 lines (144 loc) · 4.53 KB
/
ldap.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
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
var ldapbinding = require("./build/default/ldap_binding");
var Connection = function() {
var requests = {};
var binding = new ldapbinding.Connection();
var self = this;
var requestcount = 0;
var reconnects = 0;
var retries = 0;
var uri;
var openSuccessCB;
var openErrCB;
binding.addListener("search", function(msgid, result) {
if (typeof(requests[msgid].successCB) != "undefined") {
requests[msgid].successCB(result);
delete requests[msgid];
}
});
binding.addListener("event", function(msgid, error) {
if (typeof(requests[msgid].successCB) != "undefined") {
requests[msgid].successCB(error);
delete requests[msgid];
}
});
binding.addListener("unknown", function(errmsg, msgid, type) {
console.log("Unknown response detected "+errmsg+" "+msgid+" "+type);
});
self.reconnect = function() {
console.log("Reconnect starting");
binding.close();
openSuccessCB = function() {
reconnects++;
var newrequests = {};
for (msgid in requests) {
console.log("Resubmitting "+msgid);
if (typeof requests[msgid] != 'undefined') {
newrequests[requests[msgid].redo()] = requests[msgid];
}
}
requests = newrequests;
}
self.openWithRetry();
}
binding.addListener("serverdown", self.reconnect);
self.search = function(base, filter, attrs, successCB, errCB) {
requestcount++;
var r = new Request(successCB, errCB);
var msgid = r.doAction(function() {
return binding.search(base, filter, attrs);
});
requests[msgid] = r;
}
self.open = function(u, sCB, eCB) {
startup = new Date();
openSuccessCB = sCB;
openErrCB = eCB;
uri = u;
self.openWithRetry();
}
self.openWithRetry = function() {
if (binding.open(uri)) {
retries++;
setTimeout(self.openWithRetry, 1000);
console.log("Open failed. Retry in 1 s");
} else {
retries=0;
if (typeof openSuccessCB == 'function') { openSuccessCB(); }
}
}
self.reopen = function() {
binding.open(uri);
}
self.close = function() {
binding.close();
}
self.authenticate = function (username, password, callback) {
requestcount++;
var r = new Request(callback, null);
var msgid = r.doAction(function() {
return binding.authenticate(username, password);
});
requests[msgid] = r;
}
self.modify = function (dn, mods, callback) {
requestcount++;
var r = new Request(callback, null);
var msgid = r.doAction(function () {
return binding.modify(dn, mods);
});
requests[msgid] = r;
};
self.rename = function (dn, newrdn, callback) {
requestcount++;
var r = new Request(callback, null);
var msgid = r.doAction(function () {
return binding.rename(dn, newrdn, "", true);
});
requests[msgid] = r;
};
self.add = function (dn, attrs, callback) {
requestcount++;
var r = new Request(callback, null);
var msgid = r.doAction(function () {
return binding.add(dn, attrs);
});
requests[msgid] = r;
};
self.close = function(a) {
binding.close(a);
}
self.searchAuthenticate = function(base, filter, password, CB) {
self.search(base, filter, "", function(res) {
// TODO: see if there's only one result, and exit if not
if (res.length != 1) {
CB(0);
} else {
// we have the result. Use the DN to auth.
self.authenticate(res[0].dn, password, function(success, dn) {
CB(success, res[0].dn);
});
}
});
}
self.getStats = function() {
return { 'requests' : requestcount,
'startup' : startup,
'reconnects' : reconnects };
}
}
var Request = function(successCB, errCB) {
var self = this;
self.successCB = successCB;
self.errCB = errCB;
var startup = new Date();
self.doAction = function(func) {
self.action = func;
self.msgid = self.action();
return self.msgid;
}
self.redo = function() {
self.msgid = self.action();
return self.msgid;
}
}
exports.Connection = Connection;