o
    ‹¨Êh  ã                   @   sX  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 d dlm	Z	m
Z
 ddlmZ zedƒ d dlmZ ej W n eyV   z
ed	ƒ d dlZW n	 eyS   Y nw Y nw zd dlZW n eyv   zd dlZW n	 eys   Y nw Y nw zdd
lmZ W n	 eyˆ   Y nw dd„ Zdd„ ZG dd„ dejƒZG dd„ deƒZG dd„ deƒZdS )é    N)Ú
properties)ÚKeyringBackend)ÚPasswordDeleteErrorÚExceptionRaisedContexté   )Ú	file_basezwin32ctypes.pywintypes)Ú	win32credÚ
pywintypes)Ú_win_cryptoc                  C   ó6   t ƒ �} tj W d  ƒ n1 sw   Y  t| ƒ S )zŠ
    Does this environment have pywin32?
    Should return False even when Mercurial's Demand Import allowed import of
    win32cred.
    N)r   r   Ú__name__Úbool©Úexc© r   úF/var/www/html/env/lib/python3.10/site-packages/keyrings/alt/Windows.pyÚhas_pywin32(   ó   ÿ
r   c                  C   r   )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   r
   r   r   r   r   r   r   Úhas_wincrypto3   r   r   c                   @   s6   e Zd ZdZejedd„ ƒƒZdZdd„ Z	dd„ Z
d	S )
ÚEncryptedKeyringz=
    A File-based keyring secured by Windows Crypto API.
    c                 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   ÚpriorityC   s   zEncryptedKeyring.priorityzwincrypto_pass.cfgc                 C   ó
   t  |¡S )z1Encrypt the password using the CryptAPI.
        )r
   Úencrypt)r   Úpasswordr   r   r   r   P   ó   
zEncryptedKeyring.encryptc                 C   r   )z1Decrypt the password using the CryptAPI.
        )r
   Údecrypt)r   Úpassword_encryptedr   r   r   r"   U   r!   zEncryptedKeyring.decryptN)r   Ú
__module__Ú__qualname__Ú__doc__r   ÚClassPropertyÚclassmethodr   Úfilenamer   r"   r   r   r   r   r   >   s    	r   c                   @   sB   e Zd ZdZej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   a   s
   zRegistryKeyring.priorityc           	      C   sj   z(d| }t  t j|¡}t  ||¡d }| d¡}t |¡}t |¡ 	d¡}W |S  t
y4   d}Y |S w )z5Get password of the username for the service
        úSoftware\%s\Keyringr   Úasciiúutf-8N)ÚwinregÚOpenKeyÚHKEY_CURRENT_USERÚQueryValueExÚencodeÚbase64Údecodestringr
   r"   ÚdecodeÚEnvironmentError)	r   ÚserviceÚusernameÚkeyÚhkeyÚpassword_savedÚpassword_base64r#   r    r   r   r   Úget_passwordm   s   

þþzRegistryKeyring.get_passwordc           	      C   sR   t  | d¡¡}t |¡}| d¡}d| }t tj|¡}t 	||dtj
|¡ dS )z+Write the password to the registry
        r.   r-   r,   r   N)r
   r   r3   r4   Úencodestringr6   r/   Ú	CreateKeyr1   Ú
SetValueExÚREG_SZ)	r   r8   r9   r    r#   r=   r<   Úkey_namer;   r   r   r   Úset_password~   s   

zRegistryKeyring.set_passwordc                 C   sh   zd| }t  t j|dt j¡}t  ||¡ t  |¡ W n ty,   t ¡ d }t	|ƒ‚w |  
|¡ dS )z=Delete the password for the username of the service.
        r,   r   r   N)r/   r0   r1   ÚKEY_ALL_ACCESSÚDeleteValueÚCloseKeyÚWindowsErrorÚsysÚexc_infor   Ú_delete_key_if_empty)r   r8   r9   rC   r;   Úer   r   r   Údelete_password�   s   þþzRegistryKeyring.delete_passwordc                 C   s¢   d| }t  t j|dt j¡}z	t  |d¡ W d S  ty    Y nw t  |¡ |dkrO| d¡\}}}t  t j|dt j¡}t  ||¡ t  |¡ |}|dks*d S d S )Nr,   r   ÚSoftwareú\)	r/   r0   r1   rE   Ú	EnumValuerH   rG   Ú
rpartitionÚ	DeleteKey)r   r8   rC   r:   ÚparentÚsepÚbaser   r   r   rK   œ   s,   þÿ
þ
ùz$RegistryKeyring._delete_key_if_emptyN)r   r$   r%   r&   r   r'   r(   r   r>   rD   rM   rK   r   r   r   r   r*   [   s    
r*   c                   @   s<   e Zd ZdZdd„ Zedd„ ƒZedd„ ƒZedd	„ ƒZ	d
S )ÚOldPywinErrorzŠ
    A compatibility wrapper for old PyWin32 errors, such as reported in
    https://bitbucket.org/kang/python-keyring-lib/issue/140/
    c                 C   s
   || _ d S )N©Úorig)r   rX   r   r   r   Ú__init__¸   s   
zOldPywinError.__init__c                 C   ó
   | j d S )Nr   rW   r   r   r   r   Úfuncname»   ó   
zOldPywinError.funcnamec                 C   rZ   )Nr   rW   r   r   r   r   Úwinerror¿   r\   zOldPywinError.winerrorc                 C   s0   t  t|¡}tt|ddgƒƒ }|r| |ƒS |S )Nr[   r]   )Ú	functoolsÚpartialÚhasattrÚallÚmap)ÚclsÚorig_errÚ
attr_checkÚis_oldr   r   r   ÚwrapÃ   s   zOldPywinError.wrapN)
r   r$   r%   r&   rY   Úpropertyr[   r]   r(   rg   r   r   r   r   rV   ³   s    

rV   )rI   r4   r   r^   Úkeyring.utilr   Úkeyring.backendr   Úkeyring.errorsr   r   Ú r   Ú
__import__Úwin32ctypesr   r   ÚImportErrorr/   Ú_winregr
   r   r   ÚKeyringr   r*   ÚobjectrV   r   r   r   r   Ú<module>   sR    
ÿ€ûÿ€üÿX