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
|
2019-02-28 09:58:53 +00:00
|
|
|
from django.utils.translation import gettext_lazy as _
|
2020-12-10 12:48:10 +00:00
|
|
|
from captcha.fields import CaptchaField, CaptchaTextInput
|
2019-02-28 09:58:53 +00:00
|
|
|
|
|
|
|
|
2019-11-05 10:46:29 +00:00
|
|
|
class UserLoginForm(forms.Form):
|
2019-02-28 09:58:53 +00:00
|
|
|
username = forms.CharField(label=_('Username'), max_length=100)
|
|
|
|
password = forms.CharField(
|
|
|
|
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
|
|
|
)
|
|
|
|
|
|
|
|
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):
|
|
|
|
otp_code = forms.CharField(label=_('MFA code'), max_length=6)
|
|
|
|
|
|
|
|
|
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):
|
2020-12-10 12:48:10 +00:00
|
|
|
captcha = CaptchaField(widget=CustomCaptchaTextInput)
|
2019-02-28 09:58:53 +00:00
|
|
|
|
|
|
|
|
2020-07-24 07:47:01 +00:00
|
|
|
class ChallengeMixin(forms.Form):
|
|
|
|
challenge = forms.CharField(label=_('MFA code'), max_length=6,
|
|
|
|
required=False)
|
|
|
|
|
|
|
|
|
|
|
|
def get_user_login_form_cls(*, captcha=False):
|
|
|
|
bases = []
|
|
|
|
if settings.SECURITY_LOGIN_CAPTCHA_ENABLED and captcha:
|
|
|
|
bases.append(CaptchaMixin)
|
|
|
|
if settings.SECURITY_LOGIN_CHALLENGE_ENABLED:
|
|
|
|
bases.append(ChallengeMixin)
|
|
|
|
bases.append(UserLoginForm)
|
|
|
|
return type('UserLoginForm', tuple(bases), {})
|