
    hL6                        d dl Z d dlZd dlZd dlZd dlmZ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mZmZ d d	lmZ d d
lmZ d dlmZ ej4                  ej6                  z   Z G d de      Z G d de      Z G d d      Z y)    N)datetime	timedelta)settings)SuspiciousSession)signing)SuspiciousOperation)timezone)constant_time_compareget_random_stringsalted_hmac)RemovedInDjango40Warning)import_string)LANGUAGE_SESSION_KEYc                       e Zd ZdZy)CreateErrorz
    Used internally as a consistent exception type to catch from save (see the
    docstring for SessionBase.save() for details).
    N__name__
__module____qualname____doc__     h/var/www/html/ranktracker/api/venv/lib/python3.12/site-packages/django/contrib/sessions/backends/base.pyr   r      s     	r   r   c                       e Zd ZdZy)UpdateErrorzF
    Occurs if Django tries to update a session that was deleted.
    Nr   r   r   r   r   r       s     	r   r   c                   |   e Zd ZdZdZdZ e       Zd0dZd Z	d Z
d Zd	 Zed
        Zd0dZefdZd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Z d Z!d Z"d Z#d  Z$d! Z% ee$      Z& ee$e%      Z'd1d"Z( ee(      Z)d# Z*d$ Z+d% Z,d& Z-d' Z.d( Z/d) Z0d* Z1d+ Z2d1d,Z3d0d-Z4d. Z5e6d/        Z7y)2SessionBasez-
    Base class for all Session classes.
    
testcookieworkedNc                 j    || _         d| _        d| _        t        t        j
                        | _        y )NF)_session_keyaccessedmodifiedr   r   SESSION_SERIALIZER
serializerselfsession_keys     r   __init__zSessionBase.__init__0   s*    ''(C(CDr   c                     || j                   v S N_sessionr'   keys     r   __contains__zSessionBase.__contains__6       dmm##r   c                 j    |t         k(  rt        j                  dt        d       | j                  |   S )NzThe user language will no longer be stored in request.session in Django 4.0. Read it from request.COOKIES[settings.LANGUAGE_COOKIE_NAME] instead.   )
stacklevel)r   warningswarnr   r-   r.   s     r   __getitem__zSessionBase.__getitem__9   s5    &&MMJ )Q	 }}S!!r   c                 0    || j                   |<   d| _        y NTr-   r#   r'   r/   values      r   __setitem__zSessionBase.__setitem__C   s    "cr   c                 ,    | j                   |= d| _        y r9   r:   r.   s     r   __delitem__zSessionBase.__delitem__G   s    MM#r   c                 4    d| j                   j                  z   S )Nzdjango.contrib.sessions.)	__class__r   r'   s    r   key_saltzSessionBase.key_saltK   s    )DNN,G,GGGr   c                 :    | j                   j                  ||      S r+   )r-   get)r'   r/   defaults      r   rE   zSessionBase.getO   s    }}  g..r   c                     | j                   xs || j                  v | _         || j                  u rdn|f} | j                  j                  |g| S )Nr   )r#   r-   _SessionBase__not_givenpop)r'   r/   rF   argss       r   rI   zSessionBase.popR   sM    =)= 0 00rwj t}}  ,t,,r   c                 l    || j                   v r| j                   |   S d| _        || j                   |<   |S r9   r:   r;   s      r   
setdefaultzSessionBase.setdefaultW   s7    $--==%% DM!&DMM#Lr   c                 6    | j                   | | j                  <   y r+   )TEST_COOKIE_VALUETEST_COOKIE_NAMErB   s    r   set_test_cookiezSessionBase.set_test_cookie_   s    &*&<&<T""#r   c                 R    | j                  | j                        | j                  k(  S r+   )rE   rO   rN   rB   s    r   test_cookie_workedzSessionBase.test_cookie_workedb   s"    xx--.$2H2HHHr   c                     | | j                   = y r+   )rO   rB   s    r   delete_test_cookiezSessionBase.delete_test_cookiee   s    &&'r   c                 h    d| j                   j                  z   }t        ||      j                         S )Nzdjango.contrib.sessions)rA   r   r   	hexdigest)r'   r<   rC   s      r   _hashzSessionBase._hashh   s,    ,t~~/F/FF8U+5577r   c                     t         j                  dk(  r| j                  |      S t        j                  || j
                  | j                  d      S )zGReturn the given session dictionary serialized and encoded as a string.sha1T)saltr%   compress)r   DEFAULT_HASHING_ALGORITHM_legacy_encoder   dumpsrC   r%   )r'   session_dicts     r   encodezSessionBase.encodem   sF     --7&&|44}}t}}
 	
