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

+6
View File
@@ -75,12 +75,18 @@ def restore_db(request):
yield
return
client = request.getfixturevalue("c")
if client.get("/auth").json().get("enabled"):
# test_auth.py turned sign-in on: the snapshot needs editor rights, so the data is left as it is
yield
return
snapshot = {}
for name in COLLECTIONS:
resp = client.get(f"/db/{name}")
resp.raise_for_status()
snapshot[name] = resp.json()
yield snapshot
if client.get("/auth").json().get("enabled"):
return # test_auth.py switched sign-in on during the run: it cannot be restored without an admin
failed = []
for name, data in snapshot.items():
resp = client.put(f"/db/{name}", json=data)
+172
View File
@@ -0,0 +1,172 @@
"""Users, roles and permissions.
These turn auth on for the controller they run against, and the API cannot turn it off again (the last
admin cannot be removed). Run them only against a throwaway controller, and set LED_AUTH_TEST=1 to confirm:
LED_AUTH_TEST=1 python -m pytest test_auth.py --host 127.0.0.1:8091
"""
import os
import time
import uuid
import pytest
import requests
TIMEOUT = 10
ADMIN_PW = "admin-pass-1234"
OP_PW = "operator-pass-1234"
def _base(host):
return host if host.startswith("http") else f"http://{host}"
@pytest.fixture(scope="module")
def host(request):
h = request.config.getoption("--host") or os.environ.get("LED_HOST")
if not h:
pytest.skip("no controller: pass --host or set LED_HOST")
if os.environ.get("LED_AUTH_TEST") != "1":
pytest.skip("auth tests switch auth on for good: set LED_AUTH_TEST=1 on a throwaway controller")
return _base(h)
def anon(host):
s = requests.Session()
s.headers.update({"User-Agent": "pytest/requests", "Accept": "application/json"})
s.base = host
return s
def call(s, method, path, **kw):
kw.setdefault("timeout", TIMEOUT)
return s.request(method, s.base + path, **kw)
def login(host, user, pw):
s = anon(host)
r = call(s, "POST", "/auth/login", json={"username": user, "password": pw})
assert r.status_code == 200, r.text
return s
@pytest.fixture(scope="module")
def admin(host):
"""Creates the first admin (open while no user exists), then signs in as them."""
st = call(anon(host), "GET", "/auth").json()
if st.get("enabled"):
pytest.skip("this controller already has users: run against a fresh one")
name = "admin" + uuid.uuid4().hex[:6]
r = call(anon(host), "POST", "/auth/users", json={"username": name, "password": ADMIN_PW, "role": "admin"})
assert r.status_code == 200, r.text
return login(host, name, ADMIN_PW)
def test_open_until_first_user(host):
s = anon(host)
st = call(s, "GET", "/auth").json()
if st["enabled"]:
pytest.skip("auth already on")
assert call(s, "GET", "/presets").status_code == 200
created = call(s, "POST", "/presets", json={"name": "auth-open-" + uuid.uuid4().hex[:6]})
assert created.status_code in (200, 201), created.text
def test_signed_out_reads_and_edits(host, admin):
s = anon(host)
assert call(s, "GET", "/presets").status_code == 200 # guest role: view
assert call(s, "GET", "/zones").status_code == 200
assert call(s, "POST", "/presets", json={"name": "nope"}).status_code == 401 # signed out: login required
assert call(s, "GET", "/auth/users").status_code == 401 # users area is not in the guest role
def test_signed_out_has_no_settings_or_users(host, admin):
s = anon(host)
assert call(s, "GET", "/settings").status_code == 401 # guest role: no settings view
assert call(s, "GET", "/db/user").status_code == 401
def test_viewer_does_not_see_wifi_password(host, admin):
role = "view" + uuid.uuid4().hex[:6]
perms = {"presets": 1, "sequences": 1, "zones": 1, "groups": 1, "devices": 1, "outputs": 1,
"patterns": 1, "sim": 1, "settings": 1, "users": 0}
assert call(admin, "POST", "/auth/roles", json={"name": role, "perms": perms}).status_code == 200
user = "v" + uuid.uuid4().hex[:6]
assert call(admin, "POST", "/auth/users", json={"username": user, "password": OP_PW, "role": role}).status_code == 200
viewer = login(host, user, OP_PW)
r = call(viewer, "GET", "/settings")
assert r.status_code == 200
assert "ap_password" not in r.json()
assert call(viewer, "PUT", "/settings", json={"global_brightness": 10}).status_code == 403
def test_password_hashes_never_returned(host, admin):
for path in ("/auth/users", "/db/user", "/auth"):
text = call(admin, "GET", path).text
assert "hash" not in text and "salt" not in text, path
def test_wrong_password_refused(host, admin):
r = call(anon(host), "POST", "/auth/login", json={"username": "nobody-here", "password": "x" * 9})
assert r.status_code == 401
def test_admin_can_edit(host, admin):
r = call(admin, "POST", "/presets", json={"name": "auth-admin-" + uuid.uuid4().hex[:6]})
assert r.status_code in (200, 201), r.text
def test_custom_role_and_its_limits(host, admin):
role = "op" + uuid.uuid4().hex[:6]
perms = {"presets": 1, "sequences": 2, "zones": 1, "groups": 1, "devices": 1, "outputs": 1,
"patterns": 1, "sim": 1, "settings": 0, "users": 0}
assert call(admin, "POST", "/auth/roles", json={"name": role, "perms": perms}).status_code == 200
user = "u" + uuid.uuid4().hex[:6]
assert call(admin, "POST", "/auth/users", json={"username": user, "password": OP_PW, "role": role}).status_code == 200
op = login(host, user, OP_PW)
assert call(op, "POST", "/presets", json={"name": "x"}).status_code == 403 # view only on presets
assert call(op, "GET", "/auth/users").status_code == 403 # no users area
assert call(op, "POST", "/sequences/1/play", json={"zone_id": "1"}).status_code not in (401, 403) # control
assert call(op, "POST", "/sequences", json={}).status_code == 403 # editing sequences needs edit
# raising the role takes effect at once, for a session that already exists
perms["sequences"] = 3
assert call(admin, "PUT", f"/auth/roles/{role}", json={"perms": perms}).status_code == 200
assert call(op, "POST", "/sequences", json={}).status_code != 403
# logging out drops the session
assert call(op, "POST", "/auth/logout").status_code == 200
assert call(op, "GET", "/auth/users").status_code == 401
def test_admin_role_is_fixed(host, admin):
perms = {a: 3 for a in ["presets", "sequences", "zones", "groups", "devices", "outputs", "patterns", "sim",
"settings", "users"]}
perms["users"] = 2
assert call(admin, "PUT", "/auth/roles/admin", json={"perms": perms}).status_code == 400
assert call(admin, "DELETE", "/auth/roles/admin").status_code == 400
def test_last_manager_cannot_be_removed(host, admin):
me = call(admin, "GET", "/auth").json()["user"]
assert me
r = call(admin, "DELETE", f"/auth/users/{me}")
assert r.status_code == 400, r.text
def test_unknown_area_in_perms_refused(host, admin):
r = call(admin, "POST", "/auth/roles", json={"name": "bad" + uuid.uuid4().hex[:4], "perms": {"nope": 3}})
assert r.status_code == 400
def test_login_lockout(host, admin):
s = anon(host)
codes = [call(s, "POST", "/auth/login", json={"username": "nobody-x", "password": "wrong-wrong"}).status_code
for _ in range(6)]
assert 429 in codes and codes[-1] == 429, codes
# the lock is per username: the admin can still sign in while the other name is locked
me = call(admin, "GET", "/auth").json()["user"]
assert call(anon(host), "POST", "/auth/login", json={"username": me, "password": ADMIN_PW}).status_code == 200
time.sleep(31) # the first lock is 30 seconds
assert call(anon(host), "POST", "/auth/login", json={"username": "nobody-x", "password": "wrong-wrong"}).status_code == 401