o
    Þý°j“  ã                   @  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„Zd"dd	„Zd#dd„Zej		d$d%dd„ƒZ	ej		d&d'dd„ƒZ			d$d(‡ fdd„Z	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.
    ÚreturnÚintc                 C  s   t t ¡ ƒS )zTReturns the current timestamp. The function must return an
        integer.
        )r   Útime)Úself© r   ú…/root/aizidognhua/tmp/workspace/projects/ec89d86c-575f-41c9-af57-ac45cbdbf775/venv/lib/python3.10/site-packages/itsdangerous/timed.pyÚget_timestamp   s   zTimestampSigner.get_timestampÚ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Úvalueústr | bytesÚbytesc                 C  s@   t |ƒ}tt|  ¡ ƒƒ}t | jƒ}|| | }|| |  |¡ S )z:Signs the given string and also attaches time information.)r
   r   r	   r   ÚsepÚget_signature)r   r   Ú	timestampr!   r   r   r   Úsign-   s
   
zTimestampSigner.signNFÚsigned_valueÚmax_ageú
int | NoneÚreturn_timestampút.Literal[False]c                 C  ó   d S ©Nr   ©r   r%   r&   r(   r   r   r   Úunsign8   ó   zTimestampSigner.unsignTút.Literal[True]útuple[bytes, datetime]c                 C  r*   r+   r   r,   r   r   r   r-   @   r.   Úboolútuple[bytes, datetime] | bytesc              
     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)r4   Údate_signedzSignature age z > z secondsr   z < 0 seconds)Úsuperr-   r   r4   r
   r!   r   Úrsplitr   r   Ú	Exceptionr   Ú
ValueErrorÚOSErrorÚOverflowErrorÚstrr   r   )r   r%   r&   r(   ÚresultÚ	sig_errorÚer!   r   Úts_bytesÚts_intÚts_dtÚexcÚage©Ú	__class__r   r   r-   H   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)r   r   )r   r   r   r   )r   r   r   r    )NF)r%   r   r&   r'   r(   r)   r   r    )NT)r%   r   r&   r'   r(   r/   r   r0   )r%   r   r&   r'   r(   r1   r   r2   r+   )r%   r   r&   r'   r   r1   )Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   r$   ÚtÚoverloadr-   rG   Ú__classcell__r   r   rE   r   r      s"    



üü
üXr   c                      sV   e Zd ZU dZeZded< 	dd‡ fd	d
„Z			dddd„Z		dddd„Z	‡  Z
S )ÚTimedSerializerzOUses :class:`TimestampSigner` instead of the default
    :class:`.Signer`.
    ztype[TimestampSigner]Údefault_signerNÚsaltústr | bytes | Noner   úcabc.Iterator[TimestampSigner]c                   s   t  dtƒ  |¡¡S )NrS   )rL   Úcastr6   Úiter_unsigners)r   rQ   rE   r   r   rU   ±   s   zTimedSerializer.iter_unsignersFÚsr   r&   r'   r(   r1   út.Anyc                 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
   rU   r-   Úload_payloadr   r   rL   rT   )r   rV   r&   r(   rQ   Úlast_exceptionÚsignerÚbase64dr#   r4   Úerrr   r   r   Úloads¹   s$   
ÿ

€ÿzTimedSerializer.loadsútuple[bool, t.Any]c                 C  s   | j ||d|id�S )Nr&   )Úload_kwargs)Ú_loads_unsafe_impl)r   rV   r&   rQ   r   r   r   Úloads_unsafeÞ   s   zTimedSerializer.loads_unsafer+   )rQ   rR   r   rS   )NFN)
rV   r   r&   r'   r(   r1   rQ   rR   r   rW   )NN)rV   r   r&   r'   rQ   rR   r   r^   )rH   rI   rJ   rK   r   rP   Ú__annotations__rU   r]   ra   rN   r   r   rE   r   rO   ª   s   
 ÿû(ürO   )Ú
__future__r   Úcollections.abcÚabcÚcabcr   ÚtypingrL   r   r   Úencodingr   r   r   r	   r
   rC   r   r   r   Ú
serializerr   r   rZ   r   r   rO   r   r   r   r   Ú<module>   s(     