forked from colynb/request-ntlm
-
Notifications
You must be signed in to change notification settings - Fork 13
/
index.js
69 lines (56 loc) · 1.98 KB
/
index.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
var _ = require('lodash');
var async = require('async');
var request = require('request');
var ntlm = require('./lib/ntlm');
var makeRequest = function(method, options, params, callback, pipeTarget) {
var KeepAlive = require('agentkeepalive');
if (options.url.toLowerCase().indexOf('https://') === 0) {
KeepAlive = KeepAlive.HttpsAgent;
}
var keepaliveAgent = new KeepAlive();
if (!options.workstation) options.workstation = '';
if (!options.ntlm_domain) options.ntlm_domain = '';
if (!options.headers ) options.headers = {};
options.ntlm = options.ntlm || {};
options.ntlm.strict = Boolean(options.ntlm.strict);
function startAuth($) {
var type1msg = ntlm.createType1Message(options);
options.method = method;
_.extend(options.headers, {
'Connection': 'keep-alive',
'Authorization': type1msg
});
options.agent = keepaliveAgent;
request(options, $);
}
function requestComplete(res, body, $) {
if (!res.headers['www-authenticate']) {
return options.ntlm.strict
? $(new Error('www-authenticate not found on response of second request'))
: $(null, res, body);
}
var type2msg = ntlm.parseType2Message(res.headers['www-authenticate']);
var type3msg = ntlm.createType3Message(type2msg, options);
options.method = method;
_.extend(options.headers, {
'Connection': 'keep-alive',
'Authorization': type3msg
});
options.agent = keepaliveAgent;
if (typeof params == "string")
options.body = params;
else
options.json = params;
if (pipeTarget) {
request(options, $).pipe(pipeTarget);
} else {
request(options, $);
}
}
async.waterfall([startAuth, requestComplete], callback);
};
exports.get = _.partial(makeRequest, 'get');
exports.post = _.partial(makeRequest, 'post');
exports.put = _.partial(makeRequest, 'put');
exports.patch = _.partial(makeRequest, 'patch');
exports.delete = _.partial(makeRequest, 'delete');