a
    x=ic                     @   s   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	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 d dlmZmZmZmZmZ dd Zeeede edddZedddZddddZ ddddZ!dS )    N)settings)PermissionDeniedSuspiciousOperation)BadSignatureSigner)HttpResponse)
cache_page)	condition)	constants)make_qr_code_image)QRCodeOptions)get_url_protection_optionsget_qr_url_protection_tokenqr_code_etagqr_code_last_modified!allows_external_request_from_userc                  C   s   dd } | S )z}
    Decorator that caches the requested page if a settings named 'QR_CODE_CACHE_ALIAS' exists and is not empty or None.
    c                    s   t   fdd}|S )Nc                    s   t | jdddk}|rttdrtjrtjtj d }d| jdpNtj	| j
j}t|tj|d | g|R i |}n | g|R i |}|S )Ncache_enabled   QR_CODE_CACHE_ALIASTIMEOUTztoken={}.user_pk={}Zurl_signature_enabled)cache
key_prefix)intGETgethasattrr   r   ZCACHESformatr
   ZDEFAULT_URL_SIGNATURE_ENABLEDuserpkr   )requestZ	view_argsZview_kwargsr   timeoutr   response	view_func ^/home/droni/.local/share/virtualenvs/DPS-5Je3_V2c/lib/python3.9/site-packages/qr_code/views.py_wrapped_view   s     z7cache_qr_code.<locals>.decorator.<locals>._wrapped_view)	functoolswraps)r#   r&   r$   r"   r%   	decorator   s    z cache_qr_code.<locals>.decoratorr$   )r)   r$   r$   r%   cache_qr_code   s    r*   )Z	etag_funcZlast_modified_func)returnc              	   C   s  t | }t| | d}d| jv rXzt| jdd}W q tjyT   tdY q0 nd| jv rzt	| jdd}W q t
tfy   tdY q0 nXz"t| jdd	d
}d}W n4 tjy   tdY n ty   tdY n0 t|||d}t||jdkrdnddS )u  Serve an image that represents the requested QR code.

    IMPORTANT NOTE: Each boolean option mentioned below (value True/False) must be passed as `<option name>=1` for `True`, respectively `<option name>=0` for `False`.

    You may pass any argument available for :py:func:`qr_code.qrcode.utils.QRCodeOptions.__init__` to adjust the appearance of the returned QR code. The arguments must be passed as query
    string arguments in the URL. Additionally, the following arguments are available:
        * cache_enabled: boolean – Tells the generator to work around the caching mechanism if it is empty (default: True/undefined).
        * token: str – By default, the application only serves QR code images for authenticated URLs (requests generated from your application and addressed to your application).
            The authentication uses a HMAC to sign the request query arguments. The authentication code is passed as a query argument named `token` which is automatically generated
            by `qr_url_from_text` or `qr_url_from_data`.
    Fbytes    zInvalid base64 encoded data.r   NzInvalid integer value.text zutf-8TzInvalid base64 encoded text.zInvalid UTF-8 encoded text.)qr_code_options
force_textsvgzimage/svg+xmlz	image/png)contentcontent_type)get_qr_code_option_from_requestcheck_image_access_permissionr   base64	b64decoder   binasciiErrorr   r   
ValueError	TypeErrordecodeUnicodeDecodeErrorr   r   Zimage_format)r   r0   r1   dataZimgr$   r$   r%   serve_qr_code_image6   s,    


r@   c                 C   sv   | j  }dD ]}||d  qt|dddk|d< t|dddk|d< t|dddk|d< tf i |S )N)r,   r.   r   tokenr   micror   r   ZeciZboost_error)r   dictpopr   r   r   )r   Zrequest_querykeyr$   r$   r%   r5   ^   s    
r5   c                 C   s4   | j dd}|rt|| nt| js0tddS )zNHandle image access protection (we do not allow external requests for anyone).rA   r/   z+You are not allowed to access this QR code.N)r   r   check_url_signature_tokenr   r   r   )r   r0   rA   r$   r$   r%   r6   i   s
    
r6   c                 C   sp   t  }t|tj |tj d}z2||}|dd }t| ||krNtdW n t	yj   tdY n0 d S )N)rE   salt.z.Request query does not match protection token.zWrong token signature.)
r   r   r
   ZSIGNING_KEYZSIGNING_SALTZunsignsplitr   r   r   )r0   rA   Zurl_protection_optionsZsignerZurl_protection_stringZrandom_tokenr$   r$   r%   rF   s   s    
rF   )"r7   r9   r'   Zdjango.confr   Zdjango.core.exceptionsr   r   Zdjango.core.signingr   r   Zdjango.httpr   Zdjango.views.decorators.cacher   Zdjango.views.decorators.httpr	   Zqr_code.qrcoder
   Zqr_code.qrcode.makerr   Zqr_code.qrcode.utilsr   Zqr_code.qrcode.server   r   r   r   r   r*   r@   r5   r6   rF   r$   r$   r$   r%   <module>   s&   	
&
