summary refs log tree commit diff
path: root/api/src/routes
diff options
context:
space:
mode:
authorMadeline <46743919+MaddyUnderStars@users.noreply.github.com>2022-07-20 14:35:32 +1000
committerMadeline <46743919+MaddyUnderStars@users.noreply.github.com>2022-07-20 14:39:35 +1000
commit8d9816879fbc838e4a8b54001673c487a3d3a36a (patch)
treecc26e5a5c503f80a5a6bfec8dbc6f42e56f6785e /api/src/routes
parentHcaptcha support on login/register (diff)
downloadserver-8d9816879fbc838e4a8b54001673c487a3d3a36a.tar.xz
Hcaptcha backend
Diffstat (limited to 'api/src/routes')
-rw-r--r--api/src/routes/auth/login.ts14
-rw-r--r--api/src/routes/auth/register.ts17
2 files changed, 23 insertions, 8 deletions
diff --git a/api/src/routes/auth/login.ts b/api/src/routes/auth/login.ts

index a89721ea..f5a76393 100644 --- a/api/src/routes/auth/login.ts +++ b/api/src/routes/auth/login.ts
@@ -1,5 +1,5 @@ import { Request, Response, Router } from "express"; -import { route } from "@fosscord/api"; +import { route, getIpAdress, verifyHcaptcha } from "@fosscord/api"; import bcrypt from "bcrypt"; import { Config, User, generateToken, adjustEmail, FieldErrors } from "@fosscord/util"; @@ -23,8 +23,8 @@ router.post("/", route({ body: "LoginSchema" }), async (req: Request, res: Respo const config = Config.get(); if (config.login.requireCaptcha && config.security.captcha.enabled) { + const { sitekey, service, secret } = config.security.captcha; if (!captcha_key) { - const { sitekey, service } = config.security.captcha; return res.status(400).json({ captcha_key: ["captcha-required"], captcha_sitekey: sitekey, @@ -32,7 +32,15 @@ router.post("/", route({ body: "LoginSchema" }), async (req: Request, res: Respo }); } - // TODO: check captcha + const ip = getIpAdress(req); + const verify = await verifyHcaptcha(captcha_key, ip); + if (!verify.success) { + return res.status(400).json({ + captcha_key: verify["error-codes"], + captcha_sitekey: sitekey, + captcha_service: service + }); + } } const user = await User.findOneOrFail({ diff --git a/api/src/routes/auth/register.ts b/api/src/routes/auth/register.ts
index 126f3dbc..dd5aae84 100644 --- a/api/src/routes/auth/register.ts +++ b/api/src/routes/auth/register.ts
@@ -1,6 +1,6 @@ import { Request, Response, Router } from "express"; -import { Config, generateToken, Invite, FieldErrors, User, adjustEmail, trimSpecial } from "@fosscord/util"; -import { route, getIpAdress, IPAnalysis, isProxy } from "@fosscord/api"; +import { Config, generateToken, Invite, FieldErrors, User, adjustEmail } from "@fosscord/util"; +import { route, getIpAdress, IPAnalysis, isProxy, verifyHcaptcha } from "@fosscord/api"; import "missing-native-js-functions"; import bcrypt from "bcrypt"; import { HTTPError } from "lambert-server"; @@ -67,16 +67,23 @@ router.post("/", route({ body: "RegisterSchema" }), async (req: Request, res: Re } if (register.requireCaptcha && security.captcha.enabled) { + const { sitekey, service, secret } = security.captcha; if (!body.captcha_key) { - const { sitekey, service } = security.captcha; - return res?.status(400).json({ + return res.status(400).json({ captcha_key: ["captcha-required"], captcha_sitekey: sitekey, captcha_service: service }); } - // TODO: check captcha + const verify = await verifyHcaptcha(body.captcha_key, ip); + if (!verify.success) { + return res.status(400).json({ + captcha_key: verify["error-codes"], + captcha_sitekey: sitekey, + captcha_service: service + }); + } } if (!register.allowMultipleAccounts) {