summary refs log tree commit diff
diff options
context:
space:
mode:
authorFlam3rboy <34555296+Flam3rboy@users.noreply.github.com>2021-10-09 14:31:23 +0200
committerFlam3rboy <34555296+Flam3rboy@users.noreply.github.com>2021-10-09 14:31:23 +0200
commit0516357477e32f74ff8db03de5d1d6d5552d02e9 (patch)
tree388fa71b287eda1fc83b27bf5d1e148e67e55074
parent:bug: fix array key in config (diff)
parentMerge pull request #429 from fosscord/dev (diff)
downloadserver-0516357477e32f74ff8db03de5d1d6d5552d02e9.tar.xz
Merge branch 'master' of http://github.com/fosscord/fosscord-server
-rw-r--r--api/client_test/index.html2
-rw-r--r--api/src/routes/auth/register.ts15
-rw-r--r--api/src/routes/channels/#channel_id/typing.ts2
-rw-r--r--api/src/routes/users/@me/index.ts34
-rw-r--r--bundle/scripts/build.js1
-rw-r--r--cdn/src/routes/attachments.ts4
-rw-r--r--cdn/src/routes/avatars.ts2
-rw-r--r--cdn/src/routes/external.ts2
-rw-r--r--gateway/src/events/Message.ts2
-rw-r--r--util/src/entities/Config.ts2
10 files changed, 48 insertions, 18 deletions
diff --git a/api/client_test/index.html b/api/client_test/index.html
index fb2e0a2d..41d41598 100644
--- a/api/client_test/index.html
+++ b/api/client_test/index.html
@@ -47,7 +47,7 @@
 
 			// Auto register guest account:
 			const token = JSON.parse(localStorage.getItem("token"));
