-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstrategy.js
executable file
·294 lines (246 loc) · 7.7 KB
/
strategy.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
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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
"use strict";
/**
* Module dependencies.
*/
const passport = require('passport-strategy');
const util = require('util');
const uuidv4 = require('uuid/v4');
const url = require('url');
const AD = require('ad');
const AdProxy = require('./lib/adproxy.js');
const toBinary = require('./lib/tobinary.js');
const Cache = require('./lib/cache');
const cache = new Cache();
/**
* `Strategy` constructor.
* Examples:
*
* @param {Function} verify Verifies the user.
* @api public
*/
const isFlagSet = (field, flag) => {
return (field & flag) === flag;
};
const decodeHttpAuthorizatioHeader = (header) => {
const header_data = header.split(' ');
if (header_data.length == 2 && header_data[0] == 'NTLM') {
return Buffer.from(header_data[1], 'base64');
}
};
const ntlmMessageType = (msg) => {
if (msg.toString('utf8', 0, 8) != 'NTLMSSP\0') {
return null;
}
const msg_type = msg.readUInt8(8);
if (![1, 3].includes(msg_type)) {
return null;
}
return msg_type;
};
const connectToProxy = (self, req, msg) => {
const serverUrl = url.parse(self._options.domaincontroller);
const adProxy = new AdProxy(serverUrl.hostname,
serverUrl.port,
self._options.domain,
decodeURI(serverUrl.path),
(serverUrl.protocol === 'ldaps:'),
self._options.tlsOptions
);
adProxy.negotiate(msg, (err, challenge) => {
if (err) {
adProxy.close();
self._options.warn('Error query to ad', self._options.domaincontroller, err);
return self.error('Error query to ad');
}
self._options.debug(`Send msg type 2 - '${req.url}' / '${req.connection.id}}'`);
cache.setProxy(req.connection.id, adProxy);
return self.fail(`NTLM ${challenge.toString('base64')}`);
});
};
const messageType1 = (self, req, msg) => {
connectToProxy(self, req, msg);
};
const parseNtlmAuthenticate = (msg) => {
let domainNameLen = msg.readUInt16LE(0x1C),
domainNameBufferOffset = msg.readUInt32LE(0x20),
domainName = msg.slice(domainNameBufferOffset, domainNameBufferOffset + domainNameLen),
userNameLen = msg.readUInt16LE(0x24),
userNameBufferOffset = msg.readUInt32LE(0x28),
userName = msg.slice(userNameBufferOffset, userNameBufferOffset + userNameLen),
workstationLen = msg.readUInt16LE(0x2C),
workstationBufferOffset = msg.readUInt32LE(0x30),
workstation = msg.slice(workstationBufferOffset, workstationBufferOffset + workstationLen);
if (isFlagSet(msg.readUInt8(0x3C), toBinary('00000001'))) {
domainName = domainName.toString('utf16le');
userName = userName.toString('utf16le');
workstation = workstation.toString('utf16le');
} else {
domainName = domainName.toString();
userName = userName.toString();
workstation = workstation.toString();
}
return {
user: userName,
domain: domainName,
workstation: workstation
};
};
const messageType3 = (self, req, msg) => {
self._options.debug(`Query msg type 3 - '${req.url}' / '${req.connection.id}}'`);
const adProxy = cache.getProxy(req.connection.id);
if(!!!adProxy) {
self._options.warn(`Not found ad proxy '${req.connection.id}'`);
return self.error('Error get ad proxy');
}
let { user, domain, workstation } = parseNtlmAuthenticate(msg);
if (!!!domain) {
domain = self._options.domain;
}
adProxy.authenticate(msg, (err, result) => {
if (!!err) {
cache.remove(req.connection.id);
self._options.warn('Error authenticate ad proxy', req.connection.id);
return self.error('Error authenticate ad proxy');
}
const userData = {
domain: domain,
user: user,
workstation: workstation,
authenticated: false
};
if (!!!result) {
cache.remove(req.connection.id);
self._options.warn(`Forbidden ${userData.user}@${userData.domain} - '${req.url}'`);
return self.fail();
} else {
const _ = () => {
userData.authenticated = true;
self._options.debug(`Success ${userData.user}@${userData.domain} - '${req.url}' / '${req.connection.id}}'`);
return self._verify(userData, (err, userData, info) => {
if (!!err) {
cache.remove(req.connection.id);
return self.error(err);
}
if (!!!user) {
cache.remove(req.connection.id);
return self.fail(info);
}
cache.setUser(req.connection.id, userData);
cache.closeProxy(req.connection.id);
self.success(userData, info);
});
};
if(self._options.domainuser) {
(new AD(Object.assign({
url: self._options.domaincontroller
}, self._options.domainuser)))
.user(`${userData.user}@${userData.domain}`)
.get()
.then((user) => {
Object.assign(userData, {
dn: user.dn,
displayName: user.displayName,
groups: user.groups.map((group) => { return group.cn; })
});
_();
})
.catch((err) => {
cache.remove(req.connection.id);
self._options.warn(`Error ad query: ${err}`);
return self.error('Error ad query');
});
}
else {
_();
}
}
});
};
function Strategy(options, verify) {
this._options = Object.assign(options, {
ttl: 1000 * 60 * 10,
debug: console.log,
warn: console.warn
});
if (!!!this._options.domain) {
throw new Error('Not set domain option');
}
if (!!!this._options.domaincontroller) {
throw new Error('Not set domaincontroller option');
}
this._verify = verify;
if (!!!verify) {
throw new TypeError('NTLM Strategy requires a verify callback');
}
if('domainuser' in this._options) {
if(!!!this._options.domainuser.user || !!!this._options.domainuser.pass) {
throw new Error('Not set domainuser option');
}
}
passport.Strategy.call(this);
this.name = 'ntlm';
cache.setTTL(this._options.ttl);
cache.setDebug(this._options.debug);
}
/**
* Inherit from `passport.Strategy`.
*/
util.inherits(Strategy, passport.Strategy);
/**
* Authenticate request based on the contents of a form submission.
* @param {Object} req HTTP request object.
* @api protected
*/
Strategy.prototype.authenticate = function (req, options) {
const self = this;
cache.clean();
if(!!self._options.session) {
if(!!!req.session || !!!req.session.id) {
throw new Error('Not create session');
}
if(req.connection.id != req.session.id) {
if(!!req.connection.id) {
cache.copy(req.connection.id, req.session.id);
}
req.connection.id = req.session.id;
}
}
else {
if (!!!req.connection.id) {
req.connection.id = uuidv4();
}
}
const userData = cache.user(req.connection.id);
if(!!userData) {
self._options.debug(`User ${userData.user}@${userData.domain} - '${req.url}' / '${req.connection.id}}' already login`);
return self.success(userData);
}
cache.add(req.connection.id);
const authHeader = req.headers.authorization;
if (!!!authHeader) {
self._options.debug(`Send NTLM http header - '${req.url}' / '${req.connection.id}}'`);
return self.fail('NTLM');
}
const msg = decodeHttpAuthorizatioHeader(authHeader);
if (!!!msg) {
self._options.debug(`Error parse http header '${authHeader}' - '${req.url}' / '${req.connection.id}}'`);
return self.error('Error parse header');
}
const ntlmVersion = ntlmMessageType(msg);
if(!!!ntlmVersion) {
self._options.debug(`Error parse ntlm message '${authHeader}' - '${req.url}' / '${req.connection.id}}'`);
return self.error('Error parse ntlm message');
}
if (ntlmVersion == 1) {
return messageType1(self, req, msg);
}
if (ntlmVersion == 3) {
return messageType3(self, req, msg);
}
self._options.warn(`Unsupported ntlm version '${authHeader}' ${ntlmVersion} - '${req.url}' / '${req.connection.id}}'`);
return self.error('Unsupported ntlm version');
};
/**
* Expose `Strategy`.
*/
module.exports = Strategy;