diff --git a/src/config.ts b/src/config.ts index 1239660..38aab55 100644 --- a/src/config.ts +++ b/src/config.ts @@ -28,5 +28,5 @@ export const Config = { export const DeviceRedirects: Record = { web: "https://www.google.com/", - dev: "http://127.0.0.1:3000/auth/dev/" + dev: "http://127.0.0.1:3000/auth/dev/", }; diff --git a/src/middleware/role-checker.ts b/src/middleware/role-checker.ts index 8bcd50b..6fc5cf8 100644 --- a/src/middleware/role-checker.ts +++ b/src/middleware/role-checker.ts @@ -1,12 +1,14 @@ import { NextFunction, Request, Response } from "express"; import { JwtPayload, Role } from "../services/auth/auth-models"; -import {z} from "zod"; +import { z } from "zod"; import jsonwebtoken from "jsonwebtoken"; import { Config } from "../config"; import { StatusCodes } from "http-status-codes"; - -export default function RoleChecker (requiredRoles: z.infer[], weakVerification: boolean = false){ +export default function RoleChecker( + requiredRoles: z.infer[], + weakVerification: boolean = false +) { return function (req: Request, res: Response, next: NextFunction) { const jwt = req.headers.authorization; @@ -15,12 +17,15 @@ export default function RoleChecker (requiredRoles: z.infer[], weak next(); } - return res.status(StatusCodes.BAD_REQUEST).json({error: "NoJWT"}) + return res.status(StatusCodes.BAD_REQUEST).json({ error: "NoJWT" }); } - + try { console.log("in"); - const payloadData = jsonwebtoken.verify(jwt, Config.JWT_SIGNING_SECRET); + const payloadData = jsonwebtoken.verify( + jwt, + Config.JWT_SIGNING_SECRET + ); const payload = JwtPayload.parse(payloadData); res.locals.payload = payload; @@ -30,7 +35,7 @@ export default function RoleChecker (requiredRoles: z.infer[], weak if (weakVerification) { next(); } - + if (requiredRoles.length == 0) { next(); } @@ -55,9 +60,8 @@ export default function RoleChecker (requiredRoles: z.infer[], weak } throw error; - } catch (error) { - next(error) + next(error); } - } -} \ No newline at end of file + }; +} diff --git a/src/services/auth/auth-models.ts b/src/services/auth/auth-models.ts index a34b6db..bda56ab 100644 --- a/src/services/auth/auth-models.ts +++ b/src/services/auth/auth-models.ts @@ -2,7 +2,7 @@ import { z } from "zod"; export const Role = z.enum(["USER", "ADMIN", "CORPORATE"]); -export const JwtPayload = z.object({ +export const JwtPayload = z.object({ userId: z.string(), roles: Role.array(), -}) \ No newline at end of file +}); diff --git a/src/services/auth/auth-router.ts b/src/services/auth/auth-router.ts index 7b8bc9a..8ab4301 100644 --- a/src/services/auth/auth-router.ts +++ b/src/services/auth/auth-router.ts @@ -42,16 +42,23 @@ authRouter.get( async function (req, res, next) { // Authentication failed - redirect to login if (req.user == undefined) { - return res.redirect(`/auth/login/${req.params.DEVICE}`) + return res.redirect(`/auth/login/${req.params.DEVICE}`); } const userData = req.user as Profile; const userId = `user${userData.id}`; - + // Generate the JWT, and redirect to JWT initialization try { - const jwtPayload = (await getJwtPayloadFromDatabase(userId)).toObject(); - const token = jsonwebtoken.sign(jwtPayload, Config.JWT_SIGNING_SECRET, { expiresIn: Config.JWT_EXPIRATION_TIME }); - const redirectUri = DeviceRedirects[req.params.DEVICE] + `?token=${token}`; + const jwtPayload = ( + await getJwtPayloadFromDatabase(userId) + ).toObject(); + const token = jsonwebtoken.sign( + jwtPayload, + Config.JWT_SIGNING_SECRET, + { expiresIn: Config.JWT_EXPIRATION_TIME } + ); + const redirectUri = + DeviceRedirects[req.params.DEVICE] + `?token=${token}`; return res.redirect(redirectUri); } catch (error) { next(error); @@ -61,6 +68,6 @@ authRouter.get( authRouter.get("/dev/", (req, res) => { return res.status(StatusCodes.OK).json(req.query); -}) +}); export default authRouter; diff --git a/src/services/auth/auth-schema.ts b/src/services/auth/auth-schema.ts index b688077..9434d11 100644 --- a/src/services/auth/auth-schema.ts +++ b/src/services/auth/auth-schema.ts @@ -2,7 +2,6 @@ import { Schema } from "mongoose"; import { z } from "zod"; import { Role } from "./auth-models"; - export const RoleValidator = z.object({ userId: z.coerce.string().regex(/user[0-9]*/), name: z.coerce.string(), diff --git a/src/services/auth/auth-utils.ts b/src/services/auth/auth-utils.ts index fbb4e11..b4a3c2e 100644 --- a/src/services/auth/auth-utils.ts +++ b/src/services/auth/auth-utils.ts @@ -28,12 +28,14 @@ export function createGoogleStrategy(device: string) { ); } - export async function getJwtPayloadFromDatabase(userId: string) { - const payload = await Database.ROLES.findOne({userId: userId}).select(["userId", "roles"]); + const payload = await Database.ROLES.findOne({ userId: userId }).select([ + "userId", + "roles", + ]); if (!payload) { throw new Error("NoUserFound"); } - + return payload; -} \ No newline at end of file +}