-
Notifications
You must be signed in to change notification settings - Fork 25
/
index.js
47 lines (39 loc) · 1.41 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
var async = require('async');
var request = require('request');
var ntlm = require('./lib/ntlm');
var HttpsAgent = require('agentkeepalive').HttpsAgent;
var _ = require('lodash');
var makeRequest = function(method, options, params, callback) {
var keepaliveAgent = new HttpsAgent();
if (!options.workstation) options.workstation = '';
if (!options.domain) options.domain = '';
function startAuth($) {
var type1msg = ntlm.createType1Message(options);
options.method = method;
options.headers = {
'Connection': 'keep-alive',
'Authorization': type1msg
};
options.agent = keepaliveAgent;
request(options, $);
}
function requestComplete(res, body, $) {
if (!res.headers['www-authenticate'])
return $(new Error('www-authenticate not found on response of second request'));
var type2msg = ntlm.parseType2Message(res.headers['www-authenticate']);
var type3msg = ntlm.createType3Message(type2msg, options);
options.method = method;
options.headers = {
'Connection': 'keep-alive',
'Authorization': type3msg
};
options.agent = keepaliveAgent;
options.json = params;
request(options, $);
}
async.waterfall([startAuth, requestComplete], callback);
};
exports.get = _.partial(makeRequest, 'get');
exports.post = _.partial(makeRequest, 'post');
exports.put = _.partial(makeRequest, 'put');
exports.delete = _.partial(makeRequest, 'delete');