
    /d                     V   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	  e	            5 Z
	 d dlmZ d dlmZ ej         n# e$ r d dlZd dlZej         Y nw xY wddd           n# 1 swxY w Y    e j        e          Z G d	 d
          Z G d de          Z G d de          ZdS )    N   )
properties)KeyringBackend)SimpleCredential)PasswordDeleteErrorExceptionRaisedContext)
pywintypes)	win32credc                       e Zd ZddZd ZdS )PersistenceNc                 8    t          |dt          j                  S )N_persist)getattrr
   CRED_PERSIST_ENTERPRISE)selfkeyringtypes      8lib/python3.11/site-packages/keyring/backends/Windows.py__get__zPersistence.__get__   s    w
I,MNNN    c                     t          |t                    r@d|                    dd                                          z   }t	          t
          |          }t          |d|           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_ _r   N)
isinstancestrreplaceupperr   r
   setattr)r   r   valueattrs       r   __set__zPersistence.__set__    sa     eS!! 	-"U]]3%<%<%B%B%D%DDDIt,,EU+++++r   )N)__name__
__module____qualname__r   r"    r   r   r   r      s;        O O O O
, 
, 
, 
, 
,r   r   c                   $    e Zd Zed             ZdS )DecodingCredentialc                     | d         }	 |                     d          S # t          $ r4 |                     d          }t                              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-8zkRetrieved an 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.value.   s}    
 $%	%;;x(((! 	% 	% 	% $G 4 4KKB   %$$$	%s    ;AAN)r#   r$   r%   propertyr    r&   r   r   r(   r(   -   s-        % % X% % %r   r(   c                       e Zd ZdZ e            Zej        d             Ze	d             Z
d Zd Zd Zd Zd Zd	 Z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}
    c                 2    t           rt          d          dS )zA
        If available, the preferred backend on Windows.
        zRequires Windows and pywin32   )missing_depsRuntimeError)clss    r   priorityzWinVaultKeyring.priorityS   s     
  	?=>>>qr   c                     |  d| S )N@r&   )usernameservices     r   _compound_namezWinVaultKeyring._compound_name\   s    &&W&&&r   c                     |                      |          }|r|d         |k    r)|                      |                     ||                    }|sd S |j        S NUserName)_get_passwordr>   r    r   r=   r<   ress       r   get_passwordzWinVaultKeyring.get_password`   sg      )) 	Mc*o1 	M$$T%8%87%K%KLLC 	4yr   c                     	 t          j        t           j        |          }n4# t          j        $ r"}|j        dk    r|j        dk    rY d }~d S  d }~ww xY wt          |          S )NType
TargetName  CredRead)r
   rK   CRED_TYPE_GENERICr	   errorwinerrorfuncnamer(   )r   targetrD   es       r   rB   zWinVaultKeyring._get_passwordj   s    	$0V  CC  	 	 	zT! ajJ&> ttttt	 "#&&&s    # AAAAc                     |                      |          }|r:|d         }|                     ||          }|                     |||j                   |                     ||t	          |                     d S r@   )rB   r>   _set_passwordr    r   )r   r=   r<   passwordexisting_pwexisting_usernamerP   s          r   set_passwordzWinVaultKeyring.set_passwordu   s    ((11 	 +J 7(():GDDF!!  
 	7Hc(mm<<<<<r   c                 x    t          t          j        |||d| j                  }t          j        |d           d S )NzStored using python-keyring)rH   rI   rA   r*   CommentPersistr   )dictr
   rL   persist	CredWrite)r   rP   r<   rT   
credentials        r   rS   zWinVaultKeyring._set_password   sH    ,#1L
 
 

 	J*****r   c                     |                      ||          }d}||fD ]<}|                     |          }|r#|d         |k    rd}|                     |           =|st          |          d S )NFrA   T)r>   rB   _delete_passwordr   )r   r=   r<   compounddeletedrP   rU   s          r   delete_passwordzWinVaultKeyring.delete_password   s    &&x99x' 	. 	.F,,V44K .{:6(B .%%f--- 	/%g...	/ 	/r   c                     	 t          j        t           j        |           d S # t          j        $ r"}|j        dk    r|j        dk    rY d }~d S  d }~ww xY w)NrG   rJ   
CredDelete)r
   re   rL   r	   rM   rN   rO   )r   rP   rQ   s      r   r`   z WinVaultKeyring._delete_password   su    	 i&AfUUUUUU 	 	 	zT! ajL&@ 	s    $ AAAAc                     d }|r)|                      |                     ||                    }|s|                      |          }|sd S t          |d         |j                  S r@   )rB   r>   r   r    rC   s       r   get_credentialzWinVaultKeyring.get_credential   sq     	M$$T%8%87%K%KLLC 	$$W--C tJ;;;r   N)r#   r$   r%   __doc__r   r\   r   classpropertyr9   staticmethodr>   rE   rB   rW   rS   rc   r`   rg   r&   r   r   r3   r3   ?   s         " kmmG   ' ' \'  	' 	' 	'= = =	+ 	+ 	+	/ 	/ 	/  
< 
< 
< 
< 
<r   r3   )logging_compatr   backendr   credentialsr   errorsr   r   r6   win32ctypes.pywin32r	   r
   r#   ImportError	getLoggerr-   r   r[   r(   r3   r&   r   r   <module>rs      s                $ $ $ $ $ $ * * * * * * @ @ @ @ @ @ @ @  222222111111 	    	                g!!, , , , , , , ,"% % % % % % % %$k< k< k< k< k<n k< k< k< k< k<s1   A%?A%AA%AA%%A),A)