a
    ø£Be—  ã                   @   sš   d 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	 ddl
mZ ddlmZmZ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 Jupyter Server.
é    N)Úcontextmanager)Újupyter_config_dir)ÚConfigÚConfigFileNotFoundÚJSONFileConfigLoaderé   Úargon2c                 C   sä   | du rVt dƒD ]8}t d¡}t d¡}||kr:|}  qVqtjddd� qd}t|ƒ‚|d	krŽd
dl}|jdddd�}| | ¡}d ||f¡S t	 
|¡}	dttƒ d t dt ¡ }
|	 |  d¡|
 d¡ ¡ d ||
|	 ¡ f¡S )a§  Generate hashed password and salt for use in server configuration.

    In the server configuration, set `c.ServerApp.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')  # doctest: +ELLIPSIS
    'argon2:...'

    Né   zEnter password: zVerify password: zPasswords do not match.é   ©Ú
stacklevelz'No matching passwords found. Giving up.r   r   i (  é
   é   )Zmemory_costZ	time_costZparallelismú:z%0Úxé   úutf-8Úascii)ÚrangeÚgetpassÚwarningsÚwarnÚ
ValueErrorr   ÚPasswordHasherÚhashÚjoinÚhashlibÚnewÚstrÚsalt_lenÚrandomÚgetrandbitsÚupdateÚencodeÚ	hexdigest)Ú
passphraseÚ	algorithmÚ_Úp0Úp1Úmsgr   ÚphZh_phÚhÚsalt© r.   ú_/home/yuhw/wc/wire-cell-python/wcpy/lib/python3.9/site-packages/jupyter_server/auth/security.pyÚpasswd   s.    

ý

r0   c              	   C   sÜ   |   d¡rRddl}ddl}| ¡ }z| | dd… |¡W S  |jjyP   Y dS 0 z|  dd¡\}}}W n tt	fy€   Y dS 0 zt
 |¡}W n ty¤   Y dS 0 t|ƒdkr¶dS | | d¡| d	¡ ¡ | ¡ |kS )
au  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
    --------
    >>> myhash = passwd('mypassword')
    >>> passwd_check(myhash, 'mypassword')
    True

    >>> passwd_check(myhash, 'otherpassword')
    False

    >>> passwd_check('sha1:0e112c3ddfce:a68df677475c2b47b6e86d0467eec97ac5f4b85a',
    ...              'mypassword')
    True
    zargon2:r   Né   Fr   r
   r   r   )Ú
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(    
r8   é€  c                 c   s  | du rt j tƒ d¡} t jt j | ¡dd� tt j | ¡t j | ¡ƒ}z| ¡ }W n t	yl   t
ƒ }Y n0 |V  t| ddd��$}| tj|dd	�¡ W d  ƒ n1 s¬0    Y  zt  | |¡ W n6 tyü   t ¡ }tjd
| › d|› �tdd� Y n0 dS )z¾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.
    Nzjupyter_server_config.jsonT)Úexist_okÚwÚutf8)Úencodingr
   )ÚindentzFailed to set permissions on z:
r   )ÚosÚpathr   r   ÚmakedirsÚdirnamer   ÚbasenameZload_configr   r   ÚopenÚwriteÚjsonÚdumpsÚchmodÚ	ExceptionÚ	tracebackÚ
format_excr   r   ÚRuntimeWarning)Úconfig_fileÚmodeÚloaderÚconfigÚfÚtbr.   r.   r/   Úpersist_configˆ   s$    2ÿrS   c                 C   s<   t | ƒ}t|ƒ�}||j_W d  ƒ n1 s.0    Y  |S )z:Ask user for password, store it in JSON configuration fileN)r0   rS   ZIdentityProviderÚhashed_password)ÚpasswordrM   rT   rP   r.   r.   r/   Úset_password©   s    
&rV   )Nr   )Nr9   )NN)Ú__doc__r   r   rF   r?   r    rJ   r   Ú
contextlibr   Zjupyter_core.pathsr   Ztraitlets.configr   r   r   r   r0   r8   rS   rV   r.   r.   r.   r/   Ú<module>   s    
:9 