
    gQe                     V   d dl 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 ej&                  eef   Zej.                  e   Zej.                  e   Zej6                  rd dlZ G d de      Z G d de      Zy)    N)datetime)timezone   )base64_decode)base64_encode)bytes_to_int)int_to_bytes)
want_bytes)BadSignature)BadTimeSignature)SignatureExpired)
Serializer)Signerc                   N    e Zd ZdZdefdZdedefdZdede	fdZ
ej                  	 	 dded	ed
dde	fd       Zej                  	 	 dded	ed
ddej                   e	ef   fd       Z	 	 dded	ed
edej$                  ej                   e	ef   e	f   f fdZdded	edef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.
    returnc                 <    t        t        j                               S )zTReturns the current timestamp. The function must return an
        integer.
        )inttime)selfs    J/home/yussbn06/site/env/lib/python3.12/site-packages/itsdangerous/timed.pyget_timestampzTimestampSigner.get_timestamp!   s     499;    tsc                 L    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   valuec                     t        |      }t        t        | j                                     }t        | j                        }||z   |z   }||z   | j                  |      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.sign1   sZ    5!!,t/A/A/C"DE	"i's{T//666r   signed_valuemax_agereturn_timestampz_te.Literal[False]c                      y N r   r&   r'   r(   s       r   unsignzTimestampSigner.unsign<        	r   z_te.Literal[True]c                      y r*   r+   r,   s       r   r-   zTimestampSigner.unsignE   r.   r   c                 
   	 t         |   |      }d}t	        | j
                        }||vr|r|t        d|      |j                  |d      \  }}	d}
d}	 t        t        |	            }
|+|
	 | j                  |
      }t        t        |      ||      |
t        d|      |b| j!                         |
z
  }||kD  r$t#        d| d	| d
|| j                  |
            |dk  r!t#        d| d|| j                  |
            |r|| j                  |
      fS |S # t        $ r}|}|j                  xs d}Y d}~0d}~ww xY w# t        $ r Y w xY w# t        t        t        f$ r}t        d|      |d}~ww xY w)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)r1   date_signedzSignature age z > z secondsr   z < 0 seconds)superr-   r   r1   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   r-   zTimestampSigner.unsignN   s    	&W^L1FI
 " f"#6GG --Q/x!'+	!-"9:F  ! 66v>E #3y>5eTT >"#8%HH $$&/CW}&$SEWIX>! $ : :6 B  Qw&$SE6! $ : :6 B  $44V<<<G  	&IYY%#F	&.  		 #G];  +-usA   D# E 1E #	E	,EE		EEF/E==Fc                 J    	 | j                  ||       y# t        $ r Y yw xY w)zeOnly validates the given signed value. Returns ``True`` if
        the signature exists and is valid.)r'   TF)r-   r   )r   r&   r'   s      r   validatezTimestampSigner.validate   s,    	KKgK6 		s    	"")NF)NTr*   )__name__
__module____qualname____doc__r   r   r   r   _t_str_bytesbytesr%   typingoverload
_t_opt_intr-   _tTupleboolUnionrD   __classcell__rB   s   @r   r   r      sB    s  ; ; ;7, 75 7 __ #16	"  /	
 
  __ #04	"  .	
 
%/	"  #!&	V"V V 	V
 
"((5(?+U2	3Vp\ J RV r   r   c                        e Zd ZU dZeZej                  e   ed<   	 dde	dej                  e   f fdZ	 	 	 ddededede	dej                  f
d	Z	 	 ddedede	dej"                  eej                  f   fd
Z xZS )TimedSerializerzOUses :class:`TimestampSigner` instead of the default
    :class:`.Signer`.
    default_signersaltr   c                 J    t        j                  dt        |   |            S )Nz_t.Iterator[TimestampSigner])rN   castr3   iter_unsigners)r   rW   rB   s     r   rZ   zTimedSerializer.iter_unsigners   s!     ww5uw7Md7STTr   sr'   r(   c                 &   t        |      }d}| j                  |      D ]6  }	 |j                  ||d      \  }}| j                  |      }	|r|	|fc S |	c S  t        j                  t
        |      # t        $ r  t
        $ r}
|
}Y d}
~
ld}
~
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(   )r
   rZ   r-   load_payloadr   r   rN   rY   )r   r[   r'   r(   rW   last_exceptionsignerbase64dr$   r1   errs              r   loadszTimedSerializer.loads   s     qM))$/ 	%F%%+]]w &3 &" ++G4#"I--	%$ gglN33 $   %!$%s   -A2A22BBBc                 .    | j                  ||d|i      S )Nr'   )load_kwargs)_loads_unsafe_impl)r   r[   r'   rW   s       r   loads_unsafezTimedSerializer.loads_unsafe   s!     &&q$Y<P&QQr   r*   )NFN)NN)rE   rF   rG   rH   r   rV   rN   Type__annotations___t_opt_str_bytesIteratorrZ   rI   rM   rP   Anyrb   rO   rf   rR   rS   s   @r   rU   rU      s     0?NBGGO,> (,U$U	_	%U #!&!%#4#4 #4 	#4
 #4 
#4P #!%	RR R 	R
 
$,	Rr   rU   ) r   rK   rN   r   r   encodingr   r   r   r	   r
   r@   r   r   r   
serializerr   r_   r   rQ   r9   rJ   rI   Optionalri   r   rM   TYPE_CHECKINGtyping_extensions_ter   rU   r+   r   r   <module>rr      s         # # " "    ! ! " xxU
#;;|, [[
#Sf Sl:Rj :Rr   