Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import hashlib
- import hmac
- from collections import OrderedDict
- from datetime import datetime, timedelta
- from typing import Union
- TELEGRAM_BOT_TOKEN = "токен бота"
- def is_data_from_telegram(
- data: dict, timedelta_min: Union[int, float] = 1
- ) -> bool:
- """
- :param data: данные полученные от телеграма
- :param timedelta_min: кол-во минут в течении которых считать данные валидными
- :return: bool
- """
- data_alphabetical_order = OrderedDict(sorted(data.dict(exclude_unset=True).items()))
- data_check_string = "\n".join([f"{key}={value}" for key, value in data_alphabetical_order.items() if key != "hash"])
- secret_key = hashlib.sha256(TELEGRAM_BOT_TOKEN.encode()).digest()
- check_hash = hmac.new(secret_key, data_check_string.encode(), digestmod=hashlib.sha256).hexdigest()
- if data.hash != check_hash:
- return False
- if (int(datetime.now().timestamp()) - data.auth_date) > timedelta(minutes=timedelta_min).total_seconds():
- return False
- return True
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement