2023-08-24 04:15:07 +02:00
|
|
|
const { ApiKey } = require("../../models/apiKeys");
|
|
|
|
const { SystemSettings } = require("../../models/systemSettings");
|
|
|
|
|
|
|
|
async function validApiKey(request, response, next) {
|
|
|
|
const multiUserMode = await SystemSettings.isMultiUserMode();
|
|
|
|
response.locals.multiUserMode = multiUserMode;
|
|
|
|
|
|
|
|
const auth = request.header("Authorization");
|
|
|
|
const bearerKey = auth ? auth.split(" ")[1] : null;
|
|
|
|
if (!bearerKey) {
|
|
|
|
response.status(403).json({
|
|
|
|
error: "No valid api key found.",
|
|
|
|
});
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-09-28 23:00:03 +02:00
|
|
|
if (!(await ApiKey.get({ secret: bearerKey }))) {
|
2023-08-24 04:15:07 +02:00
|
|
|
response.status(403).json({
|
|
|
|
error: "No valid api key found.",
|
|
|
|
});
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
next();
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = {
|
|
|
|
validApiKey,
|
|
|
|
};
|