-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathhttp-ntlm-req.js
97 lines (88 loc) · 3.12 KB
/
http-ntlm-req.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
module.exports = function (RED) {
function HttpNtlmReqNode(config) {
var httpntlm = require('httpntlm');
RED.nodes.createNode(this, config);
const node = this;
const resetStatus = () => node.status({});
const raiseError = (text, msg) => {
node.status({ fill: "red", shape: "dot", text: text });
node.error(text, msg);
};
node.name = config.name;
node.url = config.url;
node.method = config.method;
node.authconf = RED.nodes.getNode(config.auth);
resetStatus();
node.on('input', function (msg) {
const requestCallback = (err, res) => {
resetStatus();
//console.log("[requestCallback]", res)
if (res !== undefined && res.body !== undefined) {
//msg.payload = JSON.parse(res.body);
msg.payload = res.body;
msg.statusCode = res.statusCode;
msg.headers = res.headers || {}; // return headers
if (res.statusCode >= 400) {
raiseError('Response from server: ' + res.statusCode, msg);
}
} else {
raiseError(err.message, msg);
}
node.send(msg);
};
var params = (typeof msg.params === 'undefined') ? "" : msg.params;
var url = (typeof msg.url === 'undefined') ? node.url : msg.url;
var authconf = node.authconf;
if(msg.ntlm_config !== undefined) {
authconf.user = (typeof msg.ntlm_config.user !== 'undefined') ? msg.ntlm_config.user : node.authconf.user;
authconf.pass = (typeof msg.ntlm_config.pass !== 'undefined') ? msg.ntlm_config.pass : node.authconf.pass;
authconf.doman = (typeof msg.ntlm_config.doman !== 'undefined') ? msg.ntlm_config.doman : node.authconf.doman;
}
const connData = {
username: authconf.user,
password: authconf.pass,
domain: authconf.doman,
workstation: '',
headers: (typeof msg.headers === 'undefined') ? {} : msg.headers
};
console.log("[connData]",connData);
switch (parseInt(node.method)) {
case 0: // GET
{
connData.url = url + params;
httpntlm.get(connData, requestCallback);
break;
}
case 1: // POST
{
connData.url = url + params;
if (msg.payload !== undefined) {
//console.log(msg.payload);
//connData.body = JSON.stringify(msg.payload);
connData.body = (typeof msg.payload === 'object') ? JSON.stringify(msg.payload) : msg.payload;
connData.headers['Content-Type'] = (typeof connData.headers['Content-Type'] === 'undefined') ? 'application/json' : connData.headers['Content-Type'];
}
httpntlm.post(connData, requestCallback);
break;
}
case 2: // PUT
{
connData.url = url + params;
if (msg.payload !== undefined) {
//connData.body = JSON.stringify(msg.payload);
connData.body = (typeof msg.payload === 'object') ? JSON.stringify(msg.payload) : msg.payload;
connData.headers['Content-Type'] = (typeof connData.headers['Content-Type'] === 'undefined') ? 'application/json' : connData.headers['Content-Type'];
}
httpntlm.put(connData, requestCallback);
break;
}
default:
{
raiseError('No method defined!', msg);
break;
}
}
});
}
RED.nodes.registerType("http-ntlm-req", HttpNtlmReqNode);
}