FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

SslAesTransport: handshake again when a camera answers 401 to passthrough by freeKC · Pull Request #1771 · python-kasa/python-kasa · GitHub

Repository navigation

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

Filter by extension

Filter by extension .py  (2) All 1 file type selected
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
14 changes: 14 additions & 0 deletions kasa/transports/sslaestransport.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,20 @@ async def send_secure_passthrough(self, request: str) -> dict[str, Any]:
_LOGGER.debug(msg)
raise _RetryableError(msg)

# Some devices answer 401 when the session has expired and they
# require a new handshake: reauthenticate and retry the request.
if status_code == 401:
_LOGGER.debug(
"Device %s replied with status 401 to passthrough, "
"session expired, handshake required",
self._host,
)
self._state = TransportState.HANDSHAKE_REQUIRED
raise _RetryableError(
f"{self._host} responded with status 401 to passthrough, "
"session expired"
)

if status_code != 200:
raise KasaException(
f"{self._host} responded with an unexpected "
Expand Down
21 changes: 21 additions & 0 deletions tests/transports/test_sslaestransport.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -797,3 +797,24 @@ async def _return_send_response(self, url: URL, json: dict[str, Any]):

def put_next_response(self, request: dict | bytes) -> None:
self._next_responses.append(request)


async def test_passthrough_401_requires_new_handshake(mocker):
"""A 401 on passthrough means the session expired: retryable, new handshake."""
host = "127.0.0.1"
mock_ssl_aes_device = MockSslAesDevice(host)
mocker.patch.object(
aiohttp.ClientSession, "post", side_effect=mock_ssl_aes_device.post
)
transport = SslAesTransport(
config=DeviceConfig(host, credentials=Credentials(MOCK_USER, MOCK_PWD))
)
request = {"method": "getDeviceInfo", "params": None}

await transport.perform_handshake()
assert transport._state is TransportState.ESTABLISHED

mock_ssl_aes_device.status_code = 401
with pytest.raises(_RetryableError, match="session expired"):
await transport.send(json_dumps(request))
assert transport._state is TransportState.HANDSHAKE_REQUIRED
Loading

Back | FazBrowse Home | New Git URL