-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathapp.js
322 lines (281 loc) · 9.75 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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
const createError = require("http-errors");
const express = require("express");
const path = require("path");
const fs = require("fs");
const passport = require("passport");
const cookieParser = require("cookie-parser");
const morganLogger = require("morgan");
const fileUpload = require("express-fileupload");
const openapi = require("express-openapi");
const swaggerUi = require("swagger-ui-express");
const httpProxy = require(path.resolve("bin/httpProxy."));
const userManager = require(path.resolve("bin/user."));
const querystring = require("querystring");
require("./bin/authentication.");
const { checkMainConfig, readMainConfig } = require("./model/config");
const util = require("./bin/util.");
const app = express();
const Sentry = require("@sentry/node");
const { userModel } = require("./model/users");
const { profileModel } = require("./model/profiles");
const { sourceModel } = require("./model/sources");
const { rdfDataModel } = require("./model/rdfData");
const config = readMainConfig();
const isValid = checkMainConfig(config);
if (!isValid) {
process.exit(1);
}
// sentry/glitchtip
if (config.sentryDsnNode) {
Sentry.init({ dsn: config.sentryDsnNode });
app.use(Sentry.Handlers.requestHandler());
}
// body parsers
app.use(express.json({ limit: "10mb" }));
//app.use(express.urlencoded({ extended: true }));
//ajout CF
app.use(express.urlencoded({ extended: true, limit: "20mb", parameterLimit: 10000 }));
/*
const bodyParser = require('body-parser');
app.use(bodyParser.json({
limit: '20mb'
}));
app.use(bodyParser.urlencoded({
limit: '20mb',
parameterLimit: 100000,
extended: true
}));*/
app.use(fileUpload());
// logger
app.use(morganLogger("dev"));
/**
* App middleware for authentication and session handling
*/
app.use(cookieParser());
if (config.cookieSecureTrustProxy) {
app.set("trust proxy", 1);
}
app.use(
require("express-session")({
secret: config.cookieSecret ? config.cookieSecret : "S3cRet!",
resave: false,
saveUninitialized: false,
cookie: {
maxAge: config.cookieMaxAge ? config.cookieMaxAge : config.cookieMaxAge,
sameSite: config.cookieSameSite ? config.cookieSameSite : false,
secure: config.cookieSecure ? config.cookieSecure : false,
},
})
);
app.use(function (req, res, next) {
var msgs = req.session.messages || [];
res.locals.messages = msgs;
res.locals.hasMessages = msgs.length > 0;
req.session.messages = [];
next();
});
if (config.auth !== "disabled") {
app.use(passport.initialize());
app.use(passport.authenticate("session"));
}
/**
* Routes
*/
// Static content
app.use(express.static(path.join(__dirname, "public")));
app.use(express.static(path.join(__dirname, "mainapp/static")));
app.use("/upload/rdf", express.static(path.join(__dirname, "data/uploaded_rdf_data")));
app.use("/vocables/classIcons", express.static(path.join(__dirname, "data/classIcons")));
async function loggedIn(req, _res, next) {
if (config.auth === "disabled" || req.isAuthenticated()) {
// TODO: check allowedTools and forbiddenTools
next();
} else {
throw {
status: 401,
message: "You must authenticate to access this resource.",
};
}
}
async function isPluginAllowed(tool, req, _res, next) {
const userInfo = await userManager.getUser(req.user);
const userTools = await profileModel.getUserTools(userInfo.user);
if (userTools.map((i) => i.name).includes(tool)) {
next();
} else {
throw {
status: 401,
message: "You must authenticate to access this resource.",
};
}
}
app.get("/plugins/:plugin/*", async (req, res) => {
try {
await loggedIn(req, res, () => {});
const plugin = req.params.plugin;
await isPluginAllowed(plugin, req, res, () => {});
const file = req.params["0"];
const filePath = path.join(__dirname, `plugins/${plugin}/public`, file);
if (!fs.existsSync(filePath)) {
res.status(404).send("Not found");
} else {
res.sendFile(file, { root: path.join(__dirname, `plugins/${plugin}/public`) });
}
} catch (error) {
res.status(error.status).send(error.message);
}
});
// view engine setup
app.set("views", path.join(__dirname, "views"));
app.set("view engine", "pug");
httpProxy.app = app;
// API
openapi.initialize({
apiDoc: require("./api/v1/api-doc.js"),
app: app,
paths: "./api/v1/paths",
securityHandlers: {
restrictLoggedUser: async function (req, _scopes, _definition) {
if (config.auth != "disabled") {
const token = req.headers.authorization;
if (token !== undefined) {
const output = util.parseAuthorizationFromHeader(token);
// Only accept the Bearer scheme from the Authorization header
if (output !== null && output[0] === "Bearer") {
req.user = await userModel.findUserAccountFromToken(output[1]);
}
}
if (config.auth == "keycloak") {
passport.authenticate("keycloak", { failureRedirect: "/login" });
}
if (!req.isAuthenticated || !req.isAuthenticated()) {
throw {
status: 401,
message: "You must authenticate to access this resource.",
};
}
}
return Promise.resolve(true);
},
restrictAdmin: async function (req, _scopes, _definition) {
const currentUser = await userManager.getUser(req.user);
if (req.url.indexOf("/sources") > 0 && currentUser.allowSourceCreation) {
return Promise.resolve(true);
}
if (!currentUser.logged) {
throw {
status: 401,
message: "You must authenticate to access this resource.",
};
}
if (!currentUser.user.groups.includes("admin")) {
throw {
status: 401,
message: "You must be admin to access this resource.",
};
}
return Promise.resolve(true);
},
},
});
// OpenAPI UI and documentation
app.use(
"/api/v1",
swaggerUi.serve,
swaggerUi.setup(null, {
swaggerOptions: {
url: "/api/v1/api-docs",
},
})
);
// Home (redirect to /vocables)
app.get("/", function (req, res, next) {
const query = querystring.stringify(req.query);
const redirect = query ? `vocables?${query}` : "/vocables";
res.redirect(redirect);
});
// Login routes
if (config.auth == "disabled") {
app.get("/login", function (req, res, _next) {
res.redirect("vocables");
});
} else if (config.auth == "keycloak" || config.auth == "auth0") {
app.get("/login", function (req, res, next) {
passport.authenticate(config.auth, { scope: ["openid", "email", "profile"] })(req, res, next);
});
app.get("/login/callback", function (req, res, next) {
passport.authenticate(config.auth, { successRedirect: "/vocables", failureRedirect: "/login" })(req, res, next);
});
} else {
app.get("/login", function (req, res, next) {
const redirect = formatRedirectPath(req.query.redirect);
res.render("login", { title: "souslesensVocables - Login", redirect: redirect });
});
app.post("/auth/login", function (req, res, next) {
const redirect = formatRedirectPath(req.query.redirect);
passport.authenticate("local", { successRedirect: redirect, failureRedirect: "/login", failureMessage: true })(req, res, next);
});
}
// catch 404 and forward to error handler
app.use(function (req, res, next) {
next(createError(404));
});
/**
* Error handlers
*/
// sentry/glitchtip
// (this error handler must be before any other error middleware and after all controllers)
if (config.sentryDsnNode) {
app.use(Sentry.Handlers.errorHandler());
}
// openapi error handler
app.use("/api/v1/*", function (err, req, res, _next) {
console.debug("API error", err);
const error = err.status ? err : err.stack;
if (req.app.get("env") === "development") {
res.status(err.status || 500).json(error);
} else {
res.status(err.status || 500);
}
});
// default error handler
app.use(function (err, req, res, _next) {
// set locals, only providing error in development
res.locals.message = err.message;
res.locals.error = req.app.get("env") === "development" ? err : {};
// render the error page
res.status(err.status || 500);
res.render("error");
});
function formatRedirectPath(path) {
if (!path) {
return "/vocables";
}
if (!path.startsWith("/")) {
return `/${path}`;
}
return path;
}
// Load default graph available in the "ttlUrl" key
// if no graph is already available in the Virtuoso
async function loadDefaultGraphs() {
const sources = await sourceModel.getAllSources();
try {
const graphs = await rdfDataModel.getGraphs();
Object.entries(sources).forEach(async ([_, source]) => {
const graphDownloadUrl = source.graphDownloadUrl;
if (graphDownloadUrl) {
const graphURI = source.graphUri;
const matchingGraph = graphs.find((g) => g.name === graphURI);
const hasGraph = matchingGraph !== undefined ?? matchingGraph.count > 0;
if (!hasGraph) {
await rdfDataModel.loadGraph(source.graphUri, graphDownloadUrl);
}
}
});
} catch (e) {
console.error("Could not load default graphs: " + e);
}
}
void loadDefaultGraphs();
module.exports = app;