o
    LDi                  	   @   s   d dl Z d dlZd dlZd dlmZ d dlmZ d dlmZ d dlm	Z	 d dl
mZ d dlmZ G dd	 d	eZG d
d deZG dd deZdededefddZejddedede	f defddZejddededede	f defddZdS )    N)Callable)datetime)
itemgetter)Any)	parse_qsl)TelegramObjectc                   @   sR   e Zd ZU dZeed< 	 eed< 	 eed< 	 dZedB ed< 	 dZedB ed< dS )
WebAppChatzg
    This object represents a chat.

    Source: https://core.telegram.org/bots/webapps#webappchat
    idtypetitleNusername	photo_url)	__name__
__module____qualname____doc__int__annotations__strr   r    r   r   a/var/www/www-root/data/www/77.83.87.30/venv/lib/python3.10/site-packages/aiogram/utils/web_app.pyr      s   
 r   c                   @   s   e Zd ZU dZeed< 	 dZedB ed< 	 eed< 	 dZ	edB ed< 	 dZ
edB ed< 	 dZedB ed< 	 dZedB ed	< 	 dZedB ed
< 	 dZedB ed< 	 dZedB ed< dS )
WebAppUserz{
    This object contains the data of the Web App user.

    Source: https://core.telegram.org/bots/webapps#webappuser
    r	   Nis_bot
first_name	last_namer   language_code
is_premiumadded_to_attachment_menuallows_write_to_pmr   )r   r   r   r   r   r   r   boolr   r   r   r   r   r   r   r   r   r   r   r   r   $   s,   
 r   c                   @   s   e Zd ZU dZdZedB ed< 	 dZedB ed< 	 dZ	edB ed< 	 dZ
edB ed< 	 dZedB ed< 	 dZedB ed< 	 dZedB ed	< 	 dZedB ed
< 	 eed< 	 eed< dS )WebAppInitDataz
    This object contains data that is transferred to the Web App when it is opened.
    It is empty if the Web App was launched from a keyboard button.

    Source: https://core.telegram.org/bots/webapps#webappinitdata
    Nquery_iduserreceiverchat	chat_typechat_instancestart_paramcan_send_after	auth_datehash)r   r   r   r   r!   r   r   r"   r   r#   r$   r   r%   r&   r'   r(   r   r   r   r   r   r   r    E   s,   
 r    token	init_datareturnc                 C   s   z
t t|dd}W n
 ty   Y dS w d|vrdS |d}ddd t| tdd	D }tj	d
| 
 tjd}tj	| |
 tjd }t||S )z
    Check incoming WebApp init data signature

    Source: https://core.telegram.org/bots/webapps#validating-data-received-via-the-web-app

    :param token: bot Token
    :param init_data: data from frontend to be validated
    :return:
    T)strict_parsingFr*   
c                 s   s"    | ]\}}| d | V  qdS )=Nr   ).0kvr   r   r   	<genexpr>   s    
z)check_webapp_signature.<locals>.<genexpr>r   )keys
   WebAppData)r5   msg	digestmod)dictr   
ValueErrorpopjoinsorteditemsr   hmacnewencodehashlibsha256digest	hexdigestcompare_digest)r+   r,   parsed_datahash_data_check_string
secret_keycalculated_hashr   r   r   check_webapp_signatureo   s(   


rK   loadsrM   .c                C   s\   i }t | D ] \}}|dr|ds|dr"|dr"||}|||< qtdi |S )a)  
    Parse WebApp init data and return it as WebAppInitData object

    This method doesn't make any security check, so you shall not trust to this data,
    use :code:`safe_parse_webapp_init_data` instead.

    :param init_data: data from frontend to be parsed
    :param loads:
    :return:
    []{}Nr   )r   
startswithendswithr    )r,   rM   resultr5   valuer   r   r   parse_webapp_init_data   s   
rV   c                C   s"   t | |rt||dS d}t|)a   
    Validate raw WebApp init data and return it as WebAppInitData object

    Raise :obj:`ValueError` when data is invalid

    :param token: bot token
    :param init_data: data from frontend to be parsed and validated
    :param loads:
    :return:
    rL   zInvalid init data signature)rK   rV   r9   )r+   r,   rM   r6   r   r   r   safe_parse_webapp_init_data   s   
rW   )rA   r>   jsoncollections.abcr   r   operatorr   typingr   urllib.parser   aiogram.typesr   r   r   r    r   r   rK   rM   rV   rW   r   r   r   r   <module>   s>    !*#


