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
|
|
|
|
|
|
|
from django.urls import reverse_lazy, reverse
|
|
|
|
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
|
|
|
|
from django.conf import settings
|
2020-06-22 09:04:07 +00:00
|
|
|
from django.http.response import HttpResponseForbidden
|
2020-03-12 08:24:38 +00:00
|
|
|
|
2020-06-19 09:43:15 +00:00
|
|
|
from authentication.mixins import AuthMixin
|
|
|
|
from users.models import User
|
2020-06-22 09:04:07 +00:00
|
|
|
from common.utils import get_logger, get_object_or_none
|
2020-03-12 08:24:38 +00:00
|
|
|
from common.permissions import IsValidUser
|
|
|
|
from ... import forms
|
|
|
|
from .password import UserVerifyPasswordView
|
|
|
|
from ...utils import (
|
|
|
|
generate_otp_uri, check_otp_code, get_user_or_pre_auth_user,
|
2020-06-22 09:04:07 +00:00
|
|
|
is_auth_password_time_valid, is_auth_otp_time_valid
|
2020-03-12 08:24:38 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
__all__ = [
|
|
|
|
'UserOtpEnableStartView',
|
|
|
|
'UserOtpEnableInstallAppView',
|
|
|
|
'UserOtpEnableBindView', 'UserOtpSettingsSuccessView',
|
|
|
|
'UserDisableMFAView', 'UserOtpUpdateView',
|
|
|
|
]
|
|
|
|
|
|
|
|
logger = get_logger(__name__)
|
|
|
|
|
|
|
|
|
|
|
|
class UserOtpEnableStartView(UserVerifyPasswordView):
|
|
|
|
template_name = 'users/user_otp_check_password.html'
|
|
|
|
|
|
|
|
def get_success_url(self):
|
|
|
|
if settings.OTP_IN_RADIUS:
|
2020-06-03 03:06:44 +00:00
|
|
|
success_url = reverse_lazy('authentication:user-otp-settings-success')
|
2020-03-12 08:24:38 +00:00
|
|
|
else:
|
2020-06-03 03:06:44 +00:00
|
|
|
success_url = reverse('authentication:user-otp-enable-install-app')
|
2020-03-12 08:24:38 +00:00
|
|
|
return success_url
|
|
|
|
|
|
|
|
|
|
|
|
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-03 03:06:44 +00:00
|
|
|
success_url = reverse_lazy('authentication:user-otp-settings-success')
|
2020-03-12 08:24:38 +00:00
|
|
|
|
2020-06-19 09:43:15 +00:00
|
|
|
def get(self, request, *args, **kwargs):
|
2020-06-22 09:04:07 +00:00
|
|
|
if self._check_can_bind():
|
|
|
|
return super().get(request, *args, **kwargs)
|
|
|
|
return HttpResponseForbidden()
|
2020-06-19 09:43:15 +00:00
|
|
|
|
|
|
|
def post(self, request, *args, **kwargs):
|
2020-06-22 09:04:07 +00:00
|
|
|
if self._check_can_bind():
|
|
|
|
return super().post(request, *args, **kwargs)
|
|
|
|
return HttpResponseForbidden()
|
2020-06-19 09:43:15 +00:00
|
|
|
|
2020-06-22 09:04:07 +00:00
|
|
|
def _check_authenticated_user_can_bind(self):
|
|
|
|
user = self.request.user
|
|
|
|
session = self.request.session
|
|
|
|
|
|
|
|
if not user.mfa_enabled:
|
|
|
|
return is_auth_password_time_valid(session)
|
|
|
|
|
|
|
|
if not user.otp_secret_key:
|
|
|
|
return is_auth_password_time_valid(session)
|
2020-06-19 09:43:15 +00:00
|
|
|
|
2020-06-22 09:04:07 +00:00
|
|
|
return is_auth_otp_time_valid(session)
|
|
|
|
|
|
|
|
def _check_unauthenticated_user_can_bind(self):
|
|
|
|
session_user = None
|
2020-06-19 09:43:15 +00:00
|
|
|
if not self.request.session.is_empty():
|
|
|
|
user_id = self.request.session.get('user_id')
|
|
|
|
session_user = get_object_or_none(User, pk=user_id)
|
|
|
|
|
2020-06-22 09:04:07 +00:00
|
|
|
if session_user:
|
|
|
|
if all((is_auth_password_time_valid(self.request.session), session_user.mfa_enabled, not session_user.otp_secret_key)):
|
|
|
|
return True
|
|
|
|
return False
|
|
|
|
|
|
|
|
def _check_can_bind(self):
|
|
|
|
if self.request.user.is_authenticated:
|
|
|
|
return self._check_authenticated_user_can_bind()
|
2020-06-19 09:43:15 +00:00
|
|
|
else:
|
2020-06-22 09:04:07 +00:00
|
|
|
return self._check_unauthenticated_user_can_bind()
|
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)
|
|
|
|
if valid:
|
|
|
|
self.save_otp(otp_secret_key)
|
|
|
|
return super().form_valid(form)
|
|
|
|
else:
|
|
|
|
error = _("MFA code invalid, or ntp sync server time")
|
|
|
|
form.add_error("otp_code", error)
|
|
|
|
return self.form_invalid(form)
|
|
|
|
|
|
|
|
def save_otp(self, otp_secret_key):
|
|
|
|
user = get_user_or_pre_auth_user(self.request)
|
|
|
|
user.enable_mfa()
|
|
|
|
user.otp_secret_key = otp_secret_key
|
|
|
|
user.save()
|
|
|
|
|
|
|
|
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)
|
|
|
|
|
|
|
|
|
2020-05-18 07:49:53 +00:00
|
|
|
class UserDisableMFAView(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
|
2020-06-03 03:06:44 +00:00
|
|
|
success_url = reverse_lazy('authentication:user-otp-settings-success')
|
2020-03-12 08:24:38 +00:00
|
|
|
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')
|
|
|
|
|
|
|
|
valid = user.check_mfa(otp_code)
|
|
|
|
if valid:
|
2020-05-18 07:49:53 +00:00
|
|
|
user.disable_mfa()
|
|
|
|
user.save()
|
2020-05-11 08:56:52 +00:00
|
|
|
return super().form_valid(form)
|
|
|
|
else:
|
|
|
|
error = _('MFA code invalid, or ntp sync server time')
|
|
|
|
form.add_error('otp_code', error)
|
|
|
|
return super().form_invalid(form)
|
|
|
|
|
|
|
|
|
2020-05-18 07:49:53 +00:00
|
|
|
class UserOtpUpdateView(FormView):
|
|
|
|
template_name = 'users/user_verify_mfa.html'
|
|
|
|
form_class = forms.UserCheckOtpCodeForm
|
2020-06-03 03:06:44 +00:00
|
|
|
success_url = reverse_lazy('authentication:user-otp-enable-bind')
|
2020-05-18 07:49:53 +00:00
|
|
|
permission_classes = [IsValidUser]
|
|
|
|
|
2020-03-12 08:24:38 +00:00
|
|
|
def form_valid(self, form):
|
|
|
|
user = self.request.user
|
|
|
|
otp_code = form.cleaned_data.get('otp_code')
|
|
|
|
|
|
|
|
valid = user.check_mfa(otp_code)
|
|
|
|
if valid:
|
2020-06-22 09:04:07 +00:00
|
|
|
self.request.session['auth_opt_expired_at'] = time.time() + settings.AUTH_EXPIRED_SECONDS
|
2020-03-12 08:24:38 +00:00
|
|
|
return super().form_valid(form)
|
|
|
|
else:
|
|
|
|
error = _('MFA code invalid, or ntp sync server time')
|
|
|
|
form.add_error('otp_code', error)
|
|
|
|
return super().form_invalid(form)
|
|
|
|
|
|
|
|
|
|
|
|
class UserOtpSettingsSuccessView(TemplateView):
|
|
|
|
template_name = 'flash_message_standalone.html'
|
|
|
|
|
|
|
|
def get_context_data(self, **kwargs):
|
|
|
|
title, describe = self.get_title_describe()
|
|
|
|
context = {
|
|
|
|
'title': title,
|
|
|
|
'messages': describe,
|
|
|
|
'interval': 1,
|
|
|
|
'redirect_url': reverse('authentication:login'),
|
|
|
|
'auto_redirect': True,
|
|
|
|
}
|
|
|
|
kwargs.update(context)
|
|
|
|
return super().get_context_data(**kwargs)
|
|
|
|
|
|
|
|
def get_title_describe(self):
|
|
|
|
user = get_user_or_pre_auth_user(self.request)
|
|
|
|
if self.request.user.is_authenticated:
|
|
|
|
auth_logout(self.request)
|
|
|
|
title = _('MFA enable success')
|
|
|
|
describe = _('MFA enable success, return login page')
|
|
|
|
if not user.mfa_enabled:
|
|
|
|
title = _('MFA disable success')
|
|
|
|
describe = _('MFA disable success, return login page')
|
|
|
|
return title, describe
|
|
|
|
|