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

+3 -3
View File
@@ -2,14 +2,14 @@ if(IDF_TARGET STREQUAL "linux")
# Host build for led-simulator (sim-controller/): no Ethernet, Wi-Fi, mic or OTA. The sim
# component has app_main and stands in for main.c, mic.c and ota.c.
idf_component_register(SRCS "beat.c" "beat_dsp.c" "espnow_tx.c" "bridge.c" "store.c" "api.c" "web.c" "envelope.c" "engine.c" "rel.c" "bench.c" "audio.c"
"res_profile.c" "res_misc.c" "res_device.c" "res_group.c" "seqplay.c" "res_bundle.c" "audio_api.c" "patterns.c" "outputs.c" "ws.c" "ws_client.c" "wifi_drv.c" "bridge_serial.c"
"res_profile.c" "res_misc.c" "res_device.c" "res_group.c" "seqplay.c" "res_bundle.c" "audio_api.c" "patterns.c" "outputs.c" "ws.c" "ws_client.c" "wifi_drv.c" "bridge_serial.c" "auth.c"
INCLUDE_DIRS "." "../sim-controller/components/sim/include" "../bridge/main"
REQUIRES esp_http_server esp_timer esp_event cjson
PRIV_REQUIRES mbedtls lwip esp_app_format)
else()
idf_component_register(SRCS "main.c" "mic.c" "beat.c" "beat_dsp.c" "espnow_tx.c" "bridge.c" "store.c" "api.c" "web.c" "envelope.c" "engine.c" "rel.c" "bench.c" "audio.c" "ota.c"
"res_profile.c" "res_misc.c" "res_device.c" "res_group.c" "seqplay.c" "res_bundle.c" "audio_api.c" "patterns.c" "outputs.c" "ws.c" "ws_client.c" "wifi_drv.c"
INCLUDE_DIRS "."
"res_profile.c" "res_misc.c" "res_device.c" "res_group.c" "seqplay.c" "res_bundle.c" "audio_api.c" "patterns.c" "outputs.c" "ws.c" "ws_client.c" "wifi_drv.c" "auth.c"
INCLUDE_DIRS "." "../bridge/main"
REQUIRES esp_eth esp_netif esp_event esp_driver_spi esp_driver_gpio nvs_flash app_update esp_app_format
esp_http_server esp_timer esp_driver_i2s esp_wifi esp_rom cjson esp_driver_gpio
PRIV_REQUIRES littlefs mdns mbedtls lwip w5500)
+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;
+2 -1
View File
@@ -2,6 +2,7 @@
* Live audio and beat endpoints: /audio.wav (16 kHz mono PCM stream), /beats (server-sent events from the
* beat detector), POST /tap (correct the bar position), /time (board clock). Ported from esp32-eth.
*/
#include "auth.h"
#include "audio.h"
#include <stdio.h>
@@ -217,7 +218,7 @@ esp_err_t audio_register(httpd_handle_t server)
{ .uri = "/time", .method = HTTP_GET, .handler = time_get_handler },
};
for (size_t i = 0; i < sizeof(uris) / sizeof(uris[0]); i++) {
esp_err_t err = httpd_register_uri_handler(server, &uris[i]);
esp_err_t err = auth_register_uri(server, &uris[i]);
if (err != ESP_OK) {
ESP_LOGE(TAG, "register %s: %s", uris[i].uri, esp_err_to_name(err));
return err;
+2 -1
View File
@@ -2,6 +2,7 @@
// start/stop (whether mic beats time sequences), tracking reset and bar anchoring. There is one input,
// the I2S microphone, so device selection is stored but changes nothing.
#include "auth.h"
#include "audio_api.h"
#include <math.h>
@@ -367,7 +368,7 @@ esp_err_t audio_api_register(httpd_handle_t server)
static const httpd_method_t METHODS[] = { HTTP_GET, HTTP_POST, HTTP_PUT };
for (size_t m = 0; m < 3; m++) {
httpd_uri_t u = { .uri = "/api/audio/*", .method = METHODS[m], .handler = audio_handler };
esp_err_t err = httpd_register_uri_handler(server, &u);
esp_err_t err = auth_register_uri(server, &u);
if (err != ESP_OK) {
return err;
}
+1301
View File
File diff suppressed because it is too large. Load diff
+46
View File
@@ -0,0 +1,46 @@
#pragma once
#include <stdbool.h>
#include "cJSON.h"
#include "esp_err.h"
#include "esp_http_server.h"
// Users, roles and per-area permissions. Auth is off until the first user exists: then every
// request is allowed, so a LAN controller or a fresh device behaves as before. Once a user
// exists, each request needs a session cookie and a role with enough permission for its area.
typedef enum {
AUTH_NONE = 0,
AUTH_VIEW = 1, // read
AUTH_CONTROL = 2, // play, stop, brightness, identify and the like
AUTH_EDIT = 3, // create, change, delete, import, settings
} auth_level_t;
// What a request needs. pub: no check. An area starting with '@' only needs a signed-in user.
typedef struct {
const char *area;
auth_level_t level;
bool pub;
} auth_need_t;
// Creates the built-in roles (admin, guest) and the admin named by LED_ADMIN_USER / LED_ADMIN_PASSWORD
esp_err_t auth_init(void);
// Routes under /auth (login, logout, password, users, roles)
esp_err_t auth_register(httpd_handle_t server);
// The permission a request needs, from its URI and method
auth_need_t auth_classify(const char *uri, int method);
// Registers a route behind the permission check (wraps its handler)
esp_err_t auth_register_uri(httpd_handle_t server, const httpd_uri_t *u);
// True when the request may do this (auth off, or a session whose role has the level)
bool auth_can(httpd_req_t *req, const char *area, auth_level_t level);
// WebSocket routes are not wrapped: the handshake records who opened the socket, and each frame checks it
esp_err_t auth_ws_admit(httpd_req_t *req, const char *area, auth_level_t level);
bool auth_ws_allowed(int fd, const char *area, auth_level_t level);
// The users without their password hashes, for the /db export
cJSON *auth_export_users(void);
+2
View File
@@ -14,6 +14,8 @@
#include "bridge_proto.h"
#if CONFIG_IDF_TARGET_LINUX
#include "bridge_serial.h"
#else
typedef struct bridge_serial bridge_serial_t; // serial boards are linux-only; the device never opens one
#endif
static const char *TAG = "bridge";
+2 -1
View File
@@ -1,6 +1,7 @@
/*
* v2 controller: W5500 Ethernet, INMP441 beat detection, ESP-NOW master.
*/
#include "auth.h"
#include <stdio.h>
#include <string.h>
#include "freertos/FreeRTOS.h"
@@ -308,7 +309,7 @@ static void start_webserver(void)
return;
}
const httpd_uri_t status_uri = { .uri = "/status", .method = HTTP_GET, .handler = status_get_handler };
httpd_register_uri_handler(s_server, &status_uri);
auth_register_uri(s_server, &status_uri);
web_register(s_server);
audio_register(s_server);
audio_api_register(s_server);
+4 -3
View File
@@ -1,3 +1,4 @@
#include "auth.h"
#include "ota.h"
#include <stdio.h>
@@ -174,13 +175,13 @@ esp_err_t ota_register(httpd_handle_t server)
{
const httpd_uri_t post = { .uri = "/ota", .method = HTTP_POST, .handler = ota_post_handler };
const httpd_uri_t get = { .uri = "/ota", .method = HTTP_GET, .handler = ota_get_handler };
esp_err_t err = httpd_register_uri_handler(server, &post);
esp_err_t err = auth_register_uri(server, &post);
if (err == ESP_OK) {
err = httpd_register_uri_handler(server, &get);
err = auth_register_uri(server, &get);
}
const httpd_uri_t reboot = { .uri = "/reboot", .method = HTTP_POST, .handler = reboot_post_handler };
if (err == ESP_OK) {
err = httpd_register_uri_handler(server, &reboot);
err = auth_register_uri(server, &reboot);
}
return err;
}
+1 -1
View File
@@ -28,7 +28,7 @@ static const char *db_root(void)
static const char *const NAMES[] = {
"device", "zone", "preset", "profile", "palette", "pattern",
"sequence", "scene", "group", "output", "settings",
"sequence", "scene", "group", "output", "settings", "user", "role",
};
#define N_COLL (sizeof(NAMES) / sizeof(NAMES[0]))
+2 -1
View File
@@ -1,3 +1,4 @@
#include "auth.h"
#include "web.h"
#include <stddef.h>
@@ -182,7 +183,7 @@ esp_err_t web_register(httpd_handle_t server)
{ .uri = "/favicon.ico", .method = HTTP_GET, .handler = favicon_handler },
};
for (size_t i = 0; i < sizeof(uris) / sizeof(uris[0]); i++) {
esp_err_t err = httpd_register_uri_handler(server, &uris[i]);
esp_err_t err = auth_register_uri(server, &uris[i]);
if (err != ESP_OK) {
ESP_LOGE(TAG, "register %s: %s", uris[i].uri, esp_err_to_name(err));
return err;
+9 -1
View File
@@ -1,3 +1,4 @@
#include "auth.h"
#include "ws.h"
#include <stdlib.h>
@@ -49,6 +50,9 @@ static void forward(httpd_req_t *req, const char *text)
// snapshot is always empty.
static esp_err_t on_open(httpd_req_t *req)
{
if (auth_ws_admit(req, "devices", AUTH_CONTROL) != ESP_OK) {
return ESP_FAIL; // closes the socket: driving drivers needs devices control
}
send_text(req, "{\"type\":\"device_tcp_snapshot\",\"connected_ips\":[]}");
return ESP_OK;
}
@@ -80,7 +84,11 @@ static esp_err_t ws_handler(httpd_req_t *req)
err = httpd_ws_recv_frame(req, &f, f.len);
if (err == ESP_OK) {
buf[f.len] = 0;
forward(req, buf);
if (auth_ws_allowed(httpd_req_to_sockfd(req), "devices", AUTH_CONTROL)) {
forward(req, buf);
} else {
send_text(req, "{\"error\":\"not allowed\"}");
}
}
free(buf);
return err;