summary refs log tree commit diff
path: root/src/middlewares/Authentication.ts
diff options
context:
space:
mode:
authorFlam3rboy <34555296+Flam3rboy@users.noreply.github.com>2021-07-01 16:02:54 +0200
committerGitHub <noreply@github.com>2021-07-01 16:02:54 +0200
commit7b31ca10b3c22c923cc1daf28f209c743e6b36fd (patch)
tree542a1f62dc30c96dd6e178c472e0382e67fd3be0 /src/middlewares/Authentication.ts
parent:construction: rate limit (diff)
parent:sparkles: finished Rate Limit (diff)
downloadserver-7b31ca10b3c22c923cc1daf28f209c743e6b36fd.tar.xz
Merge pull request #162 from fosscord/feat--rate-limit
[Feature] Rate Limit
Diffstat (limited to 'src/middlewares/Authentication.ts')
-rw-r--r--src/middlewares/Authentication.ts10
1 files changed, 8 insertions, 2 deletions
diff --git a/src/middlewares/Authentication.ts b/src/middlewares/Authentication.ts

index 4b38f1d4..76b335ad 100644 --- a/src/middlewares/Authentication.ts +++ b/src/middlewares/Authentication.ts
@@ -11,10 +11,14 @@ export const NO_AUTHORIZATION_ROUTES = [ /^\/api(\/v\d+)?\/guilds\/\d+\/widget\.(json|png)/ ]; +export const API_PREFIX = /^\/api(\/v\d+)?/; +export const API_PREFIX_TRAILING_SLASH = /^\/api(\/v\d+)?\//; + declare global { namespace Express { interface Request { user_id: any; + user_bot: boolean; token: any; } } @@ -23,17 +27,19 @@ declare global { export async function Authentication(req: Request, res: Response, next: NextFunction) { if (req.method === "OPTIONS") return res.sendStatus(204); if (!req.url.startsWith("/api")) return next(); - if (req.url.startsWith("/api/v8/invites") && req.method === "GET") return next(); + const apiPath = req.url.replace(API_PREFIX, ""); + if (apiPath.startsWith("/invites") && req.method === "GET") return next(); if (NO_AUTHORIZATION_ROUTES.some((x) => x.test(req.url))) return next(); if (!req.headers.authorization) return next(new HTTPError("Missing Authorization Header", 401)); try { const { jwtSecret } = Config.get().security; - const decoded: any = await checkToken(req.headers.authorization, jwtSecret); + const { decoded, user }: any = await checkToken(req.headers.authorization, jwtSecret); req.token = decoded; req.user_id = decoded.id; + req.user_bot = user.bot; return next(); } catch (error) { return next(new HTTPError(error.toString(), 400));