2023-12-19 20:35:20 +01:00
|
|
|
const path = require("path");
|
2023-06-09 20:27:27 +02:00
|
|
|
process.env.NODE_ENV === "development"
|
|
|
|
? require("dotenv").config({ path: `.env.${process.env.NODE_ENV}` })
|
2023-12-19 20:35:20 +01:00
|
|
|
: require("dotenv").config({
|
|
|
|
path: process.env.STORAGE_DIR
|
|
|
|
? path.resolve(process.env.STORAGE_DIR, ".env")
|
|
|
|
: path.resolve(__dirname, ".env"),
|
|
|
|
});
|
2023-06-09 20:27:27 +02:00
|
|
|
const JWT = require("jsonwebtoken");
|
2023-07-25 19:37:04 +02:00
|
|
|
const { User } = require("../../models/user");
|
2024-05-08 01:35:47 +02:00
|
|
|
const { jsonrepair } = require("jsonrepair");
|
2024-05-08 03:06:31 +02:00
|
|
|
const extract = require("extract-json-from-string");
|
2023-06-09 20:27:27 +02:00
|
|
|
|
2023-06-04 04:28:07 +02:00
|
|
|
function reqBody(request) {
|
2023-06-08 06:31:35 +02:00
|
|
|
return typeof request.body === "string"
|
2023-06-04 04:28:07 +02:00
|
|
|
? JSON.parse(request.body)
|
|
|
|
: request.body;
|
|
|
|
}
|
|
|
|
|
|
|
|
function queryParams(request) {
|
|
|
|
return request.query;
|
|
|
|
}
|
|
|
|
|
2023-06-09 20:27:27 +02:00
|
|
|
function makeJWT(info = {}, expiry = "30d") {
|
2023-07-21 00:25:47 +02:00
|
|
|
if (!process.env.JWT_SECRET)
|
|
|
|
throw new Error("Cannot create JWT as JWT_SECRET is unset.");
|
|
|
|
return JWT.sign(info, process.env.JWT_SECRET, { expiresIn: expiry });
|
2023-06-09 20:27:27 +02:00
|
|
|
}
|
|
|
|
|
2024-01-11 21:11:45 +01:00
|
|
|
// Note: Only valid for finding users in multi-user mode
|
|
|
|
// as single-user mode with password is not a "user"
|
2023-07-25 19:37:04 +02:00
|
|
|
async function userFromSession(request, response = null) {
|
|
|
|
if (!!response && !!response.locals?.user) {
|
|
|
|
return response.locals.user;
|
|
|
|
}
|
|
|
|
|
|
|
|
const auth = request.header("Authorization");
|
|
|
|
const token = auth ? auth.split(" ")[1] : null;
|
|
|
|
|
|
|
|
if (!token) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
const valid = decodeJWT(token);
|
|
|
|
if (!valid || !valid.id) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2023-09-28 23:00:03 +02:00
|
|
|
const user = await User.get({ id: valid.id });
|
2023-07-25 19:37:04 +02:00
|
|
|
return user;
|
|
|
|
}
|
|
|
|
|
2023-06-09 20:27:27 +02:00
|
|
|
function decodeJWT(jwtToken) {
|
|
|
|
try {
|
2023-07-21 00:25:47 +02:00
|
|
|
return JWT.verify(jwtToken, process.env.JWT_SECRET);
|
2023-06-09 20:27:27 +02:00
|
|
|
} catch {}
|
2023-07-25 19:37:04 +02:00
|
|
|
return { p: null, id: null, username: null };
|
|
|
|
}
|
|
|
|
|
|
|
|
function multiUserMode(response) {
|
|
|
|
return response?.locals?.multiUserMode;
|
2023-06-09 20:27:27 +02:00
|
|
|
}
|
|
|
|
|
2023-12-04 17:38:15 +01:00
|
|
|
function parseAuthHeader(headerValue = null, apiKey = null) {
|
|
|
|
if (headerValue === null || apiKey === null) return {};
|
|
|
|
if (headerValue === "Authorization")
|
|
|
|
return { Authorization: `Bearer ${apiKey}` };
|
|
|
|
return { [headerValue]: apiKey };
|
|
|
|
}
|
|
|
|
|
2024-03-27 00:38:32 +01:00
|
|
|
function safeJsonParse(jsonString, fallback = null) {
|
2024-09-11 02:06:02 +02:00
|
|
|
if (jsonString === null) return fallback;
|
|
|
|
|
2024-03-27 00:38:32 +01:00
|
|
|
try {
|
|
|
|
return JSON.parse(jsonString);
|
|
|
|
} catch {}
|
2024-05-08 01:35:47 +02:00
|
|
|
|
|
|
|
if (jsonString?.startsWith("[") || jsonString?.startsWith("{")) {
|
|
|
|
try {
|
|
|
|
const repairedJson = jsonrepair(jsonString);
|
|
|
|
return JSON.parse(repairedJson);
|
|
|
|
} catch {}
|
|
|
|
}
|
|
|
|
|
2024-05-08 03:06:31 +02:00
|
|
|
try {
|
|
|
|
return extract(jsonString)[0];
|
|
|
|
} catch {}
|
|
|
|
|
2024-03-27 00:38:32 +01:00
|
|
|
return fallback;
|
|
|
|
}
|
|
|
|
|
2024-03-29 21:39:11 +01:00
|
|
|
function isValidUrl(urlString = "") {
|
|
|
|
try {
|
|
|
|
const url = new URL(urlString);
|
|
|
|
if (!["http:", "https:"].includes(url.protocol)) return false;
|
|
|
|
return true;
|
|
|
|
} catch (e) {}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2024-05-18 06:44:55 +02:00
|
|
|
function toValidNumber(number = null, fallback = null) {
|
|
|
|
if (isNaN(Number(number))) return fallback;
|
|
|
|
return Number(number);
|
|
|
|
}
|
|
|
|
|
2023-06-04 04:28:07 +02:00
|
|
|
module.exports = {
|
|
|
|
reqBody,
|
2023-07-25 19:37:04 +02:00
|
|
|
multiUserMode,
|
2023-06-04 04:28:07 +02:00
|
|
|
queryParams,
|
2023-06-09 20:27:27 +02:00
|
|
|
makeJWT,
|
|
|
|
decodeJWT,
|
2023-07-25 19:37:04 +02:00
|
|
|
userFromSession,
|
2023-12-04 17:38:15 +01:00
|
|
|
parseAuthHeader,
|
2024-03-27 00:38:32 +01:00
|
|
|
safeJsonParse,
|
2024-03-29 21:39:11 +01:00
|
|
|
isValidUrl,
|
2024-05-18 06:44:55 +02:00
|
|
|
toValidNumber,
|
2023-06-04 04:28:07 +02:00
|
|
|
};
|