mirror of
https://github.com/aiogram/aiogram.git
synced 2025-12-12 18:19:34 +00:00
Cleaning the auth_widget utility
This commit is contained in:
parent
65edaeb2b5
commit
25a328793b
1 changed files with 11 additions and 7 deletions
|
|
@ -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 hashlib
|
||||||
import hmac
|
import hmac
|
||||||
|
|
||||||
|
import collections
|
||||||
|
|
||||||
def generate_hash(data, token):
|
|
||||||
|
def generate_hash(data: dict, token: str) -> str:
|
||||||
"""
|
"""
|
||||||
Generate secret hash
|
Generate secret hash
|
||||||
|
|
||||||
|
|
@ -14,16 +21,13 @@ def generate_hash(data, token):
|
||||||
secret = hashlib.sha256()
|
secret = hashlib.sha256()
|
||||||
secret.update(token.encode('utf-8'))
|
secret.update(token.encode('utf-8'))
|
||||||
sorted_params = collections.OrderedDict(sorted(data.items()))
|
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()
|
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
|
Validate auth token
|
||||||
https://core.telegram.org/widgets/login#checking-authorization
|
|
||||||
|
|
||||||
Source: https://gist.github.com/xen/e4bea72487d34caa28c762776cf655a3
|
|
||||||
|
|
||||||
:param data:
|
:param data:
|
||||||
:param token:
|
:param token:
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue