
    wdf                         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e          Z G 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                   4    e Zd ZdZd Zd Zd Zd	dZ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                 B    t          t          j                              S )zTReturns the current timestamp. The function must return an
        integer.
        )inttime)selfs    W/var/www/book.euthymeo.com/html/venv/lib/python3.11/site-packages/itsdangerous/timed.pyget_timestampzTimestampSigner.get_timestamp   s     49;;    c                 *    t          j        |          S )zaUsed to convert the timestamp from :meth:`get_timestamp` into
        a datetime object.
        )r   utcfromtimestamp)r   tss     r   timestamp_to_datetimez%TimestampSigner.timestamp_to_datetime   s     (,,,r   c                     t          |          }t          t          |                                                     }t          | j                  }||z   |z   }||z   |                     |          z   S )z:Signs the given string and also attaches time information.)r
   r   r	   r   sepget_signature)r   value	timestampr   s       r   signzTimestampSigner.sign$   sj    5!!!,t/A/A/C/C"D"DEE	""i's{T//6666r   NFc                    	 t          j        | |          }d}n"# t          $ r}|}|j        pd}Y d}~nd}~ww xY wt	          | j                  }||vr|r|t          d|          |                    |d          \  }}	 t          t          |                    }n# t          $ r d}Y nw xY w|t          t          |          ||          |t          d|          |I|                                 |z
  }	|	|k    r,t          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 a naive
        :class:`datetime.datetime` object in UTC.
        Nr   ztimestamp missing)payloadr   )r$   date_signedzMalformed timestampzSignature age z > z seconds)r   unsignr   r$   r
   r   r   rsplitr   r   	Exceptionr   r   r   r   )
r   r    max_agereturn_timestampresult	sig_errorer   r!   ages
             r   r&   zTimestampSigner.unsign,   s   	&]4//FII 	& 	& 	&IY%#FFFFFF	& "" f  "#6GGGG!==a00y	$]9%=%=>>II 	 	 	III	
  ")$$e    "#8%HHHH $$&&2CW}}&&7:ssGGGD! $ : :9 E E     	@$44Y????s"    
949B   B/.B/c                 X    	 |                      ||           dS # t          $ r Y dS w xY w)zeOnly validates the given signed value. Returns ``True`` if
        the signature exists and is valid.)r)   TF)r&   r   )r   signed_valuer)   s      r   validatezTimestampSigner.validatee   sD    	KKgK6664 	 	 	55	s    
)))NF)N)	__name__
__module____qualname____doc__r   r   r"   r&   r1    r   r   r   r      st              - - -7 7 77 7 7 7r     r   r   c                   &    e Zd ZdZeZddZddZdS )TimedSerializerzOUses :class:`TimestampSigner` instead of the default
    :class:`.Signer`.
    NFc                    t          |          }d}|                     |          D ]a}	 |                    ||d          \  }}|                     |          }	|r|	|fc S |	c S # t          $ r  t
          $ r}
|
}Y d}
~
Zd}
~
ww xY 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
   iter_unsignersr&   load_payloadr   r   )r   sr)   r*   saltlast_exceptionsignerbase64dr!   r$   errs              r   loadszTimedSerializer.loadsv   s     qMM))$// 	% 	%F%%+]]1gPT]%U%U"++G44# ."I---- $    % % %!$%s   5A&"A&&B:BBc                 >    d|i}i }|                      ||||          S )Nr)   )_loads_unsafe_impl)r   r<   r)   r=   load_kwargsload_payload_kwargss         r   loads_unsafezTimedSerializer.loads_unsafe   s-     '* &&q$=PQQQr   )NFN)NN)r2   r3   r4   r5   r   default_signerrB   rG   r6   r   r   r8   r8   o   sS          %N   4R R R R R Rr   r8   )r   r   _compatr   encodingr   r   r   r	   r
   excr   r   r   
serializerr   r?   r   r   r8   r6   r   r   <module>rM      sW                # # # # # # # # # # # # " " " " " " " " " " " "                   ! ! ! ! ! ! ! ! ! ! ! ! " " " " " "      [ [ [ [ [f [ [ [|$R $R $R $R $Rj $R $R $R $R $Rr   