
    Ri_                    X   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       5 Z	 d d	lmZmZ ej"                   ddd        ej&                  e      Z G d
 d      Z G d de      Z G d de      Zy# e$ r d dlZd dlZej"                   Y Vw xY w# 1 sw Y   [xY w)    )annotationsN)ExceptionTrap   )KeyringBackend)
properties)SimpleCredential)PasswordDeleteError)
pywintypes	win32credc                      e Zd ZddZd Zy)PersistenceNc                8    t        |dt        j                        S )N_persist)getattrr   CRED_PERSIST_ENTERPRISE)selfkeyringtypes      ~/home/jay/workspace/.worktrees/task-2117-dev1/scripts/.codegraph-venv/lib/python3.12/site-packages/keyring/backends/Windows.py__get__zPersistence.__get__   s    w
I,M,MNN    c                    t        |t              r3d|j                  dd      j                         z   }t	        t
        |      }||_        y)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attrs       r   __set__zPersistence.__set__"   s@     eS!"U]]3%<%B%B%DDDIt,E r   N)__name__
__module____qualname__r   r"    r   r   r   r      s    O
!r   r   c                      e Zd Zed        Zy)DecodingCredentialc                    | d   }	 |j                  d      S # t        $ r+ |j                  d      }t        j                  d       |cY S w xY 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_utf8s      r   r    zDecodingCredential.value0   s[    
 $%	%;;x((! 	% $G 4KKB %$	%s    1AAN)r$   r%   r&   propertyr    r'   r   r   r)   r)   /   s    % %r   r)   c                      e Zd ZdZ e       Zej                  dd       Ze	d        Z
d Z	 	 	 	 	 	 ddZd Zd Zd Zd	 Zd
 Zd Zy)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}
    c                &    t         rt        d      y)zA
        If available, the preferred backend on Windows.
        zRequires Windows and pywin32   )missing_depsRuntimeError)clss    r   priorityzWinVaultKeyring.priorityU   s    
 =>>r   c                    |  d| S )N@r'   )usernameservices     r   _compound_namezWinVaultKeyring._compound_name^   s    1WI&&r   c                F    | j                  ||      }|xr |j                  S r#   )_resolve_credentialr    r   r>   r=   ress       r   get_passwordzWinVaultKeyring.get_passwordb   s#    &&w9 syy r   c                    | j                  |      }|r
|r)|d   |k7  r!| j                  | j                  ||            }|S NUserName)_read_credentialr?   rB   s       r   rA   z#WinVaultKeyring._resolve_credentialf   sF     ##G,h3z?h#>''(;(;Hg(NOC
r   c                    	 t        j                  t         j                  |      }t        |      S # t        j                  $ r)}|j
                  dk(  r|j                  dk(  rY d }~y  d }~ww xY w)NType
TargetName  CredRead)r   rN   CRED_TYPE_GENERICr
   errorwinerrorfuncnamer)   )r   targetrC   es       r   rH   z WinVaultKeyring._read_credentialp   sd    	$$00VC "#&&	  	zzT!ajjJ&>	s   %2 A.A)(A))A.c                    | j                  |      }|r4|d   }| j                  ||      }| j                  |||j                         | j                  ||t	        |             y rF   )rH   r?   _set_passwordr    r   )r   r>   r=   passwordexisting_pwexisting_usernamerS   s          r   set_passwordzWinVaultKeyring.set_password{   sh    ++G4 +J 7(():GDF!!!
 	7Hc(m<r   c                    t        t        j                  |||d| j                        }t        j                  |d       y )NzStored using python-keyring)rK   rL   rG   r+   CommentPersistr   )dictr   rO   persist	CredWrite)r   rS   r=   rW   
credentials        r   rV   zWinVaultKeyring._set_password   s:    ,,#1LL

 	J*r   c                    | j                  ||      }d}||fD ]2  }| j                  |      }|s|d   |k(  s d}| j                  |       4 |st        |      y )NFrG   T)r?   rH   _delete_passwordr	   )r   r>   r=   compounddeletedrS   rX   s          r   delete_passwordzWinVaultKeyring.delete_password   st    &&x9x' 	.F//7K{:6(B%%f-		.
 %g.. r   c                    	 t        j                  t         j                  |       y # t        j                  $ r)}|j
                  dk(  r|j                  dk(  rY d }~y  d }~ww xY w)NrJ   rM   
CredDelete)r   rh   rO   r
   rP   rQ   rR   )r   rS   rT   s      r   rc   z WinVaultKeyring._delete_password   sP    	  i&A&AfU 	zzT!ajjL&@	s   %( A$AAA$c                `    | j                  ||      }|xr t        |d   |j                        S rF   )rA   r   r    rB   s       r   get_credentialzWinVaultKeyring.get_credential   s/    &&w9C'JCCr   N)returnfloat)r>   r   r=   z
str | Nonerk   zDecodingCredential | None)r$   r%   r&   __doc__r   r_   r   classpropertyr:   staticmethodr?   rD   rA   rH   rZ   rV   rf   rc   rj   r'   r   r   r4   r4   A   s}    " mG  ' '!&0	"	'=	+	/Dr   r4   )
__future__r   loggingjaraco.contextr   backendr   compatr   credentialsr   errorsr	   r7   win32ctypes.pywin32r
   r   r$   ImportError	getLoggerr.   r   r^   r)   r4   r'   r   r   <module>rz      s    "  ( $  * (_ = 	 g!! !"% %$gDn gD]   	 s(   B BBB BB  B)