o
    Ò>h  ã                   @   s>   d dl mZ d dlmZ dZG dd„ deƒZG dd„ dƒZdS )	é    )ÚHiddenField)ÚValidationError)ÚCSRFTokenFieldÚCSRFc                       sH   e Zd ZdZdZ‡ fdd„Zdd„ Zdd„ Zd	d
„ Z‡ fdd„Z	‡  Z
S )r   aA  
    A subclass of HiddenField designed for sending the CSRF token that is used
    for most CSRF protection schemes.

    Notably different from a normal field, this field always renders the
    current token regardless of the submitted value, and also will not be
    populated over to object data via populate_obj
    Nc                    s"   |  d¡| _tƒ j|i |¤Ž d S )NÚ	csrf_impl)Úpopr   ÚsuperÚ__init__)ÚselfÚargsÚkw©Ú	__class__© úQ/var/www/html/flask_server/venv/lib/python3.10/site-packages/wtforms/csrf/core.pyr	      s   zCSRFTokenField.__init__c                 C   s   | j S )z…
        We want to always return the current token on render, regardless of
        whether a good or bad token was passed.
        )Úcurrent_token)r
   r   r   r   Ú_value   s   zCSRFTokenField._valuec                 G   s   dS )z<
        Don't populate objects with the CSRF token
        Nr   )r
   r   r   r   r   Úpopulate_obj   s   zCSRFTokenField.populate_objc                 C   s   | j  || ¡ dS )z8
        Handle validation of this token field.
        N)r   Úvalidate_csrf_token)r
   Úformr   r   r   Úpre_validate$   s   zCSRFTokenField.pre_validatec                    s$   t ƒ j|i |¤Ž | j | ¡| _d S )N)r   Úprocessr   Úgenerate_csrf_tokenr   )r
   r   Úkwargsr   r   r   r   *   s   zCSRFTokenField.process)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r	   r   r   r   r   Ú__classcell__r   r   r   r   r      s    	r   c                   @   s(   e Zd ZeZdd„ Zdd„ Zdd„ ZdS )r   c                 C   s$   |j }|j}| jd| d}||fgS )aÆ  
        Receive the form we're attached to and set up fields.

        The default implementation creates a single field of
        type :attr:`field_class` with name taken from the
        ``csrf_field_name`` of the class meta.

        :param form:
            The form instance we're attaching to.
        :return:
            A sequence of `(field_name, unbound_field)` 2-tuples which
            are unbound fields to be added to the form.
        z
CSRF Token)Úlabelr   )ÚmetaÚcsrf_field_nameÚfield_class)r
   r   r    Ú
field_nameÚunbound_fieldr   r   r   Ú
setup_form2   s   
zCSRF.setup_formc                 C   s   t ƒ ‚)aØ  
        Implementations must override this to provide a method with which one
        can get a CSRF token for this form.

        A CSRF token is usually a string that is generated deterministically
        based on some sort of user data, though it can be anything which you
        can validate on a subsequent request.

        :param csrf_token_field:
            The field which is being used for CSRF.
        :return:
            A generated CSRF string.
        )ÚNotImplementedError)r
   Úcsrf_token_fieldr   r   r   r   E   s   zCSRF.generate_csrf_tokenc                 C   s   |j |jkrt| d¡ƒ‚dS )a>  
        Override this method to provide custom CSRF validation logic.

        The default CSRF validation logic simply checks if the recently
        generated token equals the one we received as formdata.

        :param form: The form which has this CSRF token.
        :param field: The CSRF token field.
        zInvalid CSRF Token.N)r   Údatar   Úgettext)r
   r   Úfieldr   r   r   r   U   s   
ÿzCSRF.validate_csrf_tokenN)r   r   r   r   r"   r%   r   r   r   r   r   r   r   /   s
    r   N)Úwtforms.fieldsr   Úwtforms.validatorsr   Ú__all__r   r   r   r   r   r   Ú<module>   s
    (