o
    þÞh>B  ã                   @   s¦   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 ejej ZG d	d
„ d
eƒZG dd„ deƒZG dd„ dƒZdS )é    N)ÚdatetimeÚ	timedelta)Úsync_to_async)Úsettings)Úsigning)Útimezone)Úget_random_string)Úimport_stringc                   @   ó   e Zd ZdZdS )Ú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__© r   r   úe/var/www/html/premium_crap/venv/lib/python3.10/site-packages/django/contrib/sessions/backends/base.pyr      s    r   c                   @   r
   )ÚUpdateErrorzF
    Occurs if Django tries to update a session that was deleted.
    Nr   r   r   r   r   r      s    r   c                   @   sv  e Zd ZdZdZdZeƒ ZdŒdd„Zdd„ Z	d	d
„ Z
dd„ Zdd„ Zdd„ Zedd„ ƒZdŒdd„ZdŒdd„Zefdd„Zefdd„Zdd„ Zdd„ Zdd „ Zd!d"„ Zd#d$„ Zd%d&„ Zd'd(„ Zd)d*„ Zd+d,„ Zd-d.„ Zd/d0„ Zd1d2„ Zd3d4„ Z d5d6„ Z!d7d8„ Z"d9d:„ Z#d;d<„ Z$d=d>„ Z%d?d@„ Z&dAdB„ Z'dCdD„ Z(dEdF„ Z)dGdH„ Z*dIdJ„ Z+dKdL„ Z,dMdN„ Z-dOdP„ Z.dQdR„ Z/dSdT„ Z0ee/ƒZ1ee/e0ƒZ2d�dVdW„Z3d�dXdY„Z4ee3ƒZ5dZd[„ Z6d\d]„ Z7d^d_„ Z8d`da„ Z9dbdc„ Z:ddde„ Z;dfdg„ Z<dhdi„ Z=djdk„ Z>dldm„ Z?dndo„ Z@dpdq„ ZAdrds„ ZBdtdu„ ZCdvdw„ ZDdxdy„ ZEdzd{„ ZFd�d|d}„ZGd�d~d„ZHdŒd€d�„ZIdŒd‚dƒ„ZJd„d…„ ZKd†d‡„ ZLeMdˆd‰„ ƒZNeMdŠd‹„ ƒZOdS )ŽÚSessionBasez-
    Base class for all Session classes.
    Ú
testcookieÚworkedNc                 C   s"   || _ d| _d| _ttjƒ| _d S )NF)Ú_session_keyÚaccessedÚmodifiedr	   r   ÚSESSION_SERIALIZERÚ
serializer©ÚselfÚsession_keyr   r   r   Ú__init__-   s   zSessionBase.__init__c                 C   ó
   || j v S ©N©Ú_session©r   Úkeyr   r   r   Ú__contains__3   ó   
zSessionBase.__contains__c                 C   s
   | j | S r!   r"   r$   r   r   r   Ú__getitem__6   r'   zSessionBase.__getitem__c                 C   s   || j |< d| _d S ©NT©r#   r   ©r   r%   Úvaluer   r   r   Ú__setitem__9   s   

zSessionBase.__setitem__c                 Ã   s   �||   ¡ I d H |< d| _d S r)   )Ú_aget_sessionr   r+   r   r   r   Úaset=   s   €
zSessionBase.asetc                 C   s   | j |= d| _d S r)   r*   r$   r   r   r   Ú__delitem__A   s   
zSessionBase.__delitem__c                 C   s   d| j j S )Nzdjango.contrib.sessions.)Ú	__class__r   ©r   r   r   r   Úkey_saltE   s   zSessionBase.key_saltc                 C   s   | j  ||¡S r!   )r#   Úget©r   r%   Údefaultr   r   r   r4   I   s   zSessionBase.getc                 Ã   s   �|   ¡ I d H  ||¡S r!   )r.   r4   r5   r   r   r   ÚagetL   ó   €zSessionBase.agetc                 C   s:   | j p|| jv | _ || ju rdn|f}| jj|g|¢R Ž S ©Nr   )r   r#   Ú_SessionBase__not_givenÚpop©r   r%   r6   Úargsr   r   r   r;   O   s   zSessionBase.popc                 Ã   sL   �| j p||  ¡ I d H v | _ || ju rdn|f}|  ¡ I d H j|g|¢R Ž S r9   )r   r.   r:   r;   r<   r   r   r   ÚapopT   s   €zSessionBase.apopc                 C   s    || j v r
| j | S || |< |S r!   r"   r+   r   r   r   Ú
setdefaultY   s   

