o
    ‹¨ÊhH  ã                   @   sÞ   d dl m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 d dlmZmZ d dlmZ zejZW n eyI   ejZY nw zejZW n ey[   ejZY nw G d	d
„ d
eƒZG dd„ deeƒZdS )é    )Úwith_statementN)Ústring_types)Úconfigparser)ÚPasswordDeleteError)ÚKeyringBackend)Ú	platform_Ú
properties)Úescapec                   @   sZ   e Zd Zejdd„ ƒZejdd„ ƒZejdd„ ƒZ	ejdd„ ƒZ
ejd	d
„ ƒZdd„ ZdS )Ú
FileBackedc                 C   ó   dS )z;
        The filename used to store the passwords.
        N© ©Úselfr   r   úH/var/www/html/env/lib/python3.10/site-packages/keyrings/alt/file_base.pyÚfilename   ó    zFileBacked.filenamec                 C   s   t j t ¡ | j¡S )z”
        The path to the file where passwords are stored. This property
        may be overridden by the subclass or at the instance level.
        )ÚosÚpathÚjoinr   Ú	data_rootr   r   r   r   r   Ú	file_path!   s   zFileBacked.file_pathc                 C   r   )zD
        The encryption scheme used to store the passwords.
        znot definedr   r   r   r   r   Úscheme)   ó   zFileBacked.schemec                 C   r   )zE
        The encryption version used to store the passwords.
        Nr   r   r   r   r   Úversion0   r   zFileBacked.versionc                 C   r   )zM
        The encryption version used in file to store the passwords.
        Nr   r   r   r   r   Úfile_version7   r   zFileBacked.file_versionc                 C   s   d}|j di tƒ ¤ŽS )NzS<{self.__class__.__name__} with {self.scheme} v.{self.version} at {self.file_path}>r   )ÚformatÚlocals)r   Útmplr   r   r   Ú__repr__>   s   zFileBacked.__repr__N)Ú__name__Ú
__module__Ú__qualname__ÚabcÚabstractpropertyr   r   ÚNonDataPropertyr   r   r   r   r   r   r   r   r   r
      s    




r
   c                   @   s`   e Zd ZdZejddd„ƒZejddd„ƒZdd„ Zd	d
„ Z	dd„ Z
dd„ Zdd„ Zdd„ ZdS )ÚKeyringa  
    BaseKeyring is a file-based implementation of keyring.

    This keyring stores the password directly in the file and provides methods
    which may be overridden by subclasses to support
    encryption and decryption. The encrypted payload is stored in base64
    format.
    Nc                 C   r   )zÆ
        Given a password (byte string) and assoc (byte string, optional),
        return an encrypted byte string.

        assoc provides associated data (typically: service and username)
        Nr   )r   ÚpasswordÚassocr   r   r   ÚencryptN   r   zKeyring.encryptc                 C   r   )zã
        Given a password encrypted by a previous call to `encrypt`, and assoc
        (byte string, optional), return the original byte string.

        assoc provides associated data (typically: service and username)
        Nr   )r   Úpassword_encryptedr'   r   r   r   ÚdecryptW   r   zKeyring.decryptc              	   C   sº   |   ||¡}t|ƒ}t|ƒ}t ¡ }tj | j¡r| | j¡ z-| 	||¡ 
¡ }t|ƒ}z|  ||¡ d¡}W W |S  tyL   |  |¡ d¡}Y W |S w  tjtjfy\   d}Y |S w )z2
        Read the password from the file.
        úutf-8N)Ú_generate_assocÚescape_for_inir   ÚRawConfigParserr   r   Úexistsr   ÚreadÚgetÚencodeÚdecodebytesr*   ÚdecodeÚ
ValueErrorÚNoOptionErrorÚNoSectionError)r   ÚserviceÚusernamer'   ÚconfigÚpassword_base64r)   r&   r   r   r   Úget_password`   s,   ÿûûþzKeyring.get_passwordc                 C   s^   |st dƒ‚t|tƒstdƒ‚|  ||¡}|  | d¡|¡}dt|ƒ ¡  }|  	|||¡ dS )z(Write the password in the file.
        zUsername cannot be blank.z/Password should be a unicode string, not bytes.r+   Ú
N)
r5   Ú
isinstancer   Ú	TypeErrorr,   r(   r2   Úencodebytesr4   Ú_write_config_value)r   r8   r9   r&   r'   r)   r;   r   r   r   Úset_password}   s   
zKeyring.set_passwordc                 C   s   t |ƒd t |ƒ  ¡ S )z@Generate tamper resistant bytestring of associated data
        ú )r-   r2   )r   r8   r9   r   r   r   r,   �   s
   
ÿÿzKeyring._generate_assocc                 C   sŠ   |   ¡  t ¡ }| | j¡ t|ƒ}t|ƒ}| |¡s | |¡ | |||¡ t	| jdƒ�}| 
|¡ W d   ƒ d S 1 s>w   Y  d S )NÚw)Ú_ensure_file_pathr   r.   r0   r   r-   Úhas_sectionÚadd_sectionÚsetÚopenÚwrite)r   r8   ÚkeyÚvaluer:   Úconfig_filer   r   r   rA   “   s   

"ÿzKeyring._write_config_valuec                 C   s‚   t j | j¡}|ot j |¡ }|rt  |¡ t j | j¡s?t| jdƒ� W d  ƒ n1 s/w   Y  d}t  | j|¡ dS dS )zm
        Ensure the storage path exists.
        If it doesn't, create it with "go-rwx" permissions.
        rD   Ni€  )	r   r   Údirnamer   ÚisdirÚmakedirsÚisfilerI   Úchmod)r   Ústorage_rootÚneeds_storage_rootÚuser_read_writer   r   r   rE   §   s   
ÿûzKeyring._ensure_file_pathc                 C   s    t |ƒ}t |ƒ}t ¡ }tj | j¡r| | j¡ z| ||¡s$t	dƒ‚W n tj
y1   t	dƒ‚w t| jdƒ�}| |¡ W d  ƒ dS 1 sIw   Y  dS )z=Delete the password for the username of the service.
        zPassword not foundrD   N)r-   r   r.   r   r   r/   r   r0   Úremove_optionr   r7   rI   rJ   )r   r8   r9   r:   rM   r   r   r   Údelete_password·   s   ÿÿ"ÿzKeyring.delete_password)N)r   r    r!   Ú__doc__r"   Úabstractmethodr(   r*   r<   rB   r,   rA   rE   rW   r   r   r   r   r%   D   s    	r%   )Ú
__future__r   r   r"   Úbase64Úsixr   Ú	six.movesr   Úkeyring.errorsr   Úkeyring.backendr   Úkeyring.utilr   r   Úkeyring.util.escaper	   r-   r@   ÚAttributeErrorÚencodestringr3   ÚdecodestringÚobjectr
   r%   r   r   r   r   Ú<module>   s,    

ÿ

ÿ*