o
    [h                     @   s   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 G dd deZG dd deZdS )    N)datetime   )	text_type)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d Zdd Zdd Zdd
dZdddZd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.
    c                 C   s   t t S )zTReturns the current timestamp. The function must return an
        integer.
        )inttime)self r   y/home/ubuntu/experiments/live_experiments/Pythonexperiments/Otree/venv/lib/python3.10/site-packages/itsdangerous/timed.pyget_timestamp   s   zTimestampSigner.get_timestampc                 C   s
   t |S )zaUsed to convert the timestamp from :meth:`get_timestamp` into
        a datetime object.
        )r   utcfromtimestamp)r   tsr   r   r   timestamp_to_datetime   s   
z%TimestampSigner.timestamp_to_datetimec                 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   value	timestampr   r   r   r   sign$   s
   
zTimestampSigner.signNFc           
   
   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\}}ztt	|}W n t
yR   d}Y nw |dur`tt|||d|du rjtd|d|dur|  | }	|	|krtd|	|f || |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 a naive
        :class:`datetime.datetime` object in UTC.
        N    ztimestamp missing)payloadr   )r   Zdate_signedzMalformed timestampzSignature age %s > %s seconds)r   unsignr
   r   r	   r   r   rsplitr   r   	Exceptionr   r   r   r   )
r   r   max_agereturn_timestampresultZ	sig_errorer   r   Zager   r   r   r   ,   sH   


zTimestampSigner.unsignc                 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   Zsigned_valuer"   r   r   r   validatee   s   zTimestampSigner.validate)NF)N)	__name__
__module____qualname____doc__r   r   r   r   r&   r   r   r   r   r      s    
9r   c                   @   s(   e Zd ZdZeZdddZd	ddZdS )
TimedSerializerzOUses :class:`TimestampSigner` instead of the default
    :class:`.Signer`.
    NFc                 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 |)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	   Ziter_unsignersr   Zload_payloadr   r
   )r   sr"   r#   saltZlast_exceptionsignerZbase64dr   r   errr   r   r   loadsv   s    

zTimedSerializer.loadsc                 C   s   d|i}i }|  ||||S )Nr"   )Z_loads_unsafe_impl)r   r,   r"   r-   Zload_kwargsZload_payload_kwargsr   r   r   loads_unsafe   s   zTimedSerializer.loads_unsafe)NFN)NN)r'   r(   r)   r*   r   Zdefault_signerr0   r1   r   r   r   r   r+   o   s
    
r+   )r   r   Z_compatr   encodingr   r   r   r   r	   excr
   r   r   
serializerr   r.   r   r   r+   r   r   r   r   <module>   s    ^