o
    2c                     @   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 )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 S )zTReturns the current timestamp. The function must return an
        integer.
        )inttime)self r   H/var/www/html/gps/gps/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   sep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              
      sb  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rk|
durb| |
}tt|||d|
du rutd|d|dur|  |
 }||krtd| d	| d
|| |
d|dk rt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   )r*   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#   result	sig_errorer   r   ts_bytests_intts_dtage	__class__r   r   r'   N   sX   


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Unionr9   __classcell__r   r   r7   r   r      sR    
 Qr   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])rC   castr,   iter_unsigners)r   rJ   r7   r   r   rL      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	   rL   r'   load_payloadr   r
   rC   rK   )r   rM   r"   r#   rJ   last_exceptionsignerbase64dr   r*   errr   r   r   loads   s$   


zTimedSerializer.loadsc                 C   s   | j ||d|idS )Nr"   )load_kwargs)_loads_unsafe_impl)r   rM   r"   rJ   r   r   r   loads_unsafe   s   zTimedSerializer.loads_unsafer%   )NFN)NN)r:   r;   r<   r=   r   rI   rC   Type__annotations___t_opt_str_bytesIteratorrL   r>   rB   rE   AnyrS   rD   rV   rG   r   r   r7   r   rH      sD   
 
(rH   ) r   r@   rC   r   r   encodingr   r   r   r   r	   excr
   r   r   
serializerr   rP   r   rF   r/   r?   r>   OptionalrY   r   rB   TYPE_CHECKINGtyping_extensions_ter   rH   r   r   r   r   <module>   s.    

 