✨ Added reCaptcha v3
This commit is contained in:
+2
-1
@@ -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
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -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 @@
|
||||
</script>
|
||||
|
||||
<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>
|
||||
|
||||
{#if game_pin === '' || game_pin.length <= 5}
|
||||
|
||||
Reference in New Issue
Block a user