
    IbP                         d Z ddlm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mZmZ ddlmZmZmZ ddlmZ dZddZd	 Zedd
       ZddZy)z'
Password generation for the Notebook.
    )contextmanagerN)
cast_bytesstr_to_bytescast_unicode)ConfigConfigFileNotFoundJSONFileConfigLoader)jupyter_config_dir   c                 J   | Yt        d      D ]@  }t        j                  d      }t        j                  d      }||k(  r|}  nt        d       B t        d      |dk(  r?ddlm}  |d	d
d      }|j                  |       }dj                  |t        |d      f      S t        j                  |      }t        j                  dt        z        dt         d}|j                  t        | d      t!        |d      z          dj                  |||j#                         f      S )a  Generate hashed password and salt for use in notebook configuration.

    In the notebook configuration, set `c.NotebookApp.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', algorithm='sha1')
    'sha1:7cf3:b7d6da294ea9592a9480c8f52e63cd42cfb9dd12'

       zEnter password: zVerify password: zPasswords do not match.z'No matching passwords found. Giving up.argon2r   )PasswordHasheri (  
      )memory_cost	time_costparallelism:ascii   0xutf-8)rangegetpassprint
ValueErrorr   r   hashjoinr   hashlibnewrandomgetrandbitssalt_lenupdater   r   	hexdigest)	
passphrase	algorithmip0p1r   phhsalts	            8/usr/lib/python3/dist-packages/notebook/auth/security.pypasswdr1      s   4 q 		HA!34B!45BRx
/0		H FGGH)

 GGJxxLG$<=>>KK	"$$Q\21XJa-@	J0<g3NNOxxD!++-899    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                  t        |d      t        |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
    --------
    >>> from notebook.auth.security import passwd_check
    >>> passwd_check('argon2:...', 'mypassword')
    True

    >>> passwd_check('argon2:...', '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.   s           r0   passwd_checkr@   O   s   8 ##I. ""$	99.qr2J??	):)@)@a)H&ItY	I&A y>Q	J0:dG3LLM{{}	))%   22 		
 I& 		
  		s5   B7 C C+ 7CCC('C(+	C76C7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        t        j                  |d	                   ddd       	 t        j                   | |       y# t        $ r t               }Y ww xY w# 1 sw Y   :xY w# t"        $ r>}t%        j&                         }t)        j*                  d
|  d| t,               Y d}~yd}~w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_notebook_config.jsonT)exist_okwutf8)encodingr5   )indentzFailed to set permissions on z:
)ospathr    r
   makedirsdirnamer	   basenameload_configr   r   openwriter   jsondumpschmod	Exception	traceback
format_excwarningswarnRuntimeWarning)config_filemodeloaderconfigfetbs          r0   persist_configr_      s1     ggll#5#79WXKK,t<!"''"2"2;"?Q\A]^F##% L	k3	0 <A	TZZq9:;<
d#  
< <
  !!#5k]#bTJ	 	sl   B%F	(D 8F	
0D3:F	D? F	D0-F	/D00F	3D<8F	?	F4F<F	FF	c                 |    t        |       }t        |      5 }||j                  _        ddd       y# 1 sw Y   yxY w)zCAsk user for password, store it in notebook json configuration fileN)r1   r_   NotebookApppassword)rb   rX   hashed_passwordr[   s       r0   set_passwordrd      s;     X&O		$ 6&5#6 6 6s   2;)Nr   )Ni  )NN)__doc__
contextlibr   r   r!   rO   rG   r#   rS   rU   ipython_genutils.py3compatr   r   r   traitlets.configr   r   r	   jupyter_core.pathsr
   r%   r1   r@   r_   rd    r2   r0   <module>rk      s_    &    	    M M M M 1 5:p4*l  >6r2   