From 5d7b636d23facf8c5dc0b261361649ddd69d52d4 Mon Sep 17 00:00:00 2001 From: Timothy-Gonzalez <105177619+Timothy-Gonzalez@users.noreply.github.com> Date: Sun, 5 Nov 2023 12:36:41 -0600 Subject: [PATCH] Remove getDevice Removes a hacky method that was a result of hacky code, by fixing that we can have a cleaner auth flow --- src/middleware/select-auth.ts | 4 ++-- src/services/auth/auth-lib.ts | 25 ------------------------- src/services/auth/auth-router.ts | 8 ++++++-- 3 files changed, 8 insertions(+), 29 deletions(-) diff --git a/src/middleware/select-auth.ts b/src/middleware/select-auth.ts index 126307d4..a5dd715c 100644 --- a/src/middleware/select-auth.ts +++ b/src/middleware/select-auth.ts @@ -27,7 +27,7 @@ export function SelectAuthProvider(provider: string, device: string): RequestHan ...googleOptions, callbackURL: Config.CALLBACK_URLS.GOOGLE, }; - options.callbackURL += `device=${device}`; + options.callbackURL += `${device}`; return authenticateFunction("google", options); } @@ -36,7 +36,7 @@ export function SelectAuthProvider(provider: string, device: string): RequestHan ...githubOptions, callbackURL: Config.CALLBACK_URLS.GITHUB, }; - options.callbackURL += `device=${device}`; + options.callbackURL += `${device}`; return authenticateFunction("github", options); } diff --git a/src/services/auth/auth-lib.ts b/src/services/auth/auth-lib.ts index 51578a45..8439b265 100644 --- a/src/services/auth/auth-lib.ts +++ b/src/services/auth/auth-lib.ts @@ -301,31 +301,6 @@ export function hasAdminPerms(payload?: JwtPayload): boolean { return payload.roles.includes(Role.ADMIN); } -/** - * Given a string of the format device=DEVICENAME, verify that the string is actually valid and contains a device name. - * @param k Key-value pair, representing the parameter. - * @returns Device type if valid, else throws an error - */ -export function getDevice(kv?: string): string { - if (!kv) { - throw new Error("NoInput"); - } - - // Replace everything before/after the first equal with nothing, to get KV pairs - const key: string = kv.replace(/=.*/, ""); - const possibleDevice: string = kv.replace(/.*=/, ""); - - if (!key || key != "device") { - throw new Error("NoKey"); - } - - if (!possibleDevice || !Config.REDIRECT_URLS.has(possibleDevice)) { - throw new Error("NoValue"); - } - - return possibleDevice; -} - /** * Get all id of users that have a particular role within the database. * @param role role that we want to filter for diff --git a/src/services/auth/auth-router.ts b/src/services/auth/auth-router.ts index 1895fe9f..24cd7269 100644 --- a/src/services/auth/auth-router.ts +++ b/src/services/auth/auth-router.ts @@ -13,7 +13,6 @@ import { ModifyRoleRequest } from "./auth-formats.js"; import { JwtPayload, ProfileData, Provider, Role, RoleOperation } from "./auth-models.js"; import { generateJwtToken, - getDevice, getJwtPayloadFromProfile, getRoles, hasElevatedPerms, @@ -125,7 +124,12 @@ authRouter.get( console.log("IN CALLBACK"); const provider: string = req.params.PROVIDER ?? ""; try { - const device: string = getDevice(req.params.DEVICE); + const device = req.params.DEVICE; + + if (!device || !Config.REDIRECT_URLS.has(device)) { + throw Error(`Bad device ${device}`); + } + res.locals.device = device; SelectAuthProvider(provider, device)(req, res, next); } catch (error) {