-
Notifications
You must be signed in to change notification settings - Fork 2
/
app.js
99 lines (99 loc) · 3.17 KB
/
app.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
import createError from "http-errors";
import express from "express";
import { abuseCheck } from "@cityssm/express-abuse-points";
import compression from "compression";
import path from "path";
import cookieParser from "cookie-parser";
import csurf from "csurf";
import rateLimit from "express-rate-limit";
import session from "express-session";
import sqlite from "connect-sqlite3";
import * as configFunctions from "./helpers/configFns.js";
import routerLogin from "./routes/login.js";
import routerManage from "./routes/manage.js";
import debug from "debug";
const debugApp = debug("2fa-server:app");
const __dirname = ".";
export const app = express();
app.set("views", path.join(__dirname, "views"));
app.set("view engine", "ejs");
app.use(abuseCheck({
byIP: true
}));
app.use(compression());
app.use((req, _res, next) => {
debugApp(req.method + " " + req.url);
next();
});
app.use(express.json());
app.use(express.urlencoded({
extended: false
}));
app.use(cookieParser());
app.use(csurf({ cookie: true }));
const limiter = rateLimit({
windowMs: 60 * 1000,
max: 1000
});
app.use(limiter);
app.use(express.static(path.join(__dirname, "public")));
app.use("/lib/bulma-webapp-js", express.static(path.join(__dirname, "node_modules", "@cityssm", "bulma-webapp-js", "dist")));
app.use("/lib/fa5", express.static(path.join(__dirname, "node_modules", "@fortawesome", "fontawesome-free")));
const SQLiteStore = sqlite(session);
const sessionCookieName = configFunctions.getProperty("session.cookieName");
app.use(session({
store: new SQLiteStore({
dir: "data",
db: "sessions.db"
}),
name: sessionCookieName,
secret: configFunctions.getProperty("session.secret"),
resave: true,
saveUninitialized: false,
rolling: true,
cookie: {
maxAge: configFunctions.getProperty("session.maxAgeMillis"),
sameSite: "strict"
}
}));
app.use((request, response, next) => {
if (request.cookies[sessionCookieName] && !request.session.user) {
response.clearCookie(sessionCookieName);
}
next();
});
const sessionChecker = (request, response, next) => {
if (request.session.user && request.cookies[sessionCookieName]) {
return next();
}
return response.redirect("/login");
};
app.use(function (request, response, next) {
response.locals.configFns = configFunctions;
response.locals.user = request.session.user;
response.locals.csrfToken = request.csrfToken();
next();
});
app.get("/", sessionChecker, (_request, response) => {
response.redirect("/manage");
});
app.use("/login", routerLogin);
app.get("/logout", (request, response) => {
if (request.session.user && request.cookies[sessionCookieName]) {
request.session.destroy(null);
request.session = null;
response.clearCookie(sessionCookieName);
}
response.redirect("/login");
});
app.use("/manage", sessionChecker, routerManage);
app.use(function (_req, _res, next) {
next(createError(404));
});
app.use(function (err, req, res, _next) {
res.locals.message = err.message;
res.locals.error = req.app.get("env") === "development" ? err : {};
res.status(err.status || 500);
res.render("error");
});
export default app;