2019-08-21 12:27:21 +00:00
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
#
|
2021-10-18 10:41:41 +00:00
|
|
|
|
|
|
|
|
|
from django.shortcuts import get_object_or_404
|
2023-07-24 03:52:25 +00:00
|
|
|
|
from django.utils.translation import gettext as _
|
2023-09-13 09:05:01 +00:00
|
|
|
|
from rest_framework import exceptions
|
2019-08-21 12:27:21 +00:00
|
|
|
|
from rest_framework.generics import CreateAPIView
|
2023-07-24 03:52:25 +00:00
|
|
|
|
from rest_framework.permissions import AllowAny
|
2019-11-05 10:46:29 +00:00
|
|
|
|
from rest_framework.response import Response
|
2023-07-24 03:52:25 +00:00
|
|
|
|
from rest_framework.serializers import ValidationError
|
2019-08-21 12:27:21 +00:00
|
|
|
|
|
2022-01-13 11:01:12 +00:00
|
|
|
|
from common.exceptions import UnexpectError
|
2023-07-24 03:52:25 +00:00
|
|
|
|
from common.utils import get_logger
|
2021-11-10 03:30:48 +00:00
|
|
|
|
from users.models.user import User
|
2019-11-05 10:46:29 +00:00
|
|
|
|
from .. import errors
|
2023-07-24 03:52:25 +00:00
|
|
|
|
from .. import serializers
|
2023-09-13 09:05:01 +00:00
|
|
|
|
from ..errors import SessionEmptyError
|
2019-11-05 10:46:29 +00:00
|
|
|
|
from ..mixins import AuthMixin
|
2019-08-21 12:27:21 +00:00
|
|
|
|
|
2021-11-10 03:30:48 +00:00
|
|
|
|
logger = get_logger(__name__)
|
2021-08-24 06:20:54 +00:00
|
|
|
|
|
2021-11-10 03:30:48 +00:00
|
|
|
|
__all__ = [
|
2022-07-04 03:29:39 +00:00
|
|
|
|
'MFAChallengeVerifyApi', 'MFASendCodeApi'
|
2021-11-10 03:30:48 +00:00
|
|
|
|
]
|
2021-08-24 06:20:54 +00:00
|
|
|
|
|
2021-11-10 03:30:48 +00:00
|
|
|
|
|
|
|
|
|
# MFASelectAPi 原来的名字
|
|
|
|
|
class MFASendCodeApi(AuthMixin, CreateAPIView):
|
|
|
|
|
"""
|
|
|
|
|
选择 MFA 后对应操作 api,koko 目前在用
|
|
|
|
|
"""
|
2021-08-24 06:20:54 +00:00
|
|
|
|
permission_classes = (AllowAny,)
|
|
|
|
|
serializer_class = serializers.MFASelectTypeSerializer
|
2022-01-13 11:01:12 +00:00
|
|
|
|
username = ''
|
|
|
|
|
ip = ''
|
|
|
|
|
|
2022-01-17 11:05:01 +00:00
|
|
|
|
def get_user_from_db(self, username):
|
|
|
|
|
"""避免暴力测试用户名"""
|
|
|
|
|
ip = self.get_request_ip()
|
|
|
|
|
self.check_mfa_is_block(username, ip)
|
|
|
|
|
try:
|
|
|
|
|
user = get_object_or_404(User, username=username)
|
|
|
|
|
return user
|
|
|
|
|
except Exception as e:
|
|
|
|
|
self.incr_mfa_failed_time(username, ip)
|
|
|
|
|
raise e
|
|
|
|
|
|
2021-08-24 06:20:54 +00:00
|
|
|
|
def perform_create(self, serializer):
|
2021-11-10 03:30:48 +00:00
|
|
|
|
username = serializer.validated_data.get('username', '')
|
2021-08-24 06:20:54 +00:00
|
|
|
|
mfa_type = serializer.validated_data['type']
|
2022-01-13 11:01:12 +00:00
|
|
|
|
|
2021-11-10 03:30:48 +00:00
|
|
|
|
if not username:
|
2021-08-24 06:20:54 +00:00
|
|
|
|
user = self.get_user_from_session()
|
2021-11-10 03:30:48 +00:00
|
|
|
|
else:
|
2022-01-13 11:01:12 +00:00
|
|
|
|
user = self.get_user_from_db(username)
|
2021-11-10 03:30:48 +00:00
|
|
|
|
|
2021-11-15 10:36:22 +00:00
|
|
|
|
mfa_backend = user.get_active_mfa_backend_by_type(mfa_type)
|
2021-11-10 03:30:48 +00:00
|
|
|
|
if not mfa_backend or not mfa_backend.challenge_required:
|
2022-01-13 11:01:12 +00:00
|
|
|
|
error = _('Current user not support mfa type: {}').format(mfa_type)
|
|
|
|
|
raise ValidationError({'error': error})
|
2023-09-13 09:05:01 +00:00
|
|
|
|
|
2021-11-10 03:30:48 +00:00
|
|
|
|
try:
|
2022-01-13 11:01:12 +00:00
|
|
|
|
mfa_backend.send_challenge()
|
2021-11-10 03:30:48 +00:00
|
|
|
|
except Exception as e:
|
2022-01-13 11:01:12 +00:00
|
|
|
|
raise UnexpectError(str(e))
|
2019-11-05 10:46:29 +00:00
|
|
|
|
|
|
|
|
|
|
2021-11-10 03:30:48 +00:00
|
|
|
|
class MFAChallengeVerifyApi(AuthMixin, CreateAPIView):
|
2019-08-21 12:27:21 +00:00
|
|
|
|
permission_classes = (AllowAny,)
|
|
|
|
|
serializer_class = serializers.MFAChallengeSerializer
|
2019-11-05 10:46:29 +00:00
|
|
|
|
|
2023-09-13 09:05:01 +00:00
|
|
|
|
def initial(self, request, *args, **kwargs):
|
|
|
|
|
super().initial(request, *args, **kwargs)
|
|
|
|
|
try:
|
|
|
|
|
user = self.get_user_from_session()
|
|
|
|
|
except SessionEmptyError:
|
|
|
|
|
user = None
|
|
|
|
|
if not user:
|
|
|
|
|
raise exceptions.NotAuthenticated()
|
|
|
|
|
|
2019-11-05 10:46:29 +00:00
|
|
|
|
def perform_create(self, serializer):
|
2021-11-10 03:30:48 +00:00
|
|
|
|
user = self.get_user_from_session()
|
|
|
|
|
code = serializer.validated_data.get('code')
|
|
|
|
|
mfa_type = serializer.validated_data.get('type', '')
|
|
|
|
|
self._do_check_user_mfa(code, mfa_type, user)
|
|
|
|
|
|
|
|
|
|
def create(self, request, *args, **kwargs):
|
2019-11-05 10:46:29 +00:00
|
|
|
|
try:
|
2021-11-10 03:30:48 +00:00
|
|
|
|
super().create(request, *args, **kwargs)
|
|
|
|
|
return Response({'msg': 'ok'})
|
2019-11-05 10:46:29 +00:00
|
|
|
|
except errors.AuthFailedError as e:
|
2019-11-06 04:06:30 +00:00
|
|
|
|
data = {"error": e.error, "msg": e.msg}
|
2019-11-05 10:46:29 +00:00
|
|
|
|
raise ValidationError(data)
|
2019-11-08 08:32:00 +00:00
|
|
|
|
except errors.NeedMoreInfoError as e:
|
|
|
|
|
return Response(e.as_data(), status=200)
|