-			if (!token) {
+			if (!token && location.pathname !== "/login" && location.pathname !== "/register") {
 				fetch(`${window.GLOBAL_ENV.API_ENDPOINT}/auth/register`, {
 					method: "POST",
 					headers: { "content-type": "application/json" },
diff --git a/api/src/routes/auth/register.ts b/api/src/routes/auth/register.ts
index 9f3b46f1..cd1bcb72 100644
--- a/api/src/routes/auth/register.ts
+++ b/api/src/routes/auth/register.ts
@@ -98,7 +98,6 @@ router.post("/", route({ body: "RegisterSchema" }), async (req: Request, res: Re
 		}
 	}
 
-	console.log("register", body.email, body.username, ip);
 	// TODO: gift_code_sku_id?
 	// TODO: check password strength
 
@@ -154,18 +153,22 @@ router.post("/", route({ body: "RegisterSchema" }), async (req: Request, res: Re
 		});
 	}
 
+	if (!body.invite && (register.requireInvite || (register.guestsRequireInvite && !register.email))) {
+		// require invite to register -> e.g. for organizations to send invites to their employees
+		throw FieldErrors({
+			email: { code: "INVITE_ONLY", message: req.t("auth:register.INVITE_ONLY") }
+		});
+	}
+
 	const user = await User.register({ ...body, req });
 
 	if (body.invite) {
 		// await to fail if the invite doesn't exist (necessary for requireInvite to work properly) (username only signups are possible)
 		await Invite.joinGuild(user.id, body.invite);
-	} else if (register.requireInvite) {
-		// require invite to register -> e.g. for organizations to send invites to their employees
-		throw FieldErrors({
-			email: { code: "INVITE_ONLY", message: req.t("auth:register.INVITE_ONLY") }
-		});
 	}
 
+	console.log("register", body.email, body.username, ip);
+
 	return res.json({ token: await generateToken(user.id) });
 });
 
diff --git a/api/src/routes/channels/#channel_id/typing.ts b/api/src/routes/channels/#channel_id/typing.ts
index a9dcb315..45ed76db 100644
--- a/api/src/routes/channels/#channel_id/typing.ts
+++ b/api/src/routes/channels/#channel_id/typing.ts
@@ -9,7 +9,7 @@ router.post("/", route({ permission: "SEND_MESSAGES" }), async (req: Request, re
 	const user_id = req.user_id;
 	const timestamp = Date.now();
 	const channel = await Channel.findOneOrFail({ id: channel_id });
-	const member = await Member.findOneOrFail({ where: { id: user_id }, relations: ["roles"] });
+	const member = await Member.findOneOrFail({ where: { id: user_id }, relations: ["roles", "user"] });
 
 	await emitEvent({
 		event: "TYPING_START",
diff --git a/api/src/routes/users/@me/index.ts b/api/src/routes/users/@me/index.ts
index f6bb04d7..1959704a 100644
--- a/api/src/routes/users/@me/index.ts
+++ b/api/src/routes/users/@me/index.ts
@@ -1,6 +1,7 @@
 import { Router, Request, Response } from "express";
-import { User, PrivateUserProjection, emitEvent, UserUpdateEvent, handleFile } from "@fosscord/util";
+import { User, PrivateUserProjection, emitEvent, UserUpdateEvent, handleFile, FieldErrors } from "@fosscord/util";
 import { route } from "@fosscord/api";
+import bcrypt from "bcrypt";
 
 const router: Router = Router();
 
@@ -32,10 +33,35 @@ router.patch("/", route({ body: "UserModifySchema" }), async (req: Request, res:
 	if (body.avatar) body.avatar = await handleFile(`/avatars/${req.user_id}`, body.avatar as string);
 	if (body.banner) body.banner = await handleFile(`/banners/${req.user_id}`, body.banner as string);
 
-	await new User({ ...body, id: req.user_id }).save();
+	const user = await User.findOneOrFail({ where: { id: req.user_id }, select: [...PrivateUserProjection, "data"] });
+
+	if (body.password) {
+		if (user.data?.hash) {
+			const same_password = await bcrypt.compare(body.password, user.data.hash || "");
+			if (!same_password) {
+				throw FieldErrors({ password: { message: req.t("auth:login.INVALID_PASSWORD"), code: "INVALID_PASSWORD" } });
+			}
+		} else {
+			user.data.hash = await bcrypt.hash(body.password, 12);
+		}
+	}
+
+	user.assign(body);
+
+	if (body.new_password) {
+		if (!body.password && !user.email) {
+			throw FieldErrors({
+				password: { code: "BASE_TYPE_REQUIRED", message: req.t("common:field.BASE_TYPE_REQUIRED") }
+			});
+		}
+		user.data.hash = await bcrypt.hash(body.new_password, 12);
+	}
+
+	await user.save();
+
+	// @ts-ignore
+	delete user.data;
 
-	//Need to reload user from db due to https://github.com/typeorm/typeorm/issues/3490
-	const user = await User.findOneOrFail({ where: { id: req.user_id }, select: PrivateUserProjection });
 	// TODO: send update member list event in gateway
 	await emitEvent({
 		event: "USER_UPDATE",
diff --git a/bundle/scripts/build.js b/bundle/scripts/build.js
index c6a98b80..05cf37ce 100644
--- a/bundle/scripts/build.js
+++ b/bundle/scripts/build.js
@@ -37,7 +37,6 @@ function transpileFiles() {
 		const files = walk(path.join(__dirname, "..", "..", part, "dist"));
 		for (const file of files) {
 			let content = fs.readFileSync(file, { encoding: "utf8" });
-			console.log(file);
 			content = content
 				.replace(
 					new RegExp(`@fosscord/${part}`),
diff --git a/cdn/src/routes/attachments.ts b/cdn/src/routes/attachments.ts
index 354bdde9..ae50bc48 100644
--- a/cdn/src/routes/attachments.ts
+++ b/cdn/src/routes/attachments.ts
@@ -1,9 +1,9 @@
 import { Router, Response, Request } from "express";
 import { Config, Snowflake } from "@fosscord/util";
-import { storage } from "@fosscord/cdn";
+import { storage } from "../util/Storage";
 import FileType from "file-type";
 import { HTTPError } from "lambert-server";
-import { multer } from "@fosscord/cdn";
+import { multer } from "../util/multer";
 import imageSize from "image-size";
 
 const router = Router();
diff --git a/cdn/src/routes/avatars.ts b/cdn/src/routes/avatars.ts
index 981cc417..3d5e7d77 100644
--- a/cdn/src/routes/avatars.ts
+++ b/cdn/src/routes/avatars.ts
@@ -1,6 +1,6 @@
 import { Router, Response, Request } from "express";
 import { Config, Snowflake } from "@fosscord/util";
-import { storage } from "@fosscord/cdn";
+import { storage } from "../util/Storage";
 import FileType from "file-type";
 import { HTTPError } from "lambert-server";
 import crypto from "crypto";
diff --git a/cdn/src/routes/external.ts b/cdn/src/routes/external.ts
index 50014600..dc90e3c8 100644
--- a/cdn/src/routes/external.ts
+++ b/cdn/src/routes/external.ts
@@ -2,7 +2,7 @@ import { Router, Response, Request } from "express";
 import fetch from "node-fetch";
 import { HTTPError } from "lambert-server";
 import { Snowflake } from "@fosscord/util";
-import { storage } from "@fosscord/cdn";
+import { storage } from "../util/Storage";
 import FileType from "file-type";
 import { Config } from "@fosscord/util";
 
diff --git a/gateway/src/events/Message.ts b/gateway/src/events/Message.ts
index 3648931d..af318bfd 100644
--- a/gateway/src/events/Message.ts
+++ b/gateway/src/events/Message.ts
@@ -5,7 +5,7 @@ try {
 	erlpack = require("@yukikaze-bot/erlpack");
 } catch (error) {}
 import OPCodeHandlers from "../opcodes";
-import { instanceOf, Tuple } from "lambert-server";
+import { Tuple } from "lambert-server";
 import { check } from "../opcodes/instanceOf";
 import WS from "ws";
 
diff --git a/util/src/entities/Config.ts b/util/src/entities/Config.ts
index 921a12c2..813649ac 100644
--- a/util/src/entities/Config.ts
+++ b/util/src/entities/Config.ts
@@ -128,6 +128,7 @@ export interface ConfigValue {
 		disabled: boolean;
 		requireCaptcha: boolean;
 		requireInvite: boolean;
+		guestsRequireInvite: boolean;
 		allowNewRegistration: boolean;
 		allowMultipleAccounts: boolean;
 		blockProxies: boolean;
@@ -277,6 +278,7 @@ export const DefaultConfigOptions: ConfigValue = {
 		},
 		disabled: false,
 		requireInvite: false,
+		guestsRequireInvite: true,
 		requireCaptcha: true,
 		allowNewRegistration: true,
 		allowMultipleAccounts: true,