
    wdfG                         d Z ddlZddlmZmZ ddlmZmZmZmZm	Z	m
Z
mZmZ ddlmZ ddlmZmZmZmZmZmZmZmZmZmZmZmZ ddlmZ dd	lmZm Z m!Z!m"Z"m#Z#m$Z$m%Z% dd
l&m'Z(m)Z)m*Z*m+Z+m,Z,m-Z-m.Z.  G d de/          Z0dS )zY
    flask_login.login_manager
    -------------------------
    The LoginManager class.
    N)datetime	timedelta)_request_ctx_stackabortcurrent_appflashredirecthas_app_contextrequestsession   )	text_type)COOKIE_NAMECOOKIE_DURATIONCOOKIE_SECURECOOKIE_HTTPONLYLOGIN_MESSAGELOGIN_MESSAGE_CATEGORYREFRESH_MESSAGEREFRESH_MESSAGE_CATEGORYID_ATTRIBUTEAUTH_HEADER_NAMESESSION_KEYSUSE_SESSION_FOR_NEXT)AnonymousUserMixin)user_loaded_from_cookieuser_loaded_from_headeruser_loaded_from_requestuser_unauthorizeduser_needs_refreshuser_accessedsession_protected)	login_url_create_identifier_user_context_processorencode_cookiedecode_cookiemake_next_paramexpand_login_viewc                       e Zd ZdZddZddZddZd Zd Zd	 Z	d
 Z
d Zd Zd ZddZd Zd Zd Zd Zd Zd Zd Zd Zed             Zej        d             ZdS )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                 n   t           | _        d | _        i | _        t          | _        t          | _        d | _        t          | _
        t          | _        d| _        d | _        d | _        d | _        t"          | _        d | _        d | _        d | _        t,          | _        ||                     ||           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_processors      ^/var/www/book.euthymeo.com/html/venv/lib/python3.11/site-packages/flask_login/login_manager.py__init__zLoginManager.__init__#   s     1
 
 &(" + '=# ! &5" /G+
 #* "&%)"&*#(" $!%-?*?MM#455555 ?    c                 f    t          j        dt                     |                     ||           dS )zl
        This method has been deprecated. Please use
        :meth:`LoginManager.init_app` instead.
        z5Warning setup_app is deprecated. Please use init_app.N)warningswarnDeprecationWarningr?   r@   s      rD   	setup_appzLoginManager.setup_app_   s9    
 	M(	* 	* 	*c011111rF   c                     | |_         |                    | 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@   s      rD   r?   zLoginManager.init_apph   sP     !$6777  	;!!"9:::::	; 	;rF   c                    t          j        t          j                               | j        r|                                 S t
          j        | j        v r| j        t
          j                 }n| j        }|st          d           | j
        rQ| j        /t          |                     | j
                  | j                   nt          | j
        | j                   t          j        }|                    dt                     r]t#          |          }|                                 t&          d<   t)          |t
          j                  t&          d<   t-          |          }nt-          |t
          j                  }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_objectr8   r   	blueprintr0   r/   r   r1   r7   r   r2   configgetr   r)   r>   r   r(   urlmake_login_urlr	   )rA   r/   r\   r#   redirect_urls        rD   unauthorizedzLoginManager.unauthorizedz   sf   2 	{>@@AAA% 	0--/// :::3G4EFJJJ 	#JJJ 	P%1d,,T-?@@#:< < < < < d(43NOOOO#::,.BCC 	L)*55I!??AAGEN-iEEGFO)*55LL)*w{KKKL%%%rF   c                     || _         |S )aB  
        This sets the callback for reloading a user from the session. The
        function you set should take a user ID (a ``unicode``) 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;   rA   callbacks     rD   user_loaderzLoginManager.user_loader   s     'rF   c                 2    t          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
        zRLoginManager.header_loader is deprecated. Use LoginManager.request_loader instead.)printr<   rc   s     rD   header_loaderzLoginManager.header_loader   s(     	 5 	6 	6 	6 (rF   c                     || _         |S )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
        )r=   rc   s     rD   request_loaderzLoginManager.request_loader   s     "*rF   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
        )r8   rc   s     rD   unauthorized_handlerz!LoginManager.unauthorized_handler   s     &."rF   c                     || _         |S )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
        )r9   rc   s     rD   needs_refresh_handlerz"LoginManager.needs_refresh_handler   s     '/#rF   c                    t          j        t          j                               | j        r|                                 S | j        st          d           | j        /t          |                     | j	                  | j
                   nt          | j	        | j
                   t          j        }|                    dt                    rgt          | j                  }|                                 t           d<   t#          |t$          j                  t           d<   t)          | j                  }n"| j        }t)          |t$          j                  }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.
        rR   NrS   r   rU   rV   rW   )r    rY   r   rZ   r9   r3   r   r7   r   r4   r5   r\   r]   r   r)   r>   r   r(   r   r^   r_   r	   )rA   r\   r#   r`   s       rD   needs_refreshzLoginManager.needs_refresh   sU   ( 	 ? A ABBB& 	1..000  	#JJJ!-$(()CDD>@ @ @ @ @ $,>@ @ @ @ #::,.BCC 	K)$*;<<I!??AAGEN-iEEGFO)$*;<<LL)I))gkJJJL%%%rF   c                 X    t           j        }||                                 n||_        dS )z!Store the given user as ctx.user.N)r   topr.   user)rA   rs   ctxs      rD   !_update_request_context_with_userz.LoginManager._update_request_context_with_user$  s,     !$,0L4&&(((drF   c                 >   | j         | j        t          d          t          j        t          j                               |                                 r|                                 S d}t          j
        d          }|| j         |                      |          }|t
          j        }|
                    dt                    }|
                    dt                    }|t          j        v ot          j
        d          dk    }|r(t          j        |         }|                     |          }nW| j        r|                     t                    }n5|t          j        v r't          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)r;   r=   	Exceptionr!   rY   r   rZ   _session_protection_failedru   r   r]   r\   r   r   r   cookies_load_user_from_remember_cookie_load_user_from_requestheaders_load_user_from_header)	rA   rs   user_idr\   cookie_nameheader_name
