Commit 509c7ce7 by Kálmán Viktor

dashboard: reuse copypasted form

parent 21207ef7
......@@ -1668,27 +1668,7 @@ class TwoFactorForm(ModelForm):
fields = ["two_factor_secret", ]
class DisableTwoFactorForm(ModelForm):
confirmation_code = forms.CharField(
label=_('Confirmation code'),
help_text=_("Get the code from your authenticator to disable "
"two-factor authentication."))
def __init__(self, *args, **kwargs):
super(DisableTwoFactorForm, self).__init__(*args, **kwargs)
self.fields['two_factor_secret'].initial = None
class Meta:
model = Profile
fields = ('two_factor_secret', 'confirmation_code', )
def clean_confirmation_code(self):
totp = pyotp.TOTP(self.instance.two_factor_secret)
if not totp.verify(self.cleaned_data.get('confirmation_code')):
raise ValidationError(_("Invalid confirmation code."))
class TwoFactorAuthForm(forms.Form):
class TwoFactorConfirmationForm(forms.Form):
confirmation_code = forms.CharField(
label=_('Confirmation code'),
help_text=_("Get the code from your authenticator to disable "
......@@ -1696,7 +1676,7 @@ class TwoFactorAuthForm(forms.Form):
def __init__(self, user, *args, **kwargs):
self.user = user
super(TwoFactorAuthForm, self).__init__(*args, **kwargs)
super(TwoFactorConfirmationForm, self).__init__(*args, **kwargs)
def clean_confirmation_code(self):
totp = pyotp.TOTP(self.user.profile.two_factor_secret)
......
......@@ -50,8 +50,7 @@ from vm.models import Instance, InstanceTemplate
from ..forms import (
CircleAuthenticationForm, MyProfileForm, UserCreationForm, UnsubscribeForm,
UserKeyForm, CirclePasswordChangeForm, ConnectCommandForm,
UserListSearchForm, UserEditForm, TwoFactorForm, DisableTwoFactorForm,
TwoFactorAuthForm,
UserListSearchForm, UserEditForm, TwoFactorForm, TwoFactorConfirmationForm,
)
from ..models import Profile, GroupProfile, ConnectCommand
from ..tables import (
......@@ -590,21 +589,31 @@ class EnableTwoFactorView(LoginRequiredMixin, UpdateView):
return ctx
class DisableTwoFactorView(LoginRequiredMixin, UpdateView):
model = Profile
form_class = DisableTwoFactorForm
class DisableTwoFactorView(LoginRequiredMixin, FormView):
form_class = TwoFactorConfirmationForm
template_name = "dashboard/disable-two-factor.html"
success_url = reverse_lazy("dashboard.views.profile-preferences")
def get_object(self, queryset=None):
def get_profile(self, queryset=None):
if self.request.user.is_anonymous():
raise PermissionDenied
return self.request.user.profile
def get_form_kwargs(self):
kwargs = super(DisableTwoFactorView, self).get_form_kwargs()
kwargs['user'] = self.request.user
return kwargs
def form_valid(self, form):
profile = self.get_profile()
profile.two_factor_secret = ""
profile.save()
return super(DisableTwoFactorView, self).form_valid(form)
class TwoFactorLoginView(FormView):
form_class = TwoFactorAuthForm
form_class = TwoFactorConfirmationForm
template_name = "registration/two-factor-login.html"
def dispatch(self, *args, **kwargs):
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment