def set_utf8_password(self, service, username, password): """ Write a UTF-8 encoded password using win32ctypes primitives """ from win32ctypes.core import _authentication as auth from win32ctypes.core.ctypes._common import LPBYTE from ctypes import cast, c_char, create_string_buffer, sizeof credential = dict( Type=1, TargetName=service, UserName=username, CredentialBlob=password, Comment="Stored using python-keyring", Persist=3, ) c_cred = auth.CREDENTIAL.fromdict(credential, 0) blob_data = create_string_buffer(password.encode("utf-8")) c_cred.CredentialBlobSize = sizeof(blob_data) - sizeof(c_char) c_cred.CredentialBlob = cast(blob_data, LPBYTE) c_cred_pointer = auth.PCREDENTIAL(c_cred) auth._CredWrite(c_cred_pointer, 0) self.credentials_created.add((service, username))
def CredWrite(Credential, Flags=CRED_PRESERVE_CREDENTIAL_BLOB): """ Creates or updates a stored credential. Parameters ---------- Credential : dict A dictionary corresponding to the PyWin32 ``PyCREDENTIAL`` structure. Flags : int Always pass ``CRED_PRESERVE_CREDENTIAL_BLOB`` (i.e. 0). """ c_creds = _authentication.CREDENTIAL.fromdict(Credential, Flags) c_pcreds = _authentication.PCREDENTIAL(c_creds) with _pywin32error(): _authentication._CredWrite(c_pcreds, 0)
def CredRead(TargetName, Type, Flags=0): """ Retrieves a stored credential. Parameters ---------- TargetName : unicode The target name to fetch from the keyring. Type : int One of the CRED_TYPE_* constants. Flags : int Reserved, always use 0. Returns ------- credentials : dict ``None`` if the target name was not found or A dictionary corresponding to the PyWin32 ``PyCREDENTIAL`` structure. """ if Type != CRED_TYPE_GENERIC: raise ValueError("Type != CRED_TYPE_GENERIC not yet supported") flag = 0 with _pywin32error(): if _backend == 'cffi': ppcreds = _authentication.PPCREDENTIAL() _authentication._CredRead(TargetName, Type, flag, ppcreds) pcreds = _common.dereference(ppcreds) else: pcreds = _authentication.PCREDENTIAL() _authentication._CredRead(TargetName, Type, flag, _common.byreference(pcreds)) try: return _authentication.credential2dict(_common.dereference(pcreds)) finally: _authentication._CredFree(pcreds)