o
    i|'                     @   sh   d Z ddlZddlZddlmZmZ ddlZddlmZ ddl	m
Z
mZ ddlmZ G d	d
 d
eZdS )z/Tornado handlers for logging into the notebook.    N)urlparse
urlunparse)
url_escape   )passwd_checkset_password   )IPythonHandlerc                   @   s   e Zd ZdZd"ddZd"ddZdd Zed	d
 Zdd Z	dd Z
ed"ddZedejZedd Zedd Zedd Zedd Zedd Zed"ddZedd Zed d! ZdS )#LoginHandlerzfThe basic tornado login handler

    authenticates with a hashed password from the configuration.
    Nc              	   C   s*   |  | jdt| jd| jd|d d S )Nz
login.htmlnextdefault)r   message)writerender_templater   get_argumentbase_url)selfr    r   N/var/www/edux/Edux_v2/venv/lib/python3.10/site-packages/notebook/auth/login.py_render   s   
zLoginHandler._renderc                 C   s   |du r| j }|dd}t|}t|jddd}||ks'|jd | j skd}||kr^|j d|j }|	 }|| j
j d| j
j krJd	}n| jrS| j|k}n| jr^t| j|}|sk| jd
| |}| | dS )zRedirect if url is on our PATH

        Full-domain redirects are allowed if they pass our CORS origin checks.

        Otherwise use default (self.base_url if unspecified).
        N\z%5C )netlocscheme/Fz://TzNot allowing login redirect to )r   replacer   r   _replacepath
startswithr   r   lowerrequestprotocolhostallow_originallow_origin_patboolmatchlogwarningredirect)r   urlr   parsed	path_onlyalloworiginr   r   r   _redirect_safe   s(   zLoginHandler._redirect_safec                 C   s0   | j r| jd| jd}| | d S |   d S )Nr   r   )current_userr   r   r0   r   )r   next_urlr   r   r   getB   s   zLoginHandler.getc                 C   s   |  | jS N)password_from_settingssettings)r   r   r   r   hashed_passwordI   s   zLoginHandler.hashed_passwordc                 C   s
   t ||S r4   )r   )r   abr   r   r   r   M   s   
zLoginHandler.passwd_checkc                 C   s   | j ddd}| j ddd}| | jrl| | j|r'|s'| | t j nE| j	r]| j	|kr]| | t j |r\| j
dr\| j
d}tj|d}t||d | jd	|  n| d
 | jddid d S | j d| jd}| | d S )Npasswordr   r   new_passwordallow_password_change
config_dirzjupyter_notebook_config.json)config_filezWrote hashed password to i  errorzInvalid credentials)r   r   )r   get_login_availabler6   r   r7   set_login_cookieuuiduuid4hextokenr3   osr   joinr   r(   info
set_statusr   r   r0   )r   typed_passwordr;   r=   r>   r2   r   r   r   postP   s$   
zLoginHandler.postc                 C   sd   |j di }|dd |j d|jjdkr|dd |d|j |j|j|fi | |S )z9Call this on handlers to set the login cookie for successcookie_optionshttponlyTsecure_cookiehttpssecurer   )r6   r3   
setdefaultr!   r"   r   set_secure_cookiecookie_name)clshandleruser_idrL   r   r   r   rA   i   s   zLoginHandler.set_login_cookieztoken\s+(.+)c                 C   s:   | dd}|s| j|jjdd}|r|d}|S )zGet the user token from a request

        Default:

        - in URL parameters: ?token=<token>
        - in header: Authorization: token <token>
        rE   r   Authorizationr   )r   auth_header_patr'   r!   headersr3   group)rT   rU   
user_tokenmr   r   r   	get_tokenx   s   

zLoginHandler.get_tokenc                 C   s   |  | S )a3  Should the Handler check for CORS origin validation?

        Origin check should be skipped for token-authenticated requests.

        Returns:
        - True, if Handler must check for valid CORS origin.
        - False, if Handler should skip origin check since requests are token-authenticated.
        )is_token_authenticatedrT   rU   r   r   r   should_check_origin   s   
z LoginHandler.should_check_originc                 C   s$   t |dddu r|  t |ddS )zReturns True if handler has been token authenticated. Otherwise, False.

        Login with a token is used to signal certain things, such as:

        - permit access to REST API
        - xsrf protection
        - skip origin-checks for scripts
        _user_idN_token_authenticatedF)getattrget_current_userr_   r   r   r   r^      s   
z#LoginHandler.is_token_authenticatedc                 C   s   t |ddr	|jS | |}|du r$|jdi }|j|jfi |}n	| || d|_|du rJ|	|jdurE|j
d|j |  |jsJd}||_|S )zCalled by handlers.get_current_user for identifying the current user.

        See tornado.web.RequestHandler.get_current_user for details.
        ra   Nget_secure_cookie_kwargsTz(Clearing invalid/expired login cookie %s	anonymous)rc   ra   get_user_tokenr6   r3   get_secure_cookierS   rA   rb   
get_cookier(   r)   clear_login_cookielogin_available)rT   rU   rV   re   r   r   r   get_user   s    
zLoginHandler.get_userc                 C   sL   |j }|sdS | |}d}||kr|jd|jj d}|r$t jS dS )zIdentify the user based on a token in the URL or Authorization header

        Returns:
        - uuid if authenticated
        - None if not
        NFz0Accepting token-authenticated connection from %sT)	rE   r]   r(   debugr!   	remote_iprB   rC   rD   )rT   rU   rE   r[   authenticatedr   r   r   rg      s   

zLoginHandler.get_user_tokenc                 C   sn   |j s%d}|du r|j|d  |js!|js#|j|d  dS dS dS |js3|js5|jd dS dS dS )zCheck the notebook application's security.

        Show messages, or abort if necessary, based on the security configuration.
        z=WARNING: The notebook server is listening on all IP addressesNz3 and not using encryption. This is not recommended.zK and not using authentication. This is highly insecure and not recommended.z`All authentication is disabled.  Anyone who can connect to this server will be able to run code.)ipr(   r)   r:   rE   )rT   appssl_optionsr)   r   r   r   validate_security   s   zLoginHandler.validate_securityc                 C   s   | ddS )zReturn the hashed password from the tornado settings.

        If there is no configured password, an empty string will be returned.
        r:   r   )r3   rT   r6   r   r   r   r5      s   z#LoginHandler.password_from_settingsc                 C   s   t | |p
|dS )z_Whether this LoginHandler is needed - and therefore whether the login page should be displayed.rE   )r&   r5   r3   rt   r   r   r   r@      s   z LoginHandler.get_login_availabler4   )__name__
__module____qualname____doc__r   r0   r3   propertyr7   r   rK   classmethodrA   recompile
IGNORECASErX   r]   r`   r^   rl   rg   rs   r5   r@   r   r   r   r   r
      s8    

#




"

r
   )rx   r{   rF   urllib.parser   r   rB   tornado.escaper   securityr   r   base.handlersr	   r
   r   r   r   r   <module>   s    