Optimizing database queries/ Combining cash registers into a single point/ Caching key moments/ API versioning/ ruff formatting
This commit is contained in:
@@ -0,0 +1,35 @@
|
||||
import hashlib
|
||||
import hmac
|
||||
import time
|
||||
|
||||
|
||||
def verify_telegram_login(
|
||||
payload: dict,
|
||||
bot_token: str,
|
||||
*,
|
||||
max_age_seconds: int = 86400,
|
||||
) -> bool:
|
||||
"""
|
||||
Проверяет подпись и свежесть данных от Telegram Login Widget.
|
||||
"""
|
||||
if not payload or not bot_token:
|
||||
return False
|
||||
received_hash = payload.get("hash")
|
||||
if not received_hash:
|
||||
return False
|
||||
auth_date = payload.get("auth_date")
|
||||
if auth_date is None:
|
||||
return False
|
||||
try:
|
||||
if int(auth_date) < time.time() - max_age_seconds:
|
||||
return False
|
||||
except (TypeError, ValueError):
|
||||
return False
|
||||
|
||||
check_parts = sorted((k, v) for k, v in payload.items() if k != "hash" and v is not None)
|
||||
data_check_string = "\n".join(f"{k}={v}" for k, v in check_parts)
|
||||
|
||||
secret_key = hashlib.sha256(bot_token.encode()).digest()
|
||||
computed = hmac.new(secret_key, data_check_string.encode(), hashlib.sha256).hexdigest()
|
||||
|
||||
return hmac.compare_digest(computed, received_hash)
|
||||
+1
-1
@@ -92,4 +92,4 @@ def get_git_commit_number() -> str:
|
||||
|
||||
|
||||
def get_version() -> str:
|
||||
return f"v.5.1 {get_git_commit_number()}"
|
||||
return f"a20022601 {get_git_commit_number()}"
|
||||
|
||||
Reference in New Issue
Block a user