comparison accounts/forms.py @ 690:988782c6ce6c

For #48, rework blocking code to use fail2ban.
author Brian Neal <bgneal@gmail.com>
date Sun, 01 Sep 2013 00:15:42 -0500
parents 8e6b8ffe5f34
children 9133b4626a4b
comparison
equal deleted inserted replaced
689:a8dc08cc5db4 690:988782c6ce6c
11 11
12 from core.functions import send_mail 12 from core.functions import send_mail
13 from accounts.models import PendingUser 13 from accounts.models import PendingUser
14 from accounts.models import IllegalUsername 14 from accounts.models import IllegalUsername
15 from accounts.models import IllegalEmail 15 from accounts.models import IllegalEmail
16 from antispam.rate_limit import block_ip 16
17
18 logger = logging.getLogger('auth')
17 19
18 20
19 class RegisterForm(forms.Form): 21 class RegisterForm(forms.Form):
20 """Form used to register with the website""" 22 """Form used to register with the website"""
21 username = forms.RegexField( 23 username = forms.RegexField(
111 """ 113 """
112 Honeypot field should be empty. 114 Honeypot field should be empty.
113 """ 115 """
114 answer = self.cleaned_data.get('question2') 116 answer = self.cleaned_data.get('question2')
115 if answer: 117 if answer:
116 block_ip(self.ip) 118 logger.critical('Accounts/registration: Honeypot filled [%s]', self.ip)
117 self._validation_error('Wrong answer #2: %s' % answer) 119 self._validation_error('Wrong answer #2', answer)
118 return answer 120 return answer
119 121
120 def save(self): 122 def save(self):
121 pending_user = PendingUser.objects.create_pending_user(self.cleaned_data['username'], 123 pending_user = PendingUser.objects.create_pending_user(self.cleaned_data['username'],
122 self.cleaned_data['email'], 124 self.cleaned_data['email'],
141 }) 143 })
142 144
143 subject = 'Registration Confirmation for ' + site.name 145 subject = 'Registration Confirmation for ' + site.name
144 send_mail(subject, msg, admin_email, [self.cleaned_data['email']], 146 send_mail(subject, msg, admin_email, [self.cleaned_data['email']],
145 defer=False) 147 defer=False)
146 logging.info('Accounts/registration conf. email sent to %s for user %s; IP = %s', 148 logger.info('Accounts/registration conf. email sent to %s for user %s; IP = %s',
147 self.cleaned_data['email'], pending_user.username, self.ip) 149 self.cleaned_data['email'], pending_user.username, self.ip)
148 150
149 return pending_user 151 return pending_user
150 152
151 def _validation_error(self, msg, param=None): 153 def _validation_error(self, msg, param=None):
152 logging.error('Accounts/registration [%s]: %s (%s)', self.ip, msg, param) 154 logger.error('Accounts/registration [%s]: %s (%s)', self.ip, msg, param)
153 raise forms.ValidationError(msg) 155 raise forms.ValidationError(msg)
154 156
155 157
156 class ForgotUsernameForm(forms.Form): 158 class ForgotUsernameForm(forms.Form):
157 """Form used to recover lost username""" 159 """Form used to recover lost username"""
176 'site': site, 178 'site': site,
177 'admin_email': admin_email, 179 'admin_email': admin_email,
178 }) 180 })
179 send_mail(subject, msg, admin_email, [email], defer=False) 181 send_mail(subject, msg, admin_email, [email], defer=False)
180 182
181 logging.info('Forgotten username email sent to {} <{}>'.format( 183 logger.info('Forgotten username email sent to {} <{}>'.format(
182 user.username, email)) 184 user.username, email))