added users and groups
This commit is contained in:
		
							
								
								
									
										63
									
								
								src/middlewares/auth.js
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										63
									
								
								src/middlewares/auth.js
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,63 @@
 | 
			
		||||
import log from '../utils/log.js'
 | 
			
		||||
import jwt from 'jsonwebtoken';
 | 
			
		||||
import config from '../../config.json' with {type: "json"};
 | 
			
		||||
import GroupService from '../services/group.js';
 | 
			
		||||
 | 
			
		||||
const TAG = "/middlewares/auth.js"
 | 
			
		||||
 | 
			
		||||
const requireUsernameAndPassword = async (req, res, next) => {
 | 
			
		||||
    if (req.method == "OPTIONS") next();
 | 
			
		||||
 | 
			
		||||
    try {
 | 
			
		||||
        const {username, password} = req.body;
 | 
			
		||||
            if (!username) return res.status(400).send("Username is required");
 | 
			
		||||
            if (!password) return res.status(400).send("Password is required");
 | 
			
		||||
            next();
 | 
			
		||||
    } catch (e) { return res.status(500).send(unknownError(`${TAG}/requireUsernameAndPassword: ${e}`)); }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
const authenticate = async (req, res, next) => {
 | 
			
		||||
    if (req.method == "OPTIONS") next();
 | 
			
		||||
 | 
			
		||||
    try {
 | 
			
		||||
        const token = req.headers.authorization.split(' ')[1]
 | 
			
		||||
        if (!token) return res.status(401).send("No authorization token supplied");
 | 
			
		||||
        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]
 | 
			
		||||
        if (!token) return res.status(401).send("No authorization token supplied");
 | 
			
		||||
 | 
			
		||||
        const { id } = req.params;
 | 
			
		||||
 | 
			
		||||
        let user = jwt.decode(token, config.secret)
 | 
			
		||||
        
 | 
			
		||||
        let adminId = await GroupService.getAdminId(id);
 | 
			
		||||
        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 {
 | 
			
		||||
        const { id } = req.params;
 | 
			
		||||
        const { password } = req.body;
 | 
			
		||||
 | 
			
		||||
        const groupPassword = await GroupService.getPassword(id);
 | 
			
		||||
 | 
			
		||||
        if (groupPassword != password) return res.status(403).send("Wrong password");
 | 
			
		||||
        next();
 | 
			
		||||
 | 
			
		||||
    } catch (e) {return res.status(500).send(log.unknownError(`${TAG}/checkGroupPassword: ${e}`));}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
export default { requireUsernameAndPassword, authenticate, authorizeGroupOwner, checkGroupPassword }
 | 
			
		||||
							
								
								
									
										51
									
								
								src/middlewares/existance.js
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										51
									
								
								src/middlewares/existance.js
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,51 @@
 | 
			
		||||
import UserService from '../services/user.js';
 | 
			
		||||
import GroupService from '../services/group.js';
 | 
			
		||||
import log from '../utils/log.js';
 | 
			
		||||
import statuses from '../utils/status.js';
 | 
			
		||||
 | 
			
		||||
const TAG = "/middlewares/existance.js"
 | 
			
		||||
 | 
			
		||||
const usernameExists = async (req, res, next) => {
 | 
			
		||||
    try {
 | 
			
		||||
        let { username } = req.body;
 | 
			
		||||
 | 
			
		||||
        let user = await UserService.getByUsername(username);
 | 
			
		||||
        if (!user || user == statuses.not_found) return res.status(404).send("User not found");
 | 
			
		||||
        next();
 | 
			
		||||
    } catch (e) { return res.status(500).send(log.unknownError(`${TAG}/usernameExists: ${e}`)); }
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
const usernameDoesntExist = async (req, res, next) => {
 | 
			
		||||
    try {
 | 
			
		||||
        let { username } = req.body;
 | 
			
		||||
 | 
			
		||||
        let user = await UserService.getByUsername(username);
 | 
			
		||||
        if (user || user != statuses.not_found) return res.status(400).send("Such username already taken");
 | 
			
		||||
        next();
 | 
			
		||||
    } catch (e) { return res.status(500).send(log.unknownError(`${TAG}/usernameDoesntExist: ${e}`)); }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
const groupExists = async (req, res, next) => {
 | 
			
		||||
    try {
 | 
			
		||||
 | 
			
		||||
        const { id } = req.params;
 | 
			
		||||
 | 
			
		||||
        let group = await GroupService.getById(id);
 | 
			
		||||
 | 
			
		||||
        if (!group || group == statuses.not_found) return res.status(404).send("Group not found");
 | 
			
		||||
        next();
 | 
			
		||||
    } catch (e) { return res.status(500).send(log.unknownError(`${TAG}/groupExists: ${e}`)) }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
const groupDoesntExist = async (req, res, next) => {
 | 
			
		||||
    try {
 | 
			
		||||
 | 
			
		||||
        const { id } = req.params;
 | 
			
		||||
 | 
			
		||||
        let group = await GroupService.getById(id);
 | 
			
		||||
 | 
			
		||||
        if (group || group != statuses.not_found) return res.status(400).send("Such group already exists");
 | 
			
		||||
        next();
 | 
			
		||||
    } catch (e) { return res.status(500).send(log.unknownError(`${TAG}/groupDoesntExist: ${e}`)) }
 | 
			
		||||
}
 | 
			
		||||
export default { usernameExists, usernameDoesntExist, groupExists, groupDoesntExist }
 | 
			
		||||
		Reference in New Issue
	
	Block a user