o
    l>jX                     @   s   d dl Z d dlZd dlZd dlmZm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 G d	d
 d
ZG dd dee	ZdS )    N)decodebytesencodebytes)
properties)KeyringBackend)PasswordDeleteError)	platform_   )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   K/home/thesage/.local/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   version$   r   zFileBacked.versionc                 C   r   )zM
        The encryption version used in file to store the passwords.
        Nr   r   r   r   r   file_version+   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__2   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   encryptD   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   decryptM   r   zKeyring.decryptc              	   C   s   |  ||}t|}t|}t }tj| jr!|j| jdd z-|	||
 }t|}z| ||d}W W |S  tyN   | |d}Y W |S w  tjtjfy^   d}Y |S w )z2
        Read the password from the file.
        utf-8encodingN)_generate_assocescape_for_iniconfigparserRawConfigParserr   r   existsr   readgetencoder   r*   decode
ValueErrorNoOptionErrorNoSectionError)r   serviceusernamer'   configpassword_base64r)   r&   r   r   r   get_passwordV   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)
r7   
isinstancestr	TypeErrorr.   r(   r5   r   r6   _write_config_value)r   r:   r;   r&   r'   r)   r=   r   r   r   set_passwordr   s   
zKeyring.set_passwordc                 C   s   t |d t |  S )z7Generate tamper resistant bytestring of associated dataz\0)r/   r5   )r   r:   r;   r   r   r   r.      s   zKeyring._generate_assocc                 C   s   |    t }|j| jdd t|}t|}||s"|| |||| t	| jddd}|
| W d    d S 1 sBw   Y  d S )Nr+   r,   w)_ensure_file_pathr0   r1   r3   r   r/   has_sectionadd_sectionsetopenwrite)r   r:   keyvaluer<   config_filer   r   r   rC      s   

"zKeyring._write_config_valuec                 C   s   t j| j}|ot j| }|rt | t j| jsAt| jddd W d   n1 s1w   Y  d}t | j| dS dS )zm
        Ensure the storage path exists.
        If it doesn't, create it with "go-rwx" permissions.
        rE   r+   r,   Ni  )	r   r   dirnamer   isdirmakedirsisfilerJ   chmod)r   storage_rootneeds_storage_rootuser_read_writer   r   r   rF      s   
zKeyring._ensure_file_pathc                 C   s   t |}t |}t }tj| jr|j| jdd z|||s&t	dW n tj
y3   t	dw t| jddd}|| W d   dS 1 sMw   Y  dS )z4Delete the password for the username of the service.r+   r,   zPassword not foundrE   N)r/   r0   r1   r   r   r2   r   r3   remove_optionr   r9   rJ   rK   )r   r:   r;   r<   rN   r   r   r   delete_password   s   "zKeyring.delete_password)N)r   r    r!   __doc__r"   abstractmethodr(   r*   r>   rD   r.   rC   rF   rX   r   r   r   r   r%   :   s    	r%   )r"   r0   r   base64r   r   jaraco.classesr   keyring.backendr   keyring.errorsr   keyring.utilr   r	   r/   r
   r%   r   r   r   r   <module>   s    ,