#pragma once #include #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);