HEX
Server: Apache/2.4.52 (Ubuntu)
System: Linux spn-python 5.15.0-89-generic #99-Ubuntu SMP Mon Oct 30 20:42:41 UTC 2023 x86_64
User: arjun (1000)
PHP: 8.1.2-1ubuntu2.20
Disabled: NONE
Upload Files
File: //home/arjun/projects/env/lib/python3.10/site-packages/flask_wtf/__pycache__/csrf.cpython-310.pyc
o

+we�'�@sddlZddlZddlZddlZddlmZddlmZddlmZddlm	Z	ddlm
Z
ddlmZddlm
Z
dd	lmZdd
lmZddlmZddlmZdd
lmZdZe�e�Zddd�Zd dd�Z	d!dd�ZGdd�de�ZGdd�d�ZGdd�de�Zdd�Z dS)"�N)�urlparse)�	Blueprint)�current_app)�g)�request)�session)�BadData)�SignatureExpired)�URLSafeTimedSerializer)�
BadRequest)�ValidationError)�CSRF)�
generate_csrf�
validate_csrf�CSRFProtectcCs�t|dtjdd�}t|dddd�}|tvrWt|dd�}|tvr+t�t�	d	���
�t|<z	|�t|�}WntyPt�t�	d	���
�t|<|�t|�}Ynwt
t||�t�|�S)
aGenerate a CSRF token. The token is cached for a request, so multiple
    calls to this function will generate the same token.

    During testing, it might be useful to access the signed token in
    ``g.csrf_token`` and the raw token in ``session['csrf_token']``.

    :param secret_key: Used to securely sign the token. Default is
        ``WTF_CSRF_SECRET_KEY`` or ``SECRET_KEY``.
    :param token_key: Key where token is stored in session for comparison.
        Default is ``WTF_CSRF_FIELD_NAME`` or ``'csrf_token'``.
    �WTF_CSRF_SECRET_KEY�%A secret key is required to use CSRF.��message�WTF_CSRF_FIELD_NAME�
csrf_token�%A field name is required to use CSRF.�wtf-csrf-token��salt�@)�_get_configr�
secret_keyrr
r�hashlib�sha1�os�urandom�	hexdigest�dumps�	TypeError�setattr�get)r�	token_key�
field_name�s�token�r+�G/home/arjun/projects/env/lib/python3.10/site-packages/flask_wtf/csrf.pyrs0
���
rc
Cs�t|dtjdd�}t|dddd�}t|ddd	d
�}|std��|tvr'td��t|d
d�}z	|j||d�}Wn!tyG}ztd�|�d}~wtyW}ztd�|�d}~wwt	�
t||�sdtd��dS)aCheck if the given data is a valid CSRF token. This compares the given
    signed token to the one stored in the session.

    :param data: The signed CSRF token to be checked.
    :param secret_key: Used to securely sign the token. Default is
        ``WTF_CSRF_SECRET_KEY`` or ``SECRET_KEY``.
    :param time_limit: Number of seconds that the token is valid. Default is
        ``WTF_CSRF_TIME_LIMIT`` or 3600 seconds (60 minutes).
    :param token_key: Key where token is stored in session for comparison.
        Default is ``WTF_CSRF_FIELD_NAME`` or ``'csrf_token'``.

    :raises ValidationError: Contains the reason that validation failed.

    .. versionchanged:: 0.14
        Raises ``ValidationError`` with a specific error message rather than
        returning ``True`` or ``False``.
    rrrrrr�WTF_CSRF_TIME_LIMIT�F)�requiredzThe CSRF token is missing.z"The CSRF session token is missing.rr)�max_agezThe CSRF token has expired.NzThe CSRF token is invalid.zThe CSRF tokens do not match.)rrrrrr
�loadsr	r�hmac�compare_digest)�datar�
time_limitr'r(r)r*�er+r+r,rBs<��
�
���rT�CSRF is not configured.cCs.|durtj�||�}|r|durt|��|S)a�Find config value based on provided value, Flask config, and default
    value.

    :param value: already provided config value
    :param config_name: Flask ``config`` key
    :param default: default value if not provided or configured
    :param required: whether the value must not be ``None``
    :param message: error message if required config is not found
    :raises KeyError: if required config is not found
    N)r�configr&�RuntimeError)�value�config_name�defaultr/rr+r+r,rvs
