o
    ie                     @   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 r(t dD ]}td}td}||kr|}  ntd qd}t||dkrDd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.z'No matching passwords found. Giving up.r   r   i (  
      )memory_cost	time_cost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   phh_phhsalt r/   W/var/www/edux/Edux_v2/venv/lib/python3.10/site-packages/jupyter_server/auth/security.pypasswd   s.   



r1   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 )
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   )
startswithr   argon2.exceptionsr   verify
exceptionsVerificationErrorsplitr   	TypeErrorr   r   lenr"   r#   r$   )hashed_passphraser%   r   r+   r&   r.   	pw_digestr-   r/   r/   r0   passwd_checkO   s.   
r>     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d
|  d| t 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)encodingr3   )indentzFailed to set permissions on z:
)ospathr   r   makedirsdirnamer   basenameload_configr   r   openwritejsondumpschmod	Exception	traceback
format_excr   r   RuntimeWarning)config_filemodeloaderconfigftbr/   r/   r0   persist_config   s(   
rZ   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)r1   rZ   IdentityProviderhashed_password)passwordrT   r\   rW   r/   r/   r0   set_password   s   


r^   )Nr   )Nr?   )NN)__doc__r   r   rM   rE   r    rQ   r   
contextlibr   jupyter_core.pathsr   traitlets.configr   r   r   r   r1   r>   rZ   r^   r/   r/   r/   r0   <module>   s"    
:9