2019-02-28 09:58:53 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
#
|
|
|
|
from django import forms
|
2020-07-24 07:47:01 +00:00
|
|
|
from django.conf import settings
|
2021-06-18 11:21:36 +00:00
|
|
|
from django.utils.translation import ugettext_lazy as _
|
2020-12-10 12:48:10 +00:00
|
|
|
from captcha.fields import CaptchaField, CaptchaTextInput
|
2019-02-28 09:58:53 +00:00
|
|
|
|
2022-05-10 09:28:10 +00:00
|
|
|
from common.utils import get_logger, decrypt_password
|
2022-05-07 08:20:12 +00:00
|
|
|
|
|
|
|
logger = get_logger(__name__)
|
|
|
|
|
|
|
|
|
|
|
|
class EncryptedField(forms.CharField):
|
|
|
|
def to_python(self, value):
|
|
|
|
value = super().to_python(value)
|
2022-05-10 09:28:10 +00:00
|
|
|
return decrypt_password(value)
|
2022-05-07 08:20:12 +00:00
|
|
|
|
2019-02-28 09:58:53 +00:00
|
|
|
|
2019-11-05 10:46:29 +00:00
|
|
|
class UserLoginForm(forms.Form):
|
2021-03-09 04:18:04 +00:00
|
|
|
days_auto_login = int(settings.SESSION_COOKIE_AGE / 3600 / 24)
|
2022-05-07 08:20:12 +00:00
|
|
|
disable_days_auto_login = settings.SESSION_EXPIRE_AT_BROWSER_CLOSE_FORCE \
|
|
|
|
or days_auto_login < 1
|
2021-03-09 04:18:04 +00:00
|
|
|
|
|
|
|
username = forms.CharField(
|
|
|
|
label=_('Username'), max_length=100,
|
|
|
|
widget=forms.TextInput(attrs={
|
|
|
|
'placeholder': _("Username"),
|
|
|
|
'autofocus': 'autofocus'
|
|
|
|
})
|
|
|
|
)
|
2022-05-07 08:20:12 +00:00
|
|
|
password = EncryptedField(
|
2019-02-28 09:58:53 +00:00
|
|
|
label=_('Password'), widget=forms.PasswordInput,
|
2020-06-30 09:12:38 +00:00
|
|
|
max_length=1024, strip=False
|
2019-02-28 09:58:53 +00:00
|
|
|
)
|
2021-03-09 04:18:04 +00:00
|
|
|
auto_login = forms.BooleanField(
|
2021-06-18 11:21:36 +00:00
|
|
|
required=False, initial=False,
|
|
|
|
widget=forms.CheckboxInput(
|
2021-03-09 04:18:04 +00:00
|
|
|
attrs={'disabled': disable_days_auto_login}
|
|
|
|
)
|
|
|
|
)
|
2019-02-28 09:58:53 +00:00
|
|
|
|
2021-06-18 11:21:36 +00:00
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
super().__init__(*args, **kwargs)
|
|
|
|
auto_login_field = self.fields['auto_login']
|
|
|
|
auto_login_field.label = _("{} days auto login").format(self.days_auto_login or 1)
|
|
|
|
|
2019-02-28 09:58:53 +00:00
|
|
|
def confirm_login_allowed(self, user):
|
|
|
|
if not user.is_staff:
|
|
|
|
raise forms.ValidationError(
|
|
|
|
self.error_messages['inactive'],
|
2019-11-05 10:46:29 +00:00
|
|
|
code='inactive',
|
2019-07-30 11:10:06 +00:00
|
|
|
)
|
|
|
|
|
2019-02-28 09:58:53 +00:00
|
|
|
|
2020-07-24 07:47:01 +00:00
|
|
|
class UserCheckOtpCodeForm(forms.Form):
|
2021-11-24 04:47:28 +00:00
|
|
|
code = forms.CharField(label=_('MFA Code'), max_length=128, required=False)
|
|
|
|
mfa_type = forms.CharField(label=_('MFA type'), max_length=128)
|
2020-07-24 07:47:01 +00:00
|
|
|
|
|
|
|
|
2020-12-10 12:48:10 +00:00
|
|
|
class CustomCaptchaTextInput(CaptchaTextInput):
|
|
|
|
template_name = 'authentication/_captcha_field.html'
|
|
|
|
|
|
|
|
|
2020-07-24 07:47:01 +00:00
|
|
|
class CaptchaMixin(forms.Form):
|
2022-11-14 09:16:30 +00:00
|
|
|
captcha = CaptchaField(widget=CustomCaptchaTextInput, label=_('Captcha'))
|
2019-02-28 09:58:53 +00:00
|
|
|
|
|
|
|
|
2020-07-24 07:47:01 +00:00
|
|
|
class ChallengeMixin(forms.Form):
|
2021-03-09 04:18:04 +00:00
|
|
|
challenge = forms.CharField(
|
2021-11-24 04:47:28 +00:00
|
|
|
label=_('MFA code'), max_length=128, required=False,
|
2021-03-09 04:18:04 +00:00
|
|
|
widget=forms.TextInput(attrs={
|
2021-10-18 10:41:41 +00:00
|
|
|
'placeholder': _("Dynamic code"),
|
2021-03-09 04:18:04 +00:00
|
|
|
'style': 'width: 50%'
|
|
|
|
})
|
|
|
|
)
|
2020-07-24 07:47:01 +00:00
|
|
|
|
|
|
|
|
|
|
|
def get_user_login_form_cls(*, captcha=False):
|
|
|
|
bases = []
|
|
|
|
if settings.SECURITY_LOGIN_CHALLENGE_ENABLED:
|
|
|
|
bases.append(ChallengeMixin)
|
2021-10-18 10:41:41 +00:00
|
|
|
elif settings.SECURITY_MFA_IN_LOGIN_PAGE:
|
|
|
|
bases.append(UserCheckOtpCodeForm)
|
2021-09-16 11:06:16 +00:00
|
|
|
elif settings.SECURITY_LOGIN_CAPTCHA_ENABLED and captcha:
|
|
|
|
bases.append(CaptchaMixin)
|
2020-07-24 07:47:01 +00:00
|
|
|
bases.append(UserLoginForm)
|
|
|
|
return type('UserLoginForm', tuple(bases), {})
|