o
    ä›eh%  ã                   @   s>  d Z ddlZddlZddlZddlZddlZ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 ddlmZ ddlmZ ed	ƒZd
ZG dd„ deƒZG dd„ deƒZdd„ Zdd„ Zdd„ Zdd„ Zd+dd„Zdd„ Zd,dd„ZG dd „ d ƒZ dd!e d"fd#d$„Z!dd!e ddfd%d&„Z"G d'd(„ d(ƒZ#G d)d*„ d*e#ƒZ$dS )-a_  
Functions for creating and restoring url-safe signed JSON objects.

The format used looks like this:

>>> signing.dumps("hello")
'ImhlbGxvIg:1QaUZC:YIye-ze3TTx7gtSv422nZA4sgmk'

There are two components here, separated by a ':'. The first component is a
URLsafe base64 encoded JSON of the object passed to dumps(). The second
component is a base64 encoded hmac/SHA-256 hash of "$first_component:$secret"

signing.loads(s) checks the signature and returns the deserialized object.
If the signature fails, a BadSignature exception is raised.

>>> signing.loads("ImhlbGxvIg:1QaUZC:YIye-ze3TTx7gtSv422nZA4sgmk")
'hello'
>>> signing.loads("ImhlbGxvIg:1QaUZC:YIye-ze3TTx7gtSv42-modified")
...
BadSignature: Signature "ImhlbGxvIg:1QaUZC:YIye-ze3TTx7gtSv42-modified" does not match

You can optionally compress the JSON prior to base64 encoding it to save
space, using the compress=True argument. This checks if compression actually
helps and only applies compression if the result is a shorter string:

>>> signing.dumps(list(range(1, 20)), compress=True)
'.eJwFwcERACAIwLCF-rCiILN47r-GyZVJsNgkxaFxoDgxcOHGxMKD_T7vhAml:1QaUaL:BA0thEZrp4FQVXIXuOvYJtLJSrQ'

The fact that the string is compressed is signalled by the prefixed '.' at the
start of the base64 JSON.

There are 65 url-safe characters: the 64 used by url-safe base64 and the ':'.
These functions make use of all of them.
é    N)Úsettings)Úconstant_time_compareÚsalted_hmac)ÚRemovedInDjango51Warning©Úforce_bytes)Úimport_string)Ú_lazy_re_compilez^[A-z0-9-_=]*$Ú>0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyzc                   @   ó   e Zd ZdZdS )ÚBadSignaturezSignature does not match.N©Ú__name__Ú
__module__Ú__qualname__Ú__doc__© r   r   úF/var/www/html/venv/lib/python3.10/site-packages/django/core/signing.pyr   6   ó    r   c                   @   r   )ÚSignatureExpiredz3Signature timestamp is older than required max_age.Nr   r   r   r   r   r   <   r   r   c                 C   sZ   | dkrdS | dk rdnd}t | ƒ} d}| dkr)t| dƒ\} }t| | }| dks|| S )Nr   Ú0ú-Ú é>   )ÚabsÚdivmodÚBASE62_ALPHABET)ÚsÚsignÚencodedÚ	remainderr   r   r   Ú
b62_encodeB   s   þr!   c                 C   sT   | dkrdS d}| d dkr| dd … } d}d}| D ]}|d t  |¡ }q|| S )Nr   r   é   r   éÿÿÿÿr   )r   Úindex)r   r   ÚdecodedÚdigitr   r   r   Ú
b62_decodeN   s   r'   c                 C   s   t  | ¡ d¡S )Nó   =)Úbase64Úurlsafe_b64encodeÚstrip)r   r   r   r   Ú
b64_encode[   ó   r,   c                 C   s    dt | ƒ d  }t | | ¡S )Nr(   é   )Úlenr)   Úurlsafe_b64decode)r   Úpadr   r   r   Ú
b64_decode_   s   r2   Úsha1c                 C   s   t t| |||d� ¡ ƒ ¡ S )N©Ú	algorithm)r,   r   ÚdigestÚdecode)ÚsaltÚvalueÚkeyr5   r   r   r   Úbase64_hmacd   s
   ÿþr;   c                 C   s   dt | ƒ S )Ns   django.http.cookiesr   )r:   r   r   r   Ú_cookie_signer_keyj   s   r<   ú%django.core.signing.get_cookie_signerc                 C   s&   t tjƒ}|ttjƒtttjƒ| d�S )N)r:   Úfallback_keysr8   )r   r   ÚSIGNING_BACKENDr<   Ú
SECRET_KEYÚmapÚSECRET_KEY_FALLBACKS)r8   ÚSignerr   r   r   Úget_cookie_signero   s   

ýrD   c                   @   s    e Zd ZdZdd„ Zdd„ ZdS )ÚJSONSerializerzW
    Simple wrapper around json to be used in signing.dumps and
    signing.loads.
    c                 C   s   t j|dd� d¡S )N)ú,ú:)Ú
separatorsúlatin-1)ÚjsonÚdumpsÚencode)ÚselfÚobjr   r   r   rK   ~   s   zJSONSerializer.dumpsc                 C   s   t  | d¡¡S )NrI   )rJ   Úloadsr7   )rM   Údatar   r   r   rO   �   r-   zJSONSerializer.loadsN)r   r   r   r   rK   rO   r   r   r   r   rE   x   s    rE   zdjango.core.signingFc                 C   s   t ||d�j| ||d�S )a½  
    Return URL-safe, hmac signed base64 compressed JSON string. If key is
    None, use settings.SECRET_KEY instead. The hmac algorithm is the default
    Signer algorithm.

    If compress is True (not the default), check if compressing using zlib can
    save some space. Prepend a '.' to signify compression. This is included
    in the signature, to protect against zip bombs.

    Salt can be used to namespace the hash, so that a signed string is
    only valid for a given namespace. Leaving this at the default
    value or re-using a salt value across different parts of your
    application without good cause is a security risk.

    The serializer is expected to return a bytestring.
    )r:   r8   )Ú
