a
    ø£Bef/  ã                   @   sx   d Z ddlZddlZddlZddlmZ ddlmZ ddlm	Z	 ddl
mZmZ G d	d
„ d
e	ƒZG dd„ deƒZeZdS )z5Tornado handlers for logging into the Jupyter Server.é    N)Úurlparse)Ú
url_escapeé   )ÚJupyterHandleré   )Úpasswd_checkÚset_passwordc                   @   s4   e Zd ZdZddd„Zddd„Zdd„ Zd	d
„ ZdS )ÚLoginFormHandlerzlThe basic tornado login handler

    accepts login form, passed to IdentityProvider.process_login_form.
    Nc              	   C   s*   |   | jdt| jd| jd�ƒ|d�¡ dS )zRender the login form.z
login.htmlÚnext©Údefault)r
   ÚmessageN)ÚwriteZrender_templater   Úget_argumentÚbase_url)Úselfr   © r   ú\/home/yuhw/wc/wire-cell-python/wcpy/lib/python3.9/site-packages/jupyter_server/auth/login.pyÚ_render   s    ýÿzLoginFormHandler._renderc           	      C   sæ   |du r| j }| dd¡}d|v rF| d¡\}}}|› d| d¡› �}t|ƒ}|jsl|jsl|jd  | j ¡sØd}|js||jrÀ|j› d|j› �}| 	¡ }| j
r¨| j
|k}n| jrÀtt | j|¡ƒ}|sØ| j d| ¡ |}|  |¡ dS )	z¶Redirect 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ú:z://ú/Fz!Not allowing login redirect to %r)r   ÚreplaceÚ	partitionÚlstripr   ÚschemeÚnetlocÚpathÚ
startswithÚlowerZallow_originZallow_origin_patÚboolÚreÚmatchÚlogÚwarningÚredirect)	r   Úurlr   r   Ú_ÚrestÚparsedZallowÚoriginr   r   r   Ú_redirect_safe   s(    zLoginFormHandler._redirect_safec                 C   s.   | j r"| jd| jd�}|  |¡ n|  ¡  dS )zGet the login form.r
   r   N)Úcurrent_userr   r   r+   r   )r   Únext_urlr   r   r   ÚgetL   s    zLoginFormHandler.getc                 C   sz   | j  | ¡ }| _|du r8|  d¡ | jddid� dS | j d|j› d�¡ | j  | |¡ | j	d| j
d	�}|  |¡ dS )
zPost a login.Né‘  ÚerrorúInvalid credentials©r   zUser z logged in.r
   r   )Úidentity_providerZprocess_login_formr,   Ú
set_statusr   r#   ÚinfoÚusernameÚset_login_cookier   r   r+   )r   Úuserr-   r   r   r   ÚpostT   s    
zLoginFormHandler.post)N)N)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r+   r.   r9   r   r   r   r   r	      s
   


-r	   c                   @   s¶   e Zd Z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„ ƒZeddd„ƒZedd„ ƒZedd„ ƒZdS ) ÚLegacyLoginHandlerz¤Legacy LoginHandler, implementing most custom auth configuration.

    Deprecated in jupyter-server 2.0.
    Login configuration has moved to IdentityProvider.
    c                 C   s   |   | j¡S )N)Úpassword_from_settingsÚsettings)r   r   r   r   Úhashed_passwordi   s    z"LegacyLoginHandler.hashed_passwordc                 C   s
   t ||ƒS )zCheck a passwd.)r   )r   ÚaÚbr   r   r   r   m   s    zLegacyLoginHandler.passwd_checkc                 C   s  | j ddd�}| j ddd�}|  | j¡rô|  | j|¡rN|sN|  | t ¡ j¡ n¦| j	rÖ| j	|krÖ|  | t ¡ j¡ |rôt
| jddƒrô| j dd¡}tj |d¡}t| jd	ƒrÄt||d
� | j_| jd< | j d| ¡ n|  d¡ | jddid� dS | j d| jd�}|  |¡ dS )zPost a login form.ÚpasswordÚ r   Únew_passwordZallow_password_changeFÚ
config_dirzjupyter_server_config.jsonrA   )Úconfig_filezWrote hashed password to %sr/   r0   r1   r2   Nr
   )r   Úget_login_availabler@   r   rA   r7   ÚuuidÚuuid4ÚhexÚtokenÚgetattrr3   r.   Úosr   ÚjoinÚhasattrr   r#   r5   r4   r   r   r+   )r   Ztyped_passwordrF   rG   rH   r-   r   r   r   r9   q   s*    
