B
    -[                 @   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   1lib/python3.7/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   Zutcfromtimestamp)r   Z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(  yt | |}d}W n0 tk
rD } z|}|jp2d}W dd}~X Y nX t| j}||krl|r`|td|d||d\}}ytt	|}W n t
k
r   d}Y nX |dk	rtt|||d|dkrtd|d|dk	r|  | }	|	|krt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   ,   s>    




zTimestampSigner.unsignc             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   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}x|| |D ]n}y.|j||dd\}}| |}	|rH|	|fS |	S  tk
r`    Y q tk
r }
 z|
}W dd}
~
X Y qX qW |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	   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_signerr.   r/   r   r   r   r   r)   o   s   
r)   )r   r   Z_compatr   encodingr   r   r   r   r	   excr
   r   r   Z
serializerr   r,   r   r   r)   r   r   r   r   <module>   s   ^