
    Ke                         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	Zd
 Ze	dd            ZddZdS )z-
Password generation for the Jupyter Server.
    N)contextmanager)jupyter_config_dir)Config)ConfigFileNotFoundJSONFileConfigLoader   argon2c                    | kt          d          D ]J}t          j        d          }t          j        d          }||k    r|}  n(t          j        dd           Kd}t	          |          |d	k    rHd
dl}|                    ddd          }|                    |           }d                    ||f          S t          j
        |          }	dt          t                    z   dz   t          j        dt          z            z  }
|	                    |                     d          |
                    d          z              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 (  
      )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salts              <lib/python3.11/site-packages/jupyter_server/auth/security.pypasswdr5      sg   4 q 
	" 
	"A!344B!455BRxx
7AFFFFF;CS//!H"" # 
 

 wwz""xxD)***IA3x== 3&&*<Q\*J*JJDHHZw''$++g*>*>>???88Yakkmm4555    c                 Z   |                      d          rQddl}ddl}|                                }	 |                    | dd         |          S # |j        j        $ r Y dS w xY w	 |                     dd          \  }}}n# t          t          f$ r Y dS w xY w	 t          j        |          }n# t          $ r Y dS w xY wt          |          dk    rdS |                    |                    d          |                    d	          z              |                                |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	   argon2.exceptionsr   verify
exceptionsVerificationErrorsplitr   	TypeErrorr!   r"   lenr'   r(   r)   )hashed_passphraser*   r	   r0   r+   r3   	pw_digestr2   s           r4   passwd_checkrC   P   si   8 ##I.. 	    ""$$	99.qrr2J??? 2 	 	 	55	%6%<%<S!%D%D"	4	"   uuK	""   uu 9~~uHHZw''$++g*>*>>???;;==I%%s5   A A$#A$(B BBB1 1
B?>B?  c              #   .  K   | ,t           j                            t                      d          } t          j        t           j                            |           d           t          t           j                            |           t           j                            |                     }	 |                                }n# t          $ r t                      }Y nw xY w|V  t          | dd          5 }|                    t          j        |d	                     ddd           n# 1 swxY w Y   	 t          j        | |           dS # t           $ r9 t#          j                    }t'          j        d
|  d| t*          d           Y dS w xY 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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tbs         r4   persist_configr`      s      gll#5#7#79UVVK,,t<<<<!"'"2"2;"?"?Q\A]A]^^F##%%    LLL	k3	0	0	0 .A	
6!,,,---. . . . . . . . . . . . . . .

d##### 
 
 
!##@K@@B@@.]^	
 	
 	
 	
 	
 	
 	

s6   /C CC8*D..D25D2:E ?FFc                     t          |           }t          |          5 }||j        _        ddd           n# 1 swxY w Y   |S )z:Ask user for password, store it in JSON configuration fileN)r5   r`   IdentityProviderhashed_password)passwordrZ   rc   r]   s       r4   set_passwordre      s     X&&O		$	$ B2A/B B B B B B B B B B B B B B Bs   8<<)Nr	   )NrD   )NN)__doc__r   r!   rS   rK   r%   rW   r   
contextlibr   jupyter_core.pathsr   traitlets.configr   traitlets.config.loaderr   r   r$   r5   rC   r`   re    r6   r4   <module>rl      s       				       % % % % % % 1 1 1 1 1 1 # # # # # # L L L L L L L L 76 76 76 76t6& 6& 6&r 
 
 
 
@     r6   