Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove getDevice #118

Merged
merged 1 commit into from
Nov 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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