Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion kasa/aestransport.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,8 +117,10 @@ def default_port(self) -> int:
return self.DEFAULT_PORT

@property
def credentials_hash(self) -> str:
def credentials_hash(self) -> str | None:
"""The hashed credentials used by the transport."""
if self._credentials == Credentials():
return None
return base64.b64encode(json_dumps(self._login_params).encode()).decode()

def _get_login_params(self, credentials: Credentials) -> dict[str, str]:
Expand Down
4 changes: 3 additions & 1 deletion kasa/klaptransport.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,8 +132,10 @@ def default_port(self):
return self.DEFAULT_PORT

@property
def credentials_hash(self) -> str:
def credentials_hash(self) -> str | None:
"""The hashed credentials used by the transport."""
if self._credentials == Credentials():
return None
return base64.b64encode(self._local_auth_hash).decode()

async def perform_handshake1(self) -> tuple[bytes, bytes, bytes]:
Expand Down
2 changes: 1 addition & 1 deletion kasa/protocol.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ def default_port(self) -> int:

@property
@abstractmethod
def credentials_hash(self) -> str:
def credentials_hash(self) -> str | None:
"""The hashed credentials used by the transport."""

@abstractmethod
Expand Down
4 changes: 2 additions & 2 deletions kasa/tests/fakeprotocol_iot.py
Original file line number Diff line number Diff line change
Expand Up @@ -234,8 +234,8 @@ def default_port(self) -> int:
return 9999

@property
def credentials_hash(self) -> str:
return ""
def credentials_hash(self) -> None:
return None

def set_alias(self, x, child_ids=None):
if child_ids is None:
Expand Down
64 changes: 63 additions & 1 deletion kasa/tests/test_protocol.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

from ..aestransport import AesTransport
from ..credentials import Credentials
from ..device import Device
from ..deviceconfig import DeviceConfig
from ..exceptions import KasaException
from ..iotprotocol import IotProtocol, _deprecated_TPLinkSmartHomeProtocol
Expand Down Expand Up @@ -512,11 +513,72 @@ def test_transport_init_signature(class_name_obj):
)


@pytest.mark.parametrize(
("transport_class", "login_version", "expected_hash"),
[
pytest.param(
AesTransport,
1,
"eyJwYXNzd29yZCI6IlFtRnkiLCJ1c2VybmFtZSI6Ik1qQXhZVFppTXpBMU0yTmpNVFF5TW1ReVl6TTJOekJpTmpJMk1UWXlNakZrTWpJNU1Ea3lPUT09In0=",
id="aes-lv-1",
),
pytest.param(
AesTransport,
2,
"eyJwYXNzd29yZDIiOiJaVFE1Tm1aa01qQXhNelprTkdKaU56Z3lPR1ZpWWpCaFlqa3lOV0l4WW1RNU56Y3lNRGhsTkE9PSIsInVzZXJuYW1lIjoiTWpBeFlUWmlNekExTTJOak1UUXlNbVF5WXpNMk56QmlOakkyTVRZeU1qRmtNakk1TURreU9RPT0ifQ==",
id="aes-lv-2",
),
pytest.param(KlapTransport, 1, "xBhMRGYWStVCVk9aSD8/6Q==", id="klap-lv-1"),
pytest.param(KlapTransport, 2, "xBhMRGYWStVCVk9aSD8/6Q==", id="klap-lv-2"),
pytest.param(
KlapTransportV2,
1,
"tEmiensOcZkP9twDEZKwU3JJl3asmseKCP7N9sfatVo=",
id="klapv2-lv-1",
),
pytest.param(
KlapTransportV2,
2,
"tEmiensOcZkP9twDEZKwU3JJl3asmseKCP7N9sfatVo=",
id="klapv2-lv-2",
),
pytest.param(XorTransport, None, None, id="xor"),
],
)
@pytest.mark.parametrize(
("credentials", "expected_blank"),
[
pytest.param(Credentials("Foo", "Bar"), False, id="credentials"),
pytest.param(None, True, id="no-credentials"),
pytest.param(Credentials(None, "Bar"), True, id="no-username"), # type: ignore[arg-type]
],
)
async def test_transport_credentials_hash(
mocker, transport_class, login_version, expected_hash, credentials, expected_blank
):
"""Test that the actual hashing doesn't break and empty credential returns an empty hash."""
host = "127.0.0.1"

params = Device.ConnectionParameters(
device_family=Device.Family.SmartTapoPlug,
encryption_type=Device.EncryptionType.Xor,
login_version=login_version,
)
config = DeviceConfig(host, credentials=credentials, connection_type=params)
transport = transport_class(config=config)

credentials_hash = transport.credentials_hash

expected = None if expected_blank else expected_hash
assert credentials_hash == expected


@pytest.mark.parametrize(
"transport_class",
[AesTransport, KlapTransport, KlapTransportV2, XorTransport, XorTransport],
)
async def test_transport_credentials_hash(mocker, transport_class):
async def test_transport_credentials_hash_from_config(mocker, transport_class):
"""Test that credentials_hash provided via config sets correctly."""
host = "127.0.0.1"

credentials = Credentials("Foo", "Bar")
Expand Down
4 changes: 2 additions & 2 deletions kasa/xortransport.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,9 @@ def default_port(self):
return self.DEFAULT_PORT

@property
def credentials_hash(self) -> str:
def credentials_hash(self) -> str | None:
"""The hashed credentials used by the transport."""
return ""
return None

async def _connect(self, timeout: int) -> None:
"""Try to connect or reconnect to the device."""
Expand Down