U
    /e%                     @  s   d Z ddlmZ ddlZeeZddlZddl	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 ddlmZ d	d
lmZ erd	dlmZ dZedeZG dd dZ G dd de Z!G dd de Z"dddddZ#dddddZ$dS )zK Provide a hook for supplying authorization mechanisms to a Bokeh server.

    )annotationsN)isfile)
ModuleType)TYPE_CHECKING	AwaitableCallableListNewTypeTupleType)HTTPServerRequest)RequestHandler   )make_globally_unique_id)PathLike)
AuthModuleAuthProviderNullAuthUserc                   @  s   e Zd ZdZddddZeddddZed	dd
dZeddddZeddddZ	eddddZ
eddddZeddddZeddddZddddZdS )r   a   Abstract base class for implementing authorization hooks.

    Subclasses must supply one of: ``get_user`` or ``get_user_async``.

    Subclasses must also supply one of ``login_url`` or ``get_login_url``.

    Optionally, if ``login_url`` provides a relative URL, then ``login_handler``
    may also be supplied.

    The properties ``logout_url`` and ``get_logout_handler`` are analogous to
    the corresponding login properties, and are optional.

    .. autoclasstoc::

    None)returnc                 C  s   |    d S N)	_validateself r   >/tmp/pip-unpacked-wheel-f5fndrjf/bokeh/server/auth_provider.py__init__J   s    zAuthProvider.__init__z&List[Tuple[str, Type[RequestHandler]]]c                 C  sT   g }| j r*| jdk	st|| j| j f | jrP| jdk	s>t|| j| jf |S )z3 URL patterns for login/logout endpoints.

        N)login_handler	login_urlAssertionErrorappendlogout_handler
logout_url)r   	endpointsr   r   r   r$   M   s    zAuthProvider.endpointsz)Callable[[HTTPServerRequest], str] | Nonec                 C  s   dS )a;   A function that computes a URL to redirect unathenticated users
        to for login.

        This property may return None, if a ``login_url`` is supplied
        instead.

        If a function is returned, it should accept a ``RequestHandler``
        and return a login URL for unathenticated users.

        Nr   r   r   r   r   get_login_url[   s    zAuthProvider.get_login_urlz*Callable[[HTTPServerRequest], User] | Nonec                 C  s   dS )a   A function to get the current authenticated user.

        This property may return None, if a ``get_user_async`` function is
        supplied instead.

        If a function is returned, it should accept a ``RequestHandler``
        and return the current authenticated user.

        Nr   r   r   r   r   get_useri   s    zAuthProvider.get_userz5Callable[[HTTPServerRequest], Awaitable[User]] | Nonec                 C  s   dS )a    An async function to get the current authenticated user.

        This property may return None, if a ``get_user`` function is supplied
        instead.

        If a function is returned, it should accept a ``RequestHandler``
        and return the current authenticated user.

        Nr   r   r   r   r   get_user_asyncv   s    zAuthProvider.get_user_asynczType[RequestHandler] | Nonec                 C  s   dS )a   A request handler class for a login page.

        This property may return None, if ``login_url`` is supplied
        instead.

        If a class is returned, it must be a subclass of RequestHandler,
        which will used for the endpoint specified by ``logout_url``

        Nr   r   r   r   r   r      s    zAuthProvider.login_handlerz
str | Nonec                 C  s   dS )z A URL to redirect unauthenticated users to for login.

        This proprty may return None, if a ``get_login_url`` function is
        supplied instead.

        Nr   r   r   r   r   r      s    zAuthProvider.login_urlc                 C  s   dS )z A request handler class for a logout page.

        This property may return None.

        If a class is returned, it must be a subclass of RequestHandler,
        which will used for the endpoint specified by ``logout_url``

        Nr   r   r   r   r   r"      s    
zAuthProvider.logout_handlerc                 C  s   dS )zf A URL to redirect authenticated users to for logout.

        This proprty may return None.

        Nr   r   r   r   r   r#      s    zAuthProvider.logout_urlc                 C  s   | j r| jrtd| j s | jr4| js4| js4td| jrH| jrHtd| jr\| jr\td| jrvt| jtsvtd| jrt| jstd| j	rt| j	tstd| j
