
    Xh                         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y)z-
Password generation for the Jupyter Server.
    N)contextmanager)jupyter_config_dir)Config)ConfigFileNotFoundJSONFileConfigLoader   c                 `   | gt        d      D ]L  }t        j                  d      }t        j                  d      }||k(  r|}  n&t        j                  dd       N d}t	        |      |d	k(  r0d
dl}|j                  ddd      }|j                  |       }| d| S t        j                  |      }	dt        t              z   dz   t        j                  dt        z        z  }
|	j                  | j                  d      |
j                  d      z          | d|
 d|	j!                          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.argon2r   i (  
      )memory_cost	time_cost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   phh_phhsalts              P/var/www/html/myenv/lib/python3.12/site-packages/jupyter_server/auth/security.pypasswdr4      s+   4 q 		"A!34B!45BRx
MM3B		" <CS/!H"" # 

 wwz"AdV$$IA3x= 3&&*<*<Q\*JJDHHZw'$++g*>>?[$q00    c                    | j                  d      r.ddl}ddl}|j                         }	 |j	                  | dd |      S 	 | j                  dd      \  }}}	 t        j                  |      }t        |      dk(  ry|j                  |j                  d      |j                  d	      z          |j                         |k(  S # |j
                  j                  $ r Y yw xY w# t        t        f$ r Y yw xY w# t        $ r Y yw xY w)
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   argon2.exceptionsr   verify
exceptionsVerificationErrorsplitr   	TypeErrorr    r!   lenr&   r'   r(   )hashed_passphraser)   r   r/   r*   r2   	pw_digestr1   s           r3   passwd_checkrB   P   s   6 ##I. ""$	99.qr2J??%6%<%<S!%D"	4KK	" 9~HHZw'$++g*>>?;;=I%%%   22 		
 	" 
  s5   C C  C5 CC C21C25	D Dc              #     K   | (t         j                  j                  t               d      } t        j                  t         j                  j                  |       d       t        t         j                  j                  |       t         j                  j                  |             }	 |j                         }| t        | dd      5 }|j                  t        j                  |d	             ddd       	 t        j                  | |       y# t        $ r t               }Y nw xY w# 1 sw Y   :xY w# t         $ r9 t#        j$                         }t'        j(                  d
|  d| t*        d       Y yw xY w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   basenameload_configr   r   openwritejsondumpschmod	Exception	traceback
format_excr   r   RuntimeWarning)config_filemodeloaderconfigftbs         r3   persist_configr_      s(     ggll#5#79UVKK,t<!"''"2"2;"?Q\A]^F##% L	k3	0 .A	

6!,-.

d#  
. .
  
!!#+K=B4@.]^	

sf   B%E;(D 8E;
'D*1E;:D6 E;D'$E;&D''E;*D3/E;6?E85E;7E88E;c                     t        |       }t        |      5 }||j                  _        ddd       |S # 1 sw Y   |S xY w)z:Ask user for password, store it in JSON configuration fileN)r4   r_   IdentityProviderhashed_password)passwordrY   rb   r\   s       r3   set_passwordrd      sE     X&O		$ B2A/BBs   3=)Nr   )Ni  )NN)__doc__r   r    rR   rI   r$   rV   r   
contextlibr   jupyter_core.pathsr   traitlets.configr   traitlets.config.loaderr   r   r#   r4   rB   r_   rd    r5   r3   <module>rk      sZ       	    % 1 # L 61r5&p 
 
@r5   