has_cookiecookieheaders	            rD   
_load_userzLoginManager._load_user*  s    &4+A+I!" " "
 	;:<<=== **,, 	<99;;; +j))4#6#B&&w//D < 'F **%;[IIK **%79IJJK%8 >!+k22g=  ; 5;;FCC' ;33G<<// 5226::55d;;;rF   c                    t          j                    }|                                 }t          j                    }|j                            d| j                  }|r|dvrdS |r||                    dd           k    ri|dk    s|j        rd|d<   t          j	        |           dS |dk    r;t          D ]}|                    |d            d|d	<   t          j	        |           d
S dS )NSESSION_PROTECTION)r-   strongFrU   r-   _freshr   rz   ry   T)r   rZ   r>   r   r\   r]   r6   	permanentr"   rY   r   pop)rA   sessidentrB   modeks         rD   r|   z'LoginManager._session_protection_failedR  s   *,,2244-//z~~2D4KLL 	t#6665  	ETXXeT2222w$.!&X!&s+++u!!% & &AHHQ%%%%$+[!!&s+++turF   c                     t          |          }|_|t          d<   dt          d<   d }| j        r|                     |          }|+t          j                    }t          j        ||           |S d S )Nrw   Fr   rs   )r'   r   r;   r   rZ   r   rY   )rA   r   r   rs   rB   s        rD   r~   z,LoginManager._load_user_from_remember_cookiem  s    ''")GJ %GHD" 4**733!577',St<<<<trF   c                     | j         rB|                      |          }|+t          j                    }t          j        ||           |S d S Nr   )r<   r   rZ   r   rY   )rA   r   rs   rB   s       rD   r   z#LoginManager._load_user_from_header{  sT      	((00D!577',St<<<<trF   c                     | j         rB|                      |          }|+t          j                    }t          j        ||           |S d S r   )r=   r   rZ   r   rY   )rA   r   rs   rB   s       rD   r   z$LoginManager._load_user_from_request  sT    ! 	))'22D!577(-c====trF   c                 &   dt           vr)t          j                            d          r
dt           d<   dt           v rUt          j        dd           }|dk    rdt           v r|                     |           n|dk    r|                     |           |S )Nry   $REMEMBER_COOKIE_REFRESH_EACH_REQUESTsetrw   rz   )r   r   r\   r]   r   _set_cookie_clear_cookie)rA   response	operations      rD   rO   z$LoginManager._update_remember_cookie  s    g%%"&&'MNN &#(GK '!!K66IE!!jG&;&;  ****g%%""8,,,rF   c           	         t           j        }|                    dt                    }|                    d          }|                    dd          }|                    dt                    }|                    dt
                    }dt          v rt          t          d                   }n|                    d	t                    }t          t          t          d
                             }	t          |t                    rt          |          }	 t          j                    |z   }
