o
    Ìv›a°  ã                   @   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mZ ddlm	Z	m
Z
 ddlmZ ddlmZ ddlmZ G dd	„ d	eƒZdS )
zManage password.é    N)Úopen)ÚbÚinput)Úuser_config_dir)Úsafe_makedirs)Úloggerc                   @   sT   e Zd ZdZddd„Zdd„ Zdd„ Zd	d
„ Zdd„ Zddd„Z	dd„ Z
dd„ ZdS )ÚGlancesPasswordz9This class contains all the methods relating to password.Úglancesc                 C   s2   || _ tƒ | _| j d | _tj | j| j¡| _d S )Nz.pwd)Úusernamer   Úpassword_dirZpassword_filenameÚosÚpathÚjoinÚpassword_file)Úselfr
   © r   ú2/usr/lib/python3/dist-packages/glances/password.pyÚ__init__'   s   zGlancesPassword.__init__c                 C   s   t  t|ƒ¡ ¡ S )z)Return the SHA-256 of the given password.)ÚhashlibÚsha256r   Ú	hexdigest)r   Úplain_passwordr   r   r   Úsha256_hash-   s   zGlancesPassword.sha256_hashc                 C   s   t  | ¡ | ¡  ¡ ¡ S )z+Return the hashed password, salt + SHA-256.)r   r   Úencoder   )r   Úsaltr   r   r   r   Úget_hash1   s   zGlancesPassword.get_hashc                 C   s"   t  ¡ j}|  ||¡}|d | S )zHHash password with a salt based on UUID (universally unique identifier).ú$)ÚuuidZuuid4Úhexr   )r   r   r   Úencrypted_passwordr   r   r   Úhash_password5   s   
zGlancesPassword.hash_passwordc                 C   s"   |  d¡\}}|  ||¡}||kS )z„Encode the plain_password with the salt of the hashed_password.

        Return the comparison with the encrypted_password.
        r   )Úsplitr   )r   Úhashed_passwordr   r   r   Zre_encrypted_passwordr   r   r   Úcheck_password;   s   zGlancesPassword.check_passwordÚ Fc           	      C   sÂ   t j | j¡r|st d | j¡¡ |  ¡ }|S |  t	 	|¡¡}|  
|¡}|r?|  t	 	d¡¡}|  ||¡s?t d¡ t d¡ |rD|}n|}|s_tdƒ}t|ƒdkr_|d  ¡ dkr_|  |¡ |S )aú  Get the password from a Glances client or server.

        For Glances server, get the password (confirm=True, clear=False):
            1) from the password file (if it exists)
            2) from the CLI
        Optionally: save the password to a file (hashed with salt + SHA-256)

        For Glances client, get the password (confirm=False, clear=True):
            1) from the CLI
            2) the password is hashed with SHA-256 (only SHA string transit
               through the network)
        zRead password from file {}zPassword (confirm): z$Sorry, passwords do not match. Exit.é   z,Do you want to save the password? [Yes/No]: r   ÚY)r   r   Úexistsr   r   ÚinfoÚformatÚload_passwordr   Úgetpassr    r#   ZcriticalÚsysÚexitr   ÚlenÚupperÚsave_password)	r   ÚdescriptionZconfirmÚclearZpasswordZpassword_sha256Zpassword_hashedZpassword_confirmZ
save_inputr   r   r   Úget_passwordD   s&   ê



zGlancesPassword.get_passwordc                 C   sJ   t | jƒ t| jdƒ}| t|ƒ¡ W d  ƒ dS 1 sw   Y  dS )z/Save the hashed password to the Glances folder.ÚwbN)r   r   r   r   Úwriter   )r   r"   Úfile_pwdr   r   r   r0   p   s   
"ÿzGlancesPassword.save_passwordc                 C   s:   t | jdƒ}| ¡ }W d  ƒ |S 1 sw   Y  |S )z1Load the hashed password from the Glances folder.ÚrN)r   r   Úread)r   r6   r"   r   r   r   r*   y   s   

ÿýzGlancesPassword.load_passwordN)r	   )r$   FF)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   r   r    r#   r3   r0   r*   r   r   r   r   r   #   s    

	,	r   )r<   r+   r   r   r,   r   Úior   Zglances.compatr   r   Zglances.configr   Zglances.globalsr   Zglances.loggerr   Úobjectr   r   r   r   r   Ú<module>   s   