ó
ù2¢\c           @   s  d  Z  d d l m Z d d l Z d d l Z d d l Z d d l Z d d l Z d d l Z d d l	 Z	 d d l
 Z
 d d l m Z m Z m Z d d l m Z m Z m Z d d l m Z d Z d d d	 „ Z d
 „  Z e d d d „ ƒ Z d d d „ Z d S(   s'   
Password generation for the Notebook.
iÿÿÿÿ(   t   contextmanagerN(   t
   cast_bytest   str_to_bytest   cast_unicode(   t   Configt   ConfigFileNotFoundt   JSONFileConfigLoader(   t   jupyter_config_diri   t   sha1c         C   sÞ   |  d k rk x\ t d ƒ D]? } t j d ƒ } t j d ƒ } | | k rS | }  Pq d GHq Wt d ƒ ‚ n  t j | ƒ } d t t ƒ d t j	 d t ƒ } | j
 t |  d	 ƒ t | d
 ƒ ƒ d j | | | j ƒ  f ƒ S(   s²  Generate hashed password and salt for use in notebook configuration.

    In the notebook configuration, set `c.NotebookApp.password` to
    the generated string.

    Parameters
    ----------
    passphrase : str
        Password to hash.  If unspecified, the user is asked to input
        and verify a password.
    algorithm : str
        Hashing algorithm to use (e.g, 'sha1' or any argument supported
        by :func:`hashlib.new`).

    Returns
    -------
    hashed_passphrase : str
        Hashed password, in the format 'hash_algorithm:salt:passphrase_hash'.

    Examples
    --------
    >>> passwd('mypassword')
    'sha1:7cf3:b7d6da294ea9592a9480c8f52e63cd42cfb9dd12'

    i   s   Enter password: s   Verify password: s   Passwords do not match.s'   No matching passwords found. Giving up.s   %0t   xi   s   utf-8t   asciit   :N(   t   Nonet   ranget   getpasst
   ValueErrort   hashlibt   newt   strt   salt_lent   randomt   getrandbitst   updateR   R   t   joint	   hexdigest(   t
   passphraset	   algorithmt   it   p0t   p1t   ht   salt(    (    s5   lib/python2.7/site-packages/notebook/auth/security.pyt   passwd   s    	%#c         C   s«   y |  j  d d ƒ \ } } } Wn t t f k
 r9 t SXy t j | ƒ } Wn t k
 ra t SXt | ƒ d k rx t S| j t | d ƒ t | d ƒ ƒ | j	 ƒ  | k S(   sŸ  Verify that a given passphrase matches its hashed version.

    Parameters
    ----------
    hashed_passphrase : str
        Hashed password, in the format returned by `passwd`.
    passphrase : str
        Passphrase to validate.

    Returns
    -------
    valid : bool
        True if the passphrase matches the hash.

    Examples
    --------
    >>> from notebook.auth.security import passwd_check
    >>> passwd_check('sha1:0e112c3ddfce:a68df677475c2b47b6e86d0467eec97ac5f4b85a',
    ...              'mypassword')
    True

    >>> passwd_check('sha1:0e112c3ddfce:a68df677475c2b47b6e86d0467eec97ac5f4b85a',
    ...              'anotherpassword')
    False
    R   i   i    s   utf-8R
   (
   t   splitR   t	   TypeErrort   FalseR   R   t   lenR   R   R   (   t   hashed_passphraseR   R   R   t	   pw_digestR   (    (    s5   lib/python2.7/site-packages/notebook/auth/security.pyt   passwd_checkE   s    #i€  c      
   c   s  |  d k r' t j j t ƒ  d ƒ }  n  t t j j |  ƒ t j j |  ƒ ƒ } y | j ƒ  } Wn t	 k
 rz t
 ƒ  } n X| Vt j |  d d d ƒ) } | j t t j | d d ƒƒ ƒ Wd QXy t j |  | ƒ Wn9 t k
 r} t j ƒ  } t j d |  | f t ƒ n Xd S(	   s¿   Context manager that can be used to modify a config object

    On exit of the context manager, the config will be written back to disk, 
    by default with user-only (600) permissions.
    s   jupyter_notebook_config.jsont   wt   encodingt   utf8t   indenti   Ns#   Failed to set permissions on %s:
%s(   R   t   ost   pathR   R   R   t   basenamet   dirnamet   load_configR   R   t   iot   opent   writeR   t   jsont   dumpst   chmodt	   Exceptiont	   tracebackt
   format_exct   warningst   warnt   RuntimeWarning(   t   config_filet   modet   loadert   configt   ft   et   tb(    (    s5   lib/python2.7/site-packages/notebook/auth/security.pyt   persist_configp   s     '(c         C   s1   t  |  ƒ } t | ƒ  } | | j _ Wd QXd S(   sC   Ask user for password, store it in notebook json configuration fileN(   R    RD   t   NotebookAppt   password(   RF   R=   t   hashed_passwordR@   (    (    s5   lib/python2.7/site-packages/notebook/auth/security.pyt   set_passwordŽ   s    (   t   __doc__t
   contextlibR    R   R   R1   R4   R,   R   R8   R:   t   ipython_genutils.py3compatR   R   R   t   traitlets.configR   R   R   t   jupyter_core.pathsR   R   R   R    R'   RD   RH   (    (    (    s5   lib/python2.7/site-packages/notebook/auth/security.pyt   <module>   s$   -	+