wf-radio/src/middleware/auth.js

85 lines
2.7 KiB
JavaScript
Raw Normal View History

2025-04-25 14:06:03 +02:00
const { getModByName } = require("../services/modService");
const { getModpackByName } = require("../services/modpackService");
const { getUserByName } = require("../services/userService");
const { verifyToken } = require("../utils/crypto");
const AppError = require("../utils/appError");
2025-04-25 14:06:03 +02:00
async function authenticateToken(req) {
2025-04-25 14:06:03 +02:00
const token = req.header("Authorization");
2025-04-25 14:06:03 +02:00
if (!token) {
throw new AppError(401, "Missing authorization header", "Unauthorized");
}
try {
2025-04-25 14:06:03 +02:00
req.token_infos = await verifyToken(token);
console.debug("Authorizing token from", req.token_infos);
} catch (err) {
throw new AppError(403, "Forbidden: Error verifying the authorization token");
}
return req.token_infos;
}
2025-04-25 14:06:03 +02:00
async function authorizeModModification(req) {
// Auth token
await authenticateToken(req);
// Get mod infos
2025-05-04 23:14:00 +02:00
if (!req.params || !req.params.name) {
2025-04-25 14:06:03 +02:00
throw new AppError(400, "No mod name was scpecified", "Bad request");
}
2025-05-04 23:14:00 +02:00
const mod_name = req.params.name;
const mod = await getModByName(mod_name);
2025-04-25 14:06:03 +02:00
if (!mod) {
throw new AppError(404, "No mod was found with this name", "Not found");
}
// Authorize
if ( mod.author != req.token_infos.username) {
throw new AppError(401, "Mod author differs from current user", "Unauthorized");
}
}
async function authorizeModpackModification(req) {
// Auth token
await authenticateToken(req);
// Get mod infos
2025-05-04 23:14:00 +02:00
if (!req.params || !req.params.name) {
2025-04-25 14:06:03 +02:00
throw new AppError(400, "No mod name was scpecified", "Bad request");
}
2025-05-04 23:14:00 +02:00
const modpack_name = req.params.name;
const modpack = await getModpackByName(modpack_name);
2025-04-25 14:06:03 +02:00
if (!modpack) {
throw new AppError(404, "No mod was found with this name", "Not found");
}
// Authorize
if ( modpack.author != req.token_infos.username) {
throw new AppError(401, "Mod author differs from current user", "Unauthorized");
}
}
async function authorizeUserModification(req) {
// Auth token
await authenticateToken(req);
// Get mod infos
2025-05-04 23:14:00 +02:00
if (!req.params || !req.params.name) {
2025-04-25 14:06:03 +02:00
throw new AppError(400, "No mod name was scpecified", "Bad request");
}
2025-05-04 23:14:00 +02:00
const user_name = req.params.name;
const user = await getUserByName(user_name);
2025-04-25 14:06:03 +02:00
if (!user) {
2025-05-04 23:14:00 +02:00
throw new AppError(404, "No user was found with this name", "Not found");
2025-04-25 14:06:03 +02:00
}
// Authorize
if ( user.username != req.token_infos.username) {
2025-05-04 23:14:00 +02:00
throw new AppError(401, "User to modify differs from current user", "Unauthorized");
2025-04-25 14:06:03 +02:00
}
}
2025-04-25 14:37:42 +02:00
module.exports = { authenticateToken, authorizeModModification, authorizeModpackModification, authorizeUserModification };