3
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rd 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edddZeedddZee	dd	d
Z
ejdeede	dddZejdeedeje	ef dddZdeeeejeje	ef e	f d fddZdeeedddZ  ZS )TimestampSignerzWorks 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j S )zTReturns the current timestamp. The function must return an
        integer.
        )inttime)self r   :/tmp/pip-build-3irwxpxt/itsdangerous/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)valuer   c             C   s@   t |}tt| j }t | j}|| | }|| | 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.signNFz_te.Literal[False])signed_valuemax_agereturn_timestampr   c             C   s   d S )Nr   )r   r    r!   r"   r   r   r   unsign<   s    zTimestampSigner.unsignTz_te.Literal[True]c             C   s   d S )Nr   )r   r    r!   r"   r   r   r   r#   E   s    c                sj  yt  j|}d}W n0 tk
rD } z|}|jp2d}W Y dd}~X nX t| j}||krl|r`|td|d|j|d\}}	d}
d}ytt	|	}
W n t
k
r   Y nX |dk	r|
dk	r| j|
}tt|||d|
dkrtd|d|dk	rR| j |
 }||kr,td| d	| d
|| j|
d|dk rRtd| d|| j|
d|rf|| j|
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   )r%   Zdate_signedzMalformed timestampzSignature age z > z secondsr   z < 0 seconds)superr#   r
   r%   r	   r   r   rsplitr   r   	Exceptionr   strr   r   )r   r    r!   r"   resultZ	sig_errorer   r   Zts_bytesZts_intZts_dtZage)	__class__r   r   r#   N   sN    





)r    r!   r   c             C   s,   y| j ||d dS  tk
r&   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   r   _t_str_bytesbytesr   typingZoverload
_t_opt_intr#   _tTupleboolUnionr-   __classcell__r   r   )r,   r   r      s    
   &Lr   c                   s|   e Zd ZU dZeZeje deej	e d fddZ
deeeeejddd	Zdeeeejeejf d
ddZ  ZS )TimedSerializerzOUses :class:`TimestampSigner` instead of the default
    :class:`.Signer`.
    N)saltr   c                s   t jdt j|S )Nz_t.Iterator[TimestampSigner])r6   castr&   iter_unsigners)r   r<   )r,   r   r   r>      s    zTimedSerializer.iter_unsignersF)sr!   r"   r<   r   c             C   s   t |}d}x|| j|D ]n}y.|j||dd\}}| j|}	|rH|	|fS |	S  tk
r`    Y q tk
r }
 z
|
}W Y dd}
~
X qX qW tj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	   r>   r#   Zload_payloadr   r
   r6   r=   )r   r?   r!   r"   r<   Zlast_exceptionsignerZbase64dr   r%   errr   r   r   loads   s    
zTimedSerializer.loads)r?   r!   r<   r   c             C   s   | j ||d|idS )Nr!   )Zload_kwargs)Z_loads_unsafe_impl)r   r?   r!   r<   r   r   r   loads_unsafe   s    zTimedSerializer.loads_unsafe)N)NFN)NN)r.   r/   r0   r1   r   Zdefault_signerr6   Type_t_opt_str_bytesIteratorr>   r2   r5   r8   ZAnyrB   r7   rC   r:   r   r   )r,   r   r;      s   
	  
" r;   ) r   r4   r6   r   r   encodingr   r   r   r   r	   excr
   r   r   
serializerr   r@   r   r9   r)   r3   r2   OptionalrE   r   r5   ZTYPE_CHECKINGZtyping_extensionsZ_ter   r;   r   r   r   r   <module>   s,   

 