Skip to content

Commit 47ce1c1

Browse files
committed
Fix UniFi app test() method to properly validate credentials
The test() method was using appTest() which only checked HTTP status codes, causing false "Invalid credentials" errors even when credentials were valid. This fix aligns the test() method with the working livestats() approach. Changes: - Use execute() directly with explicit POST method (same as livestats) - Handle HTTP 400 errors for self-hosted controller auth failures - Handle HTTP 401/403 errors for UDM auth failures - Validate self-hosted response body (meta.rc === "ok") - Verify login by fetching stats to confirm session is valid - Provide descriptive error messages for different failure scenarios Fixes #640, #695, #811
1 parent d3ab2ff commit 47ce1c1

File tree

1 file changed

+64
-2
lines changed

1 file changed

+64
-2
lines changed

UniFi/UniFi.php

Lines changed: 64 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,74 @@ public function __construct()
2020
public function test()
2121
{
2222
$urls = $this->getAPIURLs();
23-
$test = parent::appTest(
23+
$self_hosted = $this->getConfigValue("self_hosted", false);
24+
25+
// Perform login request
26+
$loginRes = parent::execute(
2427
$this->url($urls['loginURL']),
2528
$this->getLoginAttributes(),
29+
null,
30+
'POST'
2631
);
2732

28-
echo $test->status;
33+
if ($loginRes === null) {
34+
echo "Failed: Connection error";
35+
return;
36+
}
37+
38+
$statusCode = $loginRes->getStatusCode();
39+
$body = json_decode($loginRes->getBody());
40+
41+
// Check for explicit failure codes
42+
if ($statusCode === 401 || $statusCode === 403) {
43+
echo "Failed: Invalid credentials";
44+
return;
45+
}
46+
47+
// Self-hosted controllers return 400 on auth failure
48+
if ($statusCode === 400) {
49+
$msg = isset($body->meta->msg) ? $body->meta->msg : "Invalid credentials";
50+
echo "Failed: " . $msg;
51+
return;
52+
}
53+
54+
// For 200 responses, verify the login actually succeeded
55+
if ($statusCode === 200) {
56+
// Self-hosted: check meta.rc === "ok"
57+
if ($self_hosted) {
58+
if (!isset($body->meta->rc) || $body->meta->rc !== "ok") {
59+
$msg = isset($body->meta->msg) ? $body->meta->msg : "Login failed";
60+
echo "Failed: " . $msg;
61+
return;
62+
}
63+
}
64+
65+
// Additional verification: try to fetch stats to confirm session works
66+
$statsRes = parent::execute(
67+
$this->url($urls['statsURL']),
68+
$this->getAttributes(),
69+
null,
70+
'GET'
71+
);
72+
73+
if ($statsRes !== null && $statsRes->getStatusCode() === 200) {
74+
$statsBody = json_decode($statsRes->getBody());
75+
// UDM returns data array, self-hosted returns meta.rc
76+
$hasData = isset($statsBody->data);
77+
$hasMetaOk = isset($statsBody->meta) && isset($statsBody->meta->rc) && $statsBody->meta->rc === "ok";
78+
if ($hasData || $hasMetaOk) {
79+
echo "Successfully connected to UniFi";
80+
return;
81+
}
82+
}
83+
84+
// Stats fetch failed but login seemed ok
85+
echo "Login succeeded but unable to fetch stats - check user permissions";
86+
return;
87+
}
88+
89+
// Unexpected status code
90+
echo "Failed: Unexpected response (HTTP " . $statusCode . ")";
2991
}
3092

3193
public function livestats()

0 commit comments

Comments
 (0)