2019-11-05 10:46:29 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
#
|
|
|
|
|
|
|
|
from __future__ import unicode_literals
|
|
|
|
from django.views.generic.edit import FormView
|
2021-11-25 05:20:13 +00:00
|
|
|
from django.shortcuts import redirect
|
2019-11-05 10:46:29 +00:00
|
|
|
|
2020-04-20 02:44:45 +00:00
|
|
|
from common.utils import get_logger
|
2021-11-10 03:30:48 +00:00
|
|
|
from .. import forms, errors, mixins
|
|
|
|
from .utils import redirect_to_guard_view
|
2020-04-20 02:44:45 +00:00
|
|
|
|
|
|
|
logger = get_logger(__name__)
|
2021-11-10 03:30:48 +00:00
|
|
|
__all__ = ['UserLoginMFAView']
|
2019-11-05 10:46:29 +00:00
|
|
|
|
|
|
|
|
2021-11-10 03:30:48 +00:00
|
|
|
class UserLoginMFAView(mixins.AuthMixin, FormView):
|
|
|
|
template_name = 'authentication/login_mfa.html'
|
2019-11-05 10:46:29 +00:00
|
|
|
form_class = forms.UserCheckOtpCodeForm
|
|
|
|
redirect_field_name = 'next'
|
|
|
|
|
2021-11-10 03:30:48 +00:00
|
|
|
def get(self, *args, **kwargs):
|
|
|
|
try:
|
2021-11-25 05:20:13 +00:00
|
|
|
user = self.get_user_from_session()
|
2021-11-10 03:30:48 +00:00
|
|
|
except errors.SessionEmptyError:
|
2021-11-25 05:20:13 +00:00
|
|
|
return redirect_to_guard_view('session_empty')
|
|
|
|
|
|
|
|
try:
|
|
|
|
self._check_if_no_active_mfa(user)
|
|
|
|
except errors.MFAUnsetError as e:
|
|
|
|
return redirect(e.url + '?_=login_mfa')
|
|
|
|
|
2021-11-10 03:30:48 +00:00
|
|
|
return super().get(*args, **kwargs)
|
|
|
|
|
2019-11-05 10:46:29 +00:00
|
|
|
def form_valid(self, form):
|
2021-10-18 10:41:41 +00:00
|
|
|
code = form.cleaned_data.get('code')
|
2021-08-24 06:20:54 +00:00
|
|
|
mfa_type = form.cleaned_data.get('mfa_type')
|
|
|
|
|
2019-11-05 10:46:29 +00:00
|
|
|
try:
|
2021-11-10 03:30:48 +00:00
|
|
|
self._do_check_user_mfa(code, mfa_type)
|
2021-11-25 05:20:13 +00:00
|
|
|
return redirect_to_guard_view('mfa_ok')
|
2021-04-08 04:47:49 +00:00
|
|
|
except (errors.MFAFailedError, errors.BlockMFAError) as e:
|
2021-08-24 06:20:54 +00:00
|
|
|
form.add_error('code', e.msg)
|
2019-11-05 10:46:29 +00:00
|
|
|
return super().form_invalid(form)
|
2021-11-10 03:30:48 +00:00
|
|
|
except errors.SessionEmptyError:
|
2021-11-25 05:20:13 +00:00
|
|
|
return redirect_to_guard_view('session_empty')
|
2020-04-20 02:44:45 +00:00
|
|
|
except Exception as e:
|
|
|
|
logger.error(e)
|
2021-04-08 04:47:49 +00:00
|
|
|
import traceback
|
2021-10-13 03:40:29 +00:00
|
|
|
traceback.print_exc()
|
2021-11-25 05:20:13 +00:00
|
|
|
return redirect_to_guard_view('unexpect')
|
2019-11-05 10:46:29 +00:00
|
|
|
|
2021-08-24 06:20:54 +00:00
|
|
|
def get_context_data(self, **kwargs):
|
|
|
|
user = self.get_user_from_session()
|
2021-11-10 03:30:48 +00:00
|
|
|
mfa_context = self.get_user_mfa_context(user)
|
|
|
|
kwargs.update(mfa_context)
|
2021-10-18 10:41:41 +00:00
|
|
|
return kwargs
|
2021-11-10 03:30:48 +00:00
|
|
|
|