B
    9dP                 @   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
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d ZedddZdddZdS )z'
Password generation for the Notebook.
    )contextmanagerN)
cast_bytesstr_to_bytescast_unicode)ConfigConfigFileNotFoundJSONFileConfigLoader)jupyter_config_dir   argon2c       	      C   s   | dkrNxDt dD ]0}td}td}||kr:|} P qtd qW td|dkrdd	lm} |d
ddd}|| }d|t|dfS t	
|}tdt dt d}|t| dt|d  d||| fS dS )a  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`, or 'argon2').

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

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

    N   zEnter password: zVerify password: zPasswords do not match.z'No matching passwords found. Giving up.r   r   )PasswordHasheri (  
      )Zmemory_costZ	time_costZparallelism:ascii   0xzutf-8)rangegetpassprint
ValueErrorr   r   hashjoinr   hashlibnewrandomgetrandbitssalt_lenupdater   r   	hexdigest)	
passphrase	algorithmip0p1r   phhsalt r*   c/work/yifan.wang/ringdown/master-ringdown-env/lib/python3.7/site-packages/notebook/auth/security.pypasswd   s*    



r,   c          	   C   s   |  drRddl}ddl}| }y|| dd |S  |jjk
rN   dS X ny| dd\}}}W n tt	fk
r   dS X yt
|}W n tk
r   dS X t|dkrdS |t|dt|d	  | |kS dS )
a  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('argon2:...', 'mypassword')
    True

    >>> passwd_check('argon2:...', 'otherpassword')
    False

    >>> passwd_check('sha1:0e112c3ddfce:a68df677475c2b47b6e86d0467eec97ac5f4b85a',
    ...              'mypassword')
    True
    zargon2:r   N   Fr      zutf-8r   )
startswithr   Zargon2.exceptionsr   verify
exceptionsZVerificationErrorsplitr   	TypeErrorr   r   lenr    r   r!   )Zhashed_passphraser"   r   r'   r#   r)   Z	pw_digestr(   r*   r*   r+   passwd_checkO   s(    
r5     c          
   c   s   | dkrt jt d} t jt j| dd tt j| t j| }y| }W n t	k
rn   t
 }Y nX |V  t| ddd}|ttj|dd	 W dQ R X yt | | W nB tk
r } z$t }td
|  d| t W dd}~X Y nX dS )zContext 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.
    Nzjupyter_notebook_config.jsonT)exist_okwutf8)encodingr.   )indentzFailed to set permissions on z:
)ospathr   r	   makedirsdirnamer   basenameZload_configr   r   openwriter   jsondumpschmod	Exception	traceback
format_excwarningswarnRuntimeWarning)config_filemodeloaderconfigfetbr*   r*   r+   persist_config   s"    "rS   c          	   C   s(   t | }t|}||j_W dQ R X dS )zCAsk user for password, store it in notebook json configuration fileN)r,   rS   ZNotebookApppassword)rT   rL   Zhashed_passwordrO   r*   r*   r+   set_password   s    
rU   )Nr   )Nr6   )NN)__doc__
contextlibr   r   r   rC   r<   r   rG   rI   Zipython_genutils.py3compatr   r   r   Ztraitlets.configr   r   r   Zjupyter_core.pathsr	   r   r,   r5   rS   rU   r*   r*   r*   r+   <module>   s"   
86