Skip to content
Open
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
2 changes: 1 addition & 1 deletion src/ui_cli/commands/status.py
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ async def check_local_controller(verbose: bool = False) -> dict:
try:
client = UniFiLocalClient(timeout=STATUS_CHECK_TIMEOUT)
start = time.perf_counter()
await client.login()
await client.ensure_authenticated()
elapsed_ms = (time.perf_counter() - start) * 1000

result["connection"] = "OK"
Expand Down
4 changes: 4 additions & 0 deletions src/ui_cli/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,10 @@ class Settings(BaseSettings):
default=False,
description="Verify SSL certificates (disable for self-signed)",
)
controller_totp: str = Field(
default="",
description="TOTP code for MFA-enabled accounts",
)
Comment on lines +54 to +57
Copy link

Copilot AI Apr 27, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The new controller_totp setting is user-facing, but .env.example (and any related setup docs) doesn’t include UNIFI_CONTROLLER_TOTP, so it’s hard to discover/configure. Please add the new variable to the example env file and any relevant documentation sections so users know how to supply the MFA token.

Copilot uses AI. Check for mistakes.
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@copilot apply changes based on this feedback


@property
def is_configured(self) -> bool:
Expand Down
18 changes: 13 additions & 5 deletions src/ui_cli/local_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ def __init__(
self.password = password or settings.controller_password
self.site = site or settings.controller_site
self.verify_ssl = verify_ssl if verify_ssl is not None else settings.controller_verify_ssl
self.totp = settings.controller_totp

# Timeout priority: explicit param > --quick flag > settings
if timeout is not None:
Expand Down Expand Up @@ -200,20 +201,27 @@ async def login(self) -> bool:
try:
# Try UDM-style auth first
if self._is_udm:
login_data = {
"username": self.username,
"password": self.password,
"remember": True,
}
if self.totp:
login_data["token"] = self.totp
response = await client.post(
f"{self.controller_url}/api/auth/login",
json={
"username": self.username,
"password": self.password,
"remember": True,
},
json=login_data,
)

if response.status_code == 200:
self._cookies = dict(response.cookies)
self._csrf_token = response.headers.get("X-CSRF-Token")
self._save_session()
return True
elif response.status_code == 499:
raise LocalAuthenticationError(
"MFA token required. Set UNIFI_CONTROLLER_TOTP to your current TOTP code."
)
Comment on lines 203 to +224
Copy link

Copilot AI Apr 27, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The MFA/TOTP flow adds new behavior in login() (including conditional inclusion of token and a dedicated 499 error path), but there are no unit tests covering UDM login payload construction or the 499/MFA-required error handling. Please add tests that mock the httpx client to assert token is omitted when unset, included when set, and that a 499 response raises LocalAuthenticationError with the expected message.

Copilot uses AI. Check for mistakes.
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@copilot apply changes based on this feedback

elif response.status_code == 403:
# 403 on UDM often means wrong credentials
raise LocalAuthenticationError(
Expand Down