2019-08-21 12:27:21 +00:00
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
#
|
2019-11-05 10:46:29 +00:00
|
|
|
|
import time
|
2021-10-18 10:41:41 +00:00
|
|
|
|
|
2020-06-15 12:31:50 +00:00
|
|
|
|
from django.utils.translation import ugettext as _
|
2021-07-08 06:23:18 +00:00
|
|
|
|
from django.conf import settings
|
2021-10-18 10:41:41 +00:00
|
|
|
|
from django.shortcuts import get_object_or_404
|
2019-08-21 12:27:21 +00:00
|
|
|
|
from rest_framework.permissions import AllowAny
|
|
|
|
|
from rest_framework.generics import CreateAPIView
|
2019-11-05 10:46:29 +00:00
|
|
|
|
from rest_framework.serializers import ValidationError
|
|
|
|
|
from rest_framework.response import Response
|
2019-08-21 12:27:21 +00:00
|
|
|
|
|
2021-11-10 03:30:48 +00:00
|
|
|
|
from common.utils import get_logger
|
2022-01-13 11:01:12 +00:00
|
|
|
|
from common.exceptions import UnexpectError
|
2021-11-10 03:30:48 +00:00
|
|
|
|
from users.models.user import User
|
2019-08-21 12:27:21 +00:00
|
|
|
|
from .. import serializers
|
2019-11-05 10:46:29 +00:00
|
|
|
|
from .. import errors
|
|
|
|
|
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})
|
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
|
|
|
|
|
|
|
|
|
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)
|