rt| j
stdd S )	Nz9Only one of get_user or get_user_async should be suppliedzWWhen user authentication is enabled, one of login_url or get_login_url must be suppliedz<At most one of login_url or get_login_url should be suppliedz;LoginHandler cannot be used with a get_login_url() functionz-LoginHandler must be a Tornado RequestHandlerz7LoginHandler can only be used with a relative login_urlz.LogoutHandler must be a Tornado RequestHandlerz9LogoutHandler can only be used with a relative logout_url)r&   r'   
ValueErrorr   r%   r   
issubclassr   probably_relative_urlr"   r#   r   r   r   r   r      s     zAuthProvider._validateN)__name__
__module____qualname____doc__r   propertyr$   r%   r&   r'   r   r   r"   r#   r   r   r   r   r   r   9   s&   	r   c                      s|   e Zd ZdZddd fddZedd Zed	d
 Zedd Zedd Z	edd Z
edd Zedd Z  ZS )r   a   An AuthProvider configured from a Python module.

    The following properties return the corresponding values from the module if
    they exist, or None otherwise:

    * ``get_login_url``,
    * ``get_user``
    * ``get_user_async``
    * ``login_url``
    * ``logout_url``

    The ``login_handler`` property will return a ``LoginHandler`` class from the
    module, or None otherwise.

    The ``logout_handler`` property will return a ``LogoutHandler`` class from
    the module, or None otherwise.

    .. autoclasstoc::

    r   r   module_pathr   c                   s.   t |std|t|| _t   d S )Nzno file exists at module_path: )r   r(   load_auth_module_modulesuperr   )r   r1   	__class__r   r   r      s    
zAuthModule.__init__c                 C  s   t | jdd S )Nr&   getattrr3   r   r   r   r   r&      s    zAuthModule.get_userc                 C  s   t | jdd S )Nr'   r7   r   r   r   r   r'      s    zAuthModule.get_user_asyncc                 C  s   t | jdd S )Nr   r7   r   r   r   r   r      s    zAuthModule.login_urlc                 C  s   t | jdd S )Nr%   r7   r   r   r   r   r%      s    zAuthModule.get_login_urlc                 C  s   t | jdd S )NZLoginHandlerr7   r   r   r   r   r      s    zAuthModule.login_handlerc                 C  s   t | jdd S )Nr#   r7   r   r   r   r   r#      s    zAuthModule.logout_urlc                 C  s   t | jdd S )NZLogoutHandlerr7   r   r   r   r   r"      s    zAuthModule.logout_handler)r+   r,   r-   r.   r   r/   r&   r'   r   r%   r   r#   r"   __classcell__r   r   r5   r   r      s    





r   c                   @  sd   e Zd ZdZedd Zedd Zedd Zedd	 Zed
d Z	edd Z
edd ZdS )r   zv A default no-auth AuthProvider.

    All of the properties of this provider return None.

    .. autoclasstoc::

    c                 C  s   d S r   r   r   r   r   r   r&     s    zNullAuth.get_userc                 C  s   d S r   r   r   r   r   r   r'   
  s    zNullAuth.get_user_asyncc                 C  s   d S r   r   r   r   r   r   r     s    zNullAuth.login_urlc                 C  s   d S r   r   r   r   r   r   r%     s    zNullAuth.get_login_urlc                 C  s   d S r   r   r   r   r   r   r     s    zNullAuth.login_handlerc                 C  s   d S r   r   r   r   r   r   r#     s    zNullAuth.logout_urlc                 C  s   d S r   r   r   r   r   r   r"     s    zNullAuth.logout_handlerN)r+   r,   r-   r.   r/   r&   r'   r   r%   r   r#   r"   r   r   r   r   r      s   





r   r   r   r0   c                 C  s<   dt  dd }tj|| }tj|}|j| |S )z Load a Python source file at a given path as a module.

    Arguments:
        module_path (str): path to a Python source file

    Returns
        module

    zbokeh.auth_- )r   replace	importlibutilspec_from_file_locationmodule_from_specloaderexec_module)r1   module_namespecmoduler   r   r   r2   &  s
    
r2   strbool)urlr   c                 C  s   |  d S )z Return True if a URL is not one of the common absolute URL formats.

    Arguments:
        url (str): a URL string

    Returns
        bool

    )zhttp://zhttps://z//)
startswith)rH   r   r   r   r*   6  s    
r*   )%r.   
__future__r   logging	getLoggerr+   logimportlib.utilr=   os.pathr   typesr   typingr   r   r   r   r	   r
   r   Ztornado.httputilr   Ztornado.webr   Zutil.serializationr   Z
core.typesr   __all__objectr   r   r   r   r2   r*   r   r   r   r   <module>   s(   
$

 :(