feat: users, roles and per-area permissions, production demo image

The controller checks every route against the signed-in user's role, with
levels none, view, control and edit per area. Guests get the permissions
of a built-in guest role. Auth stays off until the first user exists;
LED_ADMIN_USER and LED_ADMIN_PASSWORD create the first admin.

Sessions are cookies, passwords are PBKDF2-SHA256, and logins lock per
username after five failures. The simulator's WebSocket checks the view
permission at handshake and per frame; /ws needs devices control. The web
UI gets a sign-in button, a sign-in prompt on refused changes, and a users
and roles screen.

Also: the device build gets the bridge include path and forward type it
was missing, Dockerfile.sim runs non-root, and Dockerfile.prod with
docker-compose.prod.yml runs the controller on an internal network behind
Caddy for HTTPS.

Co-Authored-By: Claude Haiku 5.5 <noreply@anthropic.com>
This commit is contained in:
jimmyandClaude Haiku 5.5 committed 2026-11-17 00:29:04 +13:00
1 parent 02440be55d
commit f0615dc1b7
26 files changed
+2116 -23

No files matched your search

+15 -5
View File
@@ -1,3 +1,4 @@
#include "auth.h"
#include "api.h"
#include <stdio.h>
@@ -556,7 +557,11 @@ static esp_err_t settings_handler_inner(httpd_req_t *req)
return web_send_page(req, "settings_editor.html");
}
if (n == 1 && req->method == HTTP_GET) {
return api_json(req, "200 OK", store_settings(), false);
cJSON *shown = cJSON_Duplicate(store_settings(), 1);
if (!auth_can(req, "settings", AUTH_EDIT)) {
cJSON_DeleteItemFromObject(shown, "ap_password"); // the Wi-Fi AP password is for editors only
}
return api_json(req, "200 OK", shown, true);
}
if (n == 2 && strcmp(seg[1], "bridge") == 0 && req->method == HTTP_GET) {
return bridge_get(req);
@@ -581,6 +586,9 @@ static esp_err_t db_handler_inner(httpd_req_t *req)
return api_error(req, "404 Not Found", "unknown collection");
}
if (req->method == HTTP_GET) {
if (!strcmp(seg[1], "user")) {
return api_json(req, "200 OK", auth_export_users(), true); // no password hashes
}
return api_json(req, "200 OK", store_coll(seg[1]), false);
}
if (req->method == HTTP_PUT) {
@@ -673,6 +681,8 @@ LOCKED(test_handler)
esp_err_t api_register(httpd_handle_t server)
{
ESP_ERROR_CHECK(auth_init());
ESP_ERROR_CHECK_WITHOUT_ABORT(auth_register(server));
store_lock();
api_settings_defaults();
api_seed_defaults();
@@ -688,18 +698,18 @@ esp_err_t api_register(httpd_handle_t server)
for (size_t r = 0; r < sizeof(ROUTES) / sizeof(ROUTES[0]); r++) {
for (size_t m = 0; m < 4; m++) {
httpd_uri_t u = { .uri = ROUTES[r], .method = METHODS[m], .handler = collection_handler };
ESP_ERROR_CHECK_WITHOUT_ABORT(httpd_register_uri_handler(server, &u));
ESP_ERROR_CHECK_WITHOUT_ABORT(auth_register_uri(server, &u));
}
}
for (size_t m = 0; m < 4; m++) {
httpd_uri_t t = { .uri = "/test/*", .method = METHODS[m], .handler = test_handler };
ESP_ERROR_CHECK_WITHOUT_ABORT(httpd_register_uri_handler(server, &t));
ESP_ERROR_CHECK_WITHOUT_ABORT(auth_register_uri(server, &t));
}
for (size_t m = 0; m < 4; m++) {
httpd_uri_t s = { .uri = "/settings*", .method = METHODS[m], .handler = settings_handler };
httpd_uri_t d = { .uri = "/db/*", .method = METHODS[m], .handler = db_handler };
ESP_ERROR_CHECK_WITHOUT_ABORT(httpd_register_uri_handler(server, &s));
ESP_ERROR_CHECK_WITHOUT_ABORT(httpd_register_uri_handler(server, &d));
ESP_ERROR_CHECK_WITHOUT_ABORT(auth_register_uri(server, &s));
ESP_ERROR_CHECK_WITHOUT_ABORT(auth_register_uri(server, &d));
}
ESP_LOGI(TAG, "API registered");
return ESP_OK;