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
|
from django import forms
from django.utils.translation import gettext_lazy as _
from allauth.core import context
from allauth.mfa.adapter import get_adapter
from allauth.mfa.base.internal.flows import (
check_rate_limit,
post_authentication,
)
from allauth.mfa.models import Authenticator
class BaseAuthenticateForm(forms.Form):
code = forms.CharField(
label=_("Code"),
widget=forms.TextInput(
attrs={"placeholder": _("Code"), "autocomplete": "one-time-code"},
),
)
def __init__(self, *args, **kwargs):
self.user = kwargs.pop("user")
super().__init__(*args, **kwargs)
def clean_code(self):
clear_rl = check_rate_limit(self.user)
code = self.cleaned_data["code"]
for auth in Authenticator.objects.filter(user=self.user).exclude(
# WebAuthn cannot validate manual codes.
type=Authenticator.Type.WEBAUTHN
):
if auth.wrap().validate_code(code):
self.authenticator = auth
clear_rl()
return code
raise get_adapter().validation_error("incorrect_code")
class AuthenticateForm(BaseAuthenticateForm):
def save(self):
post_authentication(context.request, self.authenticator)
class ReauthenticateForm(BaseAuthenticateForm):
def save(self):
post_authentication(context.request, self.authenticator, reauthenticated=True)
|