Add teamleader authentication

This commit is contained in:
Stephan
2022-05-27 22:48:16 +02:00
parent 197d1a52a3
commit a9c182cf64
3 changed files with 79 additions and 2 deletions
+2 -2
View File
@@ -8,9 +8,9 @@ import prisma from "../../lib/prisma";
const JWT_SECRET = process.env.JWT_SECRET || "secret";
const verifyAuthorizationFormat = (authorization: string) => /^Bearer .+$/.test(authorization);
export const verifyAuthorizationFormat = (authorization: string) => /^Bearer .+$/.test(authorization);
const getBearerToken = (authorization: string) => authorization.slice(7);
export const getBearerToken = (authorization: string) => authorization.slice(7);
export const requireAuthentication = async (req: Request, res: Response, next: NextFunction) => {
const { authorization } = req.headers;
+75
View File
@@ -0,0 +1,75 @@
import { Participant } from "@prisma/client";
import e, { NextFunction, Request, Response } from "express";
import jwt, { JsonWebTokenError } from "jsonwebtoken";
import { getBearerToken, verifyAuthorizationFormat } from "./auth";
export interface TeamleaderJWTPayload {
pid: string;
team: string;
}
const JWT_SECRET = process.env.JWT_SECRET;
export function generateTeamleaderJWT(teamleader: Participant & { relevance: "TEAMLEADER"; team: { pid: string } }) {
if (!JWT_SECRET) {
throw new Error("JWT_SECRET not set");
}
const payload: TeamleaderJWTPayload = {
pid: teamleader.pid,
team: teamleader.team.pid,
};
return jwt.sign(payload, JWT_SECRET, { expiresIn: "4 days" });
}
export async function requireTeamleaderAuthentication(req: Request, res: Response, next: NextFunction) {
if (!JWT_SECRET) {
throw new Error("JWT_SECRET not set");
}
const { authorization } = req.headers;
if (!authorization) {
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)",
},
});
}
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,
pid: token_payload.pid,
team: token_payload.team,
};
next();
} 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;
}
+2
View File
@@ -1,7 +1,9 @@
import { AuthJWTPayload } from "./Controllers/admin_auth.controller";
import { TeamleaderJWTPayload } from "./Middleware/auth/teamleaderAuth";
declare module "express-serve-static-core" {
interface Request {
auth?: AuthJWTPayload & { isAuthenticated: boolean };
teamleader?: TeamleaderJWTPayload & { isAuthenticated: boolean };
}
}