add `right` option to route()

This commit is contained in:
Flam3rboy
2021-10-03 01:55:57 +02:00
parent 62f727783d
commit 7268779dc3
6 changed files with 31 additions and 5 deletions
+3 -1
View File
@@ -1,6 +1,6 @@
import { NextFunction, Request, Response } from "express";
import { HTTPError } from "lambert-server";
import { checkToken, Config } from "@fosscord/util";
import { checkToken, Config, Rights } from "@fosscord/util";
export const NO_AUTHORIZATION_ROUTES = [
"/auth/login",
@@ -21,6 +21,7 @@ declare global {
user_id: string;
user_bot: boolean;
token: string;
rights: Rights;
}
}
}
@@ -46,6 +47,7 @@ export async function Authentication(req: Request, res: Response, next: NextFunc
req.token = decoded;
req.user_id = decoded.id;
req.user_bot = user.bot;
req.rights = new Rights(user.rights);
return next();
} catch (error: any) {
return next(new HTTPError(error?.toString(), 400));
+20 -1
View File
@@ -1,4 +1,15 @@
import { DiscordApiErrors, EVENT, Event, EventData, getPermission, PermissionResolvable, Permissions } from "@fosscord/util";
import {
DiscordApiErrors,
EVENT,
Event,
EventData,
FosscordApiErrors,
getPermission,
PermissionResolvable,
Permissions,
RightResolvable,
Rights
} from "@fosscord/util";
import { NextFunction, Request, Response } from "express";
import fs from "fs";
import path from "path";
@@ -33,6 +44,7 @@ export type RouteResponse = { status?: number; body?: `${string}Response`; heade
export interface RouteOptions {
permission?: PermissionResolvable;
right?: RightResolvable;
body?: `${string}Schema`; // typescript interface name
test?: {
response?: RouteResponse;
@@ -89,6 +101,13 @@ export function route(opts: RouteOptions) {
}
}
if (opts.right) {
const required = new Rights(opts.right);
if (!req.rights || !req.rights.has(required)) {
throw FosscordApiErrors.MISSING_RIGHTS.withParams(opts.right as string);
}
}
if (validate) {
const valid = validate(normalizeBody(req.body));
if (!valid) {