o
    vh                  
   @   s   d dl mZmZ d dlmZmZ d dlmZmZ d dl	m
Z
 d dlmZ zd dlZW n ey; Z zededZ[ww G dd	 d	eZdS )
    )OptionalUnion)cmstsp)TimeStamperTimestampRequestError)set_tsp_headers)LazySessionNz?Install pyHanko with the [async_http] optional dependency groupc                       s   e Zd Z				ddeejef deej f fddZ	de
fd	d
ZdejfddZdejf fddZdejdejfddZ  ZS )AIOHttpTimeStamperF   Nsessionauthc                    sB   |r| dstd|| _|| _|| _|| _|| _t   dS )a  
        Initialise the timestamp client.

        :param url:
            URL where the server listens for timestamp requests.
        :param https:
            Enforce HTTPS.
        :param timeout:
            Timeout (in seconds)
        :param auth:
            `aiohttp.BasicAuth` object with authentication credentials.
        :param headers:
            Other headers to include.
        zhttps:zTimestamp URL is not HTTPS.N)	
startswith
ValueErrorurltimeoutr   headers_sessionsuper__init__)selfr   r   httpsr   r   r   	__class__ d/var/www/html/hyperkenya/venv/lib/python3.10/site-packages/pyhanko/sign/timestamps/aiohttp_client.pyr      s   zAIOHttpTimeStamper.__init__returnc                    s   t | jpi S )z
        Format the HTTP request headers.
        Subclasses can override this to perform their own header generation
        logic.

        :return:
            Header dictionary.
        )r   r   )r   r   r   r   async_request_headers2   s   	z(AIOHttpTimeStamper.async_request_headersc                    s$   | j }t|tr| I d H S |S N)r   
isinstancer	   get_session)r   r   r   r   r   r    =   s
   
zAIOHttpTimeStamper.get_sessionc                    s   t  ||I d H S r   )r   async_timestamp)r   message_digestmd_algorithmr   r   r   r!   D   s   z"AIOHttpTimeStamper.async_timestampreqc           
   
      s   |   I d H }tj| jd}|  I d H }zN|j| j|| | jd|d4 I d H .}|	 I d H }|j
d}|dkrNd| d}tj|j|j||j
dW d   I d H  n1 I d H s^w   Y  W n tjyv }	 ztd	|	d }	~	ww tj|S )
N)totalT)r   r   datar   raise_for_statusr   zContent-Typezapplication/timestamp-replyz?Bad content type. Expected application/timestamp-reply,but got .)messager   z(Error while contacting timestamp service)r    aiohttpClientTimeoutr   r   postr   dumpr   readr   getContentTypeErrorrequest_infohistoryClientErrorr   r   TimeStampRespload)
r   r$   r   
cl_timeoutr   responseresponse_datactmsger   r   r   async_request_tsa_responseI   sL   (z-AIOHttpTimeStamper.async_request_tsa_response)Fr   NN)__name__
__module____qualname__r   r*   ClientSessionr	   r   	BasicAuthr   dictr   r    r   ContentInfor!   r   TimeStampReqr4   r<   __classcell__r   r   r   r   r
      s(     r
   )typingr   r   
asn1cryptor   r   pyhanko.sign.timestampsr   r   $pyhanko.sign.timestamps.common_utilsr   4pyhanko_certvalidator.fetchers.aiohttp_fetchers.utilr	   r*   ImportError_er
   r   r   r   r   <module>   s     