o
    iiN                     @   s  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 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 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 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 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' dd%l"m(Z) dd&l"m*Z* G d'd( d(Z+d)S )*    )datetime)	timedelta)abort)current_app)flash)g)has_app_context)redirect)request)session   )AUTH_HEADER_NAME)COOKIE_DURATION)COOKIE_HTTPONLY)COOKIE_NAME)COOKIE_SAMESITE)COOKIE_SECURE)ID_ATTRIBUTE)LOGIN_MESSAGE)LOGIN_MESSAGE_CATEGORY)REFRESH_MESSAGE)REFRESH_MESSAGE_CATEGORY)SESSION_KEYS)USE_SESSION_FOR_NEXT)AnonymousUserMixin)session_protected)user_accessed)user_loaded_from_cookie)user_loaded_from_request)user_needs_refresh)user_unauthorized)_create_identifier)_user_context_processor)decode_cookie)encode_cookie)expand_login_view)	login_url)make_next_paramc                   @   s   e Zd ZdZd1ddZd2ddZd2dd	Zd
d Zdd Ze	dd Z
dd Ze	dd Zdd Zdd Zdd Zdd Zd3ddZdd Zd d! Zd"d# Zd$d% Zd&d' Zd(d) Zd*d+ Zd,d- Ze	d.d/ Zejd0d/ ZdS )4LoginManagerzThis object is used to hold the settings used for logging in. Instances
    of :class:`LoginManager` are *not* bound to specific apps, so you can
    create one in the main body of your code and then bind it to your
    app in a factory function.
    NTc                 C   s   t | _d | _i | _t| _t| _d | _t	| _
t| _d| _d | _d | _d | _t| _d | _d | _d | _t| _|d ur?| || d S d S )Nbasic)r   anonymous_user
login_viewblueprint_login_viewsr   login_messager   login_message_categoryrefresh_viewr   needs_refresh_messager   needs_refresh_message_categorysession_protectionlocalize_callbackunauthorized_callbackneeds_refresh_callbackr   id_attribute_user_callback_header_callback_request_callbackr!   _session_identifier_generatorinit_appselfappadd_context_processor r@   Y/var/www/html/arapca_proje/venv/lib/python3.10/site-packages/flask_login/login_manager.py__init__1   s(   zLoginManager.__init__c                 C   s(   ddl }|jdtdd | || dS )zl
        This method has been deprecated. Please use
        :meth:`LoginManager.init_app` instead.
        r   NzY'setup_app' is deprecated and will be removed in Flask-Login 0.7. Use 'init_app' instead.   
stacklevel)warningswarnDeprecationWarningr;   )r=   r>   r?   rF   r@   r@   rA   	setup_appm   s   zLoginManager.setup_appc                 C   s(   | |_ || j |r|t dS dS )a  
        Configures an application. This registers an `after_request` call, and
        attaches this `LoginManager` to it as `app.login_manager`.

        :param app: The :class:`flask.Flask` object to configure.
        :type app: :class:`flask.Flask`
        :param add_context_processor: Whether to add a context processor to
            the app that adds a `current_user` variable to the template.
            Defaults to ``True``.
        :type add_context_processor: bool
        N)login_managerafter_request_update_remember_cookiecontext_processorr"   r<   r@   r@   rA   r;   |   s
   zLoginManager.init_appc                 C   s   t t  | jr|  S tj| jv r| jtj }n| j}|s$t	d | j
r@| jdur8t| | j
| jd nt| j
| jd tj}|dtrct|}|  td< t|tjtd< t|}t|S t|tjd}t|S )a  
        This is called when the user is required to log in. If you register a
        callback with :meth:`LoginManager.unauthorized_handler`, then it will
        be called. Otherwise, it will take the following actions:

            - Flash :attr:`LoginManager.login_message` to the user.

            - If the app is using blueprints find the login view for
              the current blueprint using `blueprint_login_views`. If the app
              is not using blueprints or the login view for the current
              blueprint is not specified use the value of `login_view`.

            - Redirect the user to the login view. (The page they were
              attempting to access will be passed in the ``next`` query
              string variable, so you can redirect there if present instead
              of the homepage. Alternatively, it will be added to the session
              as ``next`` if USE_SESSION_FOR_NEXT is set.)

        If :attr:`LoginManager.login_view` is not defined, then it will simply
        raise a HTTP 401 (Unauthorized) error instead.

        This should be returned from a view or before/after_request function,
        otherwise the redirect will have no effect.
          Ncategoryr   _idnextnext_url)r    sendr   _get_current_objectr4   r
   	blueprintr,   r+   r   r-   r3   r   r.   configgetr   r%   r:   r   r'   urlmake_login_urlr	   )r=   r+   rX   r&   redirect_urlr@   r@   rA   unauthorized   s0   

