Added reCaptcha v3

This commit is contained in:
Mawoka
2022-10-05 17:57:01 +02:00
parent 46942c7e8b
commit caaae303cf
3 changed files with 84 additions and 39 deletions
+2 -1
View File
@@ -22,7 +22,8 @@ class Settings(BaseSettings):
redis: RedisDsn = "redis://localhost:6379/0?decode_responses=True" redis: RedisDsn = "redis://localhost:6379/0?decode_responses=True"
skip_email_verification: bool = False skip_email_verification: bool = False
db_url: str | PostgresDsn = "postgresql://postgres:mysecretpassword@localhost:5432/classquiz" 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_address: str
mail_password: str mail_password: str
mail_username: str mail_username: str
+27 -14
View File
@@ -63,24 +63,37 @@ async def join_game(sid: str, data: dict):
await sio.emit("game_already_started", room=sid) await sio.emit("game_already_started", room=sid)
return return
# +++ START checking captcha +++ # +++ START checking captcha +++
async with aiohttp.ClientSession() as session: if game_data.captcha_enabled:
try: async with aiohttp.ClientSession() as session:
if game_data.captcha_enabled: try:
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( async with session.post(
"https://hcaptcha.com/siteverify", "https://www.google.com/recaptcha/api/siteverify",
data={"response": data.captcha, "secret": settings.hcaptcha_key}, data={"secret": settings.recpatcha_key, "response": data.captcha},
) as resp: ) as resp:
resp_data = await resp.json() try:
if not resp_data["success"]: resp_data = await resp.json()
if not resp_data["success"]:
print("CAPTCHA FAILED")
return
except KeyError:
print("CAPTCHA FAILED") print("CAPTCHA FAILED")
return return
except KeyError: except TypeError:
print("CAPTCHA FAILED") pass
return
except TypeError:
pass
# --- END checking captcha --- # --- END checking captcha ---
if await redis.get(f"game_session:{data.game_pin}:players:{data.username}") is not None: if await redis.get(f"game_session:{data.game_pin}:players:{data.username}") is not None:
await sio.emit("username_already_exists", room=sid) await sio.emit("username_already_exists", room=sid)
+55 -24
View File
@@ -90,33 +90,57 @@
let captcha_resp: string; let captcha_resp: string;
if (captcha_enabled) { if (captcha_enabled) {
try { if (hcaptchaSitekey) {
const { response } = await hcaptcha.execute(hcaptchaWidgetID, { try {
async: true const { response } = await hcaptcha.execute(hcaptchaWidgetID, {
}); async: true
captcha_resp = response; });
} catch (e) { captcha_resp = response;
if (import.meta.env.VITE_SENTRY !== null) { socket.emit('join_game', {
Sentry.captureException(e); username: username,
} game_pin: game_pin,
alertModal.set({ captcha: captcha_resp,
open: true, custom_field: custom_field ? custom_field_value : undefined
body: "The captcha failed, which is normal, but most of the time it's fixed by reloading!", });
title: 'Captcha failed' } catch (e) {
}); if (import.meta.env.VITE_SENTRY !== null) {
alertModal.subscribe((data) => { Sentry.captureException(e);
if (!data.open) {
window.location.reload();
} }
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', () => { socket.on('game_not_found', () => {
game_pin = ''; game_pin = '';
@@ -127,7 +151,14 @@
</script> </script>
<svelte:head> <svelte:head>
<script src="https://js.hcaptcha.com/1/api.js" async defer></script> {#if captcha_enabled && hcaptchaSitekey}
<script src="https://js.hcaptcha.com/1/api.js" async defer></script>
{/if}
{#if import.meta.env.VITE_RECAPTCHA && captcha_enabled}
<script
src="https://www.google.com/recaptcha/api.js?render={import.meta.env
.VITE_RECAPTCHA}"></script>
{/if}
</svelte:head> </svelte:head>
{#if game_pin === '' || game_pin.length <= 5} {#if game_pin === '' || game_pin.length <= 5}