Skip to content

Commit

Permalink
do not decode result_string returned by set_password (#52)
Browse files Browse the repository at this point in the history
* set_password bytes result_string

* CI bump version

* CI revert version

---------

Co-authored-by: zarganum <[email protected]>
  • Loading branch information
zarganum and zarganum authored Aug 27, 2024
1 parent 8f3983e commit e634609
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 13 deletions.
4 changes: 1 addition & 3 deletions src/krb5/_set_password.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,16 @@ class SetPasswordResult(typing.NamedTuple):
KRB5_KPASSWD_HARDERROR (2) - Server error\n
KRB5_KPASSWD_AUTHERROR (3) - Authentication error\n
KRB5_KPASSWD_SOFTERROR (4) - Password change rejected\n
Note the `result_code_string` is a byte string.
The `result_string` is a server protocol response that may contain useful
information about password policy violations or other errors.
It is decoded as a `string` according to ``RFC 3244``
"""

result_code: int
"""The library result code of the password change operation."""
result_code_string: bytes
"""The byte string representation of the result code."""
result_string: str
result_string: bytes
"""Server response string"""

def set_password(
Expand Down
12 changes: 6 additions & 6 deletions src/krb5/_set_password.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -87,16 +87,16 @@ def set_password(
if length == 0:
result_code_bytes = b""
else:
result_code_bytes = value[:length]
result_code_bytes = <bytes>value[:length]

pykrb5_get_krb5_data(&result_string, &length, &value)

if length == 0:
result_string_bytes = b""
else:
result_string_bytes = value[:length]
result_string_bytes = <bytes>value[:length]

return SetPasswordResult(result_code, result_code_bytes, result_string_bytes.decode("utf-8"))
return SetPasswordResult(result_code, result_code_bytes, result_string_bytes)

finally:
pykrb5_free_data_contents(context.raw, &result_code_string)
Expand Down Expand Up @@ -147,16 +147,16 @@ def set_password_using_ccache(
if length == 0:
result_code_bytes = b""
else:
result_code_bytes = value[:length]
result_code_bytes = <bytes>value[:length]

pykrb5_get_krb5_data(&result_string, &length, &value)

if length == 0:
result_string_bytes = b""
else:
result_string_bytes = value[:length]
result_string_bytes = <bytes>value[:length]

return SetPasswordResult(result_code, result_code_bytes, result_string_bytes.decode("utf-8"))
return SetPasswordResult(result_code, result_code_bytes, result_string_bytes)

finally:
pykrb5_free_data_contents(context.raw, &result_code_string)
Expand Down
8 changes: 4 additions & 4 deletions tests/test_changepw.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,12 @@ def test_set_password(realm: k5test.K5Realm) -> None:
(result_code, result_code_string, result_string) = krb5.set_password(ctx, creds, empty_password.encode())
assert result_code != 0
assert result_code_string.find(b"rejected") > 0
assert result_string.find("too short") > 0
assert result_string.find(b"too short") > 0

(result_code, result_code_string, result_string) = krb5.set_password(ctx, creds, weak_password.encode())
assert result_code != 0
assert result_code_string.find(b"rejected") > 0
assert result_string.find("too short") > 0
assert result_string.find(b"too short") > 0

(result_code, result_code_string, result_string) = krb5.set_password(ctx, creds, new_password.encode())
assert result_code == 0
Expand Down Expand Up @@ -91,14 +91,14 @@ def test_set_password(realm: k5test.K5Realm) -> None:
)
assert result_code != 0
assert result_code_string.find(b"rejected") > 0
assert result_string.find("too short") > 0
assert result_string.find(b"too short") > 0

(result_code, result_code_string, result_string) = krb5.set_password_using_ccache(
ctx, cc, weak_password.encode(), princ
)
assert result_code != 0
assert result_code_string.find(b"rejected") > 0
assert result_string.find("too short") > 0
assert result_string.find(b"too short") > 0

(result_code, result_code_string, result_string) = krb5.set_password_using_ccache(
ctx, cc, new_password.encode(), princ
Expand Down

0 comments on commit e634609

Please sign in to comment.