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 { AUTH_ERROR, createInsufficientPermissionsError, DataType, generateInvalidBodyError } from "./common";
import { authClient } from "../lib/redis"; import { authClient } from "../lib/redis";
require("express-async-errors");
export const regenerateRevision = async (pid: string) => { export const regenerateRevision = async (pid: string) => {
// TOOO: Add error handling // TOOO: Add error handling
const { revision } = await prisma.admin.update({ const { revision } = await prisma.admin.update({
+2
View File
@@ -14,6 +14,8 @@ import {
handleCreateByName, handleCreateByName,
} from "./common"; } from "./common";
require("express-async-errors");
const updateGroupBody = z const updateGroupBody = z
.object({ .object({
name: z.string().min(1), name: z.string().min(1),
@@ -14,6 +14,8 @@ import { PrismaClientKnownRequestError } from "@prisma/client/runtime";
import { Prisma } from "@prisma/client"; import { Prisma } from "@prisma/client";
import NotFoundError from "../Middleware/error/NotFoundError"; import NotFoundError from "../Middleware/error/NotFoundError";
require("express-async-errors");
function validateOranisationName(name: string) { function validateOranisationName(name: string) {
return name.length > 0; 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 { requireLeaderOfTeam, requireResponsibleForParticipant } from "../Middleware/auth/teamleaderAuth";
import { requireResponsibleForGroups } from "../Middleware/auth/auth"; import { requireResponsibleForGroups } from "../Middleware/auth/auth";
require("express-async-errors");
const InitialParticipant = z.object({ const InitialParticipant = z.object({
firstName: z.string(), firstName: z.string(),
lastName: z.string(), lastName: z.string(),
@@ -104,7 +106,7 @@ export const updateParticipant = async (req: Request<{ pid: string }>, res: Resp
const { pid } = req.params; const { pid } = req.params;
if (req.teamleader?.isAuthenticated) { if (req.teamleader?.isAuthenticated) {
requireResponsibleForParticipant(req.teamleader, pid); await requireResponsibleForParticipant(req.teamleader, pid);
} }
const result = InitialParticipant.partial().safeParse(req.body); 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; const { pid } = req.params;
if (req.teamleader?.isAuthenticated) { if (req.teamleader?.isAuthenticated) {
requireResponsibleForParticipant(req.teamleader, pid); await requireResponsibleForParticipant(req.teamleader, pid);
} else { } else {
await requireResponsibleForGroups(req.auth, await getGroupByParticipantPid(pid)); requireResponsibleForGroups(req.auth, await getGroupByParticipantPid(pid));
} }
try { try {
@@ -15,6 +15,8 @@ import {
validateName, validateName,
} from "./common"; } from "./common";
require("express-async-errors");
const RoleSchemaBody = z.object({ const RoleSchemaBody = z.object({
name: z.string().min(1), name: z.string().min(1),
schema: z.string(), schema: z.string(),
+7 -2
View File
@@ -6,6 +6,9 @@ import { TeamBody } from "./user_auth.controller";
import { Prisma } from "@prisma/client"; import { Prisma } from "@prisma/client";
import NotFoundError from "../Middleware/error/NotFoundError"; import NotFoundError from "../Middleware/error/NotFoundError";
import { requireResponsibleForGroups } from "../Middleware/auth/auth"; import { requireResponsibleForGroups } from "../Middleware/auth/auth";
import AuthError from "../Middleware/error/AuthError";
require("express-async-errors");
export const basicTeam = { export const basicTeam = {
pid: true, pid: true,
@@ -108,8 +111,10 @@ export const deleteTeam = async (req: Request, res: Response) => {
if (req.teamleader?.isAuthenticated) { if (req.teamleader?.isAuthenticated) {
await requireLeaderOfTeam(req.teamleader, pid); 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 } }); 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 { createRolesForTeam } from "./role.controller";
import { z } from "zod"; import { z } from "zod";
require("express-async-errors");
export const TeamBody = z.object({ export const TeamBody = z.object({
teamName: z.string().min(1), teamName: z.string().min(1),
leaderEmail: z.string().email(), leaderEmail: z.string().email(),
+48 -47
View File
@@ -4,6 +4,7 @@ import jwt, { JsonWebTokenError } from "jsonwebtoken";
import AuthError from "../error/AuthError"; import AuthError from "../error/AuthError";
import { getBearerToken, verifyAuthorizationFormat } from "./auth"; import { getBearerToken, verifyAuthorizationFormat } from "./auth";
import prisma from "../../lib/prisma"; import prisma from "../../lib/prisma";
import { checkTeamExistence } from "../../Controllers/team.controller";
export interface TeamleaderJWTPayload { export interface TeamleaderJWTPayload {
team: string; team: string;
@@ -25,63 +26,63 @@ export function generateTeamleaderJWT(teamleader: Team) {
export const _requireTeamleaderAuthentication = export const _requireTeamleaderAuthentication =
(config: { optional: Boolean; controlled: Boolean } = { optional: false, controlled: false }) => (config: { optional: Boolean; controlled: Boolean } = { optional: false, controlled: false }) =>
(req: Request, res: Response, next: NextFunction) => { (req: Request, res: Response, next: NextFunction) => {
if (!JWT_SECRET) { if (!JWT_SECRET) {
throw new Error("JWT_SECRET not set"); throw new Error("JWT_SECRET not set");
}
const { authorization } = req.headers;
if (!authorization) {
if (config.optional) {
return false;
} }
return res.status(403).send({ const { authorization } = req.headers;
type: "error",
payload: {
message:
"The request did not include the Authorization header (Only the team leader can perform this operation)",
},
});
}
if (!verifyAuthorizationFormat(authorization)) { if (!authorization) {
return res.status(400).send({ if (config.optional) {
type: "error", return false;
payload: { }
message: "Malformed Authorization header",
format: "Bearer <token>",
},
});
}
try { return res.status(403).send({
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", type: "error",
payload: { 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 }); export const requireTeamleaderAuthentication = _requireTeamleaderAuthentication({ optional: false, controlled: false });