Files
jimmyandClaude Haiku 5.5 f0615dc1b7 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>
2026-11-17 00:29:04 +13:00

194 lines
5.8 KiB
C

#include "auth.h"
#include "web.h"
#include <stddef.h>
#include <stdio.h>
#include <stdint.h>
#include <string.h>
#include "esp_app_desc.h"
#include "esp_log.h"
static const char *TAG = "web";
static const struct {
const char *ext;
const char *type;
} MIME[] = {
{ ".html", "text/html; charset=utf-8" },
{ ".js", "application/javascript; charset=utf-8" },
{ ".css", "text/css; charset=utf-8" },
{ ".json", "application/json" },
{ ".svg", "image/svg+xml" },
{ ".png", "image/png" },
{ ".ico", "image/x-icon" },
{ ".woff2", "font/woff2" },
};
static const char *mime_for(const char *path)
{
const char *dot = strrchr(path, '.');
for (size_t i = 0; dot && i < sizeof(MIME) / sizeof(MIME[0]); i++) {
if (strcmp(dot, MIME[i].ext) == 0) {
return MIME[i].type;
}
}
return "application/octet-stream";
}
// The web UI is one blob linked into the app image (www_dist/www.pack, see tools/build_web.sh):
// "WWW1", then per file u16 name length, u32 data length, the name, the gzipped data; a zero name
// length ends it.
extern const uint8_t www_pack_start[] asm("_binary_www_pack_start");
extern const uint8_t www_pack_end[] asm("_binary_www_pack_end");
static bool find_file(const char *path, const uint8_t **data, uint32_t *len)
{
const uint8_t *p = www_pack_start, *end = www_pack_end;
if (end - p < 6 || memcmp(p, "WWW1", 4) != 0) {
return false;
}
p += 4;
size_t want = strlen(path);
while (end - p >= 2) {
uint16_t nlen;
memcpy(&nlen, p, 2);
if (nlen == 0 || end - p < 6 + nlen) {
return false;
}
uint32_t dlen;
memcpy(&dlen, p + 2, 4);
const uint8_t *name = p + 6, *body = name + nlen;
if (end - body < (ptrdiff_t)dlen) {
return false;
}
if (nlen == want && memcmp(name, path, want) == 0) {
*data = body;
*len = dlen;
return true;
}
p = body + dlen;
}
return false;
}
bool web_file(const char *path, const uint8_t **data, uint32_t *len)
{
return find_file(path, data, len);
}
void web_each(const char *prefix, void (*fn)(const char *name, void *ctx), void *ctx)
{
const uint8_t *p = www_pack_start + 4, *end = www_pack_end;
if (end - www_pack_start < 6 || memcmp(www_pack_start, "WWW1", 4) != 0) {
return;
}
size_t plen = strlen(prefix);
while (end - p >= 6) {
uint16_t nlen;
uint32_t dlen;
memcpy(&nlen, p, 2);
memcpy(&dlen, p + 2, 4);
if (nlen == 0 || end - p < 6 + nlen + (ptrdiff_t)dlen) {
return;
}
char name[128];
if (nlen < sizeof(name) && nlen > plen && memcmp(p + 6, prefix, plen) == 0) {
memcpy(name, p + 6 + plen, nlen - plen);
name[nlen - plen] = 0;
fn(name, ctx);
}
p += 6 + nlen + dlen;
}
}
// Every file changes only with the web pack, so a hash of the pack is the ETag: browsers revalidate on
// each load (no-cache) and get a 304 until the UI changes, instead of running a stale UI for a day.
// (Not the app's ELF hash: the desktop and sim builds have none, so their ETag never changed.)
static const char *etag(void)
{
static char tag[20];
if (!tag[0]) {
uint64_t h = 1469598103934665603ULL; // FNV-1a
for (const uint8_t *p = www_pack_start; p < www_pack_end; p++) {
h = (h ^ *p) * 1099511628211ULL;
}
snprintf(tag, sizeof(tag), "\"%016llx\"", (unsigned long long)h);
}
return tag;
}
// path is the URL-relative name, e.g. "static/zones.js"
static esp_err_t send_gz(httpd_req_t *req, const char *path)
{
const uint8_t *data;
uint32_t len;
if (strstr(path, "..")) {
return httpd_resp_send_err(req, HTTPD_400_BAD_REQUEST, "bad path");
}
if (!find_file(path, &data, &len)) {
return httpd_resp_send_404(req);
}
httpd_resp_set_hdr(req, "Cache-Control", "no-cache");
httpd_resp_set_hdr(req, "ETag", etag());
char inm[24];
if (httpd_req_get_hdr_value_str(req, "If-None-Match", inm, sizeof(inm)) == ESP_OK && !strcmp(inm, etag())) {
httpd_resp_set_status(req, "304 Not Modified");
return httpd_resp_send(req, NULL, 0);
}
httpd_resp_set_type(req, mime_for(path));
httpd_resp_set_hdr(req, "Content-Encoding", "gzip");
return httpd_resp_send(req, (const char *)data, len);
}
esp_err_t web_send_page(httpd_req_t *req, const char *name)
{
return send_gz(req, name);
}
static esp_err_t index_handler(httpd_req_t *req)
{
return send_gz(req, "index.html");
}
static esp_err_t audio_page_handler(httpd_req_t *req)
{
return send_gz(req, "audio.html");
}
static esp_err_t static_handler(httpd_req_t *req)
{
char path[96];
const char *uri = req->uri + 1; // drop the leading '/'
size_t n = strcspn(uri, "?");
if (n >= sizeof(path)) {
return httpd_resp_send_err(req, HTTPD_400_BAD_REQUEST, "uri too long");
}
memcpy(path, uri, n);
path[n] = 0;
return send_gz(req, path);
}
static esp_err_t favicon_handler(httpd_req_t *req)
{
httpd_resp_set_status(req, "204 No Content");
return httpd_resp_send(req, NULL, 0);
}
esp_err_t web_register(httpd_handle_t server)
{
const httpd_uri_t uris[] = {
{ .uri = "/", .method = HTTP_GET, .handler = index_handler },
{ .uri = "/audio", .method = HTTP_GET, .handler = audio_page_handler },
{ .uri = "/static/*", .method = HTTP_GET, .handler = static_handler },
{ .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 = 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;
}
}
return ESP_OK;
}