增加关闭两步验证的 api

This commit is contained in:
virusdefender 2015-12-11 23:00:31 +08:00
parent 2222a56d0a
commit 0f8dedb540
2 changed files with 18 additions and 4 deletions

View File

@ -86,5 +86,5 @@ class UserProfileSerializer(serializers.ModelSerializer):
"rank", "accepted_number", "submissions_number", "problems_status", "phone_number", "school", "student_id"]
class ApplyTwoFactorAuthSerializer(serializers.Serializer):
class TwoFactorAuthCodeSerializer(serializers.Serializer):
code = serializers.IntegerField()

View File

@ -27,7 +27,7 @@ from .serializers import (UserLoginSerializer, UserRegisterSerializer,
UserSerializer, EditUserSerializer,
ApplyResetPasswordSerializer, ResetPasswordSerializer,
SSOSerializer, EditUserProfileSerializer,
UserProfileSerializer, ApplyTwoFactorAuthSerializer)
UserProfileSerializer, TwoFactorAuthCodeSerializer)
from .decorators import super_admin_required
@ -405,7 +405,7 @@ class TwoFactorAuthAPIView(APIView):
"""
开启两步验证
"""
serializer = ApplyTwoFactorAuthSerializer(data=request.data)
serializer = TwoFactorAuthCodeSerializer(data=request.data)
if serializer.is_valid():
code = serializer.data["code"]
user = request.user
@ -416,4 +416,18 @@ class TwoFactorAuthAPIView(APIView):
else:
return error_response(u"验证码错误")
else:
return serializer_invalid_response(serializer)
return serializer_invalid_response(serializer)
@login_required
def put(self, request):
serializer = TwoFactorAuthCodeSerializer(data=request.data)
if serializer.is_valid():
user = request.user
code = serializer.data["code"]
if OtpAuth(user.tfa_token).valid_totp(code):
user.two_factor_auth = False
user.save()
else:
return error_response(u"验证码错误")
else:
return serializer_invalid_response(serializer)