Adds hCaptcha support

This commit is contained in:
Mouse Reeve 2024-10-17 18:21:10 -07:00
parent 14dba48415
commit c61d8ed2ad
5 changed files with 24 additions and 5 deletions

View file

@ -23,6 +23,10 @@ DEFAULT_LANGUAGE="English"
MEDIA_ROOT=images/ MEDIA_ROOT=images/
# hCaptcha configuration
HCAPTCHA_SITEKEY=
HCAPTCHA_SECRET=
# Database configuration # Database configuration
PGPORT=5432 PGPORT=5432
POSTGRES_PASSWORD=securedbypassword123 POSTGRES_PASSWORD=securedbypassword123

View file

@ -4,6 +4,7 @@ from django.contrib.auth.password_validation import validate_password
from django.core.exceptions import ValidationError from django.core.exceptions import ValidationError
from django.utils.translation import gettext_lazy as _ from django.utils.translation import gettext_lazy as _
from hcaptcha_field import hCaptchaField
import pyotp import pyotp
from bookwyrm import models from bookwyrm import models
@ -38,9 +39,11 @@ class LoginForm(CustomForm):
class RegisterForm(CustomForm): class RegisterForm(CustomForm):
hcaptcha = hCaptchaField()
class Meta: class Meta:
model = models.User model = models.User
fields = ["localname", "email", "password"] fields = ["localname", "email", "password", "hcaptcha"]
help_texts = {f: None for f in fields} help_texts = {f: None for f in fields}
widgets = {"password": forms.PasswordInput()} widgets = {"password": forms.PasswordInput()}

View file

@ -35,6 +35,9 @@ SESSION_COOKIE_AGE = env.int("SESSION_COOKIE_AGE", 3600 * 24 * 30) # 1 month
JS_CACHE = "8a89cad7" JS_CACHE = "8a89cad7"
HCAPTCHA_SITEKEY = env("HCAPTCHA_SITEKEY")
HCAPTCHA_SECRET = env("HCAPTCHA_SECRET")
# email # email
EMAIL_BACKEND = env("EMAIL_BACKEND", "django.core.mail.backends.smtp.EmailBackend") EMAIL_BACKEND = env("EMAIL_BACKEND", "django.core.mail.backends.smtp.EmailBackend")
EMAIL_HOST = env("EMAIL_HOST") EMAIL_HOST = env("EMAIL_HOST")
@ -101,6 +104,7 @@ INSTALLED_APPS = [
"django.contrib.messages", "django.contrib.messages",
"django.contrib.staticfiles", "django.contrib.staticfiles",
"django.contrib.humanize", "django.contrib.humanize",
"hcaptcha_field",
"oauth2_provider", "oauth2_provider",
"file_resubmit", "file_resubmit",
"sass_processor", "sass_processor",
@ -479,8 +483,8 @@ elif USE_AZURE:
) )
MEDIA_FULL_URL = MEDIA_URL MEDIA_FULL_URL = MEDIA_URL
# Content Security Policy # Content Security Policy
CSP_DEFAULT_SRC = ["'self'", AZURE_CUSTOM_DOMAIN] + CSP_ADDITIONAL_HOSTS CSP_DEFAULT_SRC = ["'self'", AZURE_CUSTOM_DOMAIN] + env.list("CSP_ADDITIONAL_HOSTS")
CSP_SCRIPT_SRC = ["'self'", AZURE_CUSTOM_DOMAIN] + CSP_ADDITIONAL_HOSTS CSP_SCRIPT_SRC = ["'self'", AZURE_CUSTOM_DOMAIN] + env.list("CSP_ADDITIONAL_HOSTS")
else: else:
# Storages # Storages
STORAGES = { STORAGES = {
@ -504,8 +508,8 @@ else:
MEDIA_URL = "/images/" MEDIA_URL = "/images/"
MEDIA_FULL_URL = BASE_URL + MEDIA_URL MEDIA_FULL_URL = BASE_URL + MEDIA_URL
# Content Security Policy # Content Security Policy
CSP_DEFAULT_SRC = ["'self'"] + CSP_ADDITIONAL_HOSTS CSP_DEFAULT_SRC = ["'self'"] + env.list("CSP_ADDITIONAL_HOSTS")
CSP_SCRIPT_SRC = ["'self'"] + CSP_ADDITIONAL_HOSTS CSP_SCRIPT_SRC = ["'self'"] + env.list("CSP_ADDITIONAL_HOSTS")
CSP_INCLUDE_NONCE_IN = ["script-src"] CSP_INCLUDE_NONCE_IN = ["script-src"]

View file

@ -60,6 +60,13 @@
<input type="hidden" name="preferred_timezone" /> <input type="hidden" name="preferred_timezone" />
<div class="field">
<div class="control">
{{ register_form.hcaptcha }}
{% include 'snippets/form_errors.html' with errors_list=register_form.hcaptcha.errors id="desc_hcaptcha_register" %}
</div>
</div>
<div class="field"> <div class="field">
<div class="control"> <div class="control">
<button class="button is-primary" type="submit"> <button class="button is-primary" type="submit">

View file

@ -8,6 +8,7 @@ Django==4.2.16
django-celery-beat==2.6.0 django-celery-beat==2.6.0
django-compressor==4.4 django-compressor==4.4
django-csp==3.8 django-csp==3.8
django-hcaptcha-field==1.4.0
django-imagekit==5.0.0 django-imagekit==5.0.0
django-model-utils==4.4.0 django-model-utils==4.4.0
django-oauth-toolkit==2.3.0 django-oauth-toolkit==2.3.0