2020-03-12 08:24:38 +00:00
|
|
|
# ~*~ coding: utf-8 ~*~
|
2020-06-19 09:43:15 +00:00
|
|
|
import time
|
2020-03-12 08:24:38 +00:00
|
|
|
|
2021-11-10 03:30:48 +00:00
|
|
|
from django.urls import reverse
|
2020-03-12 08:24:38 +00:00
|
|
|
from django.utils.translation import ugettext as _
|
|
|
|
from django.views.generic.base import TemplateView
|
|
|
|
from django.views.generic.edit import FormView
|
|
|
|
from django.contrib.auth import logout as auth_logout
|
2021-11-25 05:20:13 +00:00
|
|
|
from django.shortcuts import redirect
|
2021-11-10 03:30:48 +00:00
|
|
|
from django.http.response import HttpResponseRedirect
|
2020-03-12 08:24:38 +00:00
|
|
|
|
2020-06-19 09:43:15 +00:00
|
|
|
from authentication.mixins import AuthMixin
|
2021-11-10 03:30:48 +00:00
|
|
|
from authentication.mfa import MFAOtp, otp_failed_msg
|
2021-11-25 05:20:13 +00:00
|
|
|
from authentication.errors import SessionEmptyError
|
2021-11-10 03:30:48 +00:00
|
|
|
from common.utils import get_logger, FlashMessageUtil
|
|
|
|
from common.mixins.views import PermissionsMixin
|
2020-03-12 08:24:38 +00:00
|
|
|
from common.permissions import IsValidUser
|
|
|
|
from .password import UserVerifyPasswordView
|
2021-11-10 03:30:48 +00:00
|
|
|
from ... import forms
|
2020-03-12 08:24:38 +00:00
|
|
|
from ...utils import (
|
2021-11-10 03:30:48 +00:00
|
|
|
generate_otp_uri, check_otp_code,
|
|
|
|
get_user_or_pre_auth_user,
|
2020-03-12 08:24:38 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
__all__ = [
|
|
|
|
'UserOtpEnableStartView',
|
|
|
|
'UserOtpEnableInstallAppView',
|
2021-11-10 03:30:48 +00:00
|
|
|
'UserOtpEnableBindView',
|
|
|
|
'UserOtpDisableView',
|
2020-03-12 08:24:38 +00:00
|
|
|
]
|
|
|
|
|
|
|
|
logger = get_logger(__name__)
|
|
|
|
|
|
|
|
|
2021-11-25 05:20:13 +00:00
|
|
|
class UserOtpEnableStartView(AuthMixin, TemplateView):
|
2020-03-12 08:24:38 +00:00
|
|
|
template_name = 'users/user_otp_check_password.html'
|
|
|
|
|
2021-11-25 05:20:13 +00:00
|
|
|
def get(self, request, *args, **kwargs):
|
|
|
|
try:
|
|
|
|
self.get_user_from_session()
|
|
|
|
except SessionEmptyError:
|
2021-12-14 03:10:32 +00:00
|
|
|
url = reverse('authentication:login') + '?_=otp_enable_start'
|
|
|
|
return redirect(url)
|
2021-11-25 05:20:13 +00:00
|
|
|
return super().get(request, *args, **kwargs)
|
2020-03-12 08:24:38 +00:00
|
|
|
|
|
|
|
|
|
|
|
class UserOtpEnableInstallAppView(TemplateView):
|
|
|
|
template_name = 'users/user_otp_enable_install_app.html'
|
|
|
|
|
|
|
|
def get_context_data(self, **kwargs):
|
|
|
|
user = get_user_or_pre_auth_user(self.request)
|
|
|
|
context = {'user': user}
|
|
|
|
kwargs.update(context)
|
|
|
|
return super().get_context_data(**kwargs)
|
|
|
|
|
|
|
|
|
2020-06-19 09:43:15 +00:00
|
|
|
class UserOtpEnableBindView(AuthMixin, TemplateView, FormView):
|
2020-03-12 08:24:38 +00:00
|
|
|
template_name = 'users/user_otp_enable_bind.html'
|
|
|
|
form_class = forms.UserCheckOtpCodeForm
|
|
|
|
|
2020-06-19 09:43:15 +00:00
|
|
|
def get(self, request, *args, **kwargs):
|
2021-11-10 03:30:48 +00:00
|
|
|
pre_response = self._pre_check_can_bind()
|
|
|
|
if pre_response:
|
|
|
|
return pre_response
|
|
|
|
return super().get(request, *args, **kwargs)
|
2020-06-19 09:43:15 +00:00
|
|
|
|
|
|
|
def post(self, request, *args, **kwargs):
|
2021-11-10 03:30:48 +00:00
|
|
|
pre_response = self._pre_check_can_bind()
|
|
|
|
if pre_response:
|
|
|
|
return pre_response
|
|
|
|
return super().post(request, *args, **kwargs)
|
|
|
|
|
|
|
|
def _pre_check_can_bind(self):
|
|
|
|
try:
|
|
|
|
user = self.get_user_from_session()
|
2021-12-14 03:10:32 +00:00
|
|
|
except Exception as e:
|
|
|
|
verify_url = reverse('authentication:user-otp-enable-start') + f'?e={e}'
|
2021-11-10 03:30:48 +00:00
|
|
|
return HttpResponseRedirect(verify_url)
|
|
|
|
|
|
|
|
if user.otp_secret_key:
|
|
|
|
return self.has_already_bound_message()
|
|
|
|
return None
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def has_already_bound_message():
|
|
|
|
message_data = {
|
|
|
|
'title': _('Already bound'),
|
|
|
|
'error': _('MFA already bound, disable first, then bound'),
|
|
|
|
'interval': 10,
|
|
|
|
'redirect_url': reverse('authentication:user-otp-disable'),
|
|
|
|
}
|
|
|
|
response = FlashMessageUtil.gen_and_redirect_to(message_data)
|
|
|
|
return response
|
2020-06-19 09:43:15 +00:00
|
|
|
|
2020-03-12 08:24:38 +00:00
|
|
|
def form_valid(self, form):
|
|
|
|
otp_code = form.cleaned_data.get('otp_code')
|
|
|
|
otp_secret_key = self.request.session.get('otp_secret_key', '')
|
|
|
|
|
|
|
|
valid = check_otp_code(otp_secret_key, otp_code)
|
2021-11-10 03:30:48 +00:00
|
|
|
if not valid:
|
|
|
|
form.add_error("otp_code", otp_failed_msg)
|
2020-03-12 08:24:38 +00:00
|
|
|
return self.form_invalid(form)
|
|
|
|
|
2021-11-10 03:30:48 +00:00
|
|
|
self.save_otp(otp_secret_key)
|
|
|
|
auth_logout(self.request)
|
|
|
|
return super().form_valid(form)
|
|
|
|
|
2020-03-12 08:24:38 +00:00
|
|
|
def save_otp(self, otp_secret_key):
|
|
|
|
user = get_user_or_pre_auth_user(self.request)
|
|
|
|
user.otp_secret_key = otp_secret_key
|
2021-11-10 03:30:48 +00:00
|
|
|
user.save(update_fields=['otp_secret_key'])
|
|
|
|
|
|
|
|
def get_success_url(self):
|
|
|
|
message_data = {
|
|
|
|
'title': _('OTP enable success'),
|
|
|
|
'message': _('OTP enable success, return login page'),
|
|
|
|
'interval': 5,
|
|
|
|
'redirect_url': reverse('authentication:login'),
|
|
|
|
}
|
|
|
|
url = FlashMessageUtil.gen_message_url(message_data)
|
|
|
|
return url
|
2020-03-12 08:24:38 +00:00
|
|
|
|
|
|
|
def get_context_data(self, **kwargs):
|
|
|
|
user = get_user_or_pre_auth_user(self.request)
|
|
|
|
otp_uri, otp_secret_key = generate_otp_uri(user.username)
|
|
|
|
self.request.session['otp_secret_key'] = otp_secret_key
|
|
|
|
context = {
|
|
|
|
'otp_uri': otp_uri,
|
|
|
|
'otp_secret_key': otp_secret_key,
|
|
|
|
'user': user
|
|
|
|
}
|
|
|
|
kwargs.update(context)
|
|
|
|
return super().get_context_data(**kwargs)
|
|
|
|
|
|
|
|
|
2021-11-10 03:30:48 +00:00
|
|
|
class UserOtpDisableView(PermissionsMixin, FormView):
|
2020-05-11 08:56:52 +00:00
|
|
|
template_name = 'users/user_verify_mfa.html'
|
2020-03-12 08:24:38 +00:00
|
|
|
form_class = forms.UserCheckOtpCodeForm
|
|
|
|
permission_classes = [IsValidUser]
|
|
|
|
|
2020-05-11 08:56:52 +00:00
|
|
|
def form_valid(self, form):
|
|
|
|
user = self.request.user
|
|
|
|
otp_code = form.cleaned_data.get('otp_code')
|
2021-11-10 03:30:48 +00:00
|
|
|
otp = MFAOtp(user)
|
2020-05-11 08:56:52 +00:00
|
|
|
|
2021-11-10 03:30:48 +00:00
|
|
|
ok, error = otp.check_code(otp_code)
|
|
|
|
if not ok:
|
2020-05-11 08:56:52 +00:00
|
|
|
form.add_error('otp_code', error)
|
|
|
|
return super().form_invalid(form)
|
|
|
|
|
2021-11-10 03:30:48 +00:00
|
|
|
otp.disable()
|
|
|
|
auth_logout(self.request)
|
|
|
|
return super().form_valid(form)
|
2020-03-12 08:24:38 +00:00
|
|
|
|
|
|
|
def get_context_data(self, **kwargs):
|
2021-11-10 03:30:48 +00:00
|
|
|
context = super().get_context_data(**kwargs)
|
|
|
|
context.update({
|
|
|
|
'title': _("Disable OTP")
|
|
|
|
})
|
|
|
|
return context
|
|
|
|
|
|
|
|
def get_success_url(self):
|
|
|
|
message_data = {
|
|
|
|
'title': _('OTP disable success'),
|
|
|
|
'message': _('OTP disable success, return login page'),
|
|
|
|
'interval': 5,
|
2020-03-12 08:24:38 +00:00
|
|
|
'redirect_url': reverse('authentication:login'),
|
|
|
|
}
|
2021-11-10 03:30:48 +00:00
|
|
|
url = FlashMessageUtil.gen_message_url(message_data)
|
|
|
|
return url
|
2020-03-12 08:24:38 +00:00
|
|
|
|
|
|
|
|