2024-10-26 05:31:22 +03:00
|
|
|
import log from '../utils/log.js'
|
|
|
|
import jwt from 'jsonwebtoken';
|
|
|
|
import config from '../../config.json' with {type: "json"};
|
|
|
|
import GroupService from '../services/group.js';
|
2024-10-26 20:18:14 +03:00
|
|
|
import UserService from '../services/user.js';
|
2024-10-26 05:31:22 +03:00
|
|
|
|
|
|
|
const TAG = "/middlewares/auth.js"
|
|
|
|
|
2024-10-26 20:18:14 +03:00
|
|
|
const requireUsername = async (req, res, next) => {
|
2024-10-26 05:31:22 +03:00
|
|
|
if (req.method == "OPTIONS") next();
|
|
|
|
|
|
|
|
try {
|
2024-10-27 04:45:13 +03:00
|
|
|
const { username } = req.body;
|
|
|
|
if (!username) return res.status(400).send("Username is required");
|
|
|
|
next();
|
2024-10-26 20:18:14 +03:00
|
|
|
} catch (e) { return res.status(500).send(unknownError(`${TAG}/requireUsername: ${e}`)); }
|
|
|
|
}
|
|
|
|
|
|
|
|
const requirePassword = async (req, res, next) => {
|
|
|
|
if (req.method == "OPTIONS") next();
|
|
|
|
|
|
|
|
try {
|
2024-10-27 04:45:13 +03:00
|
|
|
const { password } = req.body;
|
|
|
|
if (!password) return res.status(400).send("Password is required");
|
|
|
|
next();
|
2024-10-26 20:18:14 +03:00
|
|
|
} catch (e) { return res.status(500).send(unknownError(`${TAG}/requirePassword: ${e}`)); }
|
2024-10-26 05:31:22 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
const authenticate = async (req, res, next) => {
|
|
|
|
if (req.method == "OPTIONS") next();
|
|
|
|
|
|
|
|
try {
|
2024-10-26 20:18:14 +03:00
|
|
|
if (!req.headers.authorization) return res.status(403).send("No authorization header supplied");
|
2024-10-26 05:31:22 +03:00
|
|
|
const token = req.headers.authorization.split(' ')[1]
|
2024-10-26 20:18:14 +03:00
|
|
|
if (!token) return res.status(403).send("No authorization token supplied");
|
2024-10-26 05:31:22 +03:00
|
|
|
if (!jwt.verify(token, config.secret)) return res.status(403).send("Authorization token is incorrect");
|
|
|
|
|
|
|
|
next();
|
|
|
|
} catch (e) { return res.status(500).send(log.unknownError(`${TAG}/authenticate: ${e}`)); }
|
|
|
|
}
|
|
|
|
|
|
|
|
const authorizeGroupOwner = async (req, res, next) => {
|
|
|
|
if (req.method == "OPTIONS") next();
|
|
|
|
|
|
|
|
try {
|
|
|
|
const token = req.headers.authorization.split(' ')[1]
|
|
|
|
|
2024-10-26 20:18:14 +03:00
|
|
|
const { groupId } = req.params;
|
2024-10-26 05:31:22 +03:00
|
|
|
|
|
|
|
let user = jwt.decode(token, config.secret)
|
2024-10-27 04:45:13 +03:00
|
|
|
|
2024-10-26 20:18:14 +03:00
|
|
|
let adminId = await GroupService.getAdminId(groupId);
|
2024-10-26 05:31:22 +03:00
|
|
|
if (user.login.id != adminId) return res.status(403).send("Not your group");
|
|
|
|
next();
|
|
|
|
} catch (e) { return res.status(500).send(log.unknownError(`${TAG}/authorizeGroupOwner: ${e}`)); }
|
|
|
|
}
|
|
|
|
|
|
|
|
const checkGroupPassword = async (req, res, next) => {
|
|
|
|
if (req.method == "OPTIONS") next();
|
|
|
|
|
|
|
|
try {
|
2024-10-26 20:18:14 +03:00
|
|
|
const { groupId } = req.params;
|
2024-10-26 05:31:22 +03:00
|
|
|
const { password } = req.body;
|
|
|
|
|
2024-10-26 20:18:14 +03:00
|
|
|
const groupPassword = await GroupService.getPassword(groupId);
|
2024-10-26 05:31:22 +03:00
|
|
|
|
|
|
|
if (groupPassword != password) return res.status(403).send("Wrong password");
|
|
|
|
next();
|
|
|
|
|
2024-10-27 04:45:13 +03:00
|
|
|
} catch (e) { return res.status(500).send(log.unknownError(`${TAG}/checkGroupPassword: ${e}`)); }
|
2024-10-26 05:31:22 +03:00
|
|
|
}
|
|
|
|
|
2024-10-26 20:18:14 +03:00
|
|
|
const userIsInGroup = async (req, res, next) => {
|
|
|
|
if (req.method == "OPTIONS") next();
|
|
|
|
|
|
|
|
const groupId = req.body.groupId || req.params.groupId;
|
|
|
|
|
|
|
|
const token = req.headers.authorization.split(' ')[1]
|
|
|
|
let user = jwt.decode(token, config.secret)
|
|
|
|
if (!await UserService.isInGroup(user.login.id, groupId)) return res.status(403).send("You are not a member of this group");
|
|
|
|
next();
|
|
|
|
}
|
|
|
|
|
|
|
|
export default { requireUsername, requirePassword, authenticate, authorizeGroupOwner, checkGroupPassword, userIsInGroup }
|