o
    tf                     @   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 ddl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 r*t dD ]}td}td}||kr|}  ntjddd qd}t||d	krFd
dl}|jdddd}|| }| d| S t	|}	dt
t d tdt  }
|	| d|
d  | d|
 d|	  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hashlibnewstrsalt_lenrandomgetrandbitsupdateencode	hexdigest)
passphrase	algorithm_p0p1msgr   phZh_phhsalt r-   e/var/www/html/software/conda/envs/catlas/lib/python3.10/site-packages/jupyter_server/auth/security.pypasswd   s.   



r/   c              	   C   s   |  dr)ddl}ddl}| }z|| dd |W S  |jjy(   Y dS w z| dd\}}}W n tt	fy@   Y dS w zt
|}W n
 tyR   Y dS w t|dkr[dS ||d|d	  | |k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
    --------
    >>> 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_checkP   s.   
r7     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	y7   t
 }Y nw |V  t| ddd}|tj|dd	 W d   n1 sWw   Y  z	t | | W dS  ty   t }tjd
|  d| tdd Y dS w )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_server_config.jsonT)exist_okwutf8)encodingr
   )indentzFailed to set permissions on z:
r   )ospathjoinr   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,   
rS   c                 C   s>   t | }t|}||j_W d   |S 1 sw   Y  |S )z:Ask user for password, store it in JSON configuration fileN)r/   rS   ZIdentityProviderhashed_password)passwordrM   rT   rP   r-   r-   r.   set_password   s   


rV   )Nr   )Nr8   )NN)__doc__r   r   rF   r>   r   rJ   r   
contextlibr   Zjupyter_core.pathsr   Ztraitlets.configr   Ztraitlets.config.loaderr   r   r   r/   r7   rS   rV   r-   r-   r-   r.   <module>   s$    
98 