2022-06-07 11:26:07 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
#
|
|
|
|
import time
|
|
|
|
|
2023-07-24 03:52:25 +00:00
|
|
|
from django.utils.translation import gettext_lazy as _
|
|
|
|
from rest_framework import status
|
2023-10-13 06:40:40 +00:00
|
|
|
from rest_framework.decorators import action
|
|
|
|
from rest_framework.generics import RetrieveAPIView
|
2022-06-07 11:26:07 +00:00
|
|
|
from rest_framework.response import Response
|
|
|
|
|
2023-10-12 08:17:32 +00:00
|
|
|
from authentication.permissions import UserConfirmation
|
2023-10-13 06:40:40 +00:00
|
|
|
from common.api import JMSGenericViewSet
|
2023-10-12 08:17:32 +00:00
|
|
|
from common.permissions import IsValidUser
|
2022-06-07 11:26:07 +00:00
|
|
|
from ..const import ConfirmType
|
|
|
|
from ..serializers import ConfirmSerializer
|
|
|
|
|
|
|
|
|
2022-07-12 08:02:41 +00:00
|
|
|
class ConfirmBindORUNBindOAuth(RetrieveAPIView):
|
2023-10-10 09:52:52 +00:00
|
|
|
permission_classes = (IsValidUser, UserConfirmation.require(ConfirmType.RELOGIN),)
|
2022-07-12 08:02:41 +00:00
|
|
|
|
|
|
|
def retrieve(self, request, *args, **kwargs):
|
|
|
|
return Response('ok')
|
|
|
|
|
|
|
|
|
2023-10-13 06:40:40 +00:00
|
|
|
class UserConfirmationViewSet(JMSGenericViewSet):
|
2022-06-07 11:26:07 +00:00
|
|
|
permission_classes = (IsValidUser,)
|
|
|
|
serializer_class = ConfirmSerializer
|
|
|
|
|
2023-10-13 06:40:40 +00:00
|
|
|
@action(methods=['get'], detail=False)
|
|
|
|
def check(self, request):
|
|
|
|
confirm_type = request.query_params.get('confirm_type', 'password')
|
|
|
|
permission = UserConfirmation.require(confirm_type)()
|
|
|
|
permission.has_permission(request, self)
|
|
|
|
return Response('ok')
|
|
|
|
|
2022-07-04 03:29:39 +00:00
|
|
|
def get_confirm_backend(self, confirm_type):
|
2023-10-10 09:52:52 +00:00
|
|
|
backend_classes = ConfirmType.get_prop_backends(confirm_type)
|
2022-07-04 03:29:39 +00:00
|
|
|
if not backend_classes:
|
|
|
|
return
|
|
|
|
for backend_cls in backend_classes:
|
|
|
|
backend = backend_cls(self.request.user, self.request)
|
|
|
|
if not backend.check():
|
|
|
|
continue
|
|
|
|
return backend
|
|
|
|
|
2023-10-13 06:40:40 +00:00
|
|
|
def list(self, request, *args, **kwargs):
|
2023-10-10 09:52:52 +00:00
|
|
|
confirm_type = request.query_params.get('confirm_type', 'password')
|
2022-07-04 03:29:39 +00:00
|
|
|
backend = self.get_confirm_backend(confirm_type)
|
|
|
|
if backend is None:
|
|
|
|
msg = _('This action require verify your MFA')
|
2023-10-13 06:40:40 +00:00
|
|
|
return Response(data={'error': msg}, status=status.HTTP_400_BAD_REQUEST)
|
2022-07-04 03:29:39 +00:00
|
|
|
|
|
|
|
data = {
|
|
|
|
'confirm_type': backend.name,
|
|
|
|
'content': backend.content,
|
|
|
|
}
|
|
|
|
return Response(data=data)
|
2022-06-07 11:26:07 +00:00
|
|
|
|
|
|
|
def create(self, request, *args, **kwargs):
|
|
|
|
serializer = self.get_serializer(data=request.data)
|
|
|
|
serializer.is_valid(raise_exception=True)
|
|
|
|
validated_data = serializer.validated_data
|
2022-07-04 03:29:39 +00:00
|
|
|
|
2023-10-10 09:52:52 +00:00
|
|
|
confirm_type = validated_data.get('confirm_type', 'password')
|
2022-07-04 03:29:39 +00:00
|
|
|
mfa_type = validated_data.get('mfa_type')
|
2022-06-07 11:26:07 +00:00
|
|
|
secret_key = validated_data.get('secret_key')
|
2022-07-04 03:29:39 +00:00
|
|
|
|
|
|
|
backend = self.get_confirm_backend(confirm_type)
|
|
|
|
ok, msg = backend.authenticate(secret_key, mfa_type)
|
2022-06-07 11:26:07 +00:00
|
|
|
if ok:
|
2022-07-04 03:29:39 +00:00
|
|
|
request.session['CONFIRM_LEVEL'] = ConfirmType.values.index(confirm_type) + 1
|
|
|
|
request.session['CONFIRM_TIME'] = int(time.time())
|
2022-06-07 11:26:07 +00:00
|
|
|
return Response('ok')
|
|
|
|
return Response({'error': msg}, status=400)
|