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
|
import { route } from "@fosscord/api";
import {
adjustEmail,
Config,
FieldErrors,
generateToken,
getIpAdress,
HTTPError,
Invite,
IPAnalysis,
isProxy,
RegisterSchema,
User,
ValidRegistrationToken,
verifyCaptcha
} from "@fosscord/util";
import { Request, Response, Router } from "express";
import { red, yellow } from "picocolors";
import { LessThan, MoreThan } from "typeorm";
let bcrypt: any;
try {
bcrypt = require("bcrypt");
} catch {
bcrypt = require("bcryptjs");
console.log("Warning: using bcryptjs because bcrypt is not installed! Performance will be affected.");
}
const router: Router = Router();
router.post("/", route({ body: "RegisterSchema" }), async (req: Request, res: Response) => {
const body = req.body as RegisterSchema;
const { register, security, limits } = Config.get();
const ip = getIpAdress(req);
// email will be slightly modified version of the user supplied email -> e.g. protection against GMail Trick
let email = adjustEmail(body.email);
//check if referrer starts with any valid registration token
//!! bypasses captcha and registration disabling !!//
let validToken = false;
if (req.get("Referrer") && req.get("Referrer")?.includes("token=")) {
let token = req.get("Referrer")?.split("token=")[1].split("&")[0];
if (token) {
await ValidRegistrationToken.delete({ expires_at: LessThan(new Date()) });
let registrationToken = await ValidRegistrationToken.findOne({ where: { token: token, expires_at: MoreThan(new Date()) } });
if (registrationToken) {
console.log(yellow(`[REGISTER] Registration token ${token} used for registration!`));
await ValidRegistrationToken.delete(token);
validToken = true;
} else {
console.log(yellow(`[REGISTER] Invalid registration token ${token} used for registration by ${ip}!`));
}
}
}
// check if the user agreed to the Terms of Service
if (!body.consent) {
throw FieldErrors({
consent: { code: "CONSENT_REQUIRED", message: req.t("auth:register.CONSENT_REQUIRED") }
});
}
if (register.requireCaptcha && security.captcha.enabled && !validToken) {
const { sitekey, service } = security.captcha;
if (!body.captcha_key) {
return res?.status(400).json({
captcha_key: ["captcha-required"],
captcha_sitekey: sitekey,
captcha_service: service
});
}
const verify = await verifyCaptcha(body.captcha_key, ip);
if (!verify.success) {
return res.status(400).json({
captcha_key: verify["error-codes"],
captcha_sitekey: sitekey,
captcha_service: service
});
}
}
// check if registration is allowed
if (!register.allowNewRegistration && !validToken) {
throw FieldErrors({
email: { code: "REGISTRATION_DISABLED", message: req.t("auth:register.REGISTRATION_DISABLED") }
});
}
if (register.blockProxies) {
let data;
try {
data = await IPAnalysis(ip);
} catch (e: any) {
console.warn(red(`[REGISTER]: Failed to analyze IP ${ip}: failed to contact api.ipdata.co!`), e.message);
}
if (data && isProxy(data)) {
console.log(yellow(`[REGISTER] Proxy ${ip} blocked from registration!`));
throw new HTTPError(req.t("auth:register.IP_BLOCKED"));
}
}
// TODO: gift_code_sku_id?
// TODO: check password strength
if (email) {
// check if there is already an account with this email
const exists = await User.findOne({ where: { email: email } });
if (exists && !register.disabled) {
throw FieldErrors({
email: {
code: "EMAIL_ALREADY_REGISTERED",
message: req.t("auth:register.EMAIL_ALREADY_REGISTERED")
}
});
}
} else if (register.email.required) {
throw FieldErrors({
email: { code: "BASE_TYPE_REQUIRED", message: req.t("common:field.BASE_TYPE_REQUIRED") }
});
}
// If no password is provided, this is a guest account
if (register.dateOfBirth.required && !body.date_of_birth && body.password) {
throw FieldErrors({
date_of_birth: { code: "BASE_TYPE_REQUIRED", message: req.t("common:field.BASE_TYPE_REQUIRED") }
});
} else if (register.dateOfBirth.required && register.dateOfBirth.minimum) {
const minimum = new Date();
minimum.setFullYear(minimum.getFullYear() - register.dateOfBirth.minimum);
body.date_of_birth = new Date(body.date_of_birth as Date);
// higher is younger
if (body.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 (body.password) {
// the salt is saved in the password refer to bcrypt docs
body.password = await bcrypt.hash(body.password, 12);
} else if (register.password.required) {
throw FieldErrors({
password: { code: "BASE_TYPE_REQUIRED", message: req.t("common:field.BASE_TYPE_REQUIRED") }
});
}
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") }
});
}
if (
!validToken &&
limits.absoluteRate.register.enabled &&
(await await User.count({ where: { created_at: MoreThan(new Date(Date.now() - limits.absoluteRate.register.window)) } })) >=
limits.absoluteRate.register.limit
) {
console.log(
yellow(
`[REGISTER] Global register rate limit exceeded for ${getIpAdress(req)}: ${
process.env.LOG_SENSITIVE ? req.body.email : "<email redacted>"
}, ${req.body.username}, ${req.body.invite ?? "No invite given"}`
)
);
let oldest = await User.findOne({
where: { created_at: MoreThan(new Date(Date.now() - limits.absoluteRate.register.window)) },
order: { created_at: "ASC" }
});
if (!oldest) {
console.warn(
red(
`[REGISTER/WARN] Global rate limits exceeded, but no oldest user found. This should not happen. Did you misconfigure the limits?`
)
);
} else {
let retryAfterSec = Math.ceil(
(oldest!.created_at.getTime() - new Date(Date.now() - limits.absoluteRate.register.window).getTime()) / 1000
);
return res
.status(429)
.set("X-RateLimit-Limit", `${limits.absoluteRate.register.limit}`)
.set("X-RateLimit-Remaining", "0")
.set("X-RateLimit-Reset", `${(oldest!.created_at.getTime() + limits.absoluteRate.register.window) / 1000}`)
.set("X-RateLimit-Reset-After", `${retryAfterSec}`)
.set("X-RateLimit-Global", `true`)
.set("Retry-After", `${retryAfterSec}`)
.set("X-RateLimit-Bucket", `register`)
.send({ message: req.t("auth:register.TOO_MANY_REGISTRATIONS"), retry_after: retryAfterSec, global: true });
}
}
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);
}
return res.json({ token: await generateToken(user.id) });
});
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 200:
* @returns {token: "OMITTED"}
*/
|