
     f                       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e          Z G 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                       e Zd ZdZddZddZddZej        	 	 dd d            Z	ej        	 	 d!d"d            Z		 	 dd# fdZ	d$d%dZ
 x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                B    t          t          j                              S )zTReturns the current timestamp. The function must return an
        integer.
        )r   time)selfs    U/var/www/api.educacionweb.es/myenv/lib/python3.11/site-packages/itsdangerous/timed.pyget_timestampzTimestampSigner.get_timestamp   s     49;;    tsr   c                B    t          j        |t          j                  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   s     r   timestamp_to_datetimez%TimestampSigner.timestamp_to_datetime#   s     %bX\::::r   valuestr | bytesbytesc                    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   r"   	timestampr&   s       r   signzTimestampSigner.sign-   sj    5!!!,t/A/A/C/C"D"DEE	""i's{T//6666r   NFsigned_valuemax_age
int | Nonereturn_timestampt.Literal[False]c                    d S N r   r*   r+   r-   s       r   unsignzTimestampSigner.unsign8   s	     r   Tt.Literal[True]tuple[bytes, datetime]c                    d S r0   r1   r2   s       r   r3   zTimestampSigner.unsign@   s	     "%r   booltuple[bytes, datetime] | bytesc                   	 t                                          |          }d}n"# t          $ r}|}|j        pd}Y d}~nd}~ww xY wt	          | j                  }||vr|r|t          d|          |                    |d          \  }}	d}
d}	 t          t          |	                    }
n# t          $ r Y nw xY w|i|
H	 |                     |
          }n1# t          t          t          f$ r}t          d|          |d}~ww xY wt          t          |          ||          |
t          d|          |x|                                 |
z
  }||k    r,t#          d| d	| d
||                     |
                    |dk     r)t#          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.
        Nr   ztimestamp missing)payloadr   zMalformed timestamp)r:   date_signedzSignature age z > z secondsr   z < 0 seconds)superr3   r   r:   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__s                 r   r3   zTimestampSigner.unsignH   su    	&WW^^L11FII 	& 	& 	&IY%#FFFFFF	& "" f  "#6GGGG --Q//x!!%	!-"9"9::FF 	 	 	D	
  ! 66v>>EE"G];    +-u   #3y>>5eTTTT >"#8%HHHH $$&&/CW}}&>S>>W>>>! $ : :6 B B    Qww&6S666! $ : :6 B B     	=$44V<<<<s>   #' 
AAAB1 1
B>=B>C D
3DD
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)r3   r   )r   r*   r+   s      r   validatezTimestampSigner.validate   sD    	KKgK6664 	 	 	55	s    
)))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-   r4   r   r5   )r*   r#   r+   r,   r-   r7   r   r8   r0   )r*   r#   r+   r,   r   r7   )__name__
__module____qualname____doc__r   r!   r)   toverloadr3   rM   __classcell__rK   s   @r   r   r      s               ; ; ; ;7 7 7 7 Z #-2	    Z Z #,0	% % % % Z% #!&	V V V V V V Vp        r   r   c                  V     e Zd ZU dZeZded<   	 dd fd	Z	 	 	 dddZ	 	 dddZ	 xZ
S )TimedSerializerzOUses :class:`TimestampSigner` instead of the default
    :class:`.Signer`.
    ztype[TimestampSigner]default_signerNsaltstr | bytes | Noner   cabc.Iterator[TimestampSigner]c                l    t          j        dt                                          |                    S )Nr[   )rR   castr<   iter_unsigners)r   rY   rK   s     r   r^   zTimedSerializer.iter_unsigners   s*     v68N8Nt8T8TUUUr   Fsr#   r+   r,   r-   r7   t.Anyc                F   t          |          }d}|                     |          D ]a}	 |                    ||d          \  }}|                     |          }	|r|	|fc S |	c S # t          $ r  t
          $ r}
|
}Y d}
~
Zd}
~
ww xY wt          j        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   r^   r3   load_payloadr   r   rR   r]   )r   r_   r+   r-   rY   last_exceptionsignerbase64dr(   r:   errs              r   loadszTimedSerializer.loads   s     qMM))$// 	% 	%F%%+]]w &3 & &" ++G44# ."I----#     % % %!$% f\>222s   5A&"A&&B:BBtuple[bool, t.Any]c                6    |                      ||d|i          S )Nr+   )load_kwargs)_loads_unsafe_impl)r   r_   r+   rY   s       r   loads_unsafezTimedSerializer.loads_unsafe   s$     &&q$Y<P&QQQr   r0   )rY   rZ   r   r[   )NFN)
r_   r#   r+   r,   r-   r7   rY   rZ   r   r`   )NN)r_   r#   r+   r,   rY   rZ   r   rh   )rN   rO   rP   rQ   r   rX   __annotations__r^   rg   rl   rT   rU   s   @r   rW   rW      s           -<N;;;; *.V V V V V V V #!&#'#3 #3 #3 #3 #3P ##'	R R R R R R R R Rr   rW   )
__future__r   collections.abcabccabcr   typingrR   r   r   encodingr   r   r	   r
   r   rI   r   r   r   
serializerr   r   rd   r   r   rW   r1   r   r   <module>ru      s   " " " " " "                        # # # # # # # # # # # # " " " " " " " " " " " "                   ! ! ! ! ! ! ! ! ! ! ! ! $ $ $ $ $ $ " " " " " "      Q Q Q Q Qf Q Q Qh:R :R :R :R :Rj. :R :R :R :R :Rr   