U
    <æ{f“  ã                   @  sì   d dl mZ d dlm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 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 ddlmZ G dd„ deƒZG dd„ dee ƒZdS )é    )ÚannotationsN)Údatetime)Útimezoneé   )Úbase64_decode)Úbase64_encode)Úbytes_to_int)Úint_to_bytes)Ú
want_bytes)ÚBadSignature)ÚBadTimeSignature)ÚSignatureExpired)Ú_TSerialized)Ú
Serializer)ÚSignerc                      s¨   e Zd ZdZddœdd„Zdddœdd	„Zd
ddœdd„Zejd d
ddddœdd„ƒZ	ejd!d
ddddœdd„ƒZ	d"d
ddddœ‡ fdd„Z	d#d
dddœdd„Z
‡  ZS )$ÚTimestampSignerzþWorks like the regular :class:`.Signer` but also records the time
    of the signing and can be used to expire signatures. The
    :meth:`unsign` method can raise :exc:`.SignatureExpired` if the
    unsigning failed because the signature is expired.
    Úint)Úreturnc                 C  s   t t ¡ ƒS )zTReturns the current timestamp. The function must return an
        integer.
        )r   Útime)Úself© r   úQ/var/www/html/Darija-Ai-API/env/lib/python3.8/site-packages/itsdangerous/timed.pyÚget_timestamp   s    zTimestampSigner.get_timestampr   )Útsr   c                 C  s   t j|tjdS )a   Convert the timestamp from :meth:`get_timestamp` into an
        aware :class`datetime.datetime` in UTC.

        .. versionchanged:: 2.0
            The timestamp is returned as a timezone-aware ``datetime``
            in UTC rather than a naive ``datetime`` assumed to be UTC.
        )Útz)r   Úfromtimestampr   Úutc)r   r   r   r   r   Útimestamp_to_datetime#   s    z%TimestampSigner.timestamp_to_datetimeústr | bytesÚbytes)Úvaluer   c                 C  s@   t |ƒ}tt|  ¡ ƒƒ}t | jƒ}|| | }|| |  |¡ S )z:Signs the given string and also attaches time information.)r
   r   r	   r   ÚsepZget_signature)r   r    Ú	timestampr!   r   r   r   Úsign-   s
    
zTimestampSigner.signNFú
int | Nonezt.Literal[False])Úsigned_valueÚmax_ageÚreturn_timestampr   c                 C  s   d S ©Nr   ©r   r%   r&   r'   r   r   r   Úunsign8   s    zTimestampSigner.unsignTzt.Literal[True]ztuple[bytes, datetime]c                 C  s   d S r(   r   r)   r   r   r   r*   @   s    Úboolztuple[bytes, datetime] | bytesc              
     s®  zt ƒ  |¡}d}W n0 tk
rD } z|}|jp2d}W 5 d}~X Y nX t| jƒ}||krl|r`|‚td|d‚| |d¡\}}	d}
d}ztt	|	ƒƒ}
W n t
k
r¨   Y nX |dk	r|
dk	rz|  |
¡}W n8 tttfk
r } ztd|d|‚W 5 d}~X Y nX tt|ƒ||d‚|
dkr.td|d‚|dk	r–|  ¡ |
 }||krptd|› d	|› d
||  |
¡d‚|dk r–td|› d||  |
¡d‚|rª||  |
¡fS |S )aí  Works like the regular :meth:`.Signer.unsign` but can also
        validate the time. See the base docstring of the class for
        the general behavior. If ``return_timestamp`` is ``True`` the
        timestamp of the signature will be returned as an aware
        :class:`datetime.datetime` object in UTC.

        .. versionchanged:: 2.0
            The timestamp is returned as a timezone-aware ``datetime``
            in UTC rather than a naive ``datetime`` assumed to be UTC.
        Nó    ztimestamp missing)Úpayloadr   zMalformed timestamp)r-   Zdate_signedzSignature age z > z secondsr   z < 0 seconds)Úsuperr*   r   r-   r
   r!   r   Úrsplitr   r   Ú	Exceptionr   Ú
ValueErrorÚOSErrorÚOverflowErrorÚstrr   r   )r   r%   r&   r'   ÚresultZ	sig_errorÚer!   r    Zts_bytesZts_intZts_dtÚexcZage©Ú	__class__r   r   r*   H   sb    


 ÿþ


ý

ý)r%   r&   r   c                 C  s0   z| j ||d W dS  tk
r*   Y dS X dS )zeOnly validates the given signed value. Returns ``True`` if
        the signature exists and is valid.)r&   TFN)r*   r   )r   r%   r&   r   r   r   Úvalidate    s
    zTimestampSigner.validate)NF)NT)NF)N)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   r#   ÚtÚoverloadr*   r:   Ú__classcell__r   r   r8   r   r      s    
  ü  ü
  üXr   c                      sf   e Zd ZU dZeZded< ddddœ‡ fdd	„Zdddddddœdd„Zddddddœdd„Z	‡  Z
S )ÚTimedSerializerzOUses :class:`TimestampSigner` instead of the default
    :class:`.Signer`.
    ztype[TimestampSigner]Údefault_signerNzstr | bytes | Noneúcabc.Iterator[TimestampSigner])Úsaltr   c                   s   t  dtƒ  |¡¡S )NrD   )r?   Úcastr.   Úiter_unsigners)r   rE   r8   r   r   rG   ±   s    zTimedSerializer.iter_unsignersFr   r$   r+   zt.Any)Úsr&   r'   rE   r   c                 C  s¢   t |ƒ}d}|  |¡D ]z}z:|j||dd\}}|  |¡}	|rL|	|fW   S |	W   S  tk
rj   ‚ Y q tk
rŽ }
 z|
}W 5 d}
~
X Y qX qt t|¡‚dS )a†  Reverse of :meth:`dumps`, raises :exc:`.BadSignature` if the
        signature validation fails. If a ``max_age`` is provided it will
        ensure the signature is not older than that time in seconds. In
        case the signature is outdated, :exc:`.SignatureExpired` is
        raised. All arguments are forwarded to the signer's
        :meth:`~TimestampSigner.unsign` method.
        NT)r&   r'   )r
   rG   r*   Zload_payloadr   r   r?   rF   )r   rH   r&   r'   rE   Zlast_exceptionÚsignerZbase64dr"   r-   Úerrr   r   r   Úloads¹   s$      ÿ


zTimedSerializer.loadsztuple[bool, t.Any])rH   r&   rE   r   c                 C  s   | j ||d|idS )Nr&   )Zload_kwargs)Z_loads_unsafe_impl)r   rH   r&   rE   r   r   r   Úloads_unsafeÞ   s    zTimedSerializer.loads_unsafe)N)NFN)NN)r;   r<   r=   r>   r   rC   Ú__annotations__rG   rK   rL   rA   r   r   r8   r   rB   ª   s   
 ÿ   û(  ürB   )Ú
__future__r   Úcollections.abcÚabcZcabcr   Útypingr?   r   r   Úencodingr   r   r   r	   r
   r7   r   r   r   Ú
serializerr   r   rI   r   r   rB   r   r   r   r   Ú<module>   s&    