Skip to content

Commit

Permalink
Remove getDevice
Browse files Browse the repository at this point in the history
Removes a hacky method that was a result of hacky code, by fixing that
we can have a cleaner auth flow
  • Loading branch information
Timothy-Gonzalez committed Nov 5, 2023
1 parent c6d29f5 commit 5d7b636
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 29 deletions.
4 changes: 2 additions & 2 deletions src/middleware/select-auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand All @@ -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);
}
Expand Down
25 changes: 0 additions & 25 deletions src/services/auth/auth-lib.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 6 additions & 2 deletions src/services/auth/auth-router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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) {
Expand Down

0 comments on commit 5d7b636

Please sign in to comment.