zSessionBase.setdefaultc                 Ã   s6   �|   ¡ I d H }||v r|| S |  ||¡I d H  |S r!   )r.   r/   )r   r%   r,   Úsessionr   r   r   Úasetdefault`   s   €zSessionBase.asetdefaultc                 C   s   | j | | j< d S r!   )ÚTEST_COOKIE_VALUEÚTEST_COOKIE_NAMEr2   r   r   r   Úset_test_cookieh   s   zSessionBase.set_test_cookiec                 Ã   s   �|   | j| j¡I d H  d S r!   )r/   rC   rB   r2   r   r   r   Úaset_test_cookiek   s   €zSessionBase.aset_test_cookiec                 C   s   |   | j¡| jkS r!   )r4   rC   rB   r2   r   r   r   Útest_cookie_workedn   s   zSessionBase.test_cookie_workedc                 Ã   s   �|   | j¡I d H | jkS r!   )r7   rC   rB   r2   r   r   r   Úatest_cookie_workedq   s   €zSessionBase.atest_cookie_workedc                 C   s   | | j = d S r!   )rC   r2   r   r   r   Údelete_test_cookiet   s   zSessionBase.delete_test_cookiec                 Ã   s   �|   ¡ I d H | j= d S r!   )r.   rC   r2   r   r   r   Úadelete_test_cookiew   r8   zSessionBase.adelete_test_cookiec                 C   s   t j|| j| jdd�S )zGReturn the given session dictionary serialized and encoded as a string.T)Úsaltr   Úcompress)r   Údumpsr3   r   )r   Úsession_dictr   r   r   Úencodez   s   üzSessionBase.encodec                 C   sT   zt j|| j| jd�W S  t jy    t d¡}| d¡ Y i S  ty)   Y i S w )N)rJ   r   z!django.security.SuspiciousSessionzSession data corrupted)	r   Úloadsr3   r   ÚBadSignatureÚloggingÚ	getLoggerÚwarningÚ	Exception)r   Úsession_dataÚloggerr   r   r   Údecodeƒ   s   
ÿ
üüzSessionBase.decodec                 C   s   | j  |¡ d| _d S r)   )r#   Úupdater   ©r   Údict_r   r   r   rX   ‘   s   
zSessionBase.updatec                 Ã   s    �|   ¡ I d H  |¡ d| _d S r)   )r.   rX   r   rY   r   r   r   Úaupdate•   s   €
zSessionBase.aupdatec                 C   r    r!   r"   r$   r   r   r   Úhas_key™   r'   zSessionBase.has_keyc                 Ã   s   �||   ¡ I d H v S r!   )r.   r$   r   r   r   Úahas_keyœ   ó   €zSessionBase.ahas_keyc                 C   ó
   | j  ¡ S r!   )r#   Úkeysr2   r   r   r   r`   Ÿ   r'   zSessionBase.keysc                 Ã   ó   �|   ¡ I d H  ¡ S r!   )r.   r`   r2   r   r   r   Úakeys¢   r^   zSessionBase.akeysc                 C   r_   r!   )r#   Úvaluesr2   r   r   r   rc   ¥   r'   zSessionBase.valuesc                 Ã   ra   r!   )r.   rc   r2   r   r   r   Úavalues¨   r^   zSessionBase.avaluesc                 C   r_   r!   )r#   Úitemsr2   r   r   r   re   «   r'   zSessionBase.itemsc                 Ã   ra   r!   )r.   re   r2   r   r   r   Úaitems®   r^   zSessionBase.aitemsc                 C   s   i | _ d| _d| _d S r)   )Ú_session_cacher   r   r2   r   r   r   Úclear±   s   
zSessionBase.clearc                 C   s(   z	| j  o| j W S  ty   Y dS w )zBReturn True when there is no session_key and the session is empty.T)r   rg   ÚAttributeErrorr2   r   r   r   Úis_empty¹   s
   ÿzSessionBase.is_emptyc                 C   s   	 t dtƒ}|  |¡s|S q)z)Return session key that isn't being used.Té    )r   ÚVALID_KEY_CHARSÚexistsr   r   r   r   Ú_get_new_session_keyÀ   s
   

