o
    uvXj„  ã                   @   sˆ   d dl Z d dlmZ d dlmZmZ d dlmZ d dlm	Z	m
Z
 d dlmZmZ G dd„ de jƒZG d	d
„ d
e jƒZG dd„ deƒZdS )é    N)Úsettings)ÚBaseStorageÚMessage)ÚSimpleCookie)Úconstant_time_compareÚsalted_hmac)ÚSafeDataÚ	mark_safec                       s$   e Zd ZdZdZ‡ fdd„Z‡  ZS )ÚMessageEncoderzI
    Compactly serialize instances of the ``Message`` class as JSON.
    Ú__json_messagec                    sR   t |tƒr#t |jtƒrdnd}| j||j|jg}|jr!| |j¡ |S tƒ  	|¡S )Né   r   )
Ú
isinstancer   Úmessager   Úmessage_keyÚlevelÚ
extra_tagsÚappendÚsuperÚdefault)ÚselfÚobjÚis_safedatar   ©Ú	__class__© úc/var/www/html/myproject/venv/lib/python3.10/site-packages/django/contrib/messages/storage/cookie.pyr      s   
zMessageEncoder.default)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   Ú__classcell__r   r   r   r   r
   
   s    r
   c                       s(   e Zd ZdZdd„ Z‡ fdd„Z‡  ZS )ÚMessageDecoderzE
    Decode JSON that includes serialized ``Message`` instances.
    c                    s–   t |tƒr9|r9|d tjkr0t|ƒdkrt|dd … Ž S |d r(t|d ƒ|d< t|dd … Ž S ‡ fdd„|D ƒS t |tƒrI‡ fdd„| ¡ D ƒS |S )	Nr   é   r   é   c                    s   g | ]}ˆ   |¡‘qS r   ©Úprocess_messages)Ú.0Úitem©r   r   r   Ú
<listcomp>)   s    z3MessageDecoder.process_messages.<locals>.<listcomp>c                    s   i | ]
\}}|ˆ   |¡“qS r   r$   )r&   ÚkeyÚvaluer(   r   r   Ú
<dictcomp>+   s    ÿz3MessageDecoder.process_messages.<locals>.<dictcomp>)	r   Úlistr
   r   Úlenr   r	   ÚdictÚitems)r   r   r   r(   r   r%       s   

ÿzMessageDecoder.process_messagesc                    s   t ƒ j|fi |¤Ž}|  |¡S )N)r   Údecoder%   )r   ÚsÚkwargsÚdecodedr   r   r   r1   /   s   
zMessageDecoder.decode)r   r   r   r   r%   r1   r    r   r   r   r   r!      s    r!   c                   @   sP   e Zd ZdZdZdZdZdd„ Zdd„ Zdd
d„Z	dd„ Z
ddd„Zdd„ ZdS )ÚCookieStoragez%
    Store messages in a cookie.
    Úmessagesi   Ú__messagesnotfinished__c                 O   sF   | j j | j¡}|  |¡}|o|d | jk }|r|s| ¡  ||fS )a
  
        Retrieve a list of messages from the messages cookie. If the
        not_finished sentinel value is found at the end of the message list,
        remove it and return a result indicating that not all messages were
        retrieved by this storage.
        éÿÿÿÿ)ÚrequestÚCOOKIESÚgetÚcookie_nameÚ_decodeÚnot_finishedÚpop)r   Úargsr3   Údatar6   Úall_retrievedr   r   r   Ú_get?   s   
zCookieStorage._getc                 C   sF   |r|j | j|tjtjpdtjpdtjd� dS |j| jtjd� dS )zz
        Either set the cookie with the encoded data if there is any data to
        store, or delete the cookie.
        N)ÚdomainÚsecureÚhttponlyÚsamesite)rD   )Ú
set_cookier<   r   ÚSESSION_COOKIE_DOMAINÚSESSION_COOKIE_SECUREÚSESSION_COOKIE_HTTPONLYÚSESSION_COOKIE_SAMESITEÚdelete_cookie)r   Úencoded_dataÚresponser   r   r   Ú_update_cookieN   s   
ûzCookieStorage._update_cookieTc           	         s–   g }|   |¡}| jrCtƒ ‰ ‡ fdd„}|rC||ƒ| jkrC|r'| | d¡¡ n| d| ¡ ¡ | j || jg |d�}|rC||ƒ| jks|  ||¡ |S )aT  
        Store the messages to a cookie and return a list of any messages which
        could not be stored.

        If the encoded data is larger than ``max_cookie_size``, remove
        messages until the data fits (these are the messages which are
        returned), and add the not_finished sentinel value to indicate as much.
        c                    s   t ˆ  | ¡d ƒS )Nr   )r.   Úvalue_encode)Úval©Úcookier   r   Ústored_lengthn   s   z+CookieStorage._store.<locals>.stored_lengthr   )Úencode_empty)Ú_encodeÚmax_cookie_sizer   r   r?   Úinsertr>   rP   )	r   r6   rO   Úremove_oldestr@   r3   Úunstored_messagesrN   rU   r   rS   r   Ú_store^   s   	
ÿûzCookieStorage._storec                 C   s   d}t ||ƒ ¡ S )zŸ
        Create an HMAC/SHA1 hash based on the value and the project setting's
        SECRET_KEY, modified to make it unique for the present purpose.
        zdjango.contrib.messages)r   Ú	hexdigest)r   r+   Úkey_saltr   r   r   Ú_hash{   s   zCookieStorage._hashFc                 C   s2   |s|rt dd�}| |¡}d|  |¡|f S dS )a  
        Return an encoded version of the messages list which can be stored as
        plain text.

        Since the data will be retrieved from the client-side, the encoded data
        also contains a hash to ensure that the data was not tampered with.
        )ú,ú:)Ú
separatorsz%s$%sN)r
   Úencoder_   )r   r6   rV   Úencoderr+   r   r   r   rW   ƒ   s
   

ýzCookieStorage._encodec                 C   sh   |sdS |  dd¡}t|ƒdkr/|\}}t||  |¡ƒr/ztj|td�W S  tjy.   Y nw d| _dS )zÄ
        Safely decode an encoded text stream back into a list of messages.

        If the encoded text stream contained an invalid hash or was in an
        invalid format, return None.
        Nú$r   r#   )ÚclsT)	Úsplitr.   r   r_   ÚjsonÚloadsr!   ÚJSONDecodeErrorÚused)r   rA   ÚbitsÚhashr+   r   r   r   r=   �   s   ÿzCookieStorage._decodeN)T)F)r   r   r   r   r<   rX   r>   rC   rP   r\   r_   rW   r=   r   r   r   r   r5   4   s    

r5   )rh   Údjango.confr   Ú$django.contrib.messages.storage.baser   r   Údjango.httpr   Údjango.utils.cryptor   r   Údjango.utils.safestringr   r	   ÚJSONEncoderr
   ÚJSONDecoderr!   r5   r   r   r   r   Ú<module>   s    