summary refs log tree commit diff
path: root/api/src/routes/auth/register.ts
blob: fecde874a567df79340fa1806fee6173644755aa (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
import { Request, Response, Router } from "express";
import { trimSpecial, User, Snowflake, UserModel, Config } from "@fosscord/util";
import bcrypt from "bcrypt";
import { check, Email, EMAIL_REGEX, FieldErrors, Length } from "../../util/instanceOf";
import "missing-native-js-functions";
import { generateToken } from "./login";
import { getIpAdress, IPAnalysis, isProxy } from "../../util/ipAddress";
import { HTTPError } from "lambert-server";
import RateLimit from "../../middlewares/RateLimit";

const router: Router = Router();

router.post(
	"/",
	check({
		username: new Length(String, 2, 32),
		// TODO: check min password length in config
		// prevent Denial of Service with max length of 72 chars
		password: new Length(String, 8, 72),
		consent: Boolean,
		$email: new Length(Email, 5, 100),
		$fingerprint: String,
		$invite: String,
		$date_of_birth: Date, // "2000-04-03"
		$gift_code_sku_id: String,
		$captcha_key: String
	}),
	async (req: Request, res: Response) => {
		const {
			email,
			username,
			password,
			consent,
			fingerprint,
			invite,
			date_of_birth,
			gift_code_sku_id, // ? what is this
			captcha_key
		} = req.body;

		// get register Config
		const { register, security } = Config.get();
		const ip = getIpAdress(req);

		if (register.blockProxies) {
			if (isProxy(await IPAnalysis(ip))) {
				console.log(`proxy ${ip} blocked from registration`);
				throw new HTTPError("Your IP is blocked from registration");
			}
		}

		console.log("register", req.body.email, req.body.username, ip);
		// TODO: automatically join invite
		// TODO: gift_code_sku_id?
		// TODO: check password strength

		// adjusted_email will be slightly modified version of the user supplied email -> e.g. protection against GMail Trick
		let adjusted_email: string | null = adjustEmail(email);

		// adjusted_password will be the hash of the password
		let adjusted_password: string = "";

		// trim special uf8 control characters -> Backspace, Newline, ...
		let adjusted_username: string = trimSpecial(username);

		// discriminator will be randomly generated
		let discriminator = "";

		// check if registration is allowed
		if (!register.allowNewRegistration) {
			throw FieldErrors({
				email: { code: "REGISTRATION_DISABLED", message: req.t("auth:register.REGISTRATION_DISABLED") }
			});
		}

		// check if the user agreed to the Terms of Service
		if (!consent) {
			throw FieldErrors({
				consent: { code: "CONSENT_REQUIRED", message: req.t("auth:register.CONSENT_REQUIRED") }
			});
		}

		// 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") }
			});
		}

		if (email) {
			// replace all dots and chars after +, if its a gmail.com email
			if (!adjusted_email) throw FieldErrors({ email: { code: "INVALID_EMAIL", message: req.t("auth:register.INVALID_EMAIL") } });

			// check if there is already an account with this email
			const exists = await UserModel.findOne({ email: adjusted_email })
				.exec()
				.catch((e) => {});

			if (exists) {
				throw FieldErrors({
					email: {
						code: "EMAIL_ALREADY_REGISTERED",
						message: req.t("auth:register.EMAIL_ALREADY_REGISTERED")
					}
				});
			}
		} else if (register.email.necessary) {
			throw FieldErrors({
				email: { code: "BASE_TYPE_REQUIRED", message: req.t("common:field.BASE_TYPE_REQUIRED") }
			});
		}

		if (register.dateOfBirth.necessary && !date_of_birth) {
			throw FieldErrors({
				date_of_birth: { code: "BASE_TYPE_REQUIRED", message: req.t("common:field.BASE_TYPE_REQUIRED") }
			});
		} else if (register.dateOfBirth.minimum) {
			const minimum = new Date();
			minimum.setFullYear(minimum.getFullYear() - register.dateOfBirth.minimum);

			// higher is younger
			if (date_of_birth > minimum) {
				throw FieldErrors({
					date_of_birth: {
						code: "DATE_OF_BIRTH_UNDERAGE",
						message: req.t("auth:register.DATE_OF_BIRTH_UNDERAGE", { years: register.dateOfBirth.minimum })
					}
				});
			}
		}

		if (!register.allowMultipleAccounts) {
			// TODO: check if fingerprint was eligible generated
			const exists = await UserModel.findOne({ fingerprints: fingerprint })
				.exec()
				.catch((e) => {});

			if (exists) {
				throw FieldErrors({
					email: {
						code: "EMAIL_ALREADY_REGISTERED",
						message: req.t("auth:register.EMAIL_ALREADY_REGISTERED")
					}
				});
			}
		}

		if (register.requireCaptcha && security.captcha.enabled) {
			if (!captcha_key) {
				const { sitekey, service } = security.captcha;
				return res.status(400).json({
					captcha_key: ["captcha-required"],
					captcha_sitekey: sitekey,
					captcha_service: service
				});
			}

			// TODO: check captcha
		}

		// the salt is saved in the password refer to bcrypt docs
		adjusted_password = await bcrypt.hash(password, 12);

		let exists;
		// randomly generates a discriminator between 1 and 9999 and checks max five times if it already exists
		// if it all five times already exists, abort with USERNAME_TOO_MANY_USERS error
		// else just continue
		// TODO: is there any better way to generate a random discriminator only once, without checking if it already exists in the mongodb database?
		for (let tries = 0; tries < 5; tries++) {
			discriminator = Math.randomIntBetween(1, 9999).toString().padStart(4, "0");
			try {
				exists = await UserModel.findOne({ discriminator, username: adjusted_username }, "id").exec();
			} catch (error) {
				// doesn't exist -> break
				break;
			}
		}

		if (exists) {
			throw FieldErrors({
				username: {
					code: "USERNAME_TOO_MANY_USERS",
					message: req.t("auth:register.USERNAME_TOO_MANY_USERS")
				}
			});
		}

		// TODO: save date_of_birth
		// appearently discord doesn't save the date of birth and just calculate if nsfw is allowed
		// if nsfw_allowed is null/undefined it'll require date_of_birth to set it to true/false

		const user: User = {
			id: Snowflake.generate(),
			created_at: new Date(),
			username: adjusted_username,
			discriminator,
			avatar: null,
			accent_color: null,
			banner: null,
			bot: false,
			system: false,
			desktop: false,
			mobile: false,
			premium: true,
			premium_type: 2,
			phone: null,
			bio: "",
			mfa_enabled: false,
			verified: false,
			disabled: false,
			deleted: false,
			presence: {
				activities: [],
				client_status: {
					desktop: undefined,
					mobile: undefined,
					web: undefined
				},
				status: "offline"
			},
			email: adjusted_email,
			nsfw_allowed: true, // TODO: depending on age
			public_flags: 0n,
			flags: 0n, // TODO: generate default flags
			guilds: [],
			user_data: {
				hash: adjusted_password,
				valid_tokens_since: new Date(),
				relationships: [],
				connected_accounts: [],
				fingerprints: []
			},
			user_settings: {
				afk_timeout: 300,
				allow_accessibility_detection: true,
				animate_emoji: true,
				animate_stickers: 0,
				contact_sync_enabled: false,
				convert_emoticons: false,
				custom_status: {
					emoji_id: null,
					emoji_name: null,
					expires_at: null,
					text: null
				},
				default_guilds_restricted: false,
				detect_platform_accounts: true,
				developer_mode: false,
				disable_games_tab: false,
				enable_tts_command: true,
				explicit_content_filter: 0,
				friend_source_flags: { all: true },
				gateway_connected: false,
				gif_auto_play: true,
				guild_folders: [],
				guild_positions: [],
				inline_attachment_media: true,
				inline_embed_media: true,
				locale: req.language,
				message_display_compact: false,
				native_phone_integration_enabled: true,
				render_embeds: true,
				render_reactions: true,
				restricted_guilds: [],
				show_current_game: true,
				status: "offline",
				stream_notifications_enabled: true,
				theme: "dark",
				timezone_offset: 0
				// timezone_offset: // TODO: timezone from request
			}
		};

		// insert user into database
		await new UserModel(user).save();

		return res.json({ token: await generateToken(user.id) });
	}
);