ýz SessionBase._get_new_session_keyc                 Ã   s$   �	 t dtƒ}|  |¡I d H s|S q)NTrk   )r   rl   Úaexistsr   r   r   r   Ú_aget_new_session_keyÇ   s   €
ýz!SessionBase._aget_new_session_keyc                 C   s   | j d u r
|  ¡ | _ | j S r!   )r   rn   r2   r   r   r   Ú_get_or_create_session_keyÍ   s   

z&SessionBase._get_or_create_session_keyc                 Ã   s"   �| j d u r|  ¡ I d H | _ | j S r!   )r   rp   r2   r   r   r   Ú_aget_or_create_session_keyÒ   s   €
z'SessionBase._aget_or_create_session_keyc                 C   s   |o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$   r   r   r   Ú_validate_session_key×   s   z!SessionBase._validate_session_keyc                 C   s   | j S r!   )Ú_SessionBase__session_keyr2   r   r   r   Ú_get_session_keyÞ   ó   zSessionBase._get_session_keyc                 C   s   |   |¡r
|| _dS d| _dS )zV
        Validate session key on assignment. Invalid values will set to None.
        N)ru   rv   ©r   r,   r   r   r   Ú_set_session_keyá   s   


zSessionBase._set_session_keyFc                 C   sL   d| _ z| jW S  ty%   | jdu s|ri | _Y | jS |  ¡ | _Y | jS 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.
        TN)r   rg   ri   r   Úload©r   Úno_loadr   r   r   Ú_get_sessioní   s   ÿûzSessionBase._get_sessionc                 Ã   sT   �d| _ z| jW S  ty)   | jd u s|ri | _Y | jS |  ¡ I d H | _Y | jS w r)   )r   rg   ri   r   Úaloadr|   r   r   r   r.   ü   s   €ÿûzSessionBase._aget_sessionc                 C   s   t jS r!   )r   ÚSESSION_COOKIE_AGEr2   r   r   r   Úget_session_cookie_age	  rx   z"SessionBase.get_session_cookie_agec                 K   sœ   z|d }W n t y   t ¡ }Y nw z|d }W n t y(   |  d¡}Y nw |s/|  ¡ S t|ttfƒs8|S t|tƒrBt |¡}|| }|j	d |j
 S )zÕGet 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_expiryé€Q )ÚKeyErrorr   Únowr4   r�   Ú
isinstancer   ÚstrÚfromisoformatÚdaysÚseconds©r   Úkwargsr‚   rƒ   Údeltar   r   r   Úget_expiry_age  s$   ÿÿ

zSessionBase.get_expiry_agec                 Ë   s¤   �z|d }W n t y   t ¡ }Y nw z|d }W n t y,   |  d¡I d H }Y nw |s3|  ¡ S t|ttfƒs<|S t|tƒrFt |¡}|| }|j	d |j
 S )Nr‚   rƒ   r„   r…   )r†   r   r‡   r7   r�   rˆ   r   r‰   rŠ   r‹   rŒ   r�   r   r   r   Úaget_expiry_age'  s&   €ÿÿ

zSessionBase.aget_expiry_agec                 K   sŽ   z|d }W n t y   t ¡ }Y nw z|d }W n t y(   |  d¡}Y nw t|tƒr0|S t|tƒr:t |¡S |p?|  ¡ }|t	|d� S )zÔGet 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‡   r4   rˆ   r   r‰   rŠ   r�   r   ©r   rŽ   r‚   rƒ   r   r   r   Úget_expiry_date:  s    ÿÿ


zSessionBase.get_expiry_datec                 Ë   s–   �z|d }W n t y   t ¡ }Y nw z|d }W n t y,   |  d¡I d H }Y nw t|tƒr4|S t|tƒr>t |¡S |pC|  ¡ }|t	|d� S )Nr‚   rƒ   r„   r’   )
r†   r   r‡   r7   rˆ   r   r‰   rŠ   r�   r   r“   r   r   r   Úaget_expiry_dateQ  s"   €ÿÿ


zSessionBase.aget_expiry_datec                 C   s^   |du rz| d= W dS  t y   Y dS w t|tƒr t ¡ | }t|tƒr)| ¡ }|| d< dS )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†   rˆ   r   r   r‡   r   Ú	isoformatry   r   r   r   Ú
set_expiryb  s   þþ

zSessionBase.set_expiryc                 Ã   st   �|d u rz|   d¡I d H  W d S  ty   Y d S w t|tƒr&t ¡ | }t|tƒr/| ¡ }|  d|¡I d H  d S )Nr„   )	r>   r†   rˆ   r   r   r‡   r   r–   r/   ry   r   r   r   Úaset_expiry~  s   €þþ

