a
    y{Jh                     @   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mZm	Z	 d dl
mZ d dlmZ G dd deZG d	d
 d
eZeeedddZejdeedef edddZejdeeedef edddZdS )    N)datetime)
itemgetter)AnyCallableOptional)	parse_qsl)TelegramObjectc                   @   sr   e Zd ZU dZeed< dZee ed< e	ed< dZ
ee	 ed< dZee	 ed< dZee	 ed< dZee	 ed	< dS )

WebAppUserz{
    This object contains the data of the Web App user.

    Source: https://core.telegram.org/bots/webapps#webappuser
    idNis_botZ
first_name	last_nameusernamelanguage_code	photo_url)__name__
__module____qualname____doc__int__annotations__r   r   boolstrr   r   r   r    r   r   G/var/www/shaz/venv/lib/python3.9/site-packages/aiogram/utils/web_app.pyr	      s   
r	   c                   @   sb   e Zd ZU dZdZee ed< dZee	 ed< dZ
ee	 ed< dZee 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start_paramZ	auth_datehash)r   r   r   r   r   r   r   r   r   r	   r   r   r   r   r   r   r   r   '   s   
r   )token	init_datareturnc                 C   s   zt t|dd}W n ty(   Y dS 0 d|vr6dS |d}ddd t| tdd	D }tj	d
| 
 tjd}tj	| |
 tjd }||k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>W   s   z)check_webapp_signature.<locals>.<genexpr>r   )keys
   WebAppData)r*   msg	digestmod)dictr   
ValueErrorpopjoinsorteditemsr   hmacnewencodehashlibsha256digest	hexdigest)r    r!   Zparsed_datahash_Zdata_check_stringZ
secret_keyZcalculated_hashr   r   r   check_webapp_signatureC   s    



r;   loads.)r!   r=   r"   c                C   s\   i }t | D ]@\}}|dr(|ds<|drD|drD||}|||< qtf 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:
    []{})r   
startswithendswithr   )r!   r=   resultr*   valuer   r   r   parse_webapp_init_dataa   s    
rF   )r    r!   r=   r"   c                C   s"   t | |rt||dS tddS )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:
    r<   zInvalid init data signatureN)r;   rF   r.   )r    r!   r=   r   r   r   safe_parse_webapp_init_dataz   s    
rG   )r6   r3   jsonr   operatorr   typingr   r   r   urllib.parser   Zaiogram.typesr   r	   r   r   r   r;   r=   rF   rG   r   r   r   r   <module>   s.   !

