2021-05-31 08:35:42 +00:00
|
|
|
/*
|
|
|
|
*
|
|
|
|
* (c) Copyright Ascensio System Limited 2010-2021
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
"use strict";
|
|
|
|
|
|
|
|
process.env.NODE_ENV = process.env.NODE_ENV || "development";
|
|
|
|
|
|
|
|
const fs = require("fs"),
|
|
|
|
http = require("http"),
|
|
|
|
express = require("express"),
|
|
|
|
morgan = require("morgan"),
|
|
|
|
cookieParser = require("cookie-parser"),
|
|
|
|
bodyParser = require("body-parser"),
|
|
|
|
session = require("express-session"),
|
|
|
|
winston = require("winston"),
|
2022-11-03 11:33:36 +00:00
|
|
|
WinstonCloudWatch = require('winston-cloudwatch'),
|
2021-05-31 08:35:42 +00:00
|
|
|
config = require("./config").get(),
|
|
|
|
path = require("path"),
|
|
|
|
exphbs = require("express-handlebars"),
|
|
|
|
favicon = require("serve-favicon"),
|
2022-11-03 11:33:36 +00:00
|
|
|
cors = require("cors"),
|
|
|
|
{ randomUUID } = require('crypto'),
|
|
|
|
date = require('date-and-time'),
|
|
|
|
os = require("os");
|
2021-05-31 08:35:42 +00:00
|
|
|
|
|
|
|
require('winston-daily-rotate-file');
|
|
|
|
|
|
|
|
const app = express();
|
2022-03-09 12:49:16 +00:00
|
|
|
|
|
|
|
let logpath = config["logPath"];
|
|
|
|
if(logpath != null)
|
|
|
|
{
|
|
|
|
if(!path.isAbsolute(logpath))
|
|
|
|
{
|
|
|
|
logpath = path.join(__dirname, logpath);
|
|
|
|
}
|
|
|
|
// ensure log directory exists
|
|
|
|
fs.existsSync(logpath) || fs.mkdirSync(logpath);
|
|
|
|
}
|
2021-05-31 08:35:42 +00:00
|
|
|
|
2022-11-16 09:44:53 +00:00
|
|
|
const aws = config["aws"].cloudWatch;
|
2022-11-03 11:33:36 +00:00
|
|
|
|
|
|
|
const accessKeyId = aws.accessKeyId;
|
|
|
|
const secretAccessKey = aws.secretAccessKey;
|
|
|
|
const awsRegion = aws.region;
|
|
|
|
const logGroupName = aws.logGroupName;
|
|
|
|
const logStreamName = aws.logStreamName;
|
|
|
|
|
2021-05-31 08:35:42 +00:00
|
|
|
let transports = [];
|
|
|
|
|
|
|
|
if (config.logger.file) {
|
2022-03-09 12:49:16 +00:00
|
|
|
let logDir = logpath ? logpath : (config.app.logDir[0] === "." ? path.join(__dirname, config.app.logDir) : config.app.logDir);
|
2021-05-31 08:35:42 +00:00
|
|
|
config.logger.file.filename = path.join(logDir, config.app.logName);
|
|
|
|
transports.push(new (winston.transports.DailyRotateFile)(config.logger.file));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (config.logger.console) {
|
|
|
|
transports.push(new (winston.transports.Console)(config.logger.console));
|
|
|
|
}
|
|
|
|
|
2022-11-03 11:33:36 +00:00
|
|
|
if (aws != null && aws.accessKeyId !== '')
|
|
|
|
{
|
|
|
|
transports.push(new WinstonCloudWatch({
|
|
|
|
name: 'aws',
|
|
|
|
level: "debug",
|
|
|
|
logStreamName: () => {
|
2022-11-08 12:37:52 +00:00
|
|
|
const hostname = os.hostname();
|
2022-11-03 11:33:36 +00:00
|
|
|
const now = new Date();
|
|
|
|
const guid = randomUUID();
|
|
|
|
const dateAsString = date.format(now, 'YYYY/MM/DDTHH.mm.ss');
|
|
|
|
|
2022-11-08 12:37:52 +00:00
|
|
|
return logStreamName.replace("${hostname}", hostname)
|
|
|
|
.replace("${applicationContext}", "SsoAuth")
|
|
|
|
.replace("${guid}", guid)
|
2022-11-03 11:33:36 +00:00
|
|
|
.replace("${date}", dateAsString);
|
|
|
|
},
|
2022-11-16 09:44:53 +00:00
|
|
|
logGroupName: logGroupName,
|
2022-11-03 11:33:36 +00:00
|
|
|
awsRegion: awsRegion,
|
|
|
|
jsonMessage: true,
|
|
|
|
awsOptions: {
|
|
|
|
credentials: {
|
|
|
|
accessKeyId: accessKeyId,
|
|
|
|
secretAccessKey: secretAccessKey
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
|
|
|
|
const customFormat = winston.format(info => {
|
|
|
|
const now = new Date();
|
|
|
|
|
|
|
|
info.date = date.format(now, 'YYYY-MM-DD HH:mm:ss');
|
|
|
|
info.applicationContext = "SsoAuth";
|
|
|
|
info.level = info.level.toUpperCase();
|
|
|
|
|
|
|
|
const hostname = os.hostname();
|
|
|
|
|
|
|
|
info["instance-id"] = hostname;
|
|
|
|
|
|
|
|
return info;
|
|
|
|
})();
|
|
|
|
|
|
|
|
|
2021-05-31 08:35:42 +00:00
|
|
|
let logger = winston.createLogger({
|
2022-11-03 11:33:36 +00:00
|
|
|
format: winston.format.combine(
|
|
|
|
customFormat,
|
|
|
|
winston.format.json()
|
|
|
|
),
|
2021-05-31 08:35:42 +00:00
|
|
|
transports: transports,
|
|
|
|
exitOnError: false
|
|
|
|
});
|
|
|
|
|
|
|
|
logger.stream = {
|
|
|
|
write: function(message) {
|
|
|
|
logger.info(message.trim());
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
// view engine setup
|
|
|
|
app.set("views", path.join(__dirname, "views"));
|
|
|
|
app.engine("handlebars", exphbs({ defaultLayout: "main" }));
|
|
|
|
app.set("view engine", "handlebars");
|
|
|
|
|
|
|
|
app.use(favicon(path.join(__dirname, "public", "favicon.ico")))
|
|
|
|
.use(morgan("combined", { "stream": logger.stream }))
|
|
|
|
.use(cookieParser())
|
|
|
|
.use(bodyParser.json())
|
|
|
|
.use(bodyParser.urlencoded({ extended: false }))
|
|
|
|
.use(session(
|
|
|
|
{
|
|
|
|
resave: true,
|
|
|
|
saveUninitialized: true,
|
2022-02-24 11:46:55 +00:00
|
|
|
secret: config["core"].machinekey ? config["core"].machinekey : config.app.machinekey
|
2021-05-31 08:35:42 +00:00
|
|
|
}))
|
|
|
|
.use(cors());
|
|
|
|
|
|
|
|
require("./app/middleware/saml")(app, config, logger);
|
|
|
|
require("./app/routes")(app, config, logger);
|
|
|
|
|
|
|
|
const httpServer = http.createServer(app);
|
|
|
|
|
|
|
|
httpServer.listen(config.app.port,
|
|
|
|
function () {
|
|
|
|
logger.info(`Start SSO Service Provider listening on port ${config.app.port} for http`);
|
|
|
|
});
|