zSessionBase.aset_expiryc                 C   s    |   d¡ }du rtjS |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„   Nr   )r4   r   ÚSESSION_EXPIRE_AT_BROWSER_CLOSE©r   rƒ   r   r   r   Úget_expire_at_browser_closeŒ  s   z'SessionBase.get_expire_at_browser_closec                 Ã   s(   �|   d¡I d H  }d u rtjS |dkS )Nr„   r   )r7   r   r™   rš   r   r   r   Úaget_expire_at_browser_close—  s   €z(SessionBase.aget_expire_at_browser_closec                 C   s   |   ¡  |  ¡  d| _dS )zc
        Remove the current session data from the database and regenerate the
        key.
        N)rh   Údeleter   r2   r   r   r   Úflushœ  s   
zSessionBase.flushc                 Ã   s"   �|   ¡  |  ¡ I d H  d | _d S r!   )rh   Úadeleter   r2   r   r   r   Úaflush¥  s   €
zSessionBase.aflushc                 C   s0   | j }| j}|  ¡  || _|r|  |¡ dS dS ©zU
        Create a new session key, while retaining the current session data.
        N)r#   r   Úcreaterg   r�   ©r   Údatar%   r   r   r   Ú	cycle_keyª  s   ÿzSessionBase.cycle_keyc                 Ã   sF   �|   ¡ I dH }| j}|  ¡ I dH  || _|r!|  |¡I dH  dS dS r¡   )r.   r   Úacreaterg   rŸ   r£   r   r   r   Ú
acycle_keyµ  s   €ÿzSessionBase.acycle_keyc                 C   ó   t dƒ‚)zF
        Return True if the given session_key already exists.
        z9subclasses of SessionBase must provide an exists() method©ÚNotImplementedErrorr   r   r   r   rm   Â  ó   ÿzSessionBase.existsc                 Ã   ó   �t | jƒ|ƒI d H S r!   )r   rm   r   r   r   r   ro   Ê  ó   €zSessionBase.aexistsc                 C   r¨   )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©   r2   r   r   r   r¢   Í  ó   ÿzSessionBase.createc                 Ã   ó   �t | jƒƒ I d H S r!   )r   r¢   r2   r   r   r   r¦   ×  r^   zSessionBase.acreatec                 C   r¨   )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_creater   r   r   ÚsaveÚ  r®   zSessionBase.savec                 Ã   r¬   r!   )r   r²   r°   r   r   r   Úasaveä  r­   zSessionBase.asavec                 C   r¨   )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   r   r   r   r�   ç  s   ÿzSessionBase.deletec                 Ã   r¬   r!   )r   r�   r   r   r   r   rŸ   ð  r­   zSessionBase.adeletec                 C   r¨   )z@
        Load the session data and return a dictionary.
        z6subclasses of SessionBase must provide a load() methodr©   r2   r   r   r   r{   ó  r«   zSessionBase.loadc                 Ã   r¯   r!   )r   r{   r2   r   r   r   r   û  r^   zSessionBase.aloadc                 C   r¨   )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©   ©Úclsr   r   r   Úclear_expiredþ  s   	zSessionBase.clear_expiredc                 Ã   r¯   r!   )r   r¶   r´   r   r   r   Úaclear_expired	  s   €zSessionBase.aclear_expiredr!   )F)Pr   r   r   r   rC   rB   Úobjectr:   r   r&   r(   r-   r/   r0   Úpropertyr3   r4   r7   r;   r>   r?   rA   rD   rE   rF   rG   rH   rI   rN   rW   rX   r[   r\   r]   r`   rb   rc   rd   re   rf   rh   rj   rn   rp   rq   rr   ru   rw   rz   r   r   r~   r.   r#   r�   r�   r‘   r”   r•   r—   r˜   r›   rœ   rž   r    r¥   r§   rm   ro   r¢   r¦   r²   r³   r�   rŸ   r{   r   Úclassmethodr¶   r·   r   r   r   r   r   #   sœ    



		


	





	

r   )rQ   Ústringr   r   Úasgiref.syncr   Údjango.confr   Údjango.corer   Údjango.utilsr   Údjango.utils.cryptor   Údjango.utils.module_loadingr	   Úascii_lowercaseÚdigitsrl   rT   r   r   r   r   r   r   r   Ú<module>   s    	