Skip to content

Commit db8a77c

Browse files
committed
Add honeypot field to signup view
1 parent f4c9452 commit db8a77c

2 files changed

Lines changed: 26 additions & 0 deletions

File tree

bakeup/users/forms.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
from allauth.account.adapter import get_adapter
12
from allauth.account.forms import LoginForm as _LoginForm
23
from allauth.account.forms import ResetPasswordForm
34
from allauth.account.forms import SignupForm as _SignupForm
@@ -46,6 +47,17 @@ def get_user(self):
4647
class UserFormMixin:
4748
def __init__(self, request, *args, **kwargs):
4849
super().__init__(*args, **kwargs)
50+
self.fields[settings.HONEYPOT_FIELD_NAME] = forms.CharField(
51+
label=False,
52+
required=False,
53+
widget=forms.TextInput(
54+
attrs={
55+
"style": "position: absolute; right: -99999px;",
56+
"tabindex": "-1",
57+
"autocomplete": "nope",
58+
}
59+
),
60+
)
4961
for field in request.tenant.clientsetting.user_registration_fields:
5062
field_settings = settings.USER_REGISTRATION_FORM_FIELDS.get(field)
5163
self.fields[field] = forms.CharField(**field_settings)
@@ -80,6 +92,7 @@ def __init__(self, request, *args, **kwargs):
8092
self.helper.disable_csrf = False
8193
self.helper.layout = Layout(
8294
"email",
95+
settings.HONEYPOT_FIELD_NAME,
8396
"password1",
8497
"point_of_sale",
8598
"first_name",
@@ -133,6 +146,17 @@ def save(self, request):
133146
self.update_customer(user, request)
134147
return user
135148

149+
def try_save(self, request):
150+
if self.cleaned_data[settings.HONEYPOT_FIELD_NAME]:
151+
user = None
152+
adapter = get_adapter()
153+
# honeypot fields work best when you do not report to the bot
154+
# that anything went wrong. So we return a fake email verification
155+
# sent response but without creating a user
156+
resp = adapter.respond_email_verification_sent(request, None)
157+
return user, resp
158+
return super().try_save(request)
159+
136160

137161
class SignupForm(UserFormMixin, _SignupForm):
138162
email = forms.EmailField(

config/settings/base.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,8 @@
150150
# https://docs.djangoproject.com/en/dev/ref/settings/#login-url
151151
LOGIN_URL = "login"
152152

153+
HONEYPOT_FIELD_NAME = "email_confirm"
154+
153155
# PASSWORDS
154156
# ------------------------------------------------------------------------------
155157
# https://docs.djangoproject.com/en/dev/ref/settings/#password-hashers

0 commit comments

Comments
 (0)