o
    'h                     @  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 )+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.
    returnintc                 C  s   t t S )zTReturns the current timestamp. The function must return an
        integer.
        )r   time)self r   N/var/www/html/olx_land/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 rt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   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(     