2014-11-04 12:34:49 +01:00
|
|
|
/**
|
|
|
|
* Copyright 2014 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 express = require("express");
|
2015-07-15 23:43:24 +02:00
|
|
|
var bodyParser = require("body-parser");
|
2014-11-04 12:34:49 +01:00
|
|
|
var util = require('util');
|
2014-12-10 15:16:07 +01:00
|
|
|
var path = require('path');
|
2014-11-06 23:59:48 +01:00
|
|
|
var passport = require('passport');
|
2014-11-04 12:34:49 +01:00
|
|
|
|
|
|
|
var ui = require("./ui");
|
|
|
|
var nodes = require("./nodes");
|
|
|
|
var flows = require("./flows");
|
|
|
|
var library = require("./library");
|
2014-11-06 23:59:48 +01:00
|
|
|
var info = require("./info");
|
2015-04-13 01:11:11 +02:00
|
|
|
var theme = require("./theme");
|
2015-04-26 22:28:16 +02:00
|
|
|
var locales = require("./locales");
|
2015-11-04 12:13:02 +01:00
|
|
|
var credentials = require("./credentials");
|
2014-11-06 23:59:48 +01:00
|
|
|
|
2015-11-03 12:23:37 +01:00
|
|
|
var log = require("../log");
|
|
|
|
|
2014-11-06 23:59:48 +01:00
|
|
|
var auth = require("./auth");
|
2015-02-06 00:43:35 +01:00
|
|
|
var needsPermission = auth.needsPermission;
|
2014-11-04 12:34:49 +01:00
|
|
|
|
|
|
|
var settings = require("../settings");
|
|
|
|
|
|
|
|
var errorHandler = function(err,req,res,next) {
|
2015-11-03 12:23:37 +01:00
|
|
|
if (err.message === "request entity too large") {
|
|
|
|
log.error(err);
|
|
|
|
} else {
|
|
|
|
console.log(err.stack);
|
|
|
|
}
|
|
|
|
log.audit({event: "api.error",error:err.code||"unexpected_error",message:err.toString()},req);
|
2015-07-15 23:43:24 +02:00
|
|
|
res.status(400).json({error:"unexpected_error", message:err.toString()});
|
2014-11-06 11:00:25 +01:00
|
|
|
};
|
2014-11-04 12:34:49 +01:00
|
|
|
|
2015-03-14 00:37:59 +01:00
|
|
|
function init(adminApp,storage) {
|
2015-07-08 18:07:14 +02:00
|
|
|
|
2015-03-14 00:37:59 +01:00
|
|
|
auth.init(settings,storage);
|
2014-11-12 14:21:39 +01:00
|
|
|
// Editor
|
|
|
|
if (!settings.disableEditor) {
|
2015-04-13 01:11:11 +02:00
|
|
|
ui.init(settings);
|
2014-11-12 14:21:39 +01:00
|
|
|
var editorApp = express();
|
2015-04-08 22:29:55 +02:00
|
|
|
editorApp.get("/",ui.ensureSlash,ui.editor);
|
2014-11-12 14:21:39 +01:00
|
|
|
editorApp.get("/icons/:icon",ui.icon);
|
2015-04-13 01:11:11 +02:00
|
|
|
if (settings.editorTheme) {
|
|
|
|
editorApp.use("/theme",theme.init(settings));
|
|
|
|
}
|
2015-04-08 22:29:55 +02:00
|
|
|
editorApp.use("/",ui.editorResources);
|
2014-11-12 14:21:39 +01:00
|
|
|
adminApp.use(editorApp);
|
|
|
|
}
|
2015-11-03 12:23:37 +01:00
|
|
|
var maxApiRequestSize = settings.apiMaxLength || '1mb';
|
|
|
|
adminApp.use(bodyParser.json({limit:maxApiRequestSize}));
|
|
|
|
adminApp.use(bodyParser.urlencoded({limit:maxApiRequestSize,extended:true}));
|
2015-03-25 21:32:40 +01:00
|
|
|
|
|
|
|
adminApp.get("/auth/login",auth.login);
|
2015-07-08 18:07:14 +02:00
|
|
|
|
2015-01-28 23:41:13 +01:00
|
|
|
if (settings.adminAuth) {
|
|
|
|
//TODO: all passport references ought to be in ./auth
|
|
|
|
adminApp.use(passport.initialize());
|
|
|
|
adminApp.post("/auth/token",
|
|
|
|
auth.ensureClientSecret,
|
|
|
|
auth.authenticateClient,
|
|
|
|
auth.getToken,
|
|
|
|
auth.errorHandler
|
|
|
|
);
|
2015-05-14 15:22:28 +02:00
|
|
|
adminApp.post("/auth/revoke",needsPermission(""),auth.revoke);
|
2015-01-28 23:41:13 +01:00
|
|
|
}
|
2014-11-06 23:59:48 +01:00
|
|
|
|
|
|
|
// Flows
|
2014-12-10 15:16:07 +01:00
|
|
|
adminApp.get("/flows",needsPermission("flows.read"),flows.get);
|
|
|
|
adminApp.post("/flows",needsPermission("flows.write"),flows.post);
|
2015-07-08 18:07:14 +02:00
|
|
|
|
2014-11-06 23:59:48 +01:00
|
|
|
// Nodes
|
2014-12-10 15:16:07 +01:00
|
|
|
adminApp.get("/nodes",needsPermission("nodes.read"),nodes.getAll);
|
|
|
|
adminApp.post("/nodes",needsPermission("nodes.write"),nodes.post);
|
2014-11-06 11:00:25 +01:00
|
|
|
|
2014-12-10 15:16:07 +01:00
|
|
|
adminApp.get("/nodes/:mod",needsPermission("nodes.read"),nodes.getModule);
|
|
|
|
adminApp.put("/nodes/:mod",needsPermission("nodes.write"),nodes.putModule);
|
|
|
|
adminApp.delete("/nodes/:mod",needsPermission("nodes.write"),nodes.delete);
|
2015-07-08 18:07:14 +02:00
|
|
|
|
2014-12-10 15:16:07 +01:00
|
|
|
adminApp.get("/nodes/:mod/:set",needsPermission("nodes.read"),nodes.getSet);
|
|
|
|
adminApp.put("/nodes/:mod/:set",needsPermission("nodes.write"),nodes.putSet);
|
2014-11-05 23:41:05 +01:00
|
|
|
|
2015-11-04 12:13:02 +01:00
|
|
|
adminApp.get('/credentials/:type/:id', needsPermission("credentials.read"),credentials.get);
|
|
|
|
|
2015-07-08 18:07:14 +02:00
|
|
|
adminApp.get(/locales\/(.+)\/?$/,locales.get);
|
2015-04-26 22:28:16 +02:00
|
|
|
|
2014-11-06 23:59:48 +01:00
|
|
|
// Library
|
2014-11-12 14:21:39 +01:00
|
|
|
library.init(adminApp);
|
2014-12-10 15:16:07 +01:00
|
|
|
adminApp.post(new RegExp("/library/flows\/(.*)"),needsPermission("library.write"),library.post);
|
|
|
|
adminApp.get("/library/flows",needsPermission("library.read"),library.getAll);
|
|
|
|
adminApp.get(new RegExp("/library/flows\/(.*)"),needsPermission("library.read"),library.get);
|
2015-07-08 18:07:14 +02:00
|
|
|
|
2014-11-06 23:59:48 +01:00
|
|
|
// Settings
|
2014-12-10 15:16:07 +01:00
|
|
|
adminApp.get("/settings",needsPermission("settings.read"),info.settings);
|
2015-07-08 18:07:14 +02:00
|
|
|
|
2014-11-04 18:05:29 +01:00
|
|
|
// Error Handler
|
2014-11-04 12:34:49 +01:00
|
|
|
adminApp.use(errorHandler);
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = {
|
|
|
|
init: init
|
2014-11-06 11:00:25 +01:00
|
|
|
};
|