
    Ib&                     j    d Z ddlZddlZddl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
e      Zy)z/Tornado handlers for logging into the notebook.    N)urlparse)
url_escape   )passwd_checkset_password   )IPythonHandlerc                      e Zd ZdZddZddZd Zed        Zd Z	d Z
edd	       Z ej                  d
ej                        Zed        Zed        Zed        Zed        Zed        Zedd       Zed        Zed        Zy)LoginHandlerzfThe basic tornado login handler

    authenticates with a hashed password from the configuration.
    Nc                     | j                  | j                  dt        | j                  d| j                              |             y )Nz
login.htmlnextdefault)r   message)writerender_templater   get_argumentbase_url)selfr   s     5/usr/lib/python3/dist-packages/notebook/auth/login.py_renderzLoginHandler._render   sA    

4'' 1 1&$-- 1 PQ ( 
 	    c                 2   || j                   }|j                  dd      }t        |      }|j                  s(|j                  dz   j                  | j                         sd}|j                  rw|j                   d|j                   }|j                         }| j                  r| j                  |k(  }n0| j                  r$t        | j                  j                  |            }|s | j                  j                  d|       |}| j                  |       y)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/Fz://zNot allowing login redirect to )r   replacer   netlocpath
startswithschemelowerallow_originallow_origin_patboolmatchlogwarningredirect)r   urlr   parsedalloworigins         r   _redirect_safezLoginHandler._redirect_safe   s     ?mmG kk$&#==s!2 > >t}} ME}}"MM?#fmm_=$$ --7E** !6!6!<!<V!DEE  #B3'!JKcr   c                     | j                   r/| j                  d| j                        }| j                  |       y | j	                          y )Nr   r   )current_userr   r   r-   r   )r   next_urls     r   getzLoginHandler.get?   s;    (((GH)LLNr   c                 8    | j                  | j                        S N)password_from_settingssettings)r   s    r   hashed_passwordzLoginHandler.hashed_passwordF   s    **4==99r   c                     t        ||      S r3   )r   )r   abs      r   r   zLoginHandler.passwd_checkJ   s    Aq!!r   c                 d   | j                  dd      }| j                  dd      }| j                  | j                        r@| j                  | j                  |      r1|s/| j                  | t        j                         j                         n| j                  r| j                  |k(  r| j                  | t        j                         j                         |r| j                  j                  d      r| j                  j                  d      }t        j                  j                  |d      }t        ||       | j                  j!                  d	|        n&| j#                  d
       | j%                  ddi       y | j                  d| j&                        }| j)                  |       y )Npassword 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_availabler5   r   r6   set_login_cookieuuiduuid4hextokenr1   osr   joinr   r&   info
set_statusr   r   r-   )r   typed_passwordr=   r?   r@   r0   s         r   postzLoginHandler.postM   sN   **:r*B(((D ##DMM2  !5!5~F|%%dDJJL,<,<=

n <%%dDJJL,<,<=DMM$5$56M$N!%!2!2<!@J"$'',,z;Y"ZK ;GHHMM$=k]"KL$g/D%EF $$VT]]$CH%r   c                 `   |j                   j                  di       }|j                  dd       |j                   j                  d|j                  j                  dk(        r|j                  dd       |j                  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   )r5   r1   
setdefaultrequestprotocolr   set_secure_cookiecookie_name)clshandleruser_idrO   s       r   rC   zLoginHandler.set_login_cookief   s     !))--.>C!!*d3 1I1IW1TU%%h5!!&'*:*:;!!!'"5"5wQ.Qr   ztoken\s+(.+)c                     |j                  dd      }|sR| j                  j                  |j                  j                  j                  dd            }|r|j                  d      }|S )zGet the user token from a request

        Default:

        - in URL parameters: ?token=<token>
        - in header: Authorization: token <token>
        rG   r<   Authorizationr   )r   auth_header_patr%   rU   headersr1   group)rY   rZ   
user_tokenms       r   	get_tokenzLoginHandler.get_tokenu   s]     ))'26
##))'//*A*A*E*EoWY*Z[AWWQZ
r   c                 &    | j                  |       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rY   rZ   s     r   should_check_originz LoginHandler.should_check_origin   s     --g666r   c                 V    t        |dd      |j                          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_userrf   s     r   re   z#LoginHandler.is_token_authenticated   s/     7J-5$$&w 6>>r   c                    t        |dd      r|j                  S | j                  |      }|:|j                  j	                  di       } |j
                  |j                  fi |}n| j                  ||       d|_        |_|j                  |j                        6|j                  j                  d|j                         |j                          |j                  sd}||_        |S )zCalled by handlers.get_current_user for identifying the current user.

        See tornado.web.RequestHandler.get_current_user for details.
        ri   Nget_secure_cookie_kwargsTz(Clearing invalid/expired login cookie %s	anonymous)rk   ri   get_user_tokenr5   r1   get_secure_cookierX   rC   rj   
get_cookier&   r'   clear_login_cookielogin_available)rY   rZ   r[   rn   s       r   get_userzLoginHandler.get_user   s     7J-###$$W-?(/(8(8(<(<=WY[(\$/g//0C0CaG_aG  '2 ,0G(? !!'"5"56B##$NPWPcPcd**,** & #r   c                     |j                   }|sy| j                  |      }d}||k(  r2|j                  j                  d|j                  j
                         d}|rt        j                         j                  S y)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)	rG   rc   r&   debugrU   	remote_iprD   rE   rF   )rY   rZ   rG   ra   authenticateds        r   rp   zLoginHandler.get_user_token   sm     ]]7+
KKPRYRaRaRkRkl M::<###r   c                 <   |j                   s[d}||j                  j                  |dz          |j                  s,|j                  s|j                  j                  |dz          yyy|j                  s)|j                  s|j                  j                  d       yyy)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;   rG   )rY   appssl_optionsr'   s       r   validate_securityzLoginHandler.validate_security   s     vvUG" +* !* +<<		 +C !C D )2< <<		XY )2<r   c                 &    |j                  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<   )r1   rY   r5   s     r   r4   z#LoginHandler.password_from_settings   s     ||J++r   c                 \    t        | j                  |      xs |j                  d            S )z_Whether this LoginHandler is needed - and therefore whether the login page should be displayed.rG   )r$   r4   r1   r   s     r   rB   z LoginHandler.get_login_available   s(     C..x8QHLL<QRRr   r3   )__name__
__module____qualname____doc__r   r-   r1   propertyr6   r   rM   classmethodrC   recompile
IGNORECASEr^   rc   rg   re   ru   rp   r~   r4   rB    r   r   r   r      s   @ : :"&2 
 
 !bjj"--@O " 	7 	7 ? ?    D  0 Y Y& , , S Sr   r   )r   r   rH   urllib.parser   rD   tornado.escaper   securityr   r   base.handlersr	   r   r   r   r   <module>r      s/    5
 
 	 !  % 0 *iS> iSr   