n3# t          $ r& t!          dd                    |          z             w xY w|                    ||	|
||||           d S )Nrx   REMEMBER_COOKIE_DOMAINREMEMBER_COOKIE_PATH/REMEMBER_COOKIE_SECUREREMEMBER_COOKIE_HTTPONLY_remember_seconds)secondsREMEMBER_COOKIE_DURATIONrw   z#REMEMBER_COOKIE_DURATION must be a z$datetime.timedelta, instead got: {0})valueexpiresdomainpathsecurehttponly)r   r\   r]   r   r   r   r   r   r   r&   r   
isinstanceintr   utcnow	TypeErrorr{   format
set_cookie)rA   r   r\   r   r   r   r   r   durationdatar   s              rD   r   zLoginManager._set_cookie  s   #jj!7EE455zz0#664mDD::8/JJ')) 1D)EFFFHHzz"<oNNH Ywz':;;<<h$$ 	3 222H	+o''(2GG 	+ 	+ 	+ABII (* ** + + +	+ 	K"&$+#)!%#)%- 	 	/ 	/ 	/ 	/ 	/s   D- -0Ec                     t           j        }|                    dt                    }|                    d          }|                    dd          }|                    |||           d S )Nrx   r   r   r   )r   r   )r   r\   r]   r   delete_cookie)rA   r   r\   r   r   r   s         rD   r   zLoginManager._clear_cookie  se    #jj!7EE455zz0#66{6EEEEErF   c                 b    t                      r t          j                            dd          S dS )z:Legacy property, use app.config['LOGIN_DISABLED'] instead.LOGIN_DISABLEDF)r
   r   r\   r]   )rA   s    rD   _login_disabledzLoginManager._login_disabled  s2      	C%))*:EBBBurF   c                 $    |t           j        d<   dS )zALegacy property setter, use app.config['LOGIN_DISABLED'] instead.r   N)r   r\   )rA   newvalues     rD   r   zLoginManager._login_disabled  s     08+,,,rF   )NT)T)N)__name__
__module____qualname____doc__rE   rK   r?   ra   re   rh   rj   rl   rn   rp   ru   r   r|   r~   r   r   rO   r   r   propertyr   setter rF   rD   r+   r+      s        
:6 :6 :6 :6x2 2 2 2; ; ; ;$6& 6& 6&p
 
 
  "
 
 
    -& -& -&^C C C C&< &< &<P  6         #/ #/ #/JF F F   X 8 8 8 8 8rF   r+   )1r   rH   r   r   flaskr   r   r   r   r	   r
   r   r   _compatr   r\   r   r   r   r   r   r   r   r   r   r   r   r   mixinsr   signalsr   r   r   r   r    r!   r"   utilsr#   r_   r$   r%   r&   r'   r(   r)   objectr+   r   rF   rD   <module>r      sj     ( ( ( ( ( ( ( (6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6      K K K K K K K K K K K K K K K K K K K K K K K K K K K K ' & & & & &L L L L L L L L L L L L L L L L L L8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8
v8 v8 v8 v8 v86 v8 v8 v8 v8 v8rF   