small fixes

This commit is contained in:
Laurin
2022-06-06 19:31:54 +02:00
parent de0c457fd8
commit 0b7756d428
8 changed files with 70 additions and 52 deletions
+2
View File
@@ -5,6 +5,8 @@ import argon2 from "argon2";
import { AUTH_ERROR, createInsufficientPermissionsError, DataType, generateInvalidBodyError } from "./common";
import { authClient } from "../lib/redis";
require("express-async-errors");
export const regenerateRevision = async (pid: string) => {
// TOOO: Add error handling
const { revision } = await prisma.admin.update({
+2
View File
@@ -14,6 +14,8 @@ import {
handleCreateByName,
} from "./common";
require("express-async-errors");
const updateGroupBody = z
.object({
name: z.string().min(1),
@@ -14,6 +14,8 @@ import { PrismaClientKnownRequestError } from "@prisma/client/runtime";
import { Prisma } from "@prisma/client";
import NotFoundError from "../Middleware/error/NotFoundError";
require("express-async-errors");
function validateOranisationName(name: string) {
return name.length > 0;
}
+5 -3
View File
@@ -8,6 +8,8 @@ import NotFoundError from "../Middleware/error/NotFoundError";
import { requireLeaderOfTeam, requireResponsibleForParticipant } from "../Middleware/auth/teamleaderAuth";
import { requireResponsibleForGroups } from "../Middleware/auth/auth";
require("express-async-errors");
const InitialParticipant = z.object({
firstName: z.string(),
lastName: z.string(),
@@ -104,7 +106,7 @@ export const updateParticipant = async (req: Request<{ pid: string }>, res: Resp
const { pid } = req.params;
if (req.teamleader?.isAuthenticated) {
requireResponsibleForParticipant(req.teamleader, pid);
await requireResponsibleForParticipant(req.teamleader, pid);
}
const result = InitialParticipant.partial().safeParse(req.body);
@@ -153,9 +155,9 @@ export const deleteParticipant = async (req: Request<{ pid: string }>, res: Resp
const { pid } = req.params;
if (req.teamleader?.isAuthenticated) {
requireResponsibleForParticipant(req.teamleader, pid);
await requireResponsibleForParticipant(req.teamleader, pid);
} else {
await requireResponsibleForGroups(req.auth, await getGroupByParticipantPid(pid));
requireResponsibleForGroups(req.auth, await getGroupByParticipantPid(pid));
}
try {
@@ -15,6 +15,8 @@ import {
validateName,
} from "./common";
require("express-async-errors");
const RoleSchemaBody = z.object({
name: z.string().min(1),
schema: z.string(),
+7 -2
View File
@@ -6,6 +6,9 @@ import { TeamBody } from "./user_auth.controller";
import { Prisma } from "@prisma/client";
import NotFoundError from "../Middleware/error/NotFoundError";
import { requireResponsibleForGroups } from "../Middleware/auth/auth";
import AuthError from "../Middleware/error/AuthError";
require("express-async-errors");
export const basicTeam = {
pid: true,
@@ -108,8 +111,10 @@ export const deleteTeam = async (req: Request, res: Response) => {
if (req.teamleader?.isAuthenticated) {
await requireLeaderOfTeam(req.teamleader, pid);
} else {
await requireResponsibleForGroups(req.auth, await getGroupsByTeamPid(pid));
}
if (req.auth?.permission_level == "STANDARD") {
throw new AuthError("STANDARD Admins are not allowed to delete Teams!")
}
await prisma.team.delete({ where: { pid } });
+2
View File
@@ -8,6 +8,8 @@ import { generateTeamleaderJWT } from "../Middleware/auth/teamleaderAuth";
import { createRolesForTeam } from "./role.controller";
import { z } from "zod";
require("express-async-errors");
export const TeamBody = z.object({
teamName: z.string().min(1),
leaderEmail: z.string().email(),
+48 -47
View File
@@ -4,6 +4,7 @@ import jwt, { JsonWebTokenError } from "jsonwebtoken";
import AuthError from "../error/AuthError";
import { getBearerToken, verifyAuthorizationFormat } from "./auth";
import prisma from "../../lib/prisma";
import { checkTeamExistence } from "../../Controllers/team.controller";
export interface TeamleaderJWTPayload {
team: string;
@@ -25,63 +26,63 @@ export function generateTeamleaderJWT(teamleader: Team) {
export const _requireTeamleaderAuthentication =
(config: { optional: Boolean; controlled: Boolean } = { optional: false, controlled: false }) =>
(req: Request, res: Response, next: NextFunction) => {
if (!JWT_SECRET) {
throw new Error("JWT_SECRET not set");
}
const { authorization } = req.headers;
if (!authorization) {
if (config.optional) {
return false;
(req: Request, res: Response, next: NextFunction) => {
if (!JWT_SECRET) {
throw new Error("JWT_SECRET not set");
}
return res.status(403).send({
type: "error",
payload: {
message:
"The request did not include the Authorization header (Only the team leader can perform this operation)",
},
});
}
const { authorization } = req.headers;
if (!verifyAuthorizationFormat(authorization)) {
return res.status(400).send({
type: "error",
payload: {
message: "Malformed Authorization header",
format: "Bearer <token>",
},
});
}
if (!authorization) {
if (config.optional) {
return false;
}
try {
const token_payload = jwt.verify(getBearerToken(authorization), JWT_SECRET) as TeamleaderJWTPayload;
req.teamleader = {
isAuthenticated: true,
team: token_payload.team,
};
if (!config.controlled) {
next();
}
return true;
} catch (e) {
if (e instanceof JsonWebTokenError) {
return res.status(403).json({
return res.status(403).send({
type: "error",
payload: {
message: "Token could not be verified; It might be expired",
message:
"The request did not include the Authorization header (Only the team leader can perform this operation)",
},
});
}
throw e;
}
};
if (!verifyAuthorizationFormat(authorization)) {
return res.status(400).send({
type: "error",
payload: {
message: "Malformed Authorization header",
format: "Bearer <token>",
},
});
}
try {
const token_payload = jwt.verify(getBearerToken(authorization), JWT_SECRET) as TeamleaderJWTPayload;
req.teamleader = {
isAuthenticated: true,
team: token_payload.team,
};
if (!config.controlled) {
next();
}
return true;
} catch (e) {
if (e instanceof JsonWebTokenError) {
return res.status(403).json({
type: "error",
payload: {
message: "Token could not be verified; It might be expired",
},
});
}
throw e;
}
};
export const requireTeamleaderAuthentication = _requireTeamleaderAuthentication({ optional: false, controlled: false });