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

47 lines
1.8 KiB
C

#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);