-
Notifications
You must be signed in to change notification settings - Fork 0
/
auth_forms.py
64 lines (50 loc) · 2.47 KB
/
auth_forms.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
from django import forms
from django.contrib.auth.forms import AuthenticationForm
from django.contrib.admin.sites import AdminSite
from django.utils.translation import ugettext_lazy as _
from django.contrib.auth import authenticate
ERROR_MESSAGE = _("Please enter the correct username, password and "
"authentication code (if applicable). Note that all fields are "
"case-sensitive.")
class TwoFactorAuthenticationForm(AuthenticationForm):
token = forms.CharField( label=_("Authentication Code"),
widget=forms.TextInput(attrs={'maxlength': '6'}),
required=False )
def clean(self):
username = self.cleaned_data.get('username')
password = self.cleaned_data.get('password')
token = self.cleaned_data.get('token')
if username and password:
self.user_cache = authenticate( username=username,
password=password,
token=token )
if self.user_cache is None:
raise forms.ValidationError(ERROR_MESSAGE)
elif not self.user_cache.is_active:
raise forms.ValidationError(_("This account is inactive."))
self.check_for_test_cookie()
return self.cleaned_data
class TwoFactorAdminAuthenticationForm(AuthenticationForm):
token = forms.IntegerField(label=_("Authentication Code"),
widget=forms.TextInput(attrs={'maxlength': '6'}),
min_value=1, max_value=999999,
required=False
)
this_is_the_login_form = forms.BooleanField(widget=forms.HiddenInput,
initial=1, error_messages={'required': _("Please log in again, "
"because your session has expired.")})
def clean(self):
username = self.cleaned_data.get('username')
password = self.cleaned_data.get('password')
token = self.cleaned_data.get('token')
if username and password:
self.user_cache = authenticate( username=username,
password=password,
token=token )
if self.user_cache is None:
raise forms.ValidationError(ERROR_MESSAGE)
elif not self.user_cache.is_active:
raise forms.ValidationError(_("This account is inactive."))
self.check_for_test_cookie()
return self.cleaned_data
AdminSite.login_form = TwoFactorAdminAuthenticationForm