From 25a328793b32d4ce428d4fabf6a2214690a75d05 Mon Sep 17 00:00:00 2001 From: Alex Root Junior Date: Fri, 4 May 2018 13:45:17 +0300 Subject: [PATCH] Cleaning the `auth_widget` utility --- aiogram/utils/auth_widget.py | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/aiogram/utils/auth_widget.py b/aiogram/utils/auth_widget.py index 800dfc12..4ea3adc0 100644 --- a/aiogram/utils/auth_widget.py +++ b/aiogram/utils/auth_widget.py @@ -1,9 +1,16 @@ -import collections +""" +Implementation of Telegram site authorization checking mechanism +for more information https://core.telegram.org/widgets/login#checking-authorization + +Source: https://gist.github.com/JrooTJunior/887791de7273c9df5277d2b1ecadc839 +""" import hashlib import hmac +import collections -def generate_hash(data, token): + +def generate_hash(data: dict, token: str) -> str: """ Generate secret hash @@ -14,16 +21,13 @@ def generate_hash(data, token): secret = hashlib.sha256() secret.update(token.encode('utf-8')) sorted_params = collections.OrderedDict(sorted(data.items())) - msg = "\n".join(["{}={}".format(k, v) for k, v in sorted_params.items() if k != 'hash']) + msg = '\n'.join(["{}={}".format(k, v) for k, v in sorted_params.items() if k != 'hash']) return hmac.new(secret.digest(), msg.encode('utf-8'), digestmod=hashlib.sha256).hexdigest() -def check_token(data, token): +def check_token(data: dict, token: str) -> bool: """ Validate auth token - https://core.telegram.org/widgets/login#checking-authorization - - Source: https://gist.github.com/xen/e4bea72487d34caa28c762776cf655a3 :param data: :param token: