diff options
author | Madeline <46743919+MaddyUnderStars@users.noreply.github.com> | 2023-01-05 17:12:21 +1100 |
---|---|---|
committer | Madeline <46743919+MaddyUnderStars@users.noreply.github.com> | 2023-01-05 17:16:55 +1100 |
commit | df449169bde6c0576757700f68f2d406139cc846 (patch) | |
tree | f8bcbaf30ed3ed007d392a51614a134e543e3c17 /src/api/routes/auth | |
parent | channel flags whoops (diff) | |
download | server-df449169bde6c0576757700f68f2d406139cc846.tar.xz |
Prettier
Diffstat (limited to 'src/api/routes/auth')
-rw-r--r-- | src/api/routes/auth/generate-registration-tokens.ts | 45 | ||||
-rw-r--r-- | src/api/routes/auth/register.ts | 44 |
2 files changed, 62 insertions, 27 deletions
diff --git a/src/api/routes/auth/generate-registration-tokens.ts b/src/api/routes/auth/generate-registration-tokens.ts index e328fe5e..0d4cf067 100644 --- a/src/api/routes/auth/generate-registration-tokens.ts +++ b/src/api/routes/auth/generate-registration-tokens.ts @@ -5,24 +5,37 @@ import { Request, Response, Router } from "express"; const router: Router = Router(); export default router; -router.get("/", route({ right: "OPERATOR" }), async (req: Request, res: Response) => { - const count = req.query.count ? parseInt(req.query.count as string) : 1; - const length = req.query.length ? parseInt(req.query.length as string) : 255; +router.get( + "/", + route({ right: "OPERATOR" }), + async (req: Request, res: Response) => { + const count = req.query.count ? parseInt(req.query.count as string) : 1; + const length = req.query.length + ? parseInt(req.query.length as string) + : 255; - let tokens: ValidRegistrationToken[] = []; + let tokens: ValidRegistrationToken[] = []; - for (let i = 0; i < count; i++) { - const token = ValidRegistrationToken.create({ - token: random(length), - expires_at: Date.now() + Config.get().security.defaultRegistrationTokenExpiration - }); - tokens.push(token); - } + for (let i = 0; i < count; i++) { + const token = ValidRegistrationToken.create({ + token: random(length), + expires_at: + Date.now() + + Config.get().security.defaultRegistrationTokenExpiration, + }); + tokens.push(token); + } - // Why are these options used, exactly? - await ValidRegistrationToken.save(tokens, { chunk: 1000, reload: false, transaction: false }); + // Why are these options used, exactly? + await ValidRegistrationToken.save(tokens, { + chunk: 1000, + reload: false, + transaction: false, + }); - if (req.query.plain) return res.send(tokens.map(x => x.token).join("\n")); + if (req.query.plain) + return res.send(tokens.map((x) => x.token).join("\n")); - return res.json({ tokens: tokens.map(x => x.token) }); -}); \ No newline at end of file + return res.json({ tokens: tokens.map((x) => x.token) }); + }, +); diff --git a/src/api/routes/auth/register.ts b/src/api/routes/auth/register.ts index c8c515e7..3d968114 100644 --- a/src/api/routes/auth/register.ts +++ b/src/api/routes/auth/register.ts @@ -33,16 +33,22 @@ router.post( // Reg tokens // They're a one time use token that bypasses registration limits ( rates, disabled reg, etc ) let regTokenUsed = false; - if (req.get("Referrer") && req.get("Referrer")?.includes("token=")) { // eg theyre on https://staging.fosscord.com/register?token=whatever + if (req.get("Referrer") && req.get("Referrer")?.includes("token=")) { + // eg theyre on https://staging.fosscord.com/register?token=whatever const token = req.get("Referrer")!.split("token=")[1].split("&")[0]; if (token) { - const regToken = await ValidRegistrationToken.findOne({ where: { token, expires_at: MoreThan(new Date()), } }); + const regToken = await ValidRegistrationToken.findOne({ + where: { token, expires_at: MoreThan(new Date()) }, + }); await ValidRegistrationToken.delete({ token }); regTokenUsed = true; - console.log(`[REGISTER] Registration token ${token} used for registration!`); - } - else { - console.log(`[REGISTER] Invalid registration token ${token} used for registration by ${ip}!`); + console.log( + `[REGISTER] Registration token ${token} used for registration!`, + ); + } else { + console.log( + `[REGISTER] Invalid registration token ${token} used for registration by ${ip}!`, + ); } } @@ -78,7 +84,11 @@ router.post( }); } - if (!regTokenUsed && register.requireCaptcha && security.captcha.enabled) { + if ( + !regTokenUsed && + register.requireCaptcha && + security.captcha.enabled + ) { const { sitekey, service } = security.captcha; if (!body.captcha_key) { return res?.status(400).json({ @@ -220,14 +230,26 @@ router.post( if ( !regTokenUsed && limits.absoluteRate.register.enabled && - (await User.count({ where: { created_at: MoreThan(new Date(Date.now() - limits.absoluteRate.register.window)) } })) - >= limits.absoluteRate.register.limit + (await User.count({ + where: { + created_at: MoreThan( + new Date( + Date.now() - limits.absoluteRate.register.window, + ), + ), + }, + })) >= limits.absoluteRate.register.limit ) { console.log( - `Global register ratelimit exceeded for ${getIpAdress(req)}, ${req.body.username}, ${req.body.invite || "No invite given"}` + `Global register ratelimit exceeded for ${getIpAdress(req)}, ${ + req.body.username + }, ${req.body.invite || "No invite given"}`, ); throw FieldErrors({ - email: { code: "TOO_MANY_REGISTRATIONS", message: req.t("auth:register.TOO_MANY_REGISTRATIONS") } + email: { + code: "TOO_MANY_REGISTRATIONS", + message: req.t("auth:register.TOO_MANY_REGISTRATIONS"), + }, }); } |