r   c                 Z   	 t        j                  || j                  | j                        S # t         j                  $ rN 	 | j                  |      cY S # t        $ r- t        j                  d      }|j                  d       i cY cY S w xY wt        $ r | j                  |      cY S w xY w)N)rZ   r%   z!django.security.SuspiciousSessionSession data corrupted)
r   loadsrC   r%   BadSignature_legacy_decode	Exceptionlogging	getLoggerwarning)r'   session_dataloggers      r   decodezSessionBase.decodew   s    	5==DMMdoo^^ ## 	 **<88  **+NO78	  	5&&|44	5s3   +. B*AB*1BB*
BB*)B*c                     | j                         j                  |      }| j                  |      }t        j                  |j                         dz   |z         j                  d      S )N   :ascii)r%   r^   rW   base64	b64encoder`   rl   )r'   r_   
serializedhashs       r   r]   zSessionBase._legacy_encode   sS    __&,,\:
zz*% 4z ABII'RRr   c                    t        j                  |j                  d            }	 |j                  dd      \  }}| j	                  |      }t        |j                         |      st        d      | j                         j                  |      S # t        $ rb}t        |t              rFt        j                  d|j                  j                   z        }|j#                  t%        |             i cY d }~S d }~ww xY w)Nro   rn      rb   zdjango.security.%s)rp   	b64decoder`   splitrW   r
   rl   r   r%   rc   rf   
isinstancer   rg   rh   rA   r   ri   str)r'   rj   encoded_datars   rr   expected_hasherk   s           r   re   zSessionBase._legacy_decode   s    ''(;(;G(DE	+11$:D* JJz2M(F'(@AA(..z:: 	 !01 **+?!++BVBV+VWs1v&I	s   A)B 	C;AC60C;6C;c                 H    | j                   j                  |       d| _        y r9   )r-   updater#   )r'   dict_s     r   r~   zSessionBase.update   s    U#r   c                     || j                   v S r+   r,   r.   s     r   has_keyzSessionBase.has_key   r1   r   c                 6    | j                   j                         S r+   )r-   keysrB   s    r   r   zSessionBase.keys   s    }}!!##r   c                 6    | j                   j                         S r+   )r-   valuesrB   s    r   r   zSessionBase.values   s    }}##%%r   c                 6    | j                   j                         S r+   )r-   itemsrB   s    r   r   zSessionBase.items   s    }}""$$r   c                 .    i | _         d| _        d| _        y r9   )_session_cacher"   r#   rB   s    r   clearzSessionBase.clear   s     !r   c                 Z    	 | j                    xr | j                   S # t        $ r Y yw xY w)zBReturn True when there is no session_key and the session is empty.T)r!   r   AttributeErrorrB   s    r   is_emptyzSessionBase.is_empty   s6    	(((D1D1D-DD 		s    	**c                 L    	 t        dt              }| j                  |      s|S $)z)Return session key that isn't being used.    )r   VALID_KEY_CHARSexistsr&   s     r   _get_new_session_keyz SessionBase._get_new_session_key   s(    +B@K;;{+"" r   c                 \    | j                   | j                         | _         | j                   S r+   )r!   r   rB   s    r   _get_or_create_session_keyz&SessionBase._get_or_create_session_key   s+    $ $ 9 9 ;D   r   c                 &    |xr t        |      dk\  S )z
        Key must be truthy and at least 8 characters long. 8 characters is an
        arbitrary lower bound for some minimal key security.
           )lenr.   s     r   _validate_session_keyz!SessionBase._validate_session_key   s    
 $s3x1}$r   c                     | j                   S r+   )_SessionBase__session_keyrB   s    r   _get_session_keyzSessionBase._get_session_key   s    !!!r   c                 D    | j                  |      r|| _        yd| _        y)zV
        Validate session key on assignment. Invalid values will set to None.
        N)r   r   r'   r<   s     r   _set_session_keyzSessionBase._set_session_key   s"     %%e,!&D!%Dr   c                     d| _         	 | j                  S # t        $ r9 | j                  |ri | _        n| j	                         | _        Y | j                  S w xY w)z
        Lazily load session from storage (unless "no_load" is True, when only
        an empty dict is stored) and store it in the current instance.
        T)r"   r   r   r(   load)r'   no_loads     r   _get_sessionzSessionBase._get_session   s\    
 	2&&& 	2'7&(#&*iik#"""	2s    4AAc                 "    t         j                  S r+   )r   SESSION_COOKIE_AGErB   s    r   get_session_cookie_agez"SessionBase.get_session_cookie_age   s    ***r   c                 *   	 |d   }	 |d   }|s| j	                         S t        |t              s|S ||z
  }|j                  dz  |j                  z   S # t         $ r t        j                         }Y jw xY w# t         $ r | j                  d      }Y w xY w)zGet the number of seconds until the session expires.

        Optionally, this function accepts `modification` and `expiry` keyword
        arguments specifying the modification and expiry of the session.
        modificationexpiry_session_expiryiQ )	KeyErrorr	   nowrE   r   rx   r   daysseconds)r'   kwargsr   r   deltas        r   get_expiry_agezSessionBase.get_expiry_age   s    	*!.1L	1H%F ..00&(+M%zzE!EMM11  	*#<<>L	*  	1XX/0F	1s"   A A5 A21A25BBc                 
   	 |d   }	 |d   }t	        |t
              r|S |xs | j                         }|t        |      z   S # t         $ r t        j                         }Y Zw xY w# t         $ r | j                  d      }Y tw xY w)zGet session the expiry date (as a datetime object).

        Optionally, this function accepts `modification` and `expiry` keyword
        arguments specifying the modification and expiry of the session.
        r   r   r   )r   )r   r	   r   rE   rx   r   r   r   )r'   r   r   r   s       r   get_expiry_datezSessionBase.get_expiry_date  s    	*!.1L	1H%F fh'M84668i777  	*#<<>L	*
  	1XX/0F	1s"   A A% A"!A"%BBc                     |	 | d= yt        |t              rt        j                         |z   }|| d<   y# t         $ r Y yw xY w)a*  
        Set a custom expiration for the session. ``value`` can be an integer,
        a Python ``datetime`` or ``timedelta`` object or ``None``.

        If ``value`` is an integer, the session will expire after that many
        seconds of inactivity. If set to ``0`` then the session will expire on
        browser close.

        If ``value`` is a ``datetime`` or ``timedelta`` object, the session
        will expire at that specific future time.

        If ``value`` is ``None``, the session uses the global session expiry
        policy.
        Nr   )r   rx   r   r	   r   r   s     r   
