
    e                     L   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                   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)    N   )
properties)KeyringBackend)SimpleCredential)PasswordDeleteErrorExceptionRaisedContext)
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      :/usr/lib/python3/dist-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
        |      }t        |d|       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_ _r   N)
isinstancestrreplaceupperr   r
   setattr)r   r   valueattrs       r   __set__zPersistence.__set__    sD     eS!"U]]3%<%B%B%DDDIt,EU+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.value.   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efd       Z	e
d        Z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}
    returnc                 &    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.priorityS   s    
 =>>r   c                     |  d| S )N@r&   )usernameservices     r   _compound_namezWinVaultKeyring._compound_name\   s    1WI&&r   c                     | j                  |      }|r|d   |k7  r!| j                  | j                  ||            }|sy |j                  S NUserName)_get_passwordr?   r    r   r>   r=   ress       r   get_passwordzWinVaultKeyring.get_password`   sM      )c*o1$$T%8%87%KLCyy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
   rL   CRED_TYPE_GENERICr	   errorwinerrorfuncnamer(   )r   targetrE   es       r   rC   zWinVaultKeyring._get_passwordj   sd    	$$00VC "#&&	  	zzT!ajjJ&>	s   %2 A.A)(A))A.c                     | j                  |      }|r4|d   }| j                  ||      }| j                  |||j                         | j                  ||t	        |             y rA   )rC   r?   _set_passwordr    r   )r   r>   r=   passwordexisting_pwexisting_usernamerQ   s          r   set_passwordzWinVaultKeyring.set_passwordu   sh    ((1 +J 7(():GDF!!!
 	7Hc(m<r   c                     t        t        j                  |||d| j                        }t        j                  |d       y )NzStored using python-keyring)rI   rJ   rB   r*   CommentPersistr   )dictr
   rM   persist	CredWrite)r   rQ   r=   rU   
credentials        r   rT   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 )NFrB   T)r?   rC   _delete_passwordr   )r   r>   r=   compounddeletedrQ   rV   s          r   delete_passwordzWinVaultKeyring.delete_password   st    &&x9x' 	.F,,V4K{: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)NrH   rK   
CredDelete)r
   rf   rM   r	   rN   rO   rP   )r   rQ   rR   s      r   ra   z WinVaultKeyring._delete_password   sP    	  i&A&AfU 	zzT!ajjL&@	s   %( A$AAA$c                     d }|r!| j                  | j                  ||            }|s| j                  |      }|sy t        |d   |j                        S rA   )rC   r?   r   r    rD   s       r   get_credentialzWinVaultKeyring.get_credential   sU    $$T%8%87%KLC$$W-CJ;;r   N)r#   r$   r%   __doc__r   r]   r   classpropertyfloatr:   staticmethodr?   rF   rC   rX   rT   rd   ra   rh   r&   r   r   r3   r3   ?   sf    " mG   ' '	'=	+	/
<r   r3   )logging_compatr   backendr   credentialsr   errorsr   r   r7   win32ctypes.pywin32r	   r
   r#   ImportError	getLoggerr-   r   r\   r(   r3   r&   r   r   <module>ru      s       $ * @  21 	  g!, ,"% %$k<n k<]   	 s(   BA;;BBBBB#