rcs,eZdZ�fdd�Zdd�Zdd�Z�ZS)�_FlaskFormCSRFcs|j|_t��|�S�N)�meta�super�
setup_form)�self�form��	__class__r+r,rA�sz_FlaskFormCSRF.setup_formcCst|jj|jjd�S)N)rr')rr?�csrf_secret�csrf_field_name)rB�csrf_token_fieldr+r+r,�generate_csrf_token�s�z"_FlaskFormCSRF.generate_csrf_tokenc
Cs^t�dd�rdSzt|j|jj|jj|jj�WdSty.}z	t	�
|jd��d}~ww)N�
csrf_validFr)rr&rr4r?rF�csrf_time_limitrGr�logger�info�args)rBrC�fieldr6r+r+r,�validate_csrf_token�s
���z"_FlaskFormCSRF.validate_csrf_token)�__name__�
__module__�__qualname__rArIrP�
__classcell__r+r+rDr,r=�sr=c@sBeZdZdZddd�Zdd�Zdd�Zd	d
�Zdd�Zd
d�Z	dS)ra[Enable CSRF protection globally for a Flask app.

    ::

        app = Flask(__name__)
        csrf = CSRFProtect(app)

    Checks the ``csrf_token`` field sent with forms, or the ``X-CSRFToken``
    header sent with JavaScript requests. Render the token in templates using
    ``{{ csrf_token() }}``.

    See the :ref:`csrf` documentation.
    NcCs&t�|_t�|_|r|�|�dSdSr>)�set�
_exempt_views�_exempt_blueprints�init_app)rB�appr+r+r,�__init__�s
�zCSRFProtect.__init__cs���jd<�j�dd��j�dd�t�j�dgd����jd<�j�dd��j�d	d
dg��j�dd
��j�dd�t�jjd<��dd���j	��fdd��}dS)N�csrf�WTF_CSRF_ENABLEDT�WTF_CSRF_CHECK_DEFAULT�WTF_CSRF_METHODS)�POST�PUT�PATCH�DELETErr�WTF_CSRF_HEADERSzX-CSRFTokenzX-CSRF-Tokenr-r.�WTF_CSRF_SSL_STRICTcSsdtiS)Nr)rr+r+r+r,�<lambda>�sz&CSRFProtect.init_app.<locals>.<lambda>cs��jdsdS�jdsdStj�jdvrdStjsdS�j�tj��jvr)dS�j�tj�}|j	�d|j
��}|�jvr@dS���dS)Nr\r]r^�.)
r8r�method�endpoint�
blueprintsr&�	blueprintrW�view_functionsrRrQrV�protect)�view�dest�rYrBr+r,�csrf_protect�s


z*CSRFProtect.init_app.<locals>.csrf_protect)
�
extensionsr8�
setdefaultrUr&r�	jinja_env�globals�context_processor�before_request)rBrYrpr+ror,rX�s

�zCSRFProtect.init_appcCsvtjd}tj�|�}|r|StjD]}|�|�r$tj|}|r$|SqtjdD]}tj�|�}|r8|Sq*dS)Nrrc)rr8rrCr&�endswith�headers)rBr(�
base_token�keyr�header_namer+r+r,�_get_csrf_token�s 



��zCSRFProtect._get_csrf_tokenc
Cs�tjtjdvr
dSzt|���Wn"ty4}zt�|j	d�|�
|j	d�WYd}~nd}~wwtjrWtjdrWtjsE|�
d�dtj
�d�}ttj|�sW|�
d�dt_dS)	Nr^rrdzThe referrer header is missing.zhttps://�/z%The referrer does not match the host.T)rrgrr8rr|rrLrMrN�_error_response�	is_secure�referrer�host�same_originrrJ)rBr6�
good_referrerr+r+r,rls ��


zCSRFProtect.protectcCsLt|t�r
|j�|�|St|t�r|}n	d�|j|jf�}|j�|�|S)aMark a view or blueprint to be excluded from CSRF protection.

        ::

            @app.route('/some-view', methods=['POST'])
            @csrf.exempt
            def some_view():
                ...

        ::

            bp = Blueprint(...)
            csrf.exempt(bp)

        rf)	�
isinstancerrW�add�str�joinrRrQrV)rBrm�
view_locationr+r+r,�exempts

zCSRFProtect.exemptcCst|��r>)�	CSRFError)rB�reasonr+r+r,r~2szCSRFProtect._error_responser>)
rQrRrS�__doc__rZrXr|rlr�r~r+r+r+r,r�s
)rc@seZdZdZdZdS)r�z�Raise if the client sends invalid CSRF data with the request.

    Generates a 400 Bad Request response with the failure reason by default.
    Customize the response by registering a handler with
    :meth:`flask.Flask.errorhandler`.
    zCSRF validation failed.N)rQrRrSr��descriptionr+r+r+r,r�6sr�cCs4t|�}t|�}|j|jko|j|jko|j|jkSr>)r�scheme�hostname�port)�current_uri�compare_uri�current�comparer+r+r,r�As
�
�r�)NN)NNN)NTr7)!rr2�loggingr �urllib.parser�flaskrrrrr�itsdangerousrr	r
�werkzeug.exceptionsr�wtformsr�wtforms.csrf.corer
�__all__�	getLoggerrQrLrrrr=rr�r�r+r+r+r,�<module>s6


+5
