
_                 @   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 Gd d   d e  Z Gd 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
 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 j    S)zTReturns the current timestamp. The function must return an
        integer.
        )inttime)self r   :/tmp/pip-build-5gj8f0j9/itsdangerous/itsdangerous/timed.pyget_timestamp   s    zTimestampSigner.get_timestampc             C   s   t  j |  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   sV   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   value	timestampr   r   r   r   sign$   s
    zTimestampSigner.signNFc       
      C   s  y t  j |  |  } d } Wn: t k
 rX } z | } | j pC d } WYd d } ~ Xn Xt |  j  } | | k r | r |  t d d |  | j | d  \ } } y t t	 |   } Wn t
 k
 r d } Yn X| d k	 rt t |  d | d |  | d k r#t d d |  | d k	 rv|  j   | }	 |	 | k rvt d |	 | f d | d |  j |   | r| |  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 a naive
        :class:`datetime.datetime` object in UTC.
        N    ztimestamp missingpayloadr   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   s8   y |  j  | d | d SWn t k
 r3 d SYn Xd 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)	__name__
__module____qualname____doc__r   r   r   r   r&   r   r   r   r   r      s   9r   c               @   sC   e  Z d  Z d Z e Z d d d d d  Z d d d d  Z d S)TimedSerializerzOUses :class:`TimestampSigner` instead of the default
    :class:`.Signer`.
    NFc             C   s   t  |  } d } x |  j |  D] } yE | j | | d d \ } } |  j |  }	 | rh |	 | f S|	 SWq" t k
 r   Yq" t k
 r }
 z |
 } WYd d }
 ~
 Xq" Xq" 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.
        Nr#   T)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  } |  j  | | | |  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)r'   r(   r)   r*   r   Zdefault_signerr0   r1   r   r   r   r   r+   o   s   r+   )r   r   _compatr   encodingr   r   r   r   r	   excr
   r   r   
serializerr   r.   r   r   r+   r   r   r   r   <module>   s   ^