mirror of
https://github.com/python-kasa/python-kasa.git
synced 2026-11-26 16:01:40 +00:00
Try the account e-mail with the Kasa setup password over KLAP (#1840)
Some Kasa IoT devices accept the account e-mail with `kasaSetup` instead of the account password over KLAP. python-kasa only tries that password with the default Kasa account, so these devices fail to authenticate even with the correct account credentials. This PR checks the account e-mail with the setup password locally against handshake1, after the supplied credentials, without an extra request. In details: - Try the e-mail as given, then trimmed and lowercased as the Kasa app sends it. - Keep the fallbacks in one ordered list, with an explicit `save_hash` flag controlling hash persistence. - Save only a matching account/setup hash as `credentials_hash`, so stored configs can reconnect without the e-mail. Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import base64
|
||||
import json
|
||||
import logging
|
||||
import re
|
||||
@@ -467,12 +468,13 @@ async def test_protocol_invalid_response(
|
||||
get_default_credentials(DEFAULT_CREDENTIALS["KASA"]),
|
||||
does_not_raise(),
|
||||
),
|
||||
(Credentials("foo", "kasaSetup"), does_not_raise()),
|
||||
(
|
||||
Credentials("shouldfail", "shouldfail"),
|
||||
pytest.raises(AuthenticationError),
|
||||
),
|
||||
],
|
||||
ids=("client", "blank", "kasa_setup", "shouldfail"),
|
||||
ids=("client", "blank", "kasa_setup", "account_kasa_setup", "shouldfail"),
|
||||
)
|
||||
@pytest.mark.parametrize(
|
||||
("transport_class", "seed_auth_hash_calc"),
|
||||
@@ -520,9 +522,67 @@ async def test_handshake1(
|
||||
assert local_seed == client_seed
|
||||
assert device_remote_seed == server_seed
|
||||
assert device_auth_hash == auth_hash
|
||||
# Of the fallbacks, only the account e-mail with the setup password
|
||||
# becomes the credentials hash
|
||||
expected_hash = (
|
||||
device_auth_hash
|
||||
if device_credentials == Credentials("foo", "kasaSetup")
|
||||
else transport_class.generate_auth_hash(client_credentials)
|
||||
)
|
||||
assert transport.credentials_hash == base64.b64encode(expected_hash).decode()
|
||||
await protocol.close()
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
("transport_class", "seed_auth_hash_calc"),
|
||||
[
|
||||
pytest.param(KlapTransport, lambda c, s, a: c + a, id="KLAP"),
|
||||
pytest.param(KlapTransportV2, lambda c, s, a: c + s + a, id="KLAPV2"),
|
||||
],
|
||||
)
|
||||
async def test_handshake1_account_setup_password_is_kept(
|
||||
mocker: MockerFixture,
|
||||
transport_class: type[KlapTransport],
|
||||
seed_auth_hash_calc: Callable[..., bytes],
|
||||
) -> None:
|
||||
"""The account e-mail, as the Kasa app sends it, with the setup password.
|
||||
|
||||
Its hash becomes the credentials hash, so that a config stored with only
|
||||
the hash logs in again.
|
||||
"""
|
||||
server_seed = secrets.token_bytes(16)
|
||||
device_auth_hash = transport_class.generate_auth_hash(
|
||||
Credentials("user@example.com", "kasaSetup")
|
||||
)
|
||||
|
||||
async def _return_handshake1_response(url, params=None, data=None, *_, **__):
|
||||
seed_auth_hash = _sha256(
|
||||
seed_auth_hash_calc(data, server_seed, device_auth_hash)
|
||||
)
|
||||
return _mock_response(200, server_seed + seed_auth_hash)
|
||||
|
||||
mocker.patch.object(
|
||||
aiohttp.ClientSession, "post", side_effect=_return_handshake1_response
|
||||
)
|
||||
|
||||
credentials = Credentials(" User@Example.com", "account-password")
|
||||
transport = transport_class(
|
||||
config=DeviceConfig("127.0.0.1", credentials=credentials)
|
||||
)
|
||||
*_, auth_hash = await transport.perform_handshake1()
|
||||
assert auth_hash == device_auth_hash
|
||||
credentials_hash = transport.credentials_hash
|
||||
assert credentials_hash == base64.b64encode(device_auth_hash).decode()
|
||||
await transport.close()
|
||||
|
||||
stored = transport_class(
|
||||
config=DeviceConfig("127.0.0.1", credentials_hash=credentials_hash)
|
||||
)
|
||||
*_, auth_hash = await stored.perform_handshake1()
|
||||
assert auth_hash == device_auth_hash
|
||||
await stored.close()
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
("transport_class", "seed_auth_hash_calc1", "seed_auth_hash_calc2"),
|
||||
[
|
||||
|
||||
Reference in New Issue
Block a user