o
    b                     @   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m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 ed	ZG d
d deZG dd deZdd Zdd Zd$ddZd%ddZG dd dZddedfddZddedfddZG d d! d!ZG d"d# d#eZdS )&ae  
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/SHA1 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-ze3TTx7gtSv422nZA4sgmk-modified")
...
BadSignature: Signature failed: ImhlbGxvIg:1QaUZC:YIye-ze3TTx7gtSv422nZA4sgmk-modified

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)baseconv)constant_time_comparesalted_hmac)force_bytes)import_string)_lazy_re_compilez^[A-z0-9-_=]*$c                   @      e Zd ZdZdS )BadSignaturezSignature does not match.N__name__
__module____qualname____doc__ r   r   R/var/www/secure340b-portal/env/lib/python3.10/site-packages/django/core/signing.pyr
   4       r
   c                   @   r	   )SignatureExpiredz3Signature timestamp is older than required max_age.Nr   r   r   r   r   r   9   r   r   c                 C   s   t | dS )N   =)base64urlsafe_b64encodestrip)sr   r   r   
b64_encode>      r   c                 C   s    dt |  d  }t| | S )Nr      )lenr   urlsafe_b64decode)r   padr   r   r   
b64_decodeB   s   r   sha1c                 C   s   t t| |||d  S )N	algorithm)r   r   digestdecode)saltvaluekeyr"   r   r   r   base64_hmacG   s   r(   %django.core.signing.get_cookie_signerc                 C   s$   t tj}ttj}|d| | dS )Ns   django.http.cookiesr%   )r   r   ZSIGNING_BACKENDr   
SECRET_KEY)r%   Signerr'   r   r   r   get_cookie_signerK   s   

r-   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   r4   V      zJSONSerializer.dumpsc                 C   s   t |dS )Nr2   )r3   loadsr$   )r6   datar   r   r   r9   Y   r   zJSONSerializer.loadsN)r   r   r   r   r4   r9   r   r   r   r   r.   Q   s    r.   zdjango.core.signingFc                 C      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*   )
serializercompress)TimestampSignersign_object)r7   r'   r%   r<   r=   r   r   r   r4   ]   s   r4   c                 C   r;   )z|
    Reverse of dumps(), raise BadSignature if signature fails.

    The serializer is expected to accept a bytestring.
    r*   )r<   max_age)r>   unsign_object)r   r'   r%   r<   r@   r   r   r   r9   q   s   r9   c                   @   sT   e Zd ZdZdddZdd Zdd	 Zd
d Zdd Ze	dfddZ
e	fddZdS )r,   r    Nr0   c                 C   sT   |pt j| _|| _t| jrtd| |p d| jj| jj	f | _
|p&t j| _d S )NzJUnsafe Signer separator: %r (cannot be empty or consist of only A-z0-9-_=)z%s.%s)r   r+   r'   sep_SEP_UNSAFEmatch
ValueError	__class__r   r   r%   ZDEFAULT_HASHING_ALGORITHMr"   )r6   r'   rB   r%   r"   r   r   r   __init__~   s   zSigner.__init__c                 C      t | jd || j| jdS NZsignerr!   )r(   r%   r'   r"   r6   r&   r   r   r   	signature   s   zSigner.signaturec                 C   rH   rI   )r(   r%   r'   legacy_algorithmrJ   r   r   r   _legacy_signature   s   zSigner._legacy_signaturec                 C   s   d|| j | |f S Nz%s%s%s)rB   rK   rJ   r   r   r   sign   s   zSigner.signc                 C   s`   | j |vrtd| j  || j d\}}t|| |s(| jr*t|| |r*|S td| )NzNo "%s" found in value   zSignature "%s" does not match)rB   r
   rsplitr   rK   rL   rM   )r6   Zsigned_valuer&   sigr   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.
        FrP   T.)r4   zlibr=   r   r   r$   rO   )r6   r7   r<   r=   r:   Zis_compressed
compressedbase64dr   r   r   r?      s   


zSigner.sign_objectc                 K   sX   | j |fi | }|d d dk}|r|dd  }t|}|r&t|}| |S )NrP      .)rS   r5   r   rU   
decompressr9   )r6   Z
signed_objr<   kwargsrW   rY   r:   r   r   r   rA      s   
zSigner.unsign_object)Nr0   NN)r   r   r   rL   rG   rK   rM   rO   rS   r.   r?   rA   r   r   r   r   r,   z   s    
r,   c                       s2   e Zd Zdd Z fddZd fdd	Z  ZS )	r>   c                 C   s   t jtt S N)r   base62r5   inttime)r6   r   r   r   	timestamp   r8   zTimestampSigner.timestampc                    s    d|| j |  f }t |S rN   )rB   r_   superrO   rJ   rF   r   r   rO      s   zTimestampSigner.signNc                    sn   t  |}|| jd\}}tj|}|dur5t|tj	r#|
 }t | }||kr5td||f |S )zk
        Retrieve original value and check it wasn't signed more
        than max_age seconds ago.
        rP   NzSignature age %s > %s seconds)r`   rS   rQ   rB   r   r\   r$   
isinstancedatetime	timedeltatotal_secondsr^   r   )r6   r&   r@   resultr_   Zagera   r   r   rS      s   
zTimestampSigner.unsignr[   )r   r   r   r_   rO   rS   __classcell__r   r   ra   r   r>      s    r>   )r    )r)   ) r   r   rc   r3   r^   rU   Zdjango.confr   Zdjango.utilsr   Zdjango.utils.cryptor   r   Zdjango.utils.encodingr   Zdjango.utils.module_loadingr   Zdjango.utils.regex_helperr   rC   	Exceptionr
   r   r   r   r(   r-   r.   r4   r9   r,   r>   r   r   r   r   <module>   s0    #

	O