Move credential http API handling to api component

This commit is contained in:
Nick O'Leary
2015-11-04 11:13:02 +00:00
parent 3b769fd2de
commit c90fd1e6d8
8 changed files with 190 additions and 146 deletions

46
red/api/credentials.js Normal file
View File

@@ -0,0 +1,46 @@
/**
* Copyright 2015 IBM Corp.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
**/
var api = require("../nodes");
module.exports = {
get: function (req, res) {
// TODO: It should verify the given node id is of the type specified -
// but that would add a dependency from this module to the
// registry module that knows about node types.
var nodeType = req.params.type;
var nodeID = req.params.id;
var credentials = api.getCredentials(nodeID);
if (!credentials) {
res.json({});
return;
}
var definition = api.getCredentialDefinition(nodeType);
var sendCredentials = {};
for (var cred in definition) {
if (definition.hasOwnProperty(cred)) {
if (definition[cred].type == "password") {
var key = 'has_' + cred;
sendCredentials[key] = credentials[cred] != null && credentials[cred] !== '';
continue;
}
sendCredentials[cred] = credentials[cred] || '';
}
}
res.json(sendCredentials);
}
}

View File

@@ -27,6 +27,7 @@ var library = require("./library");
var info = require("./info");
var theme = require("./theme");
var locales = require("./locales");
var credentials = require("./credentials");
var log = require("../log");
@@ -48,7 +49,6 @@ var errorHandler = function(err,req,res,next) {
function init(adminApp,storage) {
auth.init(settings,storage);
// Editor
if (!settings.disableEditor) {
ui.init(settings);
@@ -94,6 +94,8 @@ function init(adminApp,storage) {
adminApp.get("/nodes/:mod/:set",needsPermission("nodes.read"),nodes.getSet);
adminApp.put("/nodes/:mod/:set",needsPermission("nodes.write"),nodes.putSet);
adminApp.get('/credentials/:type/:id', needsPermission("credentials.read"),credentials.get);
adminApp.get(/locales\/(.+)\/?$/,locales.get);
// Library