Migrate MQTT nodes to new credentials api

This commit is contained in:
Nick O'Leary 2014-07-18 14:47:58 +01:00
parent c379f1b197
commit 760dd022dc
2 changed files with 13 additions and 83 deletions

View File

@ -115,8 +115,8 @@
<input type="text" id="node-config-input-user">
</div>
<div class="form-row">
<label for="node-config-input-pass"><i class="icon-lock"></i> Password</label>
<input type="password" id="node-config-input-pass">
<label for="node-config-input-password"><i class="icon-lock"></i> Password</label>
<input type="password" id="node-config-input-password">
</div>
</script>
@ -127,48 +127,14 @@
broker: {value:"",required:true},
port: {value:1883,required:true,validate:RED.validators.number()},
clientid: { value:"" }
//user -> credentials
//pass -> credentials
},
credentials: {
user: {type:"text"},
password: {type: "password"}
},
label: function() {
if (this.broker == "") { this.broker = "localhost"; }
return (this.clientid?this.clientid+"@":"")+this.broker+":"+this.port;
},
oneditprepare: function() {
$.getJSON('mqtt-broker/'+this.id,function(data) {
if (data.user) {
$('#node-config-input-user').val(data.user);
}
if (data.hasPassword) {
$('#node-config-input-pass').val('__PWRD__');
} else {
$('#node-config-input-pass').val('');
}
});
},
oneditsave: function() {
if (this.broker == "") { this.broker = "localhost"; }
var newUser = $('#node-config-input-user').val();
var newPass = $('#node-config-input-pass').val();
var credentials = {};
credentials.user = newUser;
if (newPass != '__PWRD__') {
credentials.password = newPass;
}
$.ajax({
url: 'mqtt-broker/'+this.id,
type: 'POST',
data: credentials,
success:function(result){}
});
},
ondelete: function() {
$.ajax({
url: 'mqtt-broker/'+this.id,
type: 'DELETE',
success: function(result) {}
});
}
});
</script>

View File

@ -24,54 +24,18 @@ module.exports = function(RED) {
this.broker = n.broker;
this.port = n.port;
this.clientid = n.clientid;
var credentials = RED.nodes.getCredentials(n.id);
if (credentials) {
this.username = credentials.user;
this.password = credentials.password;
if (this.credentials) {
this.username = this.credentials.user;
this.password = this.credentials.password;
}
}
RED.nodes.registerType("mqtt-broker",MQTTBrokerNode);
var querystring = require('querystring');
RED.httpAdmin.get('/mqtt-broker/: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.nodes.registerType("mqtt-broker",MQTTBrokerNode,{
credentials: {
user: {type:"text"},
password: {type: "password"}
}
});
RED.httpAdmin.delete('/mqtt-broker/:id',function(req,res) {
RED.nodes.deleteCredentials(req.params.id);
res.send(200);
});
RED.httpAdmin.post('/mqtt-broker/: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);
});
});
function MQTTInNode(n) {
RED.nodes.createNode(this,n);
this.topic = n.topic;