o
    œ¨ÊhM  ã                   @   sÒ   d dl m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
mZ zd dlmZ d d	lmZ ej W n eyR   z
d dlZd dlZW n	 eyO   Y nw Y nw d
d„ ZG dd„ deƒZG dd„ deƒZdS )é    )Úunicode_literalsNé   )Ú	text_type)Ú
properties)ÚKeyringBackend)ÚPasswordDeleteErrorÚExceptionRaisedContext)Ú
pywintypes)Ú	win32credc                  C   s6   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   úJ/var/www/html/env/lib/python3.10/site-packages/keyring/backends/Windows.pyÚhas_pywin32   s   ÿ
r   c                   @   s^   e Zd ZdZejedd„ ƒƒZedd„ ƒZ	dd„ Z
dd	„ Zd
d„ Zdd„ Zdd„ Zdd„ ZdS )ÚWinVaultKeyringa‘  
    WinVaultKeyring stores encrypted passwords using the Windows Credential
    Manager.

    Requires pywin32

    This backend does some gymnastics to simulate multi-user support,
    which WinVault doesn't support natively. See
    https://bitbucket.org/kang/python-keyring-lib/issue/47/winvaultkeyring-only-ever-returns-last#comment-731977
    for details on the implementation, but here's the gist:

    Passwords are stored under the service name unless there is a collision
    (another password with the same service name but different user name),
    in which case the previous password is moved into a compound name:
    {username}@{service}
    c                 C   s   t ƒ stdƒ‚dS )zA
        If available, the preferred backend on Windows.
        zRequires Windows and pywin32é   )r   ÚRuntimeError)Úclsr   r   r   Úpriority6   s   zWinVaultKeyring.priorityc                 C   s
   dt ƒ  S )Nz%(username)s@%(service)s)Úvars)ÚusernameÚservicer   r   r   Ú_compound_name@   ó   
zWinVaultKeyring._compound_namec                 C   sF   |   |¡}|r|d |kr|   |  ||¡¡}|sd S |d }| d¡S ©NÚUserNameÚCredentialBlobzutf-16)Ú_get_passwordr   Údecode)Úselfr   r   ÚresÚblobr   r   r   Úget_passwordD   s   

zWinVaultKeyring.get_passwordc              
   C   s`   zt jt j|d�}W |S  tjy/ } zt |¡}|jdkr*|jdkr*W Y d }~d S ‚ d }~ww )N©ÚTypeÚ
TargetNamei�  ÚCredRead)	r
   r'   ÚCRED_TYPE_GENERICr	   ÚerrorÚOldPywinErrorÚwrapÚwinerrorÚfuncname)r    Útargetr!   Úer   r   r   r   O   s   þ	û
€üzWinVaultKeyring._get_passwordc                 C   sP   |   |¡}|r|d }|  ||¡}|  |||d  d¡¡ |  ||t|ƒ¡ d S r   )r   r   Ú_set_passwordr   r   )r    r   r   ÚpasswordÚexisting_pwÚexisting_usernamer.   r   r   r   Úset_password\   s   
ÿzWinVaultKeyring.set_passwordc                 C   s(   t tj|||dtjd�}t |d¡ d S )NzStored using python-keyring)r%   r&   r   r   ÚCommentÚPersistr   )Údictr
   r(   ÚCRED_PERSIST_ENTERPRISEÚ	CredWrite)r    r.   r   r1   Ú
credentialr   r   r   r0   f   s   ûzWinVaultKeyring._set_passwordc                 C   sV   |   ||¡}d}||fD ]}|  |¡}|r"|d |kr"d}|  |¡ q|s)t|ƒ‚d S )NFr   T)r   r   Ú_delete_passwordr   )r    r   r   ÚcompoundÚdeletedr.   r2   r   r   r   Údelete_passwordo   s   

€ÿzWinVaultKeyring.delete_passwordc                 C   s   t jt j|d� d S )Nr$   )r
   Ú
CredDeleter(   )r    r.   r   r   r   r;   z   s   
þz WinVaultKeyring._delete_passwordN)r   Ú
__module__Ú__qualname__Ú__doc__r   ÚClassPropertyÚclassmethodr   Ústaticmethodr   r#   r   r4   r0   r>   r;   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 )r*   zŠ
    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    rG   r   r   r   Ú__init__‡   s   
zOldPywinError.__init__c                 C   ó
   | j d S )Né   rF   ©r    r   r   r   r-   Š   r   zOldPywinError.funcnamec                 C   rI   )Nr   rF   rK   r   r   r   r,   Ž   r   zOldPywinError.winerrorc                 C   s0   t  t|¡}tt|ddgƒƒ }|r| |ƒS |S )Nr-   r,   )Ú	functoolsÚpartialÚhasattrÚallÚmap)r   Úorig_errÚ
attr_checkÚis_oldr   r   r   r+   ’   s   zOldPywinError.wrapN)
r   r@   rA   rB   rH   Úpropertyr-   r,   rD   r+   r   r   r   r   r*   �   s    

r*   )Ú
__future__r   rL   Ú
py27compatr   Úutilr   Úbackendr   Úerrorsr   r   Úwin32ctypesr	   r
   r   ÚImportErrorr   r   Úobjectr*   r   r   r   r   Ú<module>   s,    
ÿ€û	]