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

Update HTTP Request node to new credentials api

This commit is contained in:
Nick O'Leary 2014-07-19 00:22:58 +01:00
parent b604db83f6
commit d67a54a66a
2 changed files with 24 additions and 100 deletions

View File

@ -103,12 +103,12 @@
<label for="node-input-useAuth" style="width: 70%;">Use basic authentication?</label> <label for="node-input-useAuth" style="width: 70%;">Use basic authentication?</label>
</div> </div>
<div class="form-row node-input-useAuth-row"> <div class="form-row node-input-useAuth-row">
<label for="node-config-input-user"><i class="fa fa-user"></i> Username</label> <label for="node-input-user"><i class="fa fa-user"></i> Username</label>
<input type="text" id="node-config-input-user"> <input type="text" id="node-input-user">
</div> </div>
<div class="form-row node-input-useAuth-row"> <div class="form-row node-input-useAuth-row">
<label for="node-config-input-pass"><i class="fa fa-lock"></i> Password</label> <label for="node-input-password"><i class="fa fa-lock"></i> Password</label>
<input type="password" id="node-config-input-pass"> <input type="password" id="node-input-password">
</div> </div>
<div class="form-row"> <div class="form-row">
<label for="node-input-name"><i class="fa fa-tag"></i> Name</label> <label for="node-input-name"><i class="fa fa-tag"></i> Name</label>
@ -206,6 +206,10 @@
//user -> credentials //user -> credentials
//pass -> credentials //pass -> credentials
}, },
credentials: {
user: {type:"text"},
password: {type: "password"}
},
inputs:1, inputs:1,
outputs:1, outputs:1,
align: "right", align: "right",
@ -217,70 +221,23 @@
return this.name?"node_label_italic":""; return this.name?"node_label_italic":"";
}, },
oneditprepare: function() { oneditprepare: function() {
$.getJSON('http-request/'+this.id,function(data) { if (this.credentials.user || this.credentials.haspassword) {
if (data.user) { $('#node-input-useAuth').prop('checked', true);
$('#node-input-useAuth').prop('checked', true); $(".node-input-useAuth-row").show();
$(".node-input-useAuth-row").show(); } else {
$('#node-config-input-user').data("v",data.user); $('#node-input-useAuth').prop('checked', false);
$('#node-config-input-user').val(data.user); $(".node-input-useAuth-row").hide();
} 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() { $("#node-input-useAuth").change(function() {
if ($(this).is(":checked")) { if ($(this).is(":checked")) {
$(".node-input-useAuth-row").show(); $(".node-input-useAuth-row").show();
} else { } else {
$(".node-input-useAuth-row").hide(); $(".node-input-useAuth-row").hide();
$('#node-input-user').val('');
$('#node-input-password').val('');
} }
}); });
}, },
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

@ -149,7 +149,6 @@ module.exports = function(RED) {
var isTemplatedUrl = (nodeUrl||"").indexOf("{{") != -1; var isTemplatedUrl = (nodeUrl||"").indexOf("{{") != -1;
var nodeMethod = n.method || "GET"; var nodeMethod = n.method || "GET";
var node = this; var node = this;
var credentials = RED.nodes.getCredentials(n.id);
this.on("input",function(msg) { this.on("input",function(msg) {
node.status({fill:"blue",shape:"dot",text:"requesting"}); node.status({fill:"blue",shape:"dot",text:"requesting"});
var url; var url;
@ -170,10 +169,9 @@ module.exports = function(RED) {
opts.headers[v.toLowerCase()] = msg.headers[v]; opts.headers[v.toLowerCase()] = msg.headers[v];
} }
} }
if (credentials) { if (this.credentials.user) {
opts.auth = credentials.user+":"+(credentials.password||""); opts.auth = this.credentials.user+":"+(this.credentials.password||"");
} }
var payload = null; var payload = null;
if (msg.payload && (method == "POST" || method == "PUT") ) { if (msg.payload && (method == "POST" || method == "PUT") ) {
@ -221,42 +219,11 @@ module.exports = function(RED) {
req.end(); req.end();
}); });
} }
RED.nodes.registerType("http request",HTTPRequest);
RED.httpAdmin.get('/http-request/:id',function(req,res) { RED.nodes.registerType("http request",HTTPRequest,{
var credentials = RED.nodes.getCredentials(req.params.id); credentials: {
if (credentials) { user: {type:"text"},
res.send(JSON.stringify({user:credentials.user,hasPassword:(credentials.password&&credentials.password!=="")})); password: {type: "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);
});
});
} }