o
    HEDiN                     @   s   d Z ddlZddlZddlZddlmZ eddddeded	efd
dZeddddeded	e	fddZ
deded	e	fddZdeded	e	fddZdS )z
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
    N)
deprecatedzN`generate_hash` is outdated, please use `check_signature` or `check_integrity`   )
stackleveldatatokenreturnc                 C   sb   t  }||d tt|  }ddd | D }t	j
| |dt jd S )zO
    Generate secret hash

    :param data:
    :param token:
    :return:
    utf-8
c                 s   s(    | ]\}}|d krd ||V  qdS )hashz{}={}N)format).0kv r   h/var/www/www-root/data/www/ovozai.pdev.uz/venv/lib/python3.10/site-packages/aiogram/utils/auth_widget.py	<genexpr>   s   & z generate_hash.<locals>.<genexpr>	digestmod)hashlibsha256updateencodecollectionsOrderedDictsorteditemsjoinhmacnewdigest	hexdigest)r   r   secretsorted_paramsmsgr   r   r   generate_hash   s
   	 r$   z5`check_token` helper was renamed to `check_integrity`c                 C   s   |  ddpd}|t| |kS )zN
    Validate auth token

    :param data:
    :param token:
    :return:
    r
    )getr$   )r   r   
param_hashr   r   r   check_token   s   	r(   r
   c                    sT   t | d}dt fddt }tj| |dt jd	 }||kS )a	  
    Generate hexadecimal representation
    of the HMAC-SHA-256 signature of the data-check-string
    with the SHA256 hash of the bot's token used as a secret key

    :param token:
    :param hash:
    :param kwargs: all params received on auth
    :return:
    r   r	   c                    s   |  d |   S )N=r   )r   kwargsr   r   <lambda>7   s    z!check_signature.<locals>.<lambda>r   )
r   r   r   r   mapr   r   r   r   r    )r   r
   r+   r!   check_stringhmac_stringr   r*   r   check_signature+   s    r0   c                 C   s   t | fi |S )z
    Verify the authentication and the integrity
    of the data received on user's auth

    :param token: Bot's token
    :param data: all data that came on auth
    :return:
    )r0   )r   r   r   r   r   check_integrity<   s   	r1   )__doc__r   r   r   aiogram.utils.deprecatedr   dictstrr$   boolr(   r0   r1   r   r   r   r   <module>   s    

