o a @sddlZddlmZddlmZddlmZddlmZm Z e .Z zddl m Z ddl m Z e jWneyGddl Z ddl Z e jYnwWdn1sRwYeeZGd d d ZGd d d eZGd ddeZdS)N) properties)KeyringBackend)SimpleCredential)PasswordDeleteErrorExceptionRaisedContext) pywintypes) win32credc@seZdZdddZddZdS) PersistenceNcCst|dtjS)N_persist)getattrr CRED_PERSIST_ENTERPRISE)selfkeyringtyper:/usr/lib/python3/dist-packages/keyring/backends/Windows.py__get__szPersistence.__get__cCs8t|trd|dd}tt|}t|d|dS)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) isinstancestrreplaceupperr r setattr)rrvalueattrrrr__set__ s  zPersistence.__set__)N)__name__ __module__ __qualname__rrrrrrr s  r c@seZdZeddZdS)DecodingCredentialcCs@|d}z|dWSty|d}td|YSw)zM Attempt to decode the credential blob as UTF-16 then UTF-8. CredentialBlobzutf-16zutf-8zkRetrieved an UTF-8 encoded credential. Please be aware that this library only writes credentials in UTF-16.)decodeUnicodeDecodeErrorlogwarning)rcreddecoded_cred_utf8rrrr.s   zDecodingCredential.valueN)rr r!propertyrrrrrr"-sr"c@sleZdZdZeZejeddZ e ddZ ddZ dd Z d d Zd d ZddZddZddZdS)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} cCstrtddS)zA If available, the preferred backend on Windows. zRequires Windows and pywin32) missing_deps RuntimeError)clsrrrprioritySszWinVaultKeyring.prioritycCs|d|S)N@r)usernameservicerrr_compound_name]szWinVaultKeyring._compound_namecCs:||}|r |d|kr||||}|sdS|jSNUserName) _get_passwordr4rrr3r2resrrr get_passwordas zWinVaultKeyring.get_passwordc CsZz tjtj|d}Wt|Stjy,}z|jdkr'|jdkr'WYd}~dSd}~ww)NType TargetNameCredRead)r r?CRED_TYPE_GENERICrerrorwinerrorfuncnamer")rtargetr9errrr7kszWinVaultKeyring._get_passwordcCsH||}|r|d}|||}||||j|||t|dSr5)r7r4 _set_passwordrr)rr3r2password existing_pwexisting_usernamerDrrr set_passwordvs  zWinVaultKeyring.set_passwordcCs(ttj|||d|jd}t|ddS)NzStored using python-keyring)r<r=r6r#CommentPersistr)dictr r@persist CredWrite)rrDr2rG credentialrrrrFszWinVaultKeyring._set_passwordcCsV|||}d}||fD]}||}|r"|d|kr"d}||q |s)t|dS)NFr6T)r4r7_delete_passwordr)rr3r2compounddeletedrDrHrrrdelete_passwords    zWinVaultKeyring.delete_passwordc CsVz tjtj|dWdStjy*}z|jdkr%|jdkr%WYd}~dSd}~ww)Nr;r> CredDelete)r rUr@rrArBrC)rrDrErrrrQsz WinVaultKeyring._delete_passwordcCs@d}|r ||||}|s||}|sdSt|d|jSr5)r7r4rrr8rrrget_credentials zWinVaultKeyring.get_credentialN)rr r!__doc__r rNr ClassProperty classmethodr0 staticmethodr4r:r7rJrFrTrQrVrrrrr+?s       r+)loggingutilrbackendr credentialsrerrorsrrr-win32ctypes.pywin32rr r ImportError getLoggerr&r rMr"r+rrrrs*