Skip to content

Fix SslAesTransport default login and add tests #1202

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Oct 28, 2024
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
21 changes: 16 additions & 5 deletions kasa/experimental/sslaestransport.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,11 @@ def default_port(self) -> int:
"""Default port for the transport."""
return self.DEFAULT_PORT

@staticmethod
def _create_b64_credentials(credentials: Credentials) -> str:
ch = {"un": credentials.username, "pwd": credentials.password}
return base64.b64encode(json_dumps(ch).encode()).decode()

@property
def credentials_hash(self) -> str | None:
"""The hashed credentials used by the transport."""
Expand All @@ -145,8 +150,7 @@ def credentials_hash(self) -> str | None:
if not self._credentials and self._credentials_hash:
return self._credentials_hash
if (cred := self._credentials) and cred.password and cred.username:
ch = {"un": cred.username, "pwd": cred.password}
return base64.b64encode(json_dumps(ch).encode()).decode()
return self._create_b64_credentials(cred)
return None

def _get_response_error(self, resp_dict: Any) -> SmartErrorCode:
Expand Down Expand Up @@ -329,6 +333,13 @@ async def perform_handshake2(self, local_nonce, server_nonce, pwd_hash) -> None:
+ f"status code {status_code} to handshake2"
)
resp_dict = cast(dict, resp_dict)
if (
error_code := self._get_response_error(resp_dict)
) and error_code is SmartErrorCode.INVALID_NONCE:
raise AuthenticationError(
f"Invalid password hash in handshake2 for {self._host}"
)

self._handle_response_error_code(resp_dict, "Error in handshake2")

self._seq = resp_dict["result"]["start_seq"]
Expand Down Expand Up @@ -372,12 +383,12 @@ async def perform_handshake1(self) -> tuple[str, str, str]:

if not self._username:
raise AuthenticationError(
"Credentials must be supplied to connect to {self._host}"
f"Credentials must be supplied to connect to {self._host}"
)
if error_code is not SmartErrorCode.INVALID_NONCE or (
resp_dict and "nonce" not in resp_dict["result"].get("data", {})
):
raise AuthenticationError("Error trying handshake1: {resp_dict}")
raise AuthenticationError(f"Error trying handshake1: {resp_dict}")

if TYPE_CHECKING:
resp_dict = cast(Dict[str, Any], resp_dict)
Expand Down Expand Up @@ -422,7 +433,7 @@ async def try_send_handshake1(self, username: str, local_nonce: str) -> dict:
"params": {
"cnonce": local_nonce,
"encrypt_type": "3",
"username": self._username,
"username": username,
},
}
http_client = self._http_client
Expand Down
Loading
Loading