1
0
mirror of https://github.com/node-red/node-red.git synced 2023-10-10 13:36:53 +02:00

Add optional basic-auth to HTTP Request node

Closes #160
This commit is contained in:
Nick O'Leary 2014-02-19 20:31:42 +00:00
parent ec0b5da29c
commit d8f2f24b44
3 changed files with 135 additions and 2 deletions

View File

@ -160,6 +160,19 @@
<label for="node-input-url"><i class="icon-tasks"></i> URL</label> <label for="node-input-url"><i class="icon-tasks"></i> URL</label>
<input type="text" id="node-input-url" placeholder="http://"> <input type="text" id="node-input-url" placeholder="http://">
</div> </div>
<div class="form-row">
<label>&nbsp;</label>
<input type="checkbox" id="node-input-useAuth" style="display: inline-block; width: auto; vertical-align: top;">
<label for="node-input-useAuth" style="width: 70%;">Use basic authentication?</label>
</div>
<div class="form-row node-input-useAuth-row">
<label for="node-config-input-user"><i class="icon-user"></i> Username</label>
<input type="text" id="node-config-input-user">
</div>
<div class="form-row node-input-useAuth-row">
<label for="node-config-input-pass"><i class="icon-lock"></i> Password</label>
<input type="password" id="node-config-input-pass">
</div>
<div class="form-row"> <div class="form-row">
<label for="node-input-name"><i class="icon-tag"></i> Name</label> <label for="node-input-name"><i class="icon-tag"></i> Name</label>
<input type="text" id="node-input-name" placeholder="Name"> <input type="text" id="node-input-name" placeholder="Name">
@ -193,7 +206,9 @@
defaults: { defaults: {
name: {value:""}, name: {value:""},
method:{value:"GET"}, method:{value:"GET"},
url:{value:""} url:{value:""},
//user -> credentials
//pass -> credentials
}, },
inputs:1, inputs:1,
outputs:1, outputs:1,
@ -204,6 +219,72 @@
}, },
labelStyle: function() { labelStyle: function() {
return this.name?"node_label_italic":""; return this.name?"node_label_italic":"";
},
oneditprepare: function() {
$.getJSON('http-request/'+this.id,function(data) {
if (data.user) {
$('#node-input-useAuth').prop('checked', true);
$(".node-input-useAuth-row").show();
$('#node-config-input-user').data("v",data.user);
$('#node-config-input-user').val(data.user);
} else {
$('#node-input-useAuth').prop('checked', false);
$(".node-input-useAuth-row").hide();
$('#node-config-input-user').data("v",'');
}
if (data.hasPassword) {
$('#node-input-useAuth').prop('checked', true);
$(".node-input-useAuth-row").show();
$('#node-config-input-pass').data("v",'__PWRD__');
$('#node-config-input-pass').val('__PWRD__');
} else {
$('#node-config-input-pass').data("v",'');
$('#node-config-input-pass').val('');
}
});
$("#node-input-useAuth").change(function() {
if ($(this).is(":checked")) {
$(".node-input-useAuth-row").show();
} else {
$(".node-input-useAuth-row").hide();
}
});
},
oneditsave: function() {
var oldUser = $('#node-config-input-user').data("v");
var oldPass = $('#node-config-input-pass').data("v");
var newUser = $('#node-config-input-user').val();
var newPass = $('#node-config-input-pass').val();
if (!$("#node-input-useAuth").is(":checked")) {
newUser = "";
newPass = "";
}
if (oldUser != newUser || oldPass != newPass) {
if (newUser == "" && newPass == "") {
$.ajax({
url: 'http-request/'+this.id,
type: 'DELETE',
success: function(result) {}
});
} else {
var credentials = {};
credentials.user = newUser;
if (newPass != '__PWRD__') {
credentials.password = newPass;
}
$.ajax({
url: 'http-request/'+this.id,
type: 'POST',
data: credentials,
success:function(result){}
});
}
return true;
}
} }
}); });
</script> </script>

View File

@ -130,6 +130,11 @@ function HTTPRequest(n) {
var nodeUrl = n.url; var nodeUrl = n.url;
var nodeMethod = n.method || "GET"; var nodeMethod = n.method || "GET";
var node = this; var node = this;
var credentials = RED.nodes.getCredentials(n.id);
if (credentials) {
this.username = credentials.user;
this.password = credentials.password;
}
this.on("input",function(msg) { this.on("input",function(msg) {
var url = msg.url||nodeUrl; var url = msg.url||nodeUrl;
@ -139,6 +144,9 @@ function HTTPRequest(n) {
if (msg.headers) { if (msg.headers) {
opts.headers = msg.headers; opts.headers = msg.headers;
} }
if (credentials) {
opts.auth = credentials.user+":"+credentials.password;
}
var req = ((/^https/.test(url))?https:http).request(opts,function(res) { var req = ((/^https/.test(url))?https:http).request(opts,function(res) {
res.setEncoding('utf8'); res.setEncoding('utf8');
msg.statusCode = res.statusCode; msg.statusCode = res.statusCode;
@ -172,3 +180,44 @@ function HTTPRequest(n) {
} }
RED.nodes.registerType("http request",HTTPRequest); RED.nodes.registerType("http request",HTTPRequest);
var querystring = require('querystring');
RED.httpAdmin.get('/http-request/:id',function(req,res) {
var credentials = RED.nodes.getCredentials(req.params.id);
if (credentials) {
res.send(JSON.stringify({user:credentials.user,hasPassword:(credentials.password&&credentials.password!="")}));
} else {
res.send(JSON.stringify({}));
}
});
RED.httpAdmin.delete('/http-request/:id',function(req,res) {
RED.nodes.deleteCredentials(req.params.id);
res.send(200);
});
RED.httpAdmin.post('/http-request/:id',function(req,res) {
var body = "";
req.on('data', function(chunk) {
body+=chunk;
});
req.on('end', function(){
var newCreds = querystring.parse(body);
var credentials = RED.nodes.getCredentials(req.params.id)||{};
if (newCreds.user == null || newCreds.user == "") {
delete credentials.user;
} else {
credentials.user = newCreds.user;
}
if (newCreds.password == "") {
delete credentials.password;
} else {
credentials.password = newCreds.password||credentials.password;
}
RED.nodes.addCredentials(req.params.id,credentials);
res.send(200);
});
});

View File

@ -160,7 +160,10 @@ RED.editor = function() {
oldValues[d] = $.extend(true,{},{v:editing_node[d]}).v; oldValues[d] = $.extend(true,{},{v:editing_node[d]}).v;
} }
} }
editing_node._def.oneditsave.call(editing_node); var rc = editing_node._def.oneditsave.call(editing_node);
if (rc === true) {
changed = true;
}
for (var d in editing_node._def.defaults) { for (var d in editing_node._def.defaults) {
if (oldValues[d] === null || typeof oldValues[d] === "string" || typeof oldValues[d] === "number") { if (oldValues[d] === null || typeof oldValues[d] === "string" || typeof oldValues[d] === "number") {