o
    ê}”eî  ã                   @   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	 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 ejeef Zeje Zeje Zejrld dlZG dd„ deƒZG dd„ deƒZdS )é    N)Údatetime)Útimezoneé   )Úbase64_decode)Úbase64_encode)Úbytes_to_int)Úint_to_bytes)Ú
want_bytes)ÚBadSignature)ÚBadTimeSignature)ÚSignatureExpired)Ú
Serializer)ÚSignerc                       sì   e Zd ZdZdefdd„Zdedefdd„Zdede	fd	d
„Z
ej		ddededdde	fdd„ƒZej		ddededddeje	ef fdd„ƒZ		ddedededejeje	ef e	f f‡ fdd„Zddededef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.
    Úreturnc                 C   s   t t ¡ ƒS )zTReturns the current timestamp. The function must return an
        integer.
        )ÚintÚtime)Úself© r   ú[/Users/abhinavbhinder/Documents/map/venv/lib/python3.10/site-packages/itsdangerous/timed.pyÚget_timestamp!   s   zTimestampSigner.get_timestampÚtsc                 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Úvaluec                 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   Úsign1   s
   
zTimestampSigner.signNFÚsigned_valueÚmax_ageÚreturn_timestampz_te.Literal[False]c                 C   ó   d S ©Nr   ©r   r    r!   r"   r   r   r   Úunsign<   ó   zTimestampSigner.unsignTz_te.Literal[True]c                 C   r#   r$   r   r%   r   r   r   r&   E   r'   c              
      s”  z
t ƒ  |¡}d}W n ty# } z|}|jpd}W Y d}~nd}~ww t| jƒ}||vr7|r1|‚td|d�‚| |d¡\}}	d}
d}ztt	|	ƒƒ}
W n	 t
yT   Y nw |dur„|
dur{z|  |
¡}W n tttfyz } ztd|d�|‚d}~ww tt|ƒ||d�‚|
du rŽtd|d�‚|dur¿|  ¡ |
 }||kr­t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&   N   sj   €þ
ÿÿþ€ýý
ýc                 C   s*   z
| j ||d� W dS  ty   Y dS w )zeOnly validates the given signed value. Returns ``True`` if
        the signature exists and is valid.)r!   TF)r&   r
   )r   r    r!   r   r   r   Úvalidate¦   s   ÿzTimestampSigner.validate)NF)NTr$   )Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   r   r   Ú_t_str_bytesÚbytesr   ÚtypingÚoverloadÚ
_t_opt_intr&   Ú_tÚTupleÚboolÚUnionr6   Ú__classcell__r   r   r4   r   r      sR    
üþýüûüþýüûüþýüû Xr   c                       sœ   e Zd ZU dZeZeje ed< 	dde	dej
e f‡ fdd„Z			dd	ed
edede	dejf
dd„Z		dd	ed
ede	dejeejf fdd„Z‡  ZS )ÚTimedSerializerzOUses :class:`TimestampSigner` instead of the default
    :class:`.Signer`.
    Údefault_signerNÚsaltr   c                    s   t  dtƒ  |¡¡S )Nz_t.Iterator[TimestampSigner])r@   Úcastr*   Úiter_unsigners)r   rG   r4   r   r   rI   ·   s   zTimedSerializer.iter_unsignersFÚsr!   r"   c                 C   s˜   t |ƒ}d}|  |¡D ]:}z|j||dd�\}}|  |¡}	|r&|	|fW   S |	W   S  ty2   ‚  tyE }
 z|
}W Y d}
~
qd}
~
ww t t|¡‚)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	   rI   r&   Zload_payloadr   r
   r@   rH   )r   rJ   r!   r"   rG   Zlast_exceptionÚsignerZbase64dr   r)   Úerrr   r   r   Úloads¿   s$   
ÿ

€ÿzTimedSerializer.loadsc                 C   s   | j ||d|id�S )Nr!   )Zload_kwargs)Z_loads_unsafe_impl)r   rJ   r!   rG   r   r   r   Úloads_unsafeä   s   zTimedSerializer.loads_unsafer$   )NFN)NN)r7   r8   r9   r:   r   rF   r@   ÚTypeÚ__annotations__Ú_t_opt_str_bytesÚIteratorrI   r;   r?   rB   ÚAnyrM   rA   rN   rD   r   r   r4   r   rE   °   sD   
 ÿÿþûþýüû
ú(üþýüûrE   ) r   r=   r@   r   r   Úencodingr   r   r   r   r	   r3   r
   r   r   Ú
serializerr   rK   r   rC   r0   r<   r;   ÚOptionalrQ   r   r?   ÚTYPE_CHECKINGZtyping_extensionsZ_ter   rE   r   r   r   r   Ú<module>   s.    

 