This commit is contained in:
2025-06-15 19:35:11 +08:00
parent 8a043d2ffa
commit bd0a7f30f8
4 changed files with 43 additions and 7 deletions

View File

@@ -12,7 +12,7 @@ from django.db.models import Count, Q
from django.utils import timezone
import qrcode
from otpauth import OtpAuth
from otpauth import TOTP
from problem.models import Problem
from submission.models import Submission, JudgeStatus
@@ -143,7 +143,7 @@ class TwoFactorAuthAPI(APIView):
label = f"{SysOptions.website_name_shortcut}:{user.username}"
image = qrcode.make(
OtpAuth(token).to_uri(
TOTP(token).to_uri(
"totp", label, SysOptions.website_name.replace(" ", "")
)
)
@@ -157,7 +157,7 @@ class TwoFactorAuthAPI(APIView):
"""
code = request.data["code"]
user = request.user
if OtpAuth(user.tfa_token).valid_totp(code):
if TOTP(user.tfa_token).verify(code):
user.two_factor_auth = True
user.save()
return self.success("Succeeded")
@@ -171,7 +171,7 @@ class TwoFactorAuthAPI(APIView):
user = request.user
if not user.two_factor_auth:
return self.error("2FA is already turned off")
if OtpAuth(user.tfa_token).valid_totp(code):
if TOTP(user.tfa_token).verify(code):
user.two_factor_auth = False
user.save()
return self.success("Succeeded")
@@ -216,7 +216,7 @@ class UserLoginAPI(APIView):
if user.two_factor_auth and "tfa_code" not in data:
return self.error("tfa_required")
if OtpAuth(user.tfa_token).valid_totp(data["tfa_code"]):
if TOTP(user.tfa_token).verify(data["tfa_code"]):
auth.login(request, user)
return self.success("Succeeded")
else:
@@ -287,7 +287,7 @@ class UserChangeEmailAPI(APIView):
if user.two_factor_auth:
if "tfa_code" not in data:
return self.error("tfa_required")
if not OtpAuth(user.tfa_token).valid_totp(data["tfa_code"]):
if not TOTP(user.tfa_token).verify(data["tfa_code"]):
return self.error("Invalid two factor verification code")
data["new_email"] = data["new_email"].lower()
if User.objects.filter(email=data["new_email"]).exists():
@@ -313,7 +313,7 @@ class UserChangePasswordAPI(APIView):
if user.two_factor_auth:
if "tfa_code" not in data:
return self.error("tfa_required")
if not OtpAuth(user.tfa_token).valid_totp(data["tfa_code"]):
if not TOTP(user.tfa_token).verify(data["tfa_code"]):
return self.error("Invalid two factor verification code")
user.set_password(data["new_password"])
user.save()