jumpserver/apps/authentication/views/mfa.py

58 lines
1.7 KiB
Python
Raw Normal View History

2019-11-05 10:46:29 +00:00
# -*- coding: utf-8 -*-
#
from __future__ import unicode_literals
from django.views.generic.edit import FormView
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
from .. import forms, errors, mixins
from .utils import redirect_to_guard_view
2020-04-20 02:44:45 +00:00
logger = get_logger(__name__)
__all__ = ['UserLoginMFAView']
2019-11-05 10:46:29 +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'
def get(self, *args, **kwargs):
try:
user = self.get_user_from_session()
except errors.SessionEmptyError:
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')
return super().get(*args, **kwargs)
2019-11-05 10:46:29 +00:00
def form_valid(self, form):
code = form.cleaned_data.get('code')
mfa_type = form.cleaned_data.get('mfa_type')
2019-11-05 10:46:29 +00:00
try:
self._do_check_user_mfa(code, mfa_type)
return redirect_to_guard_view('mfa_ok')
2021-04-08 04:47:49 +00:00
except (errors.MFAFailedError, errors.BlockMFAError) as e:
form.add_error('code', e.msg)
2019-11-05 10:46:29 +00:00
return super().form_invalid(form)
except errors.SessionEmptyError:
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()
return redirect_to_guard_view('unexpect')
2019-11-05 10:46:29 +00:00
def get_context_data(self, **kwargs):
user = self.get_user_from_session()
mfa_context = self.get_user_mfa_context(user)
kwargs.update(mfa_context)
return kwargs