o
    Vh_                  	   @  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
 ddlmZ e *Zzd d	lmZmZ ej W n eyM   d dlZd dlZej Y nw W d   n1 sXw   Y  eeZG d
d dZG dd deZG dd deZdS )    )annotationsN)ExceptionTrap   )KeyringBackend)
properties)SimpleCredential)PasswordDeleteError)
pywintypes	win32credc                   @  s   e Zd ZdddZdd ZdS )PersistenceNc                 C  s   t |dtjS )N_persist)getattrr
   CRED_PERSIST_ENTERPRISE)selfkeyringtype r   l/var/www/html/construction_image-detection-poc/venv/lib/python3.10/site-packages/keyring/backends/Windows.py__get__   s   zPersistence.__get__c                 C  s2   t |trd|dd  }tt|}||_dS )z
        Set the persistence value on the Keyring. Value may be
        one of the win32cred.CRED_PERSIST_* constants or a
        string representing one of those constants. For example,
        'local machine' or 'session'.
        CRED_PERSIST_ _N)
isinstancestrreplaceupperr   r
   r   )r   r   valueattrr   r   r   __set__"   s   


zPersistence.__set__N)__name__
__module____qualname__r   r   r   r   r   r   r      s    
r   c                   @  s   e Zd Zedd ZdS )DecodingCredentialc                 C  s@   | d }z| dW S  ty   | d}td | Y S w )zM
        Attempt to decode the credential blob as UTF-16 then UTF-8.
        CredentialBlobzutf-16zutf-8zjRetrieved a UTF-8 encoded credential. Please be aware that this library only writes credentials in UTF-16.)decodeUnicodeDecodeErrorlogwarning)r   creddecoded_cred_utf8r   r   r   r   0   s   
zDecodingCredential.valueN)r    r!   r"   propertyr   r   r   r   r   r#   /   s    r#   c                   @  st   e Zd ZdZe ZejdddZe	dd Z
dd	 ZdddZdd Zdd Zdd Zdd Zdd Zdd ZdS ) WinVaultKeyringaf  
    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://github.com/jaraco/keyring/issues/47#issuecomment-75763152
    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}
    returnfloatc                 C  s   t rtddS )zA
        If available, the preferred backend on Windows.
        zRequires Windows and pywin32   )missing_depsRuntimeError)clsr   r   r   priorityU   s   zWinVaultKeyring.priorityc                 C  s   |  d| S )N@r   )usernameservicer   r   r   _compound_name^   s   zWinVaultKeyring._compound_namec                 C  s   |  ||}|o
|jS r   )_resolve_credentialr   r   r6   r5   resr   r   r   get_passwordb   s   
zWinVaultKeyring.get_passwordr6   r   r5   
str | NoneDecodingCredential | Nonec                 C  s4   |  |}|r|r|d |kr|  | ||}|S NUserName)_read_credentialr7   r9   r   r   r   r8   f   s   
z#WinVaultKeyring._resolve_credentialc              
   C  sZ   zt jt j|d}W t|S  tjy, } z|jdkr'|jdkr'W Y d }~d S  d }~ww )NType
TargetName  CredRead)r
   rE   CRED_TYPE_GENERICr	   errorwinerrorfuncnamer#   )r   targetr:   er   r   r   r@   p   s   z WinVaultKeyring._read_credentialc                 C  sH   |  |}|r|d }| ||}| |||j | ||t| d S r>   )r@   r7   _set_passwordr   r   )r   r6   r5   passwordexisting_pwexisting_usernamerJ   r   r   r   set_password{   s   
zWinVaultKeyring.set_passwordc                 C  s(   t tj|||d| jd}t|d d S )NzStored using python-keyring)rB   rC   r?   r$   CommentPersistr   )dictr
   rF   persist	CredWrite)r   rJ   r5   rM   
credentialr   r   r   rL      s   zWinVaultKeyring._set_passwordc                 C  sV   |  ||}d}||fD ]}| |}|r"|d |kr"d}| | q|s)t|d S )NFr?   T)r7   r@   _delete_passwordr   )r   r6   r5   compounddeletedrJ   rN   r   r   r   delete_password   s   

zWinVaultKeyring.delete_passwordc              
   C  sV   zt jt j|d W d S  tjy* } z|jdkr%|jdkr%W Y d }~d S  d }~ww )NrA   rD   
CredDelete)r
   r[   rF   r	   rG   rH   rI   )r   rJ   rK   r   r   r   rW      s   z WinVaultKeyring._delete_passwordc                 C  s    |  ||}|ot|d |jS r>   )r8   r   r   r9   r   r   r   get_credential   s   zWinVaultKeyring.get_credentialN)r-   r.   )r6   r   r5   r<   r-   r=   )r    r!   r"   __doc__r   rT   r   classpropertyr3   staticmethodr7   r;   r8   r@   rP   rL   rZ   rW   r\   r   r   r   r   r,   A   s    


r,   )
__future__r   loggingjaraco.contextr   backendr   compatr   credentialsr   errorsr   r0   win32ctypes.pywin32r	   r
   r    ImportError	getLoggerr'   r   rS   r#   r,   r   r   r   r   <module>   s,    


