o
    Õl>j]  ã                   @   sÒ   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 zd dlZW n	 ey9   Y nw zddlmZ W n	 eyK   Y nw eƒ jZed	d
„ ƒZG dd„ dejƒZG dd„ deƒZdS )é    N)Ú
properties)ÚExceptionTrap)ÚKeyringBackend)ÚPasswordDeleteErroré   )Ú	file_base)Ú_win_cryptoc                   C   s
   t j dS )z¶
    Does this environment have wincrypto?
    Should return False even when Mercurial's Demand Import allowed import of
    _win_crypto, so accesses an attribute of the module.
    N)r   Ú__name__© r
   r
   úI/home/thesage/.local/lib/python3.10/site-packages/keyrings/alt/Windows.pyÚhas_wincrypto   s   
r   c                   @   s:   e Zd ZdZdZejdd„ ƒZdZddd„Z	dd	d
„Z
dS )ÚEncryptedKeyringz=
    A File-based keyring secured by Windows Crypto API.
    z1.0c                 C   s   t  ¡ dks
tdƒ‚dS )zr
        Preferred over file.EncryptedKeyring but not other, more sophisticated
        Windows backends.
        ÚWindowsúRequires Windowsgš™™™™™é?)ÚplatformÚsystemÚRuntimeError©Úselfr
   r
   r   Úpriority,   s   zEncryptedKeyring.priorityzwincrypto_pass.cfgNc                 C   ó
   t  |¡S )z(Encrypt the password using the CryptAPI.)r   Úencrypt)r   ÚpasswordÚassocr
   r
   r   r   8   ó   
zEncryptedKeyring.encryptc                 C   r   )z(Decrypt the password using the CryptAPI.)r   Údecrypt)r   Úpassword_encryptedr   r
   r
   r   r   <   r   zEncryptedKeyring.decrypt)N)r	   Ú
__module__Ú__qualname__Ú__doc__Úversionr   Úclasspropertyr   Úfilenamer   r   r
   r
   r
   r   r   %   s    
	
r   c                   @   sJ   e Zd ZdZejdd„ ƒZedd„ ƒZdd„ Z	dd	„ Z
d
d„ Zdd„ ZdS )ÚRegistryKeyringzˆ
    RegistryKeyring is a keyring which use Windows CryptAPI to encrypt
    the user's passwords and store them under registry keys
    c                 C   s&   t  ¡ dkr
tdƒ‚tƒ stdƒ‚dS )zC
        Preferred on Windows when pywin32 isn't installed
        r   r   zRequires ctypesé   )r   r   r   r   r   r
   r
   r   r   G   s
   zRegistryKeyring.priorityc                 C   s   |   dd¡}djdi tƒ ¤ŽS )Nú\Ú__0x5c__zSoftware\{escaped}\Keyringr
   )ÚreplaceÚformatÚlocals)ÚserviceÚescapedr
   r
   r   Ú_key_for_serviceR   s   z RegistryKeyring._key_for_servicec           	      C   sl   z)|   |¡}t tj|¡}t ||¡d }| d¡}t |¡}t 	|¡ 
d¡}W |S  ty5   d}Y |S w )z,Get password of the username for the servicer   Úasciiúutf-8N)r,   ÚwinregÚOpenKeyÚHKEY_CURRENT_USERÚQueryValueExÚencodeÚbase64Údecodebytesr   r   ÚdecodeÚOSError)	r   r*   ÚusernameÚkeyÚhkeyÚpassword_savedÚpassword_base64r   r   r
   r
   r   Úget_passwordW   s   


þþzRegistryKeyring.get_passwordc           	      C   sT   t  | d¡¡}t |¡}| d¡}|  |¡}t tj	|¡}t 
||dtj|¡ dS )z"Write the password to the registryr.   r-   r   N)r   r   r3   r4   Úencodebytesr6   r,   r/   Ú	CreateKeyr1   Ú
SetValueExÚREG_SZ)	r   r*   r8   r   r   r<   r;   Úkey_namer:   r
   r
   r   Úset_passwordg   s   


zRegistryKeyring.set_passwordc                 C   sj   z|   |¡}t tj|dtj¡}t ||¡ t |¡ W n ty-   t 	¡ d }t
|ƒ‚w |  |¡ dS )z4Delete the password for the username of the service.r   r   N)r,   r/   r0   r1   ÚKEY_ALL_ACCESSÚDeleteValueÚCloseKeyr7   ÚsysÚexc_infor   Ú_delete_key_if_empty)r   r*   r8   rB   r:   Úer
   r
   r   Údelete_passwordu   s   
ÿþzRegistryKeyring.delete_passwordc                 C   s¤   |   |¡}t tj|dtj¡}z	t |d¡ W d S  ty!   Y nw t |¡ |dkrP| d¡\}}}t tj|dtj¡}t 	||¡ t |¡ |}|dks+d S d S )Nr   ÚSoftwarer%   )
r,   r/   r0   r1   rD   Ú	EnumValuer7   rF   Ú
rpartitionÚ	DeleteKey)r   r*   rB   r9   ÚparentÚsepÚbaser
   r
   r   rI   ƒ   s(   
ÿÿ
ÿ
ùz$RegistryKeyring._delete_key_if_emptyN)r	   r   r   r   r   r!   r   Ústaticmethodr,   r=   rC   rK   rI   r
   r
   r
   r   r#   A   s    


r#   )r4   r   rG   Újaraco.classesr   Újaraco.contextr   Úkeyring.backendr   Úkeyring.errorsr   Ú r   r/   ÚImportErrorr   Úpassesr   ÚKeyringr   r#   r
   r
   r
   r   Ú<module>   s.    ÿÿ
	