o
    ‹ågiN  ã                   @   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ÚLoginManagerzûThis 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@   úU/var/www/html/flaskapp/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   Núu'_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    