BOT_open_sesam/auth.py

48 lines
1.6 KiB
Python
Raw 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.

import re
import aiohttp
AUTHORIZED_USERS = {}
def normalize_phone(phone: str) -> str:
digits = re.sub(r"\D", "", phone)
if len(digits) > 10:
digits = digits[-10:]
return digits
async def authorize_user(user_id: int, phone: str) -> bool:
normalized = normalize_phone(phone)
api_url = "https://papi.dataekb.ru/check_access"
async with aiohttp.ClientSession() as session:
try:
async with session.get(api_url, params={"tel": normalized}) as response:
if response.status != 200:
print(f"Ошибка запроса к API: статус {response.status}")
return False
data = await response.json()
except Exception as e:
print(f"Исключение при запросе к API: {str(e)}")
return False
if data.get("response") != "1":
print(f"Доступ запрещён для номера: {normalized}")
return False
zone_str = data["data"].get("zone", "")
zones = [zone.strip() for zone in zone_str.split(";") if zone.strip()]
card_code = data["data"].get("card-code", "")
if not zones or not card_code:
print("Некорректный ответ API: отсутствуют зоны или код карты.")
return False
AUTHORIZED_USERS[user_id] = {"tel": normalized, "card": card_code, "zones": zones}
print(
f"{user_id} авторизован с номером: {normalized}, карта: {card_code}, зоны: {zones}"
)
return True
def is_user_auth(user_id: int) -> bool:
return user_id in AUTHORIZED_USERS