ó
ù2¢\c           @   s¾   d  Z  d d l Z d d l Z y d d l m Z m Z Wn' e k
 ra d d l m Z m Z n Xd d l Z d d l m	 Z	 d d l
 m Z m Z d d l m Z d	 e f d
 „  ƒ  YZ d S(   s/   Tornado handlers for logging into the notebook.iÿÿÿÿN(   t   urlparset
   urlunparse(   t
   url_escapei   (   t   passwd_checkt   set_passwordi   (   t   IPythonHandlert   LoginHandlerc           B   sò   e  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 RS(   sf   The basic tornado login handler

    authenticates with a hashed password from the configuration.
    c      	   C   s>   |  j  |  j d d t |  j d d |  j ƒƒ d | ƒƒ d  S(   Ns
   login.htmlt   nextt   defaultt   message(   t   writet   render_templateR   t   get_argumentt   base_url(   t   selfR	   (    (    s2   lib/python2.7/site-packages/notebook/auth/login.pyt   _render   s    c         C   sR  | d	 k r |  j } n  | j d d ƒ } t | ƒ } t | j d d d d ƒ ƒ } | | k sz | j d j |  j ƒ rAt } | | k rd | j	 | j
 f } | j ƒ  } | d |  j j |  j j f k rÙ t } q|  j rô |  j | k } q|  j rt |  j j | ƒ ƒ } qn  | sA|  j j d | ƒ | } qAn  |  j | ƒ d	 S(
   s¶   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).
        s   \s   %5Ct   netloct    t   schemet   /s   %s://%ss!   Not allowing login redirect to %rN(   t   NoneR   t   replaceR    R   t   _replacet   patht
   startswitht   FalseR   R   t   lowert   requestt   protocolt   hostt   Truet   allow_origint   allow_origin_patt   boolt   matcht   logt   warningt   redirect(   R   t   urlR   t   parsedt	   path_onlyt   allowt   origin(    (    s2   lib/python2.7/site-packages/notebook/auth/login.pyt   _redirect_safe!   s(    &"			c         C   s?   |  j  r1 |  j d d |  j ƒ} |  j | ƒ n
 |  j ƒ  d  S(   NR   R   (   t   current_userR   R   R+   R   (   R   t   next_url(    (    s2   lib/python2.7/site-packages/notebook/auth/login.pyt   getD   s    	c         C   s   |  j  |  j ƒ S(   N(   t   password_from_settingst   settings(   R   (    (    s2   lib/python2.7/site-packages/notebook/auth/login.pyt   hashed_passwordK   s    c         C   s   t  | | ƒ S(   N(   R   (   R   t   at   b(    (    s2   lib/python2.7/site-packages/notebook/auth/login.pyR   O   s    c         C   sb  |  j  d d d ƒ} |  j  d d d ƒ} |  j |  j ƒ r9|  j |  j | ƒ rt | rt |  j |  t j ƒ  j ƒ q9|  j	 r|  j	 | k r|  j |  t j ƒ  j ƒ | r6|  j j
 d ƒ r6|  j j
 d ƒ } t j j | d ƒ } t | d | ƒ|  j j d	 | ƒ q6q9|  j d
 ƒ |  j d i d d 6ƒ d  Sn  |  j  d d |  j ƒ} |  j | ƒ d  S(   Nt   passwordR   u    t   new_passwordt   allow_password_changet
   config_dirs   jupyter_notebook_config.jsont   config_files   Wrote hashed password to %si‘  R	   s   Invalid credentialst   errorR   (   R   t   get_login_availableR0   R   R1   t   set_login_cookiet   uuidt   uuid4t   hext   tokenR.   t   osR   t   joinR   R#   t   infot
   set_statusR   R   R+   (   R   t   typed_passwordR5   R7   R8   R-   (    (    s2   lib/python2.7/site-packages/notebook/auth/login.pyt   postR   s"    c         C   s†   | j  j d i  ƒ } | j d t ƒ | j  j d | j j d k ƒ rY | j d t ƒ n  | j d | j ƒ | j | j | |  | S(   s9   Call this on handlers to set the login cookie for successt   cookie_optionst   httponlyt   secure_cookiet   httpst   secureR   (	   R0   R.   t
   setdefaultR   R   R   R   t   set_secure_cookiet   cookie_name(   t   clst   handlert   user_idRF   (    (    s2   lib/python2.7/site-packages/notebook/auth/login.pyR;   k   s    !s   token\s+(.+)c         C   s[   | j  d d ƒ } | sW |  j j | j j j d d ƒ ƒ } | rW | j d ƒ } qW n  | S(   s›   Get the user token from a request

        Default:

        - in URL parameters: ?token=<token>
        - in header: Authorization: token <token>
        R?   R   t   Authorizationi   (   R   t   auth_header_patR"   R   t   headersR.   t   group(   RN   RO   t
   user_tokent   m(    (    s2   lib/python2.7/site-packages/notebook/auth/login.pyt	   get_tokenz   s    
$c         C   s   |  j  | ƒ S(   s3  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.
        (   t   is_token_authenticated(   RN   RO   (    (    s2   lib/python2.7/site-packages/notebook/auth/login.pyt   should_check_originŒ   s    
c         C   s5   t  | d d ƒ d k r% | j ƒ  n  t  | d t ƒ S(   sÿ   Returns 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
        t   _user_idt   _token_authenticatedN(   t   getattrR   t   get_current_userR   (   RN   RO   (    (    s2   lib/python2.7/site-packages/notebook/auth/login.pyRX   ˜   s    
c         C   sã   t  | d d ƒ r | j S|  j | ƒ } | d k ra | j j d i  ƒ } | j | j |  } n |  j | | ƒ t	 | _
 | d k rÖ | j | j ƒ d k	 rÁ | j j d | j ƒ | j ƒ  n  | j sÖ d } qÖ n  | | _ | S(   s”   Called by handlers.get_current_user for identifying the current user.

        See tornado.web.RequestHandler.get_current_user for details.
        RZ   t   get_secure_cookie_kwargss(   Clearing invalid/expired login cookie %st	   anonymousN(   R\   R   RZ   t   get_user_tokenR0   R.   t   get_secure_cookieRM   R;   R   R[   t
   get_cookieR#   R$   t   clear_login_cookiet   login_available(   RN   RO   RP   R^   (    (    s2   lib/python2.7/site-packages/notebook/auth/login.pyt   get_user§   s     			c         C   sq   | j  } | s d S|  j | ƒ } t } | | k rV | j j d | j j ƒ t } n  | ri t j	 ƒ  j
 Sd Sd S(   sž   Identify the user based on a token in the URL or Authorization header
        
        Returns:
        - uuid if authenticated
        - None if not
        Ns0   Accepting token-authenticated connection from %s(   R?   RW   R   R#   t   debugR   t	   remote_ipR   R<   R=   R>   R   (   RN   RO   R?   RU   t   authenticated(    (    s2   lib/python2.7/site-packages/notebook/auth/login.pyR`   Ê   s    		c         C   s‹   | j  s` d } | d k r2 | j j | d ƒ n  | j r‡ | j r‡ | j j | d ƒ q‡ n' | j r‡ | j r‡ | j j d ƒ n  d S(   s‡   Check the notebook application's security.

        Show messages, or abort if necessary, based on the security configuration.
        s=   WARNING: The notebook server is listening on all IP addressess3    and not using encryption. This is not recommended.sK    and not using authentication. This is highly insecure and not recommended.s`   All authentication is disabled.  Anyone who can connect to this server will be able to run code.N(   t   ipR   R#   R$   R4   R?   (   RN   t   appt   ssl_optionsR$   (    (    s2   lib/python2.7/site-packages/notebook/auth/login.pyt   validate_securityã   s    		c         C   s   | j  d d ƒ S(   s   Return the hashed password from the tornado settings.

        If there is no configured password, an empty string will be returned.
        R4   u    (   R.   (   RN   R0   (    (    s2   lib/python2.7/site-packages/notebook/auth/login.pyR/   ÷   s    c         C   s"   t  |  j | ƒ p | j d ƒ ƒ S(   s_   Whether this LoginHandler is needed - and therefore whether the login page should be displayed.R?   (   R!   R/   R.   (   RN   R0   (    (    s2   lib/python2.7/site-packages/notebook/auth/login.pyR:   ÿ   s    N(   t   __name__t
   __module__t   __doc__R   R   R+   R.   t   propertyR1   R   RE   t   classmethodR;   t   ret   compilet
   IGNORECASERR   RW   RY   RX   Re   R`   Rl   R/   R:   (    (    (    s2   lib/python2.7/site-packages/notebook/auth/login.pyR      s&   #			#(   Ro   Rr   R@   t   urllib.parseR    R   t   ImportErrorR<   t   tornado.escapeR   t   securityR   R   t   base.handlersR   R   (    (    (    s2   lib/python2.7/site-packages/notebook/auth/login.pyt   <module>   s   