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:
commit
87410621c7
@ -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) {
|
function validateNodeProperty(node,property,value) {
|
||||||
var valid = true;
|
var valid = true;
|
||||||
if ("required" in node._def.defaults[property] && node._def.defaults[property].required) {
|
if ("required" in node._def.defaults[property] && node._def.defaults[property].required) {
|
||||||
@ -158,11 +181,9 @@ RED.editor = function() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (editing_node._def.credentials) {
|
if (editing_node._def.credentials) {
|
||||||
// TODO: credentials
|
var prefix = 'node-input';
|
||||||
// 1. call $.ajax/POST 'credentials/'+editing_node.type+"/"+editing_node.id
|
var credDefinition = editing_node._def.credentials;
|
||||||
// with the new values - only pass back password fields
|
sendCredentials(editing_node,credDefinition,prefix);
|
||||||
// that do not equal '__PWRD__'
|
|
||||||
// See 10-mqtt.html:150 for example code
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@ -312,11 +333,22 @@ RED.editor = function() {
|
|||||||
attachPropertyChangeHandler(node,d,prefix);
|
attachPropertyChangeHandler(node,d,prefix);
|
||||||
}
|
}
|
||||||
if (definition.credentials) {
|
if (definition.credentials) {
|
||||||
// TODO: credentials
|
// TODO: Validate credentials fields
|
||||||
// 1. call $.getJSON("credentials/"+node.type+"/"+node.id)
|
|
||||||
// 2. with the response, foreach definition.credentials:
|
$.getJSON(getCredentialsURL(node.type, node.id), function (data) {
|
||||||
// 1. if response.X exists, set prefix-X input field to response.X
|
for (var cred in definition.credentials) {
|
||||||
// 2. if response.hasX exists, set prefix-X password field to '__PWRD__'
|
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) {
|
if (definition.oneditprepare) {
|
||||||
definition.oneditprepare.call(node);
|
definition.oneditprepare.call(node);
|
||||||
@ -370,8 +402,12 @@ RED.editor = function() {
|
|||||||
var configTypeDef = RED.nodes.getType(configType);
|
var configTypeDef = RED.nodes.getType(configType);
|
||||||
|
|
||||||
if (configTypeDef.credentials) {
|
if (configTypeDef.credentials) {
|
||||||
// CREDENTIAL TODO
|
$.ajax({
|
||||||
// 1. call $.ajax/DELETE "credentials/"+configType+"/"+configId
|
url: getCredentialsURL(configType, configId),
|
||||||
|
type: 'DELETE',
|
||||||
|
success: function (result) {
|
||||||
|
}
|
||||||
|
});
|
||||||
}
|
}
|
||||||
if (configTypeDef.ondelete) {
|
if (configTypeDef.ondelete) {
|
||||||
configTypeDef.ondelete.call(RED.nodes.node(configId));
|
configTypeDef.ondelete.call(RED.nodes.node(configId));
|
||||||
@ -462,11 +498,7 @@ RED.editor = function() {
|
|||||||
updateConfigNodeSelect(configProperty,configType,configId);
|
updateConfigNodeSelect(configProperty,configType,configId);
|
||||||
}
|
}
|
||||||
if (configTypeDef.credentials) {
|
if (configTypeDef.credentials) {
|
||||||
// TODO: credentials
|
sendCredentials(configNode,configTypeDef.credentials,"node-config-input");
|
||||||
// 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
|
|
||||||
}
|
}
|
||||||
if (configTypeDef.oneditsave) {
|
if (configTypeDef.oneditsave) {
|
||||||
configTypeDef.oneditsave.call(RED.nodes.node(configId));
|
configTypeDef.oneditsave.call(RED.nodes.node(configId));
|
||||||
|
@ -19,7 +19,7 @@ var EventEmitter = require("events").EventEmitter;
|
|||||||
var clone = require("clone");
|
var clone = require("clone");
|
||||||
|
|
||||||
var flows = require("./flows");
|
var flows = require("./flows");
|
||||||
|
var credentials = require('./credentials')
|
||||||
var comms = require("../comms");
|
var comms = require("../comms");
|
||||||
|
|
||||||
function Node(n) {
|
function Node(n) {
|
||||||
@ -120,5 +120,4 @@ Node.prototype.error = function(msg) {
|
|||||||
Node.prototype.status = function(status,retain) {
|
Node.prototype.status = function(status,retain) {
|
||||||
comms.publish("status/"+this.id,status,retain);
|
comms.publish("status/"+this.id,status,retain);
|
||||||
}
|
}
|
||||||
|
|
||||||
module.exports = Node;
|
module.exports = Node;
|
||||||
|
@ -18,33 +18,114 @@ var util = require("util");
|
|||||||
|
|
||||||
var credentials = {};
|
var credentials = {};
|
||||||
var storage = null;
|
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 = {
|
module.exports = {
|
||||||
init: function(_storage) {
|
init: function (_storage) {
|
||||||
storage = _storage;
|
storage = _storage;
|
||||||
|
redApp = require("../server").app;
|
||||||
|
Credentials = this;
|
||||||
},
|
},
|
||||||
load: function() {
|
load: function () {
|
||||||
return storage.getCredentials().then(function(creds) {
|
return storage.getCredentials().then(function (creds) {
|
||||||
credentials = creds;
|
credentials = creds;
|
||||||
}).otherwise(function(err) {
|
}).otherwise(function (err) {
|
||||||
util.log("[red] Error loading credentials : "+err);
|
util.log("[red] Error loading credentials : " + err);
|
||||||
});
|
});
|
||||||
},
|
},
|
||||||
add: function(id,creds) {
|
add: function (id, creds) {
|
||||||
credentials[id] = creds;
|
credentials[id] = creds;
|
||||||
storage.saveCredentials(credentials);
|
storage.saveCredentials(credentials);
|
||||||
},
|
},
|
||||||
|
|
||||||
get: function(id) {
|
get: function (id) {
|
||||||
return credentials[id];
|
return credentials[id];
|
||||||
},
|
},
|
||||||
|
|
||||||
delete: function(id) {
|
delete: function (id) {
|
||||||
delete credentials[id];
|
delete credentials[id];
|
||||||
storage.saveCredentials(credentials);
|
storage.saveCredentials(credentials);
|
||||||
},
|
},
|
||||||
|
|
||||||
clean: function(getNode) {
|
clean: function (getNode) {
|
||||||
var deletedCredentials = false;
|
var deletedCredentials = false;
|
||||||
for (var c in credentials) {
|
for (var c in credentials) {
|
||||||
var n = getNode(c);
|
var n = getNode(c);
|
||||||
@ -57,5 +138,12 @@ module.exports = {
|
|||||||
storage.saveCredentials(credentials);
|
storage.saveCredentials(credentials);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
},
|
||||||
|
register: function (type, definition) {
|
||||||
|
var dashedType = type.replace(/\s+/g, '-');
|
||||||
|
credentialsDef[dashedType] = definition;
|
||||||
|
restDELETE(dashedType);
|
||||||
|
restGET(dashedType);
|
||||||
|
restPOST(dashedType);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -36,6 +36,7 @@ module.exports = {
|
|||||||
addCredentials: credentials.add,
|
addCredentials: credentials.add,
|
||||||
getCredentials: credentials.get,
|
getCredentials: credentials.get,
|
||||||
deleteCredentials: credentials.delete,
|
deleteCredentials: credentials.delete,
|
||||||
|
registerCredentials: credentials.register,
|
||||||
createNode: createNode,
|
createNode: createNode,
|
||||||
registerType: registry.registerType,
|
registerType: registry.registerType,
|
||||||
getType: registry.get,
|
getType: registry.get,
|
||||||
|
Loading…
Reference in New Issue
Block a user