diff --git a/classquiz/config.py b/classquiz/config.py index ada8b20..71f28ef 100644 --- a/classquiz/config.py +++ b/classquiz/config.py @@ -22,7 +22,8 @@ class Settings(BaseSettings): redis: RedisDsn = "redis://localhost:6379/0?decode_responses=True" skip_email_verification: bool = False db_url: str | PostgresDsn = "postgresql://postgres:mysecretpassword@localhost:5432/classquiz" - hcaptcha_key: str + hcaptcha_key: str | None = None + recpatcha_key: str | None = None mail_address: str mail_password: str mail_username: str diff --git a/classquiz/socket_server/__init__.py b/classquiz/socket_server/__init__.py index 483a6d7..ca1586c 100644 --- a/classquiz/socket_server/__init__.py +++ b/classquiz/socket_server/__init__.py @@ -63,24 +63,37 @@ async def join_game(sid: str, data: dict): await sio.emit("game_already_started", room=sid) return # +++ START checking captcha +++ - async with aiohttp.ClientSession() as session: - try: - if game_data.captcha_enabled: - try: + if game_data.captcha_enabled: + async with aiohttp.ClientSession() as session: + try: + if settings.hcaptcha_key is not None: + try: + async with session.post( + "https://hcaptcha.com/siteverify", + data={"response": data.captcha, "secret": settings.hcaptcha_key}, + ) as resp: + resp_data = await resp.json() + if not resp_data["success"]: + print("CAPTCHA FAILED") + return + except KeyError: + print("CAPTCHA FAILED") + return + elif settings.recpatcha_key is not None: async with session.post( - "https://hcaptcha.com/siteverify", - data={"response": data.captcha, "secret": settings.hcaptcha_key}, + "https://www.google.com/recaptcha/api/siteverify", + data={"secret": settings.recpatcha_key, "response": data.captcha}, ) as resp: - resp_data = await resp.json() - if not resp_data["success"]: + try: + resp_data = await resp.json() + if not resp_data["success"]: + print("CAPTCHA FAILED") + return + except KeyError: print("CAPTCHA FAILED") return - except KeyError: - print("CAPTCHA FAILED") - - return - except TypeError: - pass + except TypeError: + pass # --- END checking captcha --- if await redis.get(f"game_session:{data.game_pin}:players:{data.username}") is not None: await sio.emit("username_already_exists", room=sid) diff --git a/frontend/src/lib/play/join.svelte b/frontend/src/lib/play/join.svelte index 19e8b16..a425ad0 100644 --- a/frontend/src/lib/play/join.svelte +++ b/frontend/src/lib/play/join.svelte @@ -90,33 +90,57 @@ let captcha_resp: string; if (captcha_enabled) { - try { - const { response } = await hcaptcha.execute(hcaptchaWidgetID, { - async: true - }); - captcha_resp = response; - } catch (e) { - if (import.meta.env.VITE_SENTRY !== null) { - Sentry.captureException(e); - } - alertModal.set({ - open: true, - body: "The captcha failed, which is normal, but most of the time it's fixed by reloading!", - title: 'Captcha failed' - }); - alertModal.subscribe((data) => { - if (!data.open) { - window.location.reload(); + if (hcaptchaSitekey) { + try { + const { response } = await hcaptcha.execute(hcaptchaWidgetID, { + async: true + }); + captcha_resp = response; + socket.emit('join_game', { + username: username, + game_pin: game_pin, + captcha: captcha_resp, + custom_field: custom_field ? custom_field_value : undefined + }); + } catch (e) { + if (import.meta.env.VITE_SENTRY !== null) { + Sentry.captureException(e); } + alertModal.set({ + open: true, + body: "The captcha failed, which is normal, but most of the time it's fixed by reloading!", + title: 'Captcha failed' + }); + alertModal.subscribe((data) => { + if (!data.open) { + window.location.reload(); + } + }); + } + } else if (import.meta.env.VITE_RECAPTCHA) { + // eslint-disable-next-line no-undef + grecaptcha.ready(() => { + // eslint-disable-next-line no-undef + grecaptcha + .execute(import.meta.env.VITE_RECAPTCHA, { action: 'submit' }) + .then(function (token) { + socket.emit('join_game', { + username: username, + game_pin: game_pin, + captcha: token, + custom_field: custom_field ? custom_field_value : undefined + }); + }); }); } + } else { + socket.emit('join_game', { + username: username, + game_pin: game_pin, + captcha: undefined, + custom_field: custom_field ? custom_field_value : undefined + }); } - socket.emit('join_game', { - username: username, - game_pin: game_pin, - captcha: captcha_resp, - custom_field: custom_field ? custom_field_value : undefined - }); }; socket.on('game_not_found', () => { game_pin = ''; @@ -127,7 +151,14 @@ - + {#if captcha_enabled && hcaptchaSitekey} + + {/if} + {#if import.meta.env.VITE_RECAPTCHA && captcha_enabled} + + {/if} {#if game_pin === '' || game_pin.length <= 5}