serializerÚcompress)ÚTimestampSignerÚsign_object)rN   r:   r8   rQ   rR   r   r   r   rK   …   s   ÿrK   c                 C   s   t |||d�j| ||d�S )z|
    Reverse of dumps(), raise BadSignature if signature fails.

    The serializer is expected to accept a bytestring.
    )r:   r8   r>   )rQ   Úmax_age)rS   Úunsign_object)r   r:   r8   rQ   rU   r>   r   r   r   rO   �   s   ÿûrO   c                   @   sV   e Zd Zddddddœdd„Zddd„Zdd	„ Zd
d„ Zedfdd„Zefdd„Z	dS )rC   NrG   ©r:   Úsepr8   r5   r>   c          	      G   s¶   |pt j| _|d ur|nt j| _|| _|pd| jj| jjf | _	|p#d| _
|rMtjd| jj› d�tdd� t|g d¢ƒD ]\}}|sF|dkrLt| ||ƒ q<t | j¡rYtd	| ƒ‚d S )
Nz%s.%sÚsha256z Passing positional arguments to z is deprecated.é   )Ú
stacklevelrW   rX   zJUnsafe Signer separator: %r (cannot be empty or consist of only A-z0-9-_=))r   r@   r:   rB   r>   rX   Ú	__class__r   r   r8   r5   ÚwarningsÚwarnr   ÚzipÚsetattrÚ_SEP_UNSAFEÚmatchÚ
ValueError)	rM   r:   rX   r8   r5   r>   ÚargsÚargÚattrr   r   r   Ú__init__¸   s<   	ÿýþ
üÿ€ÿÿÿzSigner.__init__c                 C   s"   |p| j }t| jd ||| jd�S )NÚsignerr4   )r:   r;   r8   r5   )rM   r9   r:   r   r   r   Ú	signatureà   s   
zSigner.signaturec                 C   s   d|| j |  |¡f S ©Nz%s%s%s)rX   ri   ©rM   r9   r   r   r   r   ä   s   zSigner.signc                 C   sd   | j |vrtd| j  ƒ‚| | j d¡\}}| jg| j¢D ]}t||  ||¡ƒr+|  S qtd| ƒ‚)NzNo "%s" found in valuer"   zSignature "%s" does not match)rX   r   Úrsplitr:   r>   r   ri   )rM   Úsigned_valuer9   Úsigr:   r   r   r   Úunsignç   s   
ÿzSigner.unsignFc                 C   s\   |ƒ   |¡}d}|rt |¡}t|ƒt|ƒd k r|}d}t|ƒ ¡ }|r)d| }|  |¡S )ae  
        Return URL-safe, hmac signed base64 compressed JSON string.

        If compress is True (not the default), check if compressing using zlib
        can save some space. Prepend a '.' to signify compression. This is
        included in the signature, to protect against zip bombs.

        The serializer is expected to return a bytestring.
        Fr"   TÚ.)rK   ÚzlibrR   r/   r,   r7   r   )rM   rN   rQ   rR   rP   Úis_compressedÚ
compressedÚbase64dr   r   r   rT   ð   s   


zSigner.sign_objectc                 K   sX   | j |fi |¤Ž ¡ }|d d… dk}|r|dd … }t|ƒ}|r&t |¡}|ƒ  |¡S )Nr"   ó   .)ro   rL   r2   rq   Ú
decompressrO   )rM   Ú
signed_objrQ   Úkwargsrt   rv   rP   r   r   r   rV   	  s   
zSigner.unsign_object©N)
r   r   r   rg   ri   r   ro   rE   rT   rV   r   r   r   r   rC   ³   s    ù
(	rC   c                       s2   e Zd Zdd„ Z‡ fdd„Zd‡ fdd„	Z‡  ZS )	rS   c                 C   s   t tt ¡ ƒƒS ry   )r!   ÚintÚtime)rM   r   r   r   Ú	timestamp  r-   zTimestampSigner.timestampc                    s    d|| j |  ¡ f }tƒ  |¡S rj   )rX   r|   Úsuperr   rk   ©r\   r   r   r     s   zTimestampSigner.signNc                    sj   t ƒ  |¡}| | jd¡\}}t|ƒ}|dur3t|tjƒr!| ¡ }t	 	¡ | }||kr3t
d||f ƒ‚|S )zk
        Retrieve original value and check it wasn't signed more
        than max_age seconds ago.
        r"   NzSignature age %s > %s seconds)r}   ro   rl   rX   r'   Ú
isinstanceÚdatetimeÚ	timedeltaÚtotal_secondsr{   r   )rM   r9   rU   Úresultr|   Úager~   r   r   ro     s   zTimestampSigner.unsignry   )r   r   r   r|   r   ro   Ú__classcell__r   r   r~   r   rS     s    rS   )r3   )r=   )%r   r)   r€   rJ   r{   r]   rq   Údjango.confr   Údjango.utils.cryptor   r   Údjango.utils.deprecationr   Údjango.utils.encodingr   Údjango.utils.module_loadingr   Údjango.utils.regex_helperr	   ra   r   Ú	Exceptionr   r   r!   r'   r,   r2   r;   r<   rD   rE   rK   rO   rC   rS   r   r   r   r   Ú<module>   sF    #

	
ÿ
úd