feat(backend): DB sessions, login modes, single-session rules
Platform v2 step 1.6 (APPS_PLATFORM D-8, D-9, §6.2, §6.4).
- sessions table (revision 0006); every JWT carries `sid` and is accepted
only while its session is active (validated per request, 10 s cache that
is dropped the moment a session ends); the in-memory blacklist is gone, so
logout and "end session" survive restarts
- one login path, POST /api/auth/login {client, username?, user_id?, pin?,
password?}: manager mode password|pin (username-only "none" removed, D-8),
waiter mode pin|password|tap (tap answers auth.mode_needs_device until
paired devices exist), optional "waiter must type username", and the app's
access key (core.dashboard.access / waiter_app.access)
- auth.single_session_manager / _waiter: off | refuse_new | end_old
- /login without `client` = the current manager UI with v1 rules (pin or
password; refuse_new treated as end_old because its lock screen re-logs-in);
/login-by-id is a deprecated wrapper for the current waiter app;
/login-no-auth removed
- GET /api/auth/sessions, POST /api/auth/sessions/{id}/end (core.staff.manage);
blocking a user ends their sessions; refresh keeps the session; logout
takes the bearer header (or ?token= as before)
- settings API validates enum/bool/int values from the manifests;
security.login_method migrated to auth.manager_login_mode
- tests: modes, single-session rules, logout, refresh, forged token without
session, manager session list/end
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,170 @@
|
||||
"""Sessions + login modes (plan step 1.6, APPS_PLATFORM D-8, D-9, §6.2, §6.4)."""
|
||||
import jwt
|
||||
import pytest
|
||||
|
||||
from conftest import TEST_PASSWORD, TEST_PIN
|
||||
|
||||
|
||||
def _set(client, admin_h, key, value):
|
||||
r = client.put(f"/api/settings/{key}", headers=admin_h, json={"value": value})
|
||||
assert r.status_code == 200, r.text
|
||||
|
||||
|
||||
def _bearer(r):
|
||||
assert r.status_code == 200, r.text
|
||||
return {"Authorization": f"Bearer {r.json()['access_token']}"}
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def admin_h(make_user, login):
|
||||
return login(make_user(role="superadmin"))
|
||||
|
||||
|
||||
# ── manager logins ───────────────────────────────────────────────────────────
|
||||
|
||||
def test_current_manager_ui_login_keeps_v1_rules(client, make_user):
|
||||
m = make_user(role="store_manager")
|
||||
assert client.post("/api/auth/login", json={"username": m.username, "password": TEST_PASSWORD}).status_code == 200
|
||||
assert client.post("/api/auth/login", json={"username": m.username, "pin": TEST_PIN}).status_code == 200 # lock screen
|
||||
|
||||
|
||||
def test_manager_password_mode_refuses_pin(client, make_user, admin_h):
|
||||
m = make_user(role="store_manager")
|
||||
r = client.post("/api/auth/login", json={"client": "manager", "username": m.username, "pin": TEST_PIN})
|
||||
assert r.status_code == 403 and r.json()["error"]["code"] == "auth.login_mode_not_allowed"
|
||||
assert client.post("/api/auth/login", json={"client": "manager", "username": m.username,
|
||||
"password": TEST_PASSWORD}).status_code == 200
|
||||
_set(client, admin_h, "auth.manager_login_mode", "pin")
|
||||
assert client.post("/api/auth/login", json={"client": "manager", "username": m.username, "pin": TEST_PIN}).status_code == 200
|
||||
|
||||
|
||||
def test_manager_none_mode_is_gone(client, admin_h): # D-8
|
||||
r = client.put("/api/settings/auth.manager_login_mode", headers=admin_h, json={"value": "none"})
|
||||
assert r.status_code == 422 and r.json()["error"]["code"] == "validation.setting_value"
|
||||
assert client.post("/api/auth/login-no-auth", json={"username": "x"}).status_code == 404
|
||||
|
||||
|
||||
def test_wrong_password_and_wrong_app(client, make_user):
|
||||
m = make_user(role="store_manager")
|
||||
r = client.post("/api/auth/login", json={"username": m.username, "password": "nope"})
|
||||
assert r.status_code == 401 and r.json()["error"]["code"] == "auth.invalid_credentials"
|
||||
w = make_user(role="waiter")
|
||||
r = client.post("/api/auth/login", json={"username": w.username, "password": TEST_PASSWORD})
|
||||
assert r.status_code == 403 and r.json()["error"] == {"code": "auth.client_not_allowed", "params": {"client": "manager"},
|
||||
"message": "This account can't use the manager app."}
|
||||
|
||||
|
||||
# ── waiter logins ────────────────────────────────────────────────────────────
|
||||
|
||||
def test_waiter_pin_by_id_and_deprecated_endpoint(client, make_user):
|
||||
w = make_user(role="waiter")
|
||||
assert client.post("/api/auth/login", json={"client": "waiter", "user_id": w.id, "pin": TEST_PIN}).status_code == 200
|
||||
assert client.post("/api/auth/login-by-id", json={"waiter_id": w.id, "pin": TEST_PIN}).status_code == 200
|
||||
chef = make_user(role="chef")
|
||||
r = client.post("/api/auth/login", json={"client": "waiter", "user_id": chef.id, "pin": TEST_PIN})
|
||||
assert r.json()["error"]["code"] == "auth.client_not_allowed"
|
||||
|
||||
|
||||
def test_waiter_modes(client, make_user, admin_h):
|
||||
w = make_user(role="waiter")
|
||||
_set(client, admin_h, "auth.waiter_login_mode", "password")
|
||||
r = client.post("/api/auth/login", json={"client": "waiter", "user_id": w.id, "pin": TEST_PIN})
|
||||
assert r.json()["error"]["code"] == "auth.login_mode_not_allowed"
|
||||
assert client.post("/api/auth/login", json={"client": "waiter", "user_id": w.id, "password": TEST_PASSWORD}).status_code == 200
|
||||
|
||||
_set(client, admin_h, "auth.waiter_login_mode", "tap")
|
||||
r = client.post("/api/auth/login", json={"client": "waiter", "user_id": w.id})
|
||||
assert r.status_code == 403 and r.json()["error"] == {"code": "auth.mode_needs_device", "params": {"mode": "tap"},
|
||||
"message": "Logging in by tap only works on a paired device."}
|
||||
|
||||
_set(client, admin_h, "auth.waiter_login_mode", "pin")
|
||||
_set(client, admin_h, "auth.waiter_require_username", "true")
|
||||
r = client.post("/api/auth/login", json={"client": "waiter", "user_id": w.id, "pin": TEST_PIN})
|
||||
assert r.json()["error"]["code"] == "auth.username_required"
|
||||
assert client.post("/api/auth/login", json={"client": "waiter", "username": w.username, "pin": TEST_PIN}).status_code == 200
|
||||
|
||||
|
||||
# ── sessions ─────────────────────────────────────────────────────────────────
|
||||
|
||||
def test_token_carries_sid_and_logout_ends_it(client, make_user):
|
||||
m = make_user(role="store_manager")
|
||||
h = _bearer(client.post("/api/auth/login", json={"username": m.username, "password": TEST_PASSWORD}))
|
||||
payload = jwt.decode(h["Authorization"][7:], options={"verify_signature": False})
|
||||
assert len(payload["sid"]) == 32
|
||||
assert client.get("/api/auth/me", headers=h).status_code == 200
|
||||
assert client.post("/api/auth/logout", headers=h).status_code == 200
|
||||
r = client.get("/api/auth/me", headers=h)
|
||||
assert r.status_code == 401 and r.json()["error"]["code"] == "auth.session_ended"
|
||||
|
||||
|
||||
def test_end_old_ends_the_first_phone(client, make_user, admin_h): # "done when"
|
||||
_set(client, admin_h, "auth.single_session_waiter", "end_old")
|
||||
w = make_user(role="waiter")
|
||||
phone1 = _bearer(client.post("/api/auth/login", json={"client": "waiter", "user_id": w.id, "pin": TEST_PIN}))
|
||||
phone2 = _bearer(client.post("/api/auth/login", json={"client": "waiter", "user_id": w.id, "pin": TEST_PIN}))
|
||||
r = client.get("/api/auth/me", headers=phone1)
|
||||
assert r.status_code == 401 and r.json()["error"]["code"] == "auth.session_ended"
|
||||
assert client.get("/api/auth/me", headers=phone2).status_code == 200
|
||||
|
||||
|
||||
def test_refuse_new(client, make_user, admin_h):
|
||||
_set(client, admin_h, "auth.single_session_waiter", "refuse_new")
|
||||
w = make_user(role="waiter")
|
||||
assert client.post("/api/auth/login", json={"client": "waiter", "user_id": w.id, "pin": TEST_PIN}).status_code == 200
|
||||
r = client.post("/api/auth/login", json={"client": "waiter", "user_id": w.id, "pin": TEST_PIN})
|
||||
assert r.status_code == 409 and r.json()["error"]["code"] == "auth.session_exists"
|
||||
|
||||
|
||||
def test_single_session_is_per_client(client, make_user, admin_h):
|
||||
_set(client, admin_h, "auth.single_session_waiter", "end_old")
|
||||
owner = make_user(role="owner") # owners can use both apps
|
||||
mgr = _bearer(client.post("/api/auth/login", json={"username": owner.username, "password": TEST_PASSWORD}))
|
||||
client.post("/api/auth/login", json={"client": "waiter", "user_id": owner.id, "pin": TEST_PIN})
|
||||
assert client.get("/api/auth/me", headers=mgr).status_code == 200
|
||||
|
||||
|
||||
def test_manager_lists_and_ends_sessions(client, make_user, login):
|
||||
owner = make_user(role="owner")
|
||||
w = make_user(role="waiter")
|
||||
wh = _bearer(client.post("/api/auth/login", json={"client": "waiter", "user_id": w.id, "pin": TEST_PIN}))
|
||||
oh = login(owner)
|
||||
rows = client.get("/api/auth/sessions", headers=oh).json()
|
||||
mine = next(s for s in rows if s["user_id"] == w.id)
|
||||
assert mine["client"] == "waiter" and mine["created_at"].endswith("Z")
|
||||
assert client.post(f"/api/auth/sessions/{mine['id']}/end", headers=oh).status_code == 200
|
||||
assert client.get("/api/auth/me", headers=wh).json()["error"]["code"] == "auth.session_ended"
|
||||
assert client.get("/api/auth/sessions", headers=login(w, "waiter")).status_code == 403
|
||||
|
||||
|
||||
def test_blocking_a_user_ends_their_sessions(client, make_user, login):
|
||||
owner = make_user(role="owner")
|
||||
w = make_user(role="waiter")
|
||||
wh = login(w, "waiter")
|
||||
assert client.put(f"/api/waiters/{w.id}/block", headers=login(owner)).json()["is_active"] is False
|
||||
assert client.get("/api/auth/me", headers=wh).status_code == 401
|
||||
|
||||
|
||||
def test_refresh_keeps_the_session(client, make_user):
|
||||
m = make_user(role="store_manager")
|
||||
h = _bearer(client.post("/api/auth/login", json={"username": m.username, "password": TEST_PASSWORD}))
|
||||
new = _bearer(client.post("/api/auth/refresh", headers=h))
|
||||
sid = lambda hh: jwt.decode(hh["Authorization"][7:], options={"verify_signature": False})["sid"] # noqa: E731
|
||||
assert sid(new) == sid(h)
|
||||
|
||||
|
||||
def test_token_without_session_is_refused(client, make_user):
|
||||
from config import settings
|
||||
m = make_user(role="store_manager")
|
||||
forged = jwt.encode({"sub": str(m.id), "role": m.role, "exp": 9999999999}, settings.SECRET_KEY, algorithm="HS256")
|
||||
r = client.get("/api/auth/me", headers={"Authorization": f"Bearer {forged}"})
|
||||
assert r.status_code == 401 and r.json()["error"]["code"] == "auth.session_ended"
|
||||
|
||||
|
||||
def test_session_survives_cache_loss_like_a_restart(client, make_user):
|
||||
"""The session lives in the DB: dropping every in-memory cache (what a restart does)
|
||||
keeps the token valid. The real restart is checked in the plan notes."""
|
||||
from core import sessions
|
||||
m = make_user(role="store_manager")
|
||||
h = _bearer(client.post("/api/auth/login", json={"username": m.username, "password": TEST_PASSWORD}))
|
||||
sessions.clear_cache()
|
||||
assert client.get("/api/auth/me", headers=h).status_code == 200
|
||||
Reference in New Issue
Block a user