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
+15 -2
View File
@@ -63,9 +63,10 @@ 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 +++
if game_data.captcha_enabled:
async with aiohttp.ClientSession() as session: async with aiohttp.ClientSession() as session:
try: try:
if game_data.captcha_enabled: if settings.hcaptcha_key is not None:
try: try:
async with session.post( async with session.post(
"https://hcaptcha.com/siteverify", "https://hcaptcha.com/siteverify",
@@ -77,7 +78,19 @@ async def join_game(sid: str, data: dict):
return return
except KeyError: except KeyError:
print("CAPTCHA FAILED") print("CAPTCHA FAILED")
return
elif settings.recpatcha_key is not None:
async with session.post(
"https://www.google.com/recaptcha/api/siteverify",
data={"secret": settings.recpatcha_key, "response": data.captcha},
) as resp:
try:
resp_data = await resp.json()
if not resp_data["success"]:
print("CAPTCHA FAILED")
return
except KeyError:
print("CAPTCHA FAILED")
return return
except TypeError: except TypeError:
pass pass
+33 -2
View File
@@ -90,11 +90,18 @@
let captcha_resp: string; let captcha_resp: string;
if (captcha_enabled) { if (captcha_enabled) {
if (hcaptchaSitekey) {
try { try {
const { response } = await hcaptcha.execute(hcaptchaWidgetID, { const { response } = await hcaptcha.execute(hcaptchaWidgetID, {
async: true async: true
}); });
captcha_resp = response; 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) { } catch (e) {
if (import.meta.env.VITE_SENTRY !== null) { if (import.meta.env.VITE_SENTRY !== null) {
Sentry.captureException(e); Sentry.captureException(e);
@@ -110,13 +117,30 @@
} }
}); });
} }
} } 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', { socket.emit('join_game', {
username: username, username: username,
game_pin: game_pin, game_pin: game_pin,
captcha: captcha_resp, captcha: token,
custom_field: custom_field ? custom_field_value : undefined 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.on('game_not_found', () => { socket.on('game_not_found', () => {
game_pin = ''; game_pin = '';
@@ -127,7 +151,14 @@
</script> </script>
<svelte:head> <svelte:head>
{#if captcha_enabled && hcaptchaSitekey}
<script src="https://js.hcaptcha.com/1/api.js" async defer></script> <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}