/
romaaaka
/
queuebot
Обзор
Документация
Войти
/
romaaaka
/
queuebot
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
Аналитика
Безопасность
2.0.2
src/api/utils/initdata.py
36 строк
1 KB
Roman
finalize api
23 апр 2025, 20:44
23 апр 2025, 20:44
eb1ed84
Код
Авторство
О чём код?
import json import hashlib import hmac import urllib.parse from fastapi import HTTPException from starlette.status import HTTP_401_UNAUTHORIZED def parse_init_data(init_data: str) -> dict: return dict(urllib.parse.parse_qsl(init_data)) def validate_telegram_init_data(init_data: str, bot_token: str) -> dict: try: data = parse_init_data(init_data) hash_received = data.pop("hash") except Exception: raise HTTPException(status_code=HTTP_401_UNAUTHORIZED, detail="Invalid init data") # Собираем строку в правильном порядке data_check_string = "\n".join( f"{k}={v}" for k, v in sorted(data.items()) ) secret_key = hmac.new("WebAppData".encode(), bot_token.encode(), hashlib.sha256).digest() calculated_hash = hmac.new( key=secret_key, msg=data_check_string.encode(), digestmod=hashlib.sha256 ).hexdigest() if calculated_hash != hash_received: raise HTTPException(status_code=HTTP_401_UNAUTHORIZED, detail="Invalid hash") data['user'] = json.loads(data['user']) return data