zLoginManager.unauthorizedc                 C      || _ | jS )a>  
        This sets the callback for reloading a user from the session. The
        function you set should take a user ID (a ``str``) and return a
        user object, or ``None`` if the user does not exist.

        :param callback: The callback for retrieving a user object.
        :type callback: callable
        )r7   user_callbackr=   callbackr@   r@   rA   user_loader      	zLoginManager.user_loaderc                 C      | j S )z;Gets the user_loader callback set by user_loader decorator.)r7   r=   r@   r@   rA   r_         zLoginManager.user_callbackc                 C   r^   )a=  
        This sets the callback for loading a user from a Flask request.
        The function you set should take Flask request object and
        return a user object, or `None` if the user does not exist.

        :param callback: The callback for retrieving a user object.
        :type callback: callable
        )r9   request_callbackr`   r@   r@   rA   request_loader   rc   zLoginManager.request_loaderc                 C   rd   )zAGets the request_loader callback set by request_loader decorator.)r9   re   r@   r@   rA   rg      rf   zLoginManager.request_callbackc                 C   
   || _ |S )ab  
        This will set the callback for the `unauthorized` method, which among
        other things is used by `login_required`. It takes no arguments, and
        should return a response to be sent to the user instead of their
        normal view.

        :param callback: The callback for unauthorized users.
        :type callback: callable
        )r4   r`   r@   r@   rA   unauthorized_handler      
