Cleaning the auth_widget utility

This commit is contained in:
Alex Root Junior 2018-05-04 13:45:17 +03:00
parent 65edaeb2b5
commit 25a328793b

View file

@ -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: