BugFix in verification subsystem
This commit is contained in:
@@ -1,5 +1,4 @@
|
||||
from django.apps import AppConfig
|
||||
from .models import PhoneVerificationService
|
||||
|
||||
|
||||
class AccountConfig(AppConfig):
|
||||
@@ -7,4 +6,6 @@ class AccountConfig(AppConfig):
|
||||
name = 'account'
|
||||
|
||||
def ready(self):
|
||||
from .models import PhoneVerificationService
|
||||
PhoneVerificationService.create()
|
||||
return True
|
||||
|
@@ -37,14 +37,13 @@ class PhoneVerificationService:
|
||||
with PhoneVerificationService.__lock:
|
||||
if PhoneVerificationService.__event.is_set():
|
||||
PhoneVerificationService.__event.clear()
|
||||
print("Event reached!")
|
||||
phones = PhoneVerificationService.__to_verify.copy()
|
||||
PhoneVerificationService.__to_verify.clear()
|
||||
|
||||
if phones is not None:
|
||||
for phone in phones:
|
||||
# тут должна быть проверка, есть ли телефон в списке кодов
|
||||
print(f"Verify {phone}")
|
||||
|
||||
obj = {
|
||||
"code": None,
|
||||
"attempts": PHONE_VERIFICATION_ATTEMPTS,
|
||||
@@ -68,6 +67,7 @@ class PhoneVerificationService:
|
||||
if res_json["status"] == "OK":
|
||||
with PhoneVerificationService.__lock:
|
||||
PhoneVerificationService.__codes[phone]["code"] = res_json["code"]
|
||||
print(f"Verify code for {phone}: {res_json['code']}")
|
||||
else:
|
||||
with PhoneVerificationService.__lock:
|
||||
PhoneVerificationService.__codes[phone]["code"] = "FAILED"
|
||||
@@ -79,41 +79,50 @@ class PhoneVerificationService:
|
||||
else:
|
||||
with PhoneVerificationService.__lock:
|
||||
obj["code"] = random.randint(1000, 9999)
|
||||
PhoneVerificationService.__codes[phone] = obj
|
||||
print(f"Verify code for {phone}: {obj['code']}")
|
||||
except:
|
||||
traceback.print_exc()
|
||||
|
||||
@staticmethod
|
||||
def create():
|
||||
if PhoneVerificationService.__instance is None:
|
||||
PhoneVerificationService.__instance = Thread(target=PhoneVerificationService.__service_run)
|
||||
PhoneVerificationService.__instance = Thread(target=PhoneVerificationService.__service_run, daemon=True)
|
||||
PhoneVerificationService.__instance.start()
|
||||
|
||||
CHECK_PHONE_NOT_FOUND = "not-found"
|
||||
CHECK_PHONE_NOT_READY = "not-ready"
|
||||
CHECK_PHONE_FAILED = "failed"
|
||||
CHECK_PHONE_INVALID_CODE = "invalid"
|
||||
CHECK_PHONE_MAX_ATTEMPTS = "max-attempts"
|
||||
|
||||
@staticmethod
|
||||
def check_code(phone: str, code: int):
|
||||
with PhoneVerificationService.__lock:
|
||||
if phone not in PhoneVerificationService.__codes:
|
||||
return False
|
||||
return False, PhoneVerificationService.CHECK_PHONE_NOT_FOUND
|
||||
c = PhoneVerificationService.__codes[phone]
|
||||
print(f"verify struct: {c}")
|
||||
|
||||
if c["code"] is None:
|
||||
print(f"[PhoneVerificationService] for phone {phone} code not received yet")
|
||||
return False
|
||||
return False, PhoneVerificationService.CHECK_PHONE_NOT_READY
|
||||
|
||||
if c["code"] == "FAILED":
|
||||
PhoneVerificationService.__codes.pop(phone)
|
||||
return False
|
||||
return False, PhoneVerificationService.CHECK_PHONE_FAILED
|
||||
|
||||
if c["attempts"] > 0:
|
||||
if c["code"] == code:
|
||||
PhoneVerificationService.__codes.pop(phone)
|
||||
return True
|
||||
else:
|
||||
c["attepts"] -= 1
|
||||
return False
|
||||
else:
|
||||
raise Exception("Number of attempts exceeded")
|
||||
return True, None
|
||||
|
||||
return False
|
||||
else:
|
||||
print(f"invalid code! attempts: {c['attempts']}")
|
||||
PhoneVerificationService.__codes[phone]["attempts"] -= 1
|
||||
return False, PhoneVerificationService.CHECK_PHONE_INVALID_CODE
|
||||
else:
|
||||
return False, PhoneVerificationService.CHECK_PHONE_MAX_ATTEMPTS
|
||||
|
||||
@staticmethod
|
||||
def send_verify(phone: str):
|
||||
@@ -129,6 +138,7 @@ class SiteAccountManager(BaseUserManager):
|
||||
user.set_password(password)
|
||||
user.is_staff = False
|
||||
user.is_superuser = False
|
||||
user.is_phone_verified = False
|
||||
user.save(using=self._db)
|
||||
return user
|
||||
|
||||
@@ -161,12 +171,12 @@ class SiteUser(AbstractBaseUser, PermissionsMixin):
|
||||
name = models.CharField(max_length=60, verbose_name="Имя")
|
||||
email = models.EmailField(unique=True, verbose_name="Email")
|
||||
phone = models.CharField(unique=True, max_length=16, verbose_name="Телефон", validators=[
|
||||
RegexValidator(regex="^\\+7[0-9]*$"), # +79208109798
|
||||
RegexValidator(regex="^\\+7[0-9]*$"),
|
||||
MaxLengthValidator(limit_value=12),
|
||||
MinLengthValidator(limit_value=12)
|
||||
])
|
||||
is_staff = models.BooleanField(default=False, verbose_name="Разрешение на вход в админку")
|
||||
is_valid_phone = models.BooleanField(default=False, verbose_name="Телефон верифицирован")
|
||||
is_phone_verified = models.BooleanField(default=False, verbose_name="Телефон верифицирован")
|
||||
REQUIRED_FIELDS = ['name', 'surname', 'phone']
|
||||
USERNAME_FIELD = 'email'
|
||||
|
||||
|
Reference in New Issue
Block a user