Skip to content

Commit 06afaf5

Browse files
committed
Remove constrains to AttributeCount and Attribute
This is a cleanup commit. The code is still not fully functional.
1 parent a9fdb43 commit 06afaf5

File tree

2 files changed

+28
-43
lines changed

2 files changed

+28
-43
lines changed

win32ctypes/core/ctypes/_authentication.py

Lines changed: 27 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,6 @@
1616
from ._nl_support import _GetACP
1717

1818

19-
SUPPORTED_CREDKEYS = set((
20-
u'Type', u'TargetName', u'Persist',
21-
u'UserName', u'Comment', u'CredentialBlob'))
22-
23-
2419
class CREDENTIAL(Structure):
2520
_fields_ = [
2621
("Flags", DWORD),
@@ -31,38 +26,26 @@ class CREDENTIAL(Structure):
3126
("CredentialBlobSize", DWORD),
3227
("CredentialBlob", LPBYTE),
3328
("Persist", DWORD),
34-
("_DO_NOT_USE_AttributeCount", DWORD),
35-
("__DO_NOT_USE_Attribute", c_void_p),
29+
("AttributeCount", DWORD),
30+
("Attribute", c_void_p),
3631
("TargetAlias", c_wchar_p),
3732
("UserName", c_wchar_p)]
3833

3934
@classmethod
40-
def fromdict(cls, credential, flags=0):
41-
unsupported = set(credential.keys()) - SUPPORTED_CREDKEYS
42-
if len(unsupported):
43-
raise ValueError("Unsupported keys: {0}".format(unsupported))
44-
if flags != 0:
45-
raise ValueError("flag != 0 not yet supported")
46-
35+
def fromdict(cls, credential):
4736
c_creds = cls()
4837
c_pcreds = PCREDENTIAL(c_creds)
4938

5039
# zero-out memory
5140
ctypes.memset(c_pcreds, 0, ctypes.sizeof(c_creds))
5241

53-
for key in SUPPORTED_CREDKEYS:
54-
if key in credential:
55-
if key != 'CredentialBlob':
56-
setattr(c_creds, key, credential[key])
57-
else:
58-
blob = make_unicode(credential['CredentialBlob'])
59-
blob_data = ctypes.create_unicode_buffer(blob)
60-
# Create_unicode_buffer adds a NULL at the end of the
61-
# string we do not want that.
62-
c_creds.CredentialBlobSize = \
63-
ctypes.sizeof(blob_data) - \
64-
ctypes.sizeof(ctypes.c_wchar)
65-
c_creds.CredentialBlob = ctypes.cast(blob_data, LPBYTE)
42+
for key in credential:
43+
if key == 'CredentialBlob':
44+
blob_data, blob_size = _make_blob(credential['CredentialBlob'])
45+
c_creds.CredentialBlob = ctypes.cast(blob_data, LPBYTE)
46+
c_creds.CredentialBlobSize = blob_size
47+
else:
48+
setattr(c_creds, key, credential[key])
6649
return c_creds
6750

6851

@@ -71,30 +54,32 @@ def fromdict(cls, credential, flags=0):
7154
PPPCREDENTIAL = POINTER(PPCREDENTIAL)
7255

7356

74-
def make_unicode(text):
75-
""" Convert the input string to unicode.
76-
77-
"""
78-
if is_text(text):
79-
return text
80-
else:
81-
code_page = _GetACP()
82-
return text.decode(encoding=str(code_page), errors='strict')
83-
84-
8557
def credential2dict(creds):
8658
credential = {}
87-
for key in SUPPORTED_CREDKEYS:
88-
if key != u'CredentialBlob':
89-
credential[key] = getattr(creds, key)
90-
else:
59+
for key, type_ in CREDENTIAL._fields_:
60+
if key == u'CredentialBlob':
9161
blob = _PyBytes_FromStringAndSize(
9262
cast(creds.CredentialBlob, c_char_p),
9363
creds.CredentialBlobSize)
9464
credential[u'CredentialBlob'] = blob
65+
else:
66+
credential[key] = getattr(creds, key)
9567
return credential
9668

9769

70+
def _make_blob(data):
71+
""" Convert a string to credential compatible blob dict values
72+
73+
"""
74+
blob_data = ctypes.create_unicode_buffer(data)
75+
# Create_unicode_buffer adds a NULL at the end of the
76+
# string we do not want that.
77+
blob_size = (
78+
ctypes.sizeof(blob_data) - ctypes.sizeof(ctypes.c_wchar))
79+
blob_pointer = ctypes.cast(blob_data, LPBYTE)
80+
return blob_pointer, blob_size
81+
82+
9883
def _CredEnumerate(Filter, Flags, Count, pppCredential):
9984
filter_ = LPCWSTR(Filter)
10085
_BaseCredEnumerate(filter_, Flags, Count, pppCredential)

win32ctypes/pywin32/win32cred.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ def CredWrite(Credential, Flags=0):
3131
``CRED_PRESERVE_CREDENTIAL_BLOB`` or 0. Default is 0.
3232
3333
"""
34-
c_creds = _authentication.CREDENTIAL.fromdict(Credential, Flags)
34+
c_creds = _authentication.CREDENTIAL.fromdict(Credential)
3535
c_pcreds = _authentication.PCREDENTIAL(c_creds)
3636
with _pywin32error():
3737
_authentication._CredWrite(c_pcreds, 0)

0 commit comments

Comments
 (0)