export function adjustEmail(email: string): string | null {
	// body parser already checked if it is a valid email
	const parts = <RegExpMatchArray>email.match(EMAIL_REGEX);
	// @ts-ignore
	if (!parts || parts.length < 5) return undefined;
	const domain = parts[5];
	const user = parts[1];

	// TODO: check accounts with uncommon email domains
	if (domain === "gmail.com" || domain === "googlemail.com") {
		// replace .dots and +alternatives -> Gmail Dot Trick https://support.google.com/mail/answer/7436150 and https://generator.email/blog/gmail-generator
		return user.replace(/[.]|(\+.*)/g, "") + "@gmail.com";
	}

	return email;
}

export default router;

/**
 * POST /auth/register
 * @argument { "fingerprint":"805826570869932034.wR8vi8lGlFBJerErO9LG5NViJFw", "email":"qo8etzvaf@gmail.com", "username":"qp39gr98", "password":"wtp9gep9gw", "invite":null, "consent":true, "date_of_birth":"2000-04-04", "gift_code_sku_id":null, "captcha_key":null}
 *
 * Field Error
 * @returns { "code": 50035, "errors": { "consent": { "_errors": [{ "code": "CONSENT_REQUIRED", "message": "You must agree to Discord's Terms of Service and Privacy Policy." }]}}, "message": "Invalid Form Body"}
 *
 * Success 201:
 * @returns {token: "OMITTED"}
 */