BOT_open_sesam/auth.py

39 lines
1.0 KiB
Python
Raw Permalink Blame History

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

from config import config
import re
ALLOWED_PHONE_NUMBERS = list(config.get("users", {}).keys())
AUTHORIZED_USERS = {}
def check_user_auth(phone: str) -> bool:
return phone in ALLOWED_PHONE_NUMBERS
def normalize_phone(phone: str) -> str:
phone = phone.strip()
phone = re.sub(r"[^\d+]", "", phone)
if not phone.startswith("+"):
phone = "+" + phone
return phone
def authorize_user(user_id: int, phone: str) -> bool:
normalized_phone = normalize_phone(phone)
if normalized_phone in ALLOWED_PHONE_NUMBERS:
AUTHORIZED_USERS[user_id] = normalized_phone
# if check_user_auth(phone):
# AUTHORIZED_USERS[user_id] = phone
print(f"{user_id} авторизован с номером: {normalized_phone}")
return True
else:
print(
f"Пользователь {user_id} пытался авторизоваться с номером {normalized_phone}"
)
return False
def is_user_auth(user_id: int) -> bool:
return user_id in AUTHORIZED_USERS