mirror of
https://github.com/aiogram/aiogram.git
synced 2025-12-12 10:11:52 +00:00
Add tests for token validator and widget util.
This commit is contained in:
parent
f3580def03
commit
65edaeb2b5
2 changed files with 58 additions and 9 deletions
|
|
@ -3,6 +3,21 @@ import hashlib
|
|||
import hmac
|
||||
|
||||
|
||||
def generate_hash(data, token):
|
||||
"""
|
||||
Generate secret hash
|
||||
|
||||
:param data:
|
||||
:param token:
|
||||
:return:
|
||||
"""
|
||||
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'])
|
||||
return hmac.new(secret.digest(), msg.encode('utf-8'), digestmod=hashlib.sha256).hexdigest()
|
||||
|
||||
|
||||
def check_token(data, token):
|
||||
"""
|
||||
Validate auth token
|
||||
|
|
@ -14,12 +29,5 @@ def check_token(data, token):
|
|||
:param token:
|
||||
:return:
|
||||
"""
|
||||
secret = hashlib.sha256()
|
||||
secret.update(token.encode('utf-8'))
|
||||
sorted_params = collections.OrderedDict(sorted(data.items()))
|
||||
param_hash = sorted_params.pop('hash', '') or ''
|
||||
msg = "\n".join(["{}={}".format(k, v) for k, v in sorted_params.items()])
|
||||
|
||||
if param_hash == hmac.new(secret.digest(), msg.encode('utf-8'), digestmod=hashlib.sha256).hexdigest():
|
||||
return True
|
||||
return False
|
||||
param_hash = data.get('hash', '') or ''
|
||||
return param_hash == generate_hash(data, token)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue