Commit 509c7ce7 by Kálmán Viktor

dashboard: reuse copypasted form

parent 21207ef7
...@@ -1668,27 +1668,7 @@ class TwoFactorForm(ModelForm): ...@@ -1668,27 +1668,7 @@ class TwoFactorForm(ModelForm):
fields = ["two_factor_secret", ] fields = ["two_factor_secret", ]
class DisableTwoFactorForm(ModelForm): class TwoFactorConfirmationForm(forms.Form):
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):
confirmation_code = forms.CharField( confirmation_code = forms.CharField(
label=_('Confirmation code'), label=_('Confirmation code'),
help_text=_("Get the code from your authenticator to disable " help_text=_("Get the code from your authenticator to disable "
...@@ -1696,7 +1676,7 @@ class TwoFactorAuthForm(forms.Form): ...@@ -1696,7 +1676,7 @@ class TwoFactorAuthForm(forms.Form):
def __init__(self, user, *args, **kwargs): def __init__(self, user, *args, **kwargs):
self.user = user self.user = user
super(TwoFactorAuthForm, self).__init__(*args, **kwargs) super(TwoFactorConfirmationForm, self).__init__(*args, **kwargs)
def clean_confirmation_code(self): def clean_confirmation_code(self):
totp = pyotp.TOTP(self.user.profile.two_factor_secret) totp = pyotp.TOTP(self.user.profile.two_factor_secret)
......
...@@ -50,8 +50,7 @@ from vm.models import Instance, InstanceTemplate ...@@ -50,8 +50,7 @@ from vm.models import Instance, InstanceTemplate
from ..forms import ( from ..forms import (
CircleAuthenticationForm, MyProfileForm, UserCreationForm, UnsubscribeForm, CircleAuthenticationForm, MyProfileForm, UserCreationForm, UnsubscribeForm,
UserKeyForm, CirclePasswordChangeForm, ConnectCommandForm, UserKeyForm, CirclePasswordChangeForm, ConnectCommandForm,
UserListSearchForm, UserEditForm, TwoFactorForm, DisableTwoFactorForm, UserListSearchForm, UserEditForm, TwoFactorForm, TwoFactorConfirmationForm,
TwoFactorAuthForm,
) )
from ..models import Profile, GroupProfile, ConnectCommand from ..models import Profile, GroupProfile, ConnectCommand
from ..tables import ( from ..tables import (
...@@ -590,21 +589,31 @@ class EnableTwoFactorView(LoginRequiredMixin, UpdateView): ...@@ -590,21 +589,31 @@ class EnableTwoFactorView(LoginRequiredMixin, UpdateView):
return ctx return ctx
class DisableTwoFactorView(LoginRequiredMixin, UpdateView): class DisableTwoFactorView(LoginRequiredMixin, FormView):
model = Profile form_class = TwoFactorConfirmationForm
form_class = DisableTwoFactorForm
template_name = "dashboard/disable-two-factor.html" template_name = "dashboard/disable-two-factor.html"
success_url = reverse_lazy("dashboard.views.profile-preferences") 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(): if self.request.user.is_anonymous():
raise PermissionDenied raise PermissionDenied
return self.request.user.profile 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): class TwoFactorLoginView(FormView):
form_class = TwoFactorAuthForm form_class = TwoFactorConfirmationForm
template_name = "registration/two-factor-login.html" template_name = "registration/two-factor-login.html"
def dispatch(self, *args, **kwargs): 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