o
    gP                     @   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   Zid_attribute_user_callback_header_callback_request_callbackr!   _session_identifier_generatorinit_appselfappadd_context_processor r?   ZC:\Users\micha\Documents\internet-seite\env\lib\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>   rE   r?   r?   r@   	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?   r@   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Znext_url)r    sendr   _get_current_objectr4   r
   	blueprintr,   r+   r   r-   r3   r   r.   configgetr   r%   r9   r   r'   urlmake_login_urlr	   )r<   r+   rV   r&   redirect_urlr?   r?   r@   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
        )r6   user_callbackr<   callbackr?   r?   r@   user_loader      	zLoginManager.user_loaderc                 C      | j S )z;Gets the user_loader callback set by user_loader decorator.)r6   r<   r?   r?   r@   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
        )r8   request_callbackr^   r?   r?   r@   request_loader   ra   zLoginManager.request_loaderc                 C   rb   )zAGets the request_loader callback set by request_loader decorator.)r8   rc   r?   r?   r@   re      rd   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?   r@   unauthorized_handler      
z!LoginManager.unauthorized_handlerc                 C   rg   )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?   r@   needs_refresh_handler   ri   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.
        rM   NrN   r   rP   rQ   rR   )r   rS   r   rT   r5   r/   r   r0   r3   r   r1   rV   rW   r   r%   r9   r   r'   r
   rX   rY   r	   )r<   rV   r&   rZ   r?   r?   r@   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.rB   rC   )rE   rF   rG   r7   )r<   r_   rE   r?   r?   r@   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   Z_login_user)r<   userr?   r?   r@   !_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)r6   r8   	Exceptionr   rS   r   rT   _session_protection_failedrn   r   rW   rV   r   r   r
   cookies_load_user_from_remember_cookie_load_user_from_requestheaders_load_user_from_header)	r<   rm   user_idrV   cookie_nameheader_nameZ
has_cookiecookieheaderr?   r?   r@   
_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 )NZSESSION_PROTECTION)r)   strongFrP   r)   _freshr   rr   rq   T)r   rT   r9   r   rV   rW   r2   	permanentr   rS   r   pop)r<   sessidentr=   modekr?   r?   r@   rt     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 )Nro   Fr   rm   )r#   r   r6   r   rT   r   rS   )r<   r}   rz   rm   r=   r?   r?   r@   rv     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   )r7   r   rT   signalsr   rS   )r<   r~   rm   r=   r   r?   r?   r@   ry     s   
z#LoginManager._load_user_from_headerc                 C   s6   | j r|  |}|d urt }tj||d |S d S )Nr   )r8   r   rT   r   rS   )r<   r
   rm   r=   r?   r?   r@   rw     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 )Nrq   Z$REMEMBER_COOKIE_REFRESH_EACH_REQUESTsetro   rr   )r   r   rV   rW   r   _set_cookie_clear_cookie)r<   response	operationr?   r?   r@   rK     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 )Nrp   REMEMBER_COOKIE_DOMAINREMEMBER_COOKIE_PATH/ZREMEMBER_COOKIE_SECUREZREMEMBER_COOKIE_HTTPONLYZREMEMBER_COOKIE_SAMESITE_remember_seconds)secondsZREMEMBER_COOKIE_DURATIONro   zDREMEMBER_COOKIE_DURATION must be a datetime.timedelta, instead got: )valueexpiresdomainpathsecurehttponlysamesite)r   rV   rW   r   r   r   r   r   r   r   r$   str
isinstanceintr   utcnow	TypeErrorrs   
set_cookie)r<   r   rV   r{   r   r   r   r   r   Zdurationdatar   er?   r?   r@   r     sF   



zLoginManager._set_cookiec                 C   s<   t j}|dt}|d}|dd}|j|||d d S )Nrp   r   r   r   )r   r   )r   rV   rW   r   delete_cookie)r<   r   rV   r{   r   r   r?   r?   r@   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.rB   rC   LOGIN_DISABLEDF)rE   rF   rG   r   r   rV   rW   )r<   rE   r?   r?   r@   _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   rB   rC   r   )rE   rF   rG   r   rV   )r<   newvaluerE   r?   r?   r@   r     s   )NT)T)N)__name__
__module____qualname____doc__rA   rH   r:   r[   r`   propertyr]   rf   re   rh   rj   rk   rl   rn   r   rt   rv   ry   rw   rK   r   r   r   setterr?   r?   r?   r@   r(   *   s:    

<
:

4
*	*
r(   N),r   r   flaskr   r   r   r   r   r	   r
   r   rV   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&   rY   r'   r(   r?   r?   r?   r@   <module>   sL    