mirror of
https://github.com/node-red/node-red.git
synced 2025-03-01 10:36:34 +00:00
Rewrite credentials API to work with save on deploy.
This commit is contained in:
@@ -15,6 +15,7 @@
|
||||
**/
|
||||
|
||||
var util = require("util");
|
||||
var when = require("when");
|
||||
|
||||
var credentials = {};
|
||||
var storage = null;
|
||||
@@ -32,40 +33,6 @@ 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 (definition.hasOwnProperty(cred)) {
|
||||
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;
|
||||
@@ -93,14 +60,6 @@ function restGET(type) {
|
||||
|
||||
});
|
||||
}
|
||||
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) {
|
||||
@@ -148,8 +107,41 @@ module.exports = {
|
||||
register: function (type, definition) {
|
||||
var dashedType = type.replace(/\s+/g, '-');
|
||||
credentialsDef[dashedType] = definition;
|
||||
restDELETE(dashedType);
|
||||
restGET(dashedType);
|
||||
restPOST(dashedType);
|
||||
},
|
||||
/**
|
||||
* Merge the new credentials with the existings one
|
||||
* @param nodeID
|
||||
* @param nodeType
|
||||
* @param newCreds
|
||||
*/
|
||||
merge: function (nodeID, nodeType, newCreds) {
|
||||
var savedCredentials = Credentials.get(nodeID) || {};
|
||||
|
||||
if (!isRegistered(nodeType)) {
|
||||
util.log('Credential Type ' + nodeType + ' is not registered.');
|
||||
return;
|
||||
}
|
||||
|
||||
var definition = getCredDef(nodeType);
|
||||
for (var cred in definition) {
|
||||
if (definition.hasOwnProperty(cred)) {
|
||||
if (newCreds[cred] === undefined) {
|
||||
continue;
|
||||
}
|
||||
if (definition[cred].type == "password" && newCreds[cred] == '__PWRD__') {
|
||||
continue;
|
||||
}
|
||||
if (0 === newCreds[cred].length || /^\s*$/.test(newCreds[cred])) {
|
||||
delete savedCredentials[cred];
|
||||
continue;
|
||||
}
|
||||
savedCredentials[cred] = newCreds[cred];
|
||||
}
|
||||
}
|
||||
credentials[nodeID] = savedCredentials;
|
||||
},
|
||||
save: function () {
|
||||
return storage.saveCredentials(credentials);
|
||||
}
|
||||
}
|
||||
|
@@ -40,7 +40,26 @@ events.on('type-registered',function(type) {
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
var parseCredentials = function (config) {
|
||||
return when.promise(function (resolve, defect) {
|
||||
for (var i in config) {
|
||||
if (config.hasOwnProperty(i)) {
|
||||
var node = config[i];
|
||||
if (!node._creds) {
|
||||
continue;
|
||||
}
|
||||
var type = node.type;
|
||||
credentials.merge(node.id, type, node._creds);
|
||||
delete node._creds;
|
||||
}
|
||||
}
|
||||
credentials.save().then(function () {
|
||||
resolve(config);
|
||||
}).otherwise(function (err) {
|
||||
defect(err);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
var parseConfig = function() {
|
||||
var i;
|
||||
@@ -155,13 +174,16 @@ var flowNodes = module.exports = {
|
||||
getFlows: function() {
|
||||
return activeConfig;
|
||||
},
|
||||
setFlows: function(conf) {
|
||||
return storage.saveFlows(conf).then(function() {
|
||||
return stopFlows().then(function() {
|
||||
activeConfig = conf;
|
||||
parseConfig();
|
||||
});
|
||||
});
|
||||
setFlows: function (conf) {
|
||||
return parseCredentials(conf).then(function (confCredsRemoved) {
|
||||
return storage.saveFlows(confCredsRemoved).then(function () {
|
||||
return stopFlows().then(function () {
|
||||
activeConfig = confCredsRemoved;
|
||||
parseConfig();
|
||||
});
|
||||
})
|
||||
|
||||
})
|
||||
},
|
||||
stopFlows: stopFlows
|
||||
};
|
||||
|
Reference in New Issue
Block a user