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

Merge pull request #222 from Belphemur/node-credentials

Node credentials
This commit is contained in:
Nick O'Leary 2014-05-09 15:11:55 +01:00
commit 87410621c7
4 changed files with 184 additions and 64 deletions

View File

@ -31,6 +31,29 @@ RED.editor = function() {
}
}
function getCredentialsURL(nodeType, nodeID) {
var dashedType = nodeType.replace(/\s+/g, '-');
return 'credentials/' + dashedType + "/" + nodeID;
}
function sendCredentials(node,credDefinition,prefix) {
var credentials = {};
for (var cred in credDefinition) {
var input = $("#" + prefix + '-' + cred);
var value = input.val();
if (credDefinition[cred].type == 'password' && value == '__PWRD__') {
continue;
}
credentials[cred] = value;
}
$.ajax({
url: getCredentialsURL(node.type, node.id),
type: 'POST',
data: credentials,
success: function (result) {
}
});
}
function validateNodeProperty(node,property,value) {
var valid = true;
if ("required" in node._def.defaults[property] && node._def.defaults[property].required) {
@ -158,11 +181,9 @@ RED.editor = function() {
}
}
if (editing_node._def.credentials) {
// TODO: credentials
// 1. call $.ajax/POST 'credentials/'+editing_node.type+"/"+editing_node.id
// with the new values - only pass back password fields
// that do not equal '__PWRD__'
// See 10-mqtt.html:150 for example code
var prefix = 'node-input';
var credDefinition = editing_node._def.credentials;
sendCredentials(editing_node,credDefinition,prefix);
}
@ -312,11 +333,22 @@ RED.editor = function() {
attachPropertyChangeHandler(node,d,prefix);
}
if (definition.credentials) {
// TODO: credentials
// 1. call $.getJSON("credentials/"+node.type+"/"+node.id)
// 2. with the response, foreach definition.credentials:
// 1. if response.X exists, set prefix-X input field to response.X
// 2. if response.hasX exists, set prefix-X password field to '__PWRD__'
// TODO: Validate credentials fields
$.getJSON(getCredentialsURL(node.type, node.id), function (data) {
for (var cred in definition.credentials) {
if (definition.credentials[cred].type == 'password') {
if (data['has' + cred]) {
$('#' + prefix + '-' + cred).val('__PWRD__');
}
else {
$('#' + prefix + '-' + cred).val('');
}
} else {
preparePropertyEditor(data, cred, prefix);
}
}
});
}
if (definition.oneditprepare) {
definition.oneditprepare.call(node);
@ -370,8 +402,12 @@ RED.editor = function() {
var configTypeDef = RED.nodes.getType(configType);
if (configTypeDef.credentials) {
// CREDENTIAL TODO
// 1. call $.ajax/DELETE "credentials/"+configType+"/"+configId
$.ajax({
url: getCredentialsURL(configType, configId),
type: 'DELETE',
success: function (result) {
}
});
}
if (configTypeDef.ondelete) {
configTypeDef.ondelete.call(RED.nodes.node(configId));
@ -462,11 +498,7 @@ RED.editor = function() {
updateConfigNodeSelect(configProperty,configType,configId);
}
if (configTypeDef.credentials) {
// TODO: credentials
// 1. call $.ajax/POST 'credentials/'+configType+"/"+configId
// with the new values - only pass back password fields
// that do not equal '__PWRD__'
// See 10-mqtt.html:150 for example code
sendCredentials(configNode,configTypeDef.credentials,"node-config-input");
}
if (configTypeDef.oneditsave) {
configTypeDef.oneditsave.call(RED.nodes.node(configId));

View File

@ -19,7 +19,7 @@ var EventEmitter = require("events").EventEmitter;
var clone = require("clone");
var flows = require("./flows");
var credentials = require('./credentials')
var comms = require("../comms");
function Node(n) {
@ -120,5 +120,4 @@ Node.prototype.error = function(msg) {
Node.prototype.status = function(status,retain) {
comms.publish("status/"+this.id,status,retain);
}
module.exports = Node;

View File

@ -18,33 +18,114 @@ var util = require("util");
var credentials = {};
var storage = null;
var credentialsDef = {};
var redApp = null;
var querystring = require('querystring');
var Credentials;
function getCredDef(type) {
var dashedType = type.replace(/\s+/g, '-');
return credentialsDef[dashedType];
}
function isRegistered(type) {
return getCredDef(type) != undefined;
}
function restPOST(type) {
redApp.post('/credentials/' + type + '/:id', function (req, res) {
var body = "";
req.on('data', function (chunk) {
body += chunk;
});
req.on('end', function () {
var nodeType = type;
var nodeID = req.params.id;
var newCreds = querystring.parse(body);
var credentials = Credentials.get(nodeID) || {};
var definition = getCredDef(nodeType);
for (var cred in definition) {
if (newCreds[cred] == undefined) {
continue;
}
if (definition[cred].type == "password" && newCreds[cred] == '__PWRD__') {
continue;
}
if (newCreds[cred] == '') {
delete credentials[cred];
}
credentials[cred] = newCreds[cred];
}
Credentials.add(nodeID, credentials);
res.send(200);
});
});
}
function restGET(type) {
redApp.get('/credentials/' + type + '/:id', function (req, res) {
var nodeType = type;
var nodeID = req.params.id;
var credentials = Credentials.get(nodeID);
if (credentials == undefined) {
res.json({});
return;
}
var definition = getCredDef(nodeType);
var sendCredentials = {};
for (var cred in definition) {
if (definition[cred].type == "password") {
var key = 'has' + cred;
sendCredentials[key] = credentials[cred] != null && credentials[cred] != '';
continue;
}
sendCredentials[cred] = credentials[cred] || '';
}
res.json(sendCredentials);
});
}
function restDELETE(type) {
redApp.delete('/credentials/' + type + '/:id', function (req, res) {
var nodeID = req.params.id;
Credentials.delete(nodeID);
res.send(200);
});
}
module.exports = {
init: function(_storage) {
init: function (_storage) {
storage = _storage;
redApp = require("../server").app;
Credentials = this;
},
load: function() {
return storage.getCredentials().then(function(creds) {
load: function () {
return storage.getCredentials().then(function (creds) {
credentials = creds;
}).otherwise(function(err) {
util.log("[red] Error loading credentials : "+err);
}).otherwise(function (err) {
util.log("[red] Error loading credentials : " + err);
});
},
add: function(id,creds) {
add: function (id, creds) {
credentials[id] = creds;
storage.saveCredentials(credentials);
},
get: function(id) {
get: function (id) {
return credentials[id];
},
delete: function(id) {
delete: function (id) {
delete credentials[id];
storage.saveCredentials(credentials);
},
clean: function(getNode) {
clean: function (getNode) {
var deletedCredentials = false;
for (var c in credentials) {
var n = getNode(c);
@ -57,5 +138,12 @@ module.exports = {
storage.saveCredentials(credentials);
}
},
register: function (type, definition) {
var dashedType = type.replace(/\s+/g, '-');
credentialsDef[dashedType] = definition;
restDELETE(dashedType);
restGET(dashedType);
restPOST(dashedType);
}
}

View File

@ -36,6 +36,7 @@ module.exports = {
addCredentials: credentials.add,
getCredentials: credentials.get,
deleteCredentials: credentials.delete,
registerCredentials: credentials.register,
createNode: createNode,
registerType: registry.registerType,
getType: registry.get,