þÿ
zLegacyLoginHandler.postNc                 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ÚhttponlyTZsecure_cookieÚhttpsÚsecurer   )r@   r.   Ú
setdefaultÚrequestÚprotocolr   Zset_secure_cookieÚcookie_name)ÚclsÚhandlerÚuser_idrR   r   r   r   r7   ‹   s    z#LegacyLoginHandler.set_login_cookieztoken\s+(.+)c                 C   s:   |  dd¡}|s6| j |jj dd¡¡}|r6| d¡}|S )z›Get the user token from a request

        Default:

        - in URL parameters: ?token=<token>
        - in header: Authorization: token <token>
        rM   rE   ÚAuthorizationr   )r   Úauth_header_patr"   rW   Úheadersr.   Úgroup)rZ   r[   Ú
user_tokenÚmr   r   r   Ú	get_tokenš   s    

zLegacyLoginHandler.get_tokenc                 C   s   |   |¡ S )ú+DEPRECATED in 2.0, use IdentityProvider API)Úis_token_authenticated©rZ   r[   r   r   r   Úshould_check_origin¬   s    z&LegacyLoginHandler.should_check_originc                 C   s"   t |ddƒdu r|j t |ddƒS )rd   Ú_user_idNÚ_token_authenticatedF)rN   r,   rf   r   r   r   re   ±   s    z)LegacyLoginHandler.is_token_authenticatedc                 C   s�   t |ddƒr|jS |  |¡}|  |¡}|p,|}|rL||krF|  ||¡ d|_|du r†| |j¡dur||j 	d|j¡ | 
¡  |js†d}||_|S )rd   rh   NTz(Clearing invalid/expired login cookie %sZ	anonymous)rN   rh   Úget_user_tokenÚget_user_cookier7   ri   Z
get_cookierY   r#   r$   Zclear_login_cookieZlogin_available)rZ   r[   Ztoken_user_idZcookie_user_idr\   r   r   r   Úget_user¹   s"    

zLegacyLoginHandler.get_userc                 C   s2   |j  di ¡}|j|jfi |¤Ž}|r.| ¡ }|S )rd   Úget_secure_cookie_kwargs)r@   r.   Zget_secure_cookierY   Údecode)rZ   r[   rm   r\   r   r   r   rk   Þ   s
    z"LegacyLoginHandler.get_user_cookiec                 C   sx   |j }|sdS |  |¡}d}||kr:|j d|jj¡ d}|rp|  |¡}|du rlt ¡ j	}|j 
d|› �¡ |S dS dS )rd   NFz0Accepting token-authenticated connection from %sTz8Generating new user_id for token-authenticated request: )rM   rc   r#   ÚdebugrW   Z	remote_iprk   rJ   rK   rL   r5   )rZ   r[   rM   ra   Úauthenticatedr\   r   r   r   rj   ç   s(    
þ

ÿz!LegacyLoginHandler.get_user_tokenc                 C   s`   |j sDd}|du r$|j |› d�¡ |js\|js\|j |› d�¡ n|js\|js\|j d¡ dS )rd   z<WARNING: The Jupyter 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$   rD   rM   )rZ   ZappZssl_optionsr$   r   r   r   Úvalidate_security  s    ÿÿz$LegacyLoginHandler.validate_securityc                 C   s   |  dd¡S )rd   rD   rE   )r.   ©rZ   r@   r   r   r   r?     s    z)LegacyLoginHandler.password_from_settingsc                 C   s   t |  |¡p| d¡ƒS )rd   rM   )r    r?   r.   rs   r   r   r   rI     s    z&LegacyLoginHandler.get_login_available)N)N)r:   r;   r<   r=   ÚpropertyrA   r   r9   Úclassmethodr7   r!   ÚcompileÚ
IGNORECASEr^   rc   rg   re   rl   rk   rj   rr   r?   rI   r   r   r   r   r>   b   s4   




$


r>   )r=   rO   r!   rJ   Úurllib.parser   Ztornado.escaper   Zbase.handlersr   Úsecurityr   r   r	   r>   ZLoginHandlerr   r   r   r   Ú<module>   s   S E