set_expiryzSessionBase.set_expiry$  sX     =*+ eY'LLNU*E"'  s   5 	A Ac                 l    | j                  d      t        j                  S | j                  d      dk(  S )a  
        Return ``True`` if the session is set to expire when the browser
        closes, and ``False`` if there's an expiry date. Use
        ``get_expiry_date()`` or ``get_expiry_age()`` to find the actual expiry
        date/age, if there is one.
        r   r   )rE   r   SESSION_EXPIRE_AT_BROWSER_CLOSErB   s    r   get_expire_at_browser_closez'SessionBase.get_expire_at_browser_close>  s5     88%&.;;;xx)*a//r   c                 R    | j                          | j                          d| _        y)zc
        Remove the current session data from the database and regenerate the
        key.
        N)r   deleter!   rB   s    r   flushzSessionBase.flushI  s    
 	

 r   c                     | j                   }| j                  }| j                          || _        |r| j	                  |       yy)zU
        Create a new session key, while retaining the current session data.
        N)r-   r(   creater   r   )r'   datar/   s      r   	cycle_keyzSessionBase.cycle_keyR  s=     }}"KK r   c                     t        d      )zF
        Return True if the given session_key already exists.
        z9subclasses of SessionBase must provide an exists() methodNotImplementedErrorr&   s     r   r   zSessionBase.exists_  s     ""]^^r   c                     t        d      )z
        Create a new session instance. Guaranteed to create a new object with
        a unique key and will have saved the result once (with empty data)
        before the method returns.
        z8subclasses of SessionBase must provide a create() methodr   rB   s    r   r   zSessionBase.createe  s     ""\]]r   c                     t        d      )z
        Save the session data. If 'must_create' is True, create a new session
        object (or raise CreateError). Otherwise, only update an existing
        object and don't create one (raise UpdateError if needed).
        z6subclasses of SessionBase must provide a save() methodr   )r'   must_creates     r   savezSessionBase.savem  s     ""Z[[r   c                     t        d      )zx
        Delete the session data under this key. If the key is None, use the
        current session key value.
        z8subclasses of SessionBase must provide a delete() methodr   r&   s     r   r   zSessionBase.deleteu  s    
 ""\]]r   c                     t        d      )z@
        Load the session data and return a dictionary.
        z6subclasses of SessionBase must provide a load() methodr   rB   s    r   r   zSessionBase.load|  s     ""Z[[r   c                     t        d      )a  
        Remove expired sessions from the session store.

        If this operation isn't possible on a given backend, it should raise
        NotImplementedError. If it isn't necessary, because the backend has
        a built-in expiration mechanism, it should be a no-op.
        z.This backend does not support clear_expired().r   )clss    r   clear_expiredzSessionBase.clear_expired  s     ""RSSr   r+   )F)8r   r   r   r   rO   rN   objectrH   r)   r0   r7   r=   r?   propertyrC   rE   rI   rL   rP   rR   rT   rW   r`   rl   r]   re   r~   r   r   r   r   r   r   r   r   r   r   r   r(   r!   r   r-   r   r   r   r   r   r   r   r   r   r   r   r   classmethodr   r   r   r   r   r   '   sI    $ (KE$" H H/  + -
=I(8

5"S&$$&%#!
%"& +,K,.>?L# %H+228*(4	0!	_^\^\ T Tr   r   )!rp   rg   stringr5   r   r   django.confr   "django.contrib.sessions.exceptionsr   django.corer   django.core.exceptionsr   django.utilsr	   django.utils.cryptor
   r   r   django.utils.deprecationr   django.utils.module_loadingr   django.utils.translationr   ascii_lowercasedigitsr   rf   r   r   r   r   r   r   <module>r      st        (   @  6 !  > 5 9 ((6==8	) 		) 	dT dTr   