summary refs log tree commit diff
diff options
context:
space:
mode:
-rw-r--r--src/Server.ts1
-rw-r--r--src/middlewares/Authentication.ts17
-rw-r--r--src/routes/api/v8/auth/login.ts3
-rw-r--r--src/routes/api/v8/auth/register.ts13
-rw-r--r--src/test/mongo_test.ts2
5 files changed, 16 insertions, 20 deletions
diff --git a/src/Server.ts b/src/Server.ts

index 4d949353..d427b964 100644 --- a/src/Server.ts +++ b/src/Server.ts
@@ -1,3 +1,4 @@ +import "missing-native-js-functions"; import fs from "fs/promises"; import { Server, ServerOptions } from "lambert-server"; import { Authentication, GlobalRateLimit } from "./middlewares/"; diff --git a/src/middlewares/Authentication.ts b/src/middlewares/Authentication.ts
index 8fbae122..979c954a 100644 --- a/src/middlewares/Authentication.ts +++ b/src/middlewares/Authentication.ts
@@ -1,8 +1,6 @@ -import jwt from "jsonwebtoken"; import { NextFunction, Request, Response } from "express"; import { HTTPError } from "lambert-server"; -import Config from "../util/Config"; -import { JWTOptions } from "../util/Constants"; +import { checkToken } from "discord-server-util"; export const NO_AUTHORIZATION_ROUTES = ["/api/v8/auth/login", "/api/v8/auth/register"]; @@ -15,16 +13,13 @@ declare global { } } -export function Authentication(req: Request, res: Response, next: NextFunction) { +export async function Authentication(req: Request, res: Response, next: NextFunction) { if (NO_AUTHORIZATION_ROUTES.includes(req.url)) return next(); if (!req.headers.authorization) return next(new HTTPError("Missing Authorization Header", 401)); + // TODO: check if user is banned/token expired - return jwt.verify(req.headers.authorization, Config.get().security.jwtSecret, JWTOptions, (err, decoded: any) => { - if (err || !decoded) return next(new HTTPError("Invalid Token", 401)); + const decoded: any = await checkToken(req.headers.authorization); - req.token = decoded; - req.userid = decoded.id; - - return next(); - }); + req.token = decoded; + req.userid = decoded.id; } diff --git a/src/routes/api/v8/auth/login.ts b/src/routes/api/v8/auth/login.ts
index 9cccbca5..f12c0a64 100644 --- a/src/routes/api/v8/auth/login.ts +++ b/src/routes/api/v8/auth/login.ts
@@ -2,10 +2,9 @@ import { Request, Response, Router } from "express"; import { check, FieldErrors, Length } from "../../../../util/instanceOf"; import bcrypt from "bcrypt"; import jwt from "jsonwebtoken"; +import { db, User } from "discord-server-util"; import Config from "../../../../util/Config"; -import { User } from "../../../../models/User"; import { adjustEmail } from "./register"; -import { db } from "discord-server-util"; const router: Router = Router(); export default router; diff --git a/src/routes/api/v8/auth/register.ts b/src/routes/api/v8/auth/register.ts
index b5800d1f..d53beb74 100644 --- a/src/routes/api/v8/auth/register.ts +++ b/src/routes/api/v8/auth/register.ts
@@ -1,13 +1,10 @@ -import { NextFunction, Request, Response, Router } from "express"; +import { Request, Response, Router } from "express"; import Config from "../../../../util/Config"; -import { db } from "discord-server-util"; +import { db, trimSpecial, User, Snowflake } from "discord-server-util"; import bcrypt from "bcrypt"; import { check, Email, EMAIL_REGEX, FieldErrors, Length } from "../../../../util/instanceOf"; -import { Snowflake } from "../../../../util/Snowflake"; import "missing-native-js-functions"; -import { User } from "../../../../models/User"; import { generateToken } from "./login"; -import { trimSpecial } from "../../../../util/String"; const router: Router = Router(); @@ -54,6 +51,7 @@ router.post( // discriminator will be randomly generated let discriminator = ""; + // get register Config const { register } = Config.get(); // check if registration is allowed @@ -70,7 +68,7 @@ router.post( }); } - // require invite to register -> for organizations to send invites to their employees + // require invite to register -> e.g. for organizations to send invites to their employees if (register.requireInvite && !invite) { throw FieldErrors({ email: { code: "INVITE_ONLY", message: req.t("auth:register.INVITE_ONLY") }, @@ -78,6 +76,7 @@ router.post( } if (email) { + // replace all dots and chars after +, if its a gmail.com email adjusted_email = adjustEmail(email); // check if there is already an account with this email @@ -164,6 +163,7 @@ router.post( }); } + // constructing final user object const user: User = { id: Snowflake.generate(), created_at: Date.now(), @@ -218,6 +218,7 @@ router.post( }, }; + // insert user into database await db.data.users.push(user); return res.json({ token: await generateToken(user.id) }); diff --git a/src/test/mongo_test.ts b/src/test/mongo_test.ts
index d6906402..655bd67f 100644 --- a/src/test/mongo_test.ts +++ b/src/test/mongo_test.ts
@@ -1,6 +1,6 @@ import mongoose from "mongoose"; import { Long } from "mongodb"; -import { Snowflake } from "../util/Snowflake"; +import { Snowflake } from "../../../server-util/src/util/Snowflake"; async function main() { const conn = await mongoose.createConnection(