z!LoginManager.unauthorized_handlerc                 C   ri   )ai  
        This will set the callback for the `needs_refresh` method, which among
        other things is used by `fresh_login_required`. It takes no arguments,
        and should return a response to be sent to the user instead of their
        normal view.

        :param callback: The callback for unauthorized users.
        :type callback: callable
        )r5   r`   r@   r@   rA   needs_refresh_handler   rk   z"LoginManager.needs_refresh_handlerc                 C   s   t t  | jr|  S | jstd | jr1| jdur)t	| | j| j
d nt	| j| j
d tj}|dtrVt| j}|  td< t|tjtd< t| j}t|S | j}t|tjd}t|S )a  
        This is called when the user is logged in, but they need to be
        reauthenticated because their session is stale. If you register a
        callback with `needs_refresh_handler`, then it will be called.
        Otherwise, it will take the following actions:

            - Flash :attr:`LoginManager.needs_refresh_message` to the user.

            - Redirect the user to :attr:`LoginManager.refresh_view`. (The page
              they were attempting to access will be passed in the ``next``
              query string variable, so you can redirect there if present
              instead of the homepage.)

        If :attr:`LoginManager.refresh_view` is not defined, then it will
        simply raise a HTTP 401 (Unauthorized) error instead.

        This should be returned from a view or before/after_request function,
        otherwise the redirect will have no effect.
        rN   NrO   r   rQ   rR   rS   )r   rU   r   rV   r5   r/   r   r0   r3   r   r1   rX   rY   r   r%   r:   r   r'   r
   rZ   r[   r	   )r=   rX   r&   r\   r@   r@   rA   needs_refresh  s2   



zLoginManager.needs_refreshc                 C   s"   ddl }|jdtdd || _|S )a  
        This function has been deprecated. Please use
        :meth:`LoginManager.request_loader` instead.

        This sets the callback for loading a user from a header value.
        The function you set should take an authentication token and
        return a user object, or `None` if the user does not exist.

        :param callback: The callback for retrieving a user object.
        :type callback: callable
        r   Nzc'header_loader' is deprecated and will be removed in Flask-Login 0.7. Use 'request_loader' instead.rC   rD   )rF   rG   rH   r8   )r=   ra   rF   r@   r@   rA   header_loader8  s   zLoginManager.header_loaderc                 C   s   |du r|   }|t_dS )z!Store the given user as ctx.user.N)r*   r   _login_user)r=   userr@   r@   rA   !_update_request_context_with_userO  s   
z.LoginManager._update_request_context_with_userc           	      C   s   | j du r| jdu rtdtt  |  r|  S d}t	
d}|dur2| j dur2|  |}|du rvtj}|
dt}|
dt}|tjv oPt	
ddk}|r^tj| }| |}n| jrg| t}n|tjv rvtj| }| |}| |S )z;Loads user from session or remember_me cookie as applicableNznMissing user_loader or request_loader. Refer to http://flask-login.readthedocs.io/#how-it-works for more info._user_idREMEMBER_COOKIE_NAMEr   	_rememberclear)r7   r9   	Exceptionr   rU   r   rV   _session_protection_failedrq   r   rY   rX   r   r   r
   cookies_load_user_from_remember_cookie_load_user_from_requestheaders_load_user_from_header)	r=   rp   user_idrX   cookie_nameheader_name
has_cookiecookieheaderr@   r@   rA   
_load_userW  s4   






zLoginManager._load_userc                 C   s   t  }|  }t }|jd| j}|r|dvrdS |rY||dd krY|dks-|jr?|ddur8d|d< t	| dS |dkrYt
D ]}||d  qEd|d	< t	| d
S dS )NSESSION_PROTECTION)r)   strongFrQ   r)   _freshr   ru   rt   T)r   rV   r:   r   rX   rY   r2   	permanentr   rU   r   pop)r=   sessidentr>   modekr@   r@   rA   rw     s&   

z'LoginManager._session_protection_failedc                 C   sZ   t |}|d ur+|td< dtd< d }| jr| |}|d ur+t }tj||d |S d S )Nrr   Fr   rp   )r#   r   r7   r   rV   r   rU   )r=   r   r}   rp   r>   r@   r@   rA   ry     s   
z,LoginManager._load_user_from_remember_cookiec                 C   sB   | j r|  |}|d urt }ddlm} |j||d |S d S )Nr   )_user_loaded_from_headerr   )r8   r   rV   signalsr   rU   )r=   r   rp   r>   r   r@   r@   rA   r|     s   
z#LoginManager._load_user_from_headerc                 C   s6   | j r|  |}|d urt }tj||d |S d S )Nr   )r9   r   rV   r   rU   )r=   r
   rp   r>   r@   r@   rA   rz     s   
z$LoginManager._load_user_from_requestc                 C   sd   dt vrtjdrdt d< dt v r0t dd }|dkr'dt v r'| | |S |dkr0| | |S )Nrt   $REMEMBER_COOKIE_REFRESH_EACH_REQUESTsetrr   ru   )r   r   rX   rY   r   _set_cookie_clear_cookie)r=   response	operationr@   r@   rA   rL     s   

z$LoginManager._update_remember_cookiec              
   C   s   t j}|dt}|d}|dd}|dt}|dt}|dt}dtv r2ttd d	}	n|d
t	}	t
ttd }
t|	trJt|	d	}	zt |	 }W n tyf } ztd|	 |d }~ww |j||
||||||d d S )Nrs   REMEMBER_COOKIE_DOMAINREMEMBER_COOKIE_PATH/REMEMBER_COOKIE_SECUREREMEMBER_COOKIE_HTTPONLYREMEMBER_COOKIE_SAMESITE_remember_seconds)secondsREMEMBER_COOKIE_DURATIONrr   zDREMEMBER_COOKIE_DURATION must be a datetime.timedelta, instead got: )valueexpiresdomainpathsecurehttponlysamesite)r   rX   rY   r   r   r   r   r   r   r   r$   str
isinstanceintr   utcnow	TypeErrorrv   
set_cookie)r=   r   rX   r~   r   r   r   r   r   durationdatar   er@   r@   rA   r     sF   



zLoginManager._set_cookiec                 C   s<   t j}|dt}|d}|dd}|j|||d d S )Nrs   r   r   r   )r   r   )r   rX   rY   r   delete_cookie)r=   r   rX   r~   r   r   r@   r@   rA   r     s
   
zLoginManager._clear_cookiec                 C   s0   ddl }|jdtdd t rtjddS dS )z:Legacy property, use app.config['LOGIN_DISABLED'] instead.r   Nu'_login_disabled' is deprecated and will be removed in Flask-Login 0.7. Use 'LOGIN_DISABLED' in 'app.config' instead.rC   rD   LOGIN_DISABLEDF)rF   rG   rH   r   r   rX   rY   )r=   rF   r@   r@   rA   _login_disabled  s   zLoginManager._login_disabledc                 C   s&   ddl }|jdtdd |tjd< dS )zALegacy property setter, use app.config['LOGIN_DISABLED'] instead.r   Nr   rC   rD   r   )rF   rG   rH   r   rX   )r=   newvaluerF   r@   r@   rA   r     s   )NT)T)N)__name__
__module____qualname____doc__rB   rI   r;   r]   rb   propertyr_   rh   rg   rj   rl   rm   rn   rq   r   rw   ry   r|   rz   rL   r   r   r   setterr@   r@   r@   rA   r(   *   s:    

<
:

4
*	*
r(   N),r   r   flaskr   r   r   r   r   r	   r
   r   rX   r   r   r   r   r   r   r   r   r   r   r   r   r   mixinsr   r   r   r   r   r   r   r    utilsr!   r"   r#   r$   r%   r&   r[   r'   r(   r@   r@   r@   rA   <module>   sL    