Multi-user sign-in with role-based access control (RBAC) #1

Closed
opened 2026-10-13 18:53:03 +00:00 by ben · 12 comments
Owner

Replace the open, no-login model with real accounts.

Scope

  • Roles: at minimum admin (practice staff/counsellor) and client; consider owner for practice-level settings.
  • Clients see only their own sessions/activities; admins see their clients'. Remove the public session list.
  • Login: email + password and/or magic link (pairs with the SMTP issue); secure session cookies, CSRF protection, password hashing (argon2/bcrypt), rate-limited login.
  • Migrate existing anonymous sessions (assign to a user or archive).
  • Authorise every API route and the SSE stream server-side.

Acceptance

  • No data is readable or writable without an authenticated session with the right role.
  • Tests cover cross-tenant access denial for every endpoint.

Blocks: admin dashboard, invites, activity builder. Note: client values/notes are sensitive personal data; privacy obligations (e.g. PIPEDA / provincial health privacy rules) should be confirmed with the practice.


Handoff context: the app is being prepared so a counselling practice can self-host it (see README and docs/self-hosting.md). Today the app has no auth by design, two hard-coded decks in shared/cards.json, three fixed columns, and Fastify + SQLite + SSE sync (docs/api.md).

Replace the open, no-login model with real accounts. **Scope** - Roles: at minimum `admin` (practice staff/counsellor) and `client`; consider `owner` for practice-level settings. - Clients see only their own sessions/activities; admins see their clients'. Remove the public session list. - Login: email + password and/or magic link (pairs with the SMTP issue); secure session cookies, CSRF protection, password hashing (argon2/bcrypt), rate-limited login. - Migrate existing anonymous sessions (assign to a user or archive). - Authorise every API route and the SSE stream server-side. **Acceptance** - No data is readable or writable without an authenticated session with the right role. - Tests cover cross-tenant access denial for every endpoint. Blocks: admin dashboard, invites, activity builder. Note: client values/notes are sensitive personal data; privacy obligations (e.g. PIPEDA / provincial health privacy rules) should be confirmed with the practice. --- Handoff context: the app is being prepared so a counselling practice can self-host it (see README and docs/self-hosting.md). Today the app has no auth by design, two hard-coded decks in `shared/cards.json`, three fixed columns, and Fastify + SQLite + SSE sync (docs/api.md).
Author
Owner

Filed during handoff prep (README and docs/self-hosting.md in c133282). No implementation yet. Recommended order: #2 (activities data model + migration) before #1 (RBAC), pending the maintainer's confirmation.

Filed during handoff prep (README and docs/self-hosting.md in c133282). No implementation yet. Recommended order: #2 (activities data model + migration) before #1 (RBAC), pending the maintainer's confirmation.
Author
Owner

Maintainer decisions, 2026-10-13:

  • Sign-in is a magic link with a password fallback, so #1 is planned together with #3 SMTP.
  • Single practice per install, staying on SQLite.

#2 is done (7a4378c). Two seams are ready for authorization checks: resolveSessionActivity() and listAccessibleSessions() in server/app.js, plus the session_activities table for assignment.

Maintainer decisions, 2026-10-13: - Sign-in is a **magic link with a password fallback**, so #1 is planned together with #3 SMTP. - **Single practice per install**, staying on SQLite. #2 is done (7a4378c). Two seams are ready for authorization checks: `resolveSessionActivity()` and `listAccessibleSessions()` in server/app.js, plus the `session_activities` table for assignment.
Author
Owner

Maintainer decisions, 2026-10-13:

  • Roles: owner (practice settings and staff), admin (counsellor, manages their clients), client (own sessions only).
  • Sessions: a client can own many sessions; each assignment creates one.
  • Migration: The maintainer's own session goes to the maintainer's owner account. Other anonymous sessions are archived (kept but hidden). The public session list is removed.
  • SMTP (#3): generic env-based SMTP config. The provider is chosen at handoff; Mailpit for dev.
Maintainer decisions, 2026-10-13: - **Roles:** `owner` (practice settings and staff), `admin` (counsellor, manages their clients), `client` (own sessions only). - **Sessions:** a client can own many sessions; each assignment creates one. - **Migration:** The maintainer's own session goes to the maintainer's owner account. Other anonymous sessions are archived (kept but hidden). The public session list is removed. - **SMTP (#3):** generic env-based SMTP config. The provider is chosen at handoff; Mailpit for dev.
Author
Owner

Plan written and reviewed by a High panel (3/3 voices): docs/plans/2026-10-13-14-50_rbac-sign-in-and-smtp-outbox.md. More decisions: counsellors are view-only by default (ADMIN_CAN_EDIT flag); privacy guidance gates real-client use rather than the release; the source PDFs stay private. No code yet.

Plan written and reviewed by a High panel (3/3 voices): `docs/plans/2026-10-13-14-50_rbac-sign-in-and-smtp-outbox.md`. More decisions: counsellors are view-only by default (`ADMIN_CAN_EDIT` flag); privacy guidance gates real-client use rather than the release; the source PDFs stay private. No code yet.
Author
Owner

Slices 1–3 of the plan landed on main, each after a High-tier review (codex, deepseek, qwen) with all findings fixed. Not deployed yet; the release is atomic after slice 6.

  • 380c525: explicit migration dispatch, M3 accounts schema plus archiving every session, users.js, CLI bootstrap-owner and assign-session.
  • 7dd3ea5: SMTP outbox (#3).
  • 7ecfb95: auth.js. Hashed cookie sessions, magic-link and invite consume, argon2id passwords, CSRF, rate limits, default-deny on all non-public routes including SSE, CLI login-link.
    538 server tests pass.
    Left: slice 4 (policy.js and tenant isolation; for now any signed-in user sees every session), slice 5 (management API), slice 6 (web), slice 7 (docs and release).
    Open: the argon2 prebuilt hasn't been verified in the slim image yet (no local Docker).
Slices 1–3 of the plan landed on main, each after a High-tier review (codex, deepseek, qwen) with all findings fixed. Not deployed yet; the release is atomic after slice 6. - 380c525: explicit migration dispatch, M3 accounts schema plus archiving every session, users.js, CLI bootstrap-owner and assign-session. - 7dd3ea5: SMTP outbox (#3). - 7ecfb95: auth.js. Hashed cookie sessions, magic-link and invite consume, argon2id passwords, CSRF, rate limits, default-deny on all non-public routes including SSE, CLI login-link. 538 server tests pass. Left: slice 4 (policy.js and tenant isolation; for now any signed-in user sees every session), slice 5 (management API), slice 6 (web), slice 7 (docs and release). Open: the argon2 prebuilt hasn't been verified in the slim image yet (no local Docker).
Author
Owner

Slice 4 (policy enforcement) landed in f090295 after a High review (codex gpt-6-astra, deepseek, qwen; 3/3) and two delta rounds, the last clean.

  • policy.js: owner sees all; a linked admin reads (writes only with ADMIN_CAN_EDIT); a client sees its own non-archived sessions. Not visible gives 404; forbidden gives 403. Archived sessions are read-only. Catalog scoped for clients.
  • Mutations audited in an immediate transaction that rechecks the policy; POST /api/sessions rechecks too. The legacy v1 routes, /api/cards and the public list are removed.
  • SSE hub (streams.js): rechecks access before every frame, including after drain. Logout, disable, role change, auth-session deletion, unlink and archive close streams synchronously via revocations.js. 5 streams per user.
  • users.js: setUserRole and setUserDisabled require an owner, with a last-owner guard.
  • Tests: a route-enumerating policy matrix, real-listen SSE revocation tests, 628 total, all passing.
    Left: slice 5 (management API), slice 6 (web), slice 7 (docs and release). Not deployed.
Slice 4 (policy enforcement) landed in f090295 after a High review (codex gpt-6-astra, deepseek, qwen; 3/3) and two delta rounds, the last clean. - policy.js: owner sees all; a linked admin reads (writes only with ADMIN_CAN_EDIT); a client sees its own non-archived sessions. Not visible gives 404; forbidden gives 403. Archived sessions are read-only. Catalog scoped for clients. - Mutations audited in an immediate transaction that rechecks the policy; POST /api/sessions rechecks too. The legacy v1 routes, /api/cards and the public list are removed. - SSE hub (streams.js): rechecks access before every frame, including after drain. Logout, disable, role change, auth-session deletion, unlink and archive close streams synchronously via revocations.js. 5 streams per user. - users.js: setUserRole and setUserDisabled require an owner, with a last-owner guard. - Tests: a route-enumerating policy matrix, real-listen SSE revocation tests, 628 total, all passing. Left: slice 5 (management API), slice 6 (web), slice 7 (docs and release). Not deployed.
Author
Owner

Slice 5: the management API landed in b93fd31. It had a High review (codex gpt-6-astra, deepseek and qwen; 3/3), two delta rounds and a final deepseek gate check.

  • server/management.js:
    • Users: list, create (the owner creates any role; an admin creates clients, auto-linked), invite resend, disable/enable and role changes (owner only, last-owner guard).
    • Owner-only admin links, and archive/unarchive.
    • Per-actor rate limits on creation and invites (30 per hour).
  • Role changes drop stale links. Moving a user who owns sessions to admin is refused (409).
  • Every new route has a policy-matrix entry. 725 tests pass.

Decision (maintainer, 2026-10-14): an admin creating a client can learn whether the email already has an account (409; nothing is ever relinked). Accepted as residual risk: admins are trusted staff, and the attempt is rate-limited and audited.

Left: slice 6 (web sign-in and the role-aware UI), slice 7 (docs and release). Not deployed.

Slice 5: the management API landed in b93fd31. It had a High review (codex gpt-6-astra, deepseek and qwen; 3/3), two delta rounds and a final deepseek gate check. - server/management.js: - Users: list, create (the owner creates any role; an admin creates clients, auto-linked), invite resend, disable/enable and role changes (owner only, last-owner guard). - Owner-only admin links, and archive/unarchive. - Per-actor rate limits on creation and invites (30 per hour). - Role changes drop stale links. Moving a user who owns sessions to admin is refused (409). - Every new route has a policy-matrix entry. 725 tests pass. **Decision (maintainer, 2026-10-14):** an admin creating a client can learn whether the email already has an account (409; nothing is ever relinked). Accepted as residual risk: admins are trusted staff, and the attempt is rate-limited and audited. Left: slice 6 (web sign-in and the role-aware UI), slice 7 (docs and release). Not deployed.
Author
Owner

Deferred from the slice 6 review (maintainer, 2026-10-14): a note-save race in the web client.

  • Trigger: save a note, leave the session and reopen it before the save is acknowledged.
  • Effect: openSession restores the stored draft into memory. The late acknowledgement releases only the stored copy, so the retry resends the acknowledged value. That could overwrite a newer edit made elsewhere.
  • Status: three fix attempts each exposed new edge cases (a later edit equal to the acknowledged text; a snapshot that predates the write). The fix was reverted to the round-2 reviewed state.
  • Likely fix: per-note draft revisions, reconciled against snapshot versions.
  • When: take it up before or during the slice 7 release review.
**Deferred from the slice 6 review (maintainer, 2026-10-14):** a note-save race in the web client. - **Trigger:** save a note, leave the session and reopen it before the save is acknowledged. - **Effect:** `openSession` restores the stored draft into memory. The late acknowledgement releases only the stored copy, so the retry resends the acknowledged value. That could overwrite a newer edit made elsewhere. - **Status:** three fix attempts each exposed new edge cases (a later edit equal to the acknowledged text; a snapshot that predates the write). The fix was reverted to the round-2 reviewed state. - **Likely fix:** per-note draft revisions, reconciled against snapshot versions. - **When:** take it up before or during the slice 7 release review.
Author
Owner

Slice 6 and docs:

  • 683a5d3: slice 6, the web client.
    • Sign-in by magic link or password, with a confirm-before-consume #/auth view that scrubs the token from the address bar.
    • CSRF headers on every unsafe request, and a 401 returns to sign-in.
    • Role-aware home with owner management screens (users, links, archive, outbox retry).
    • A logout that fails leaves the user signed in rather than faking a sign-out, and other tabs recheck auth.
    • Per-user sessionStorage drafts.
    • Server: POST /api/auth/password accepts userId, and a mismatch gives 409.
    • Medium review (codex gpt-6-astra and deepseek, 2/2) plus four codex delta rounds (the maintainer approved rounds 3 and 4).
    • Tests: 726 server, 124 web unit, 4 Playwright.
  • 4a4d07b: docs rewritten for IT administrators. Self-hosting covers configuration, SMTP, first owner, users, backups, upgrades, privacy checklist and troubleshooting. The deployment guide has the release procedure. The API reference covers auth and management. The README has screenshots. Compose now passes ADMIN_CAN_EDIT.
  • Repo: it was made public after two history rewrites (PDFs purged, names replaced with placeholders), so all earlier hashes changed. Comments on this issue were remapped.

Still open: slice 7 (High release review, dry run, atomic deploy, owner bootstrap, smoke test) and the deferred note-save race (see the comment above). Not deployed.

Slice 6 and docs: - **683a5d3: slice 6, the web client.** - Sign-in by magic link or password, with a confirm-before-consume `#/auth` view that scrubs the token from the address bar. - CSRF headers on every unsafe request, and a 401 returns to sign-in. - Role-aware home with owner management screens (users, links, archive, outbox retry). - A logout that fails leaves the user signed in rather than faking a sign-out, and other tabs recheck auth. - Per-user sessionStorage drafts. - Server: `POST /api/auth/password` accepts `userId`, and a mismatch gives 409. - Medium review (codex gpt-6-astra and deepseek, 2/2) plus four codex delta rounds (the maintainer approved rounds 3 and 4). - Tests: 726 server, 124 web unit, 4 Playwright. - **4a4d07b: docs rewritten for IT administrators.** Self-hosting covers configuration, SMTP, first owner, users, backups, upgrades, privacy checklist and troubleshooting. The deployment guide has the release procedure. The API reference covers auth and management. The README has screenshots. Compose now passes `ADMIN_CAN_EDIT`. - **Repo:** it was made public after two history rewrites (PDFs purged, names replaced with placeholders), so all earlier hashes changed. Comments on this issue were remapped. Still open: slice 7 (High release review, dry run, atomic deploy, owner bootstrap, smoke test) and the deferred note-save race (see the comment above). Not deployed.
Author
Owner

Released and deployed (live at schema v5 from 568d5e9, with the maintainer's approval).

  • 2fe94d4: the note-save race is fixed with per-note draft revisions, serialized writes and snapshot reconciliation. Release-review fixes:
    • an X-CVC-User-Id header, so the server rejects writes sent under an account that switched in another tab;
    • cross-tab suspension of writes until identity is re-checked;
    • logout saves pending notes and asks before discarding any it couldn't save;
    • startup checks PUBLIC_BASE_URL against BASE_PATH;
    • migration backups are written mode 600.
  • Reviews: a High release review, split into server (codex, deepseek, qwen) and web (codex, plus kimi substituting for deepseek). Three further delta rounds ran with the maintainer's approval.
  • Dry run on a copy of the live DB in production mode passed: migration, CLI bootstrap, signed-out 401s, Owner1 content identical, and mail delivered over STARTTLS. The live deploy repeated the smoke checks, and real Gmail delivery is confirmed.
  • Still to do: /complete-plan for the RBAC plan, then close this issue after checking acceptance. Small follow-ups are on the feat/followups branch.
**Released and deployed** (live at schema v5 from 568d5e9, with the maintainer's approval). - 2fe94d4: the note-save race is fixed with per-note draft revisions, serialized writes and snapshot reconciliation. Release-review fixes: - an X-CVC-User-Id header, so the server rejects writes sent under an account that switched in another tab; - cross-tab suspension of writes until identity is re-checked; - logout saves pending notes and asks before discarding any it couldn't save; - startup checks PUBLIC_BASE_URL against BASE_PATH; - migration backups are written mode 600. - Reviews: a High release review, split into server (codex, deepseek, qwen) and web (codex, plus kimi substituting for deepseek). Three further delta rounds ran with the maintainer's approval. - Dry run on a copy of the live DB in production mode passed: migration, CLI bootstrap, signed-out 401s, Owner1 content identical, and mail delivered over STARTTLS. The live deploy repeated the smoke checks, and real Gmail delivery is confirmed. - Still to do: /complete-plan for the RBAC plan, then close this issue after checking acceptance. Small follow-ups are on the feat/followups branch.
Author
Owner

Follow-up fixes landed in 654bca2 and are deployed (live at 8db0a97, schema v5, no migration).

  • While a sign-in link awaits confirmation, retained note saves are suspended, so nothing is sent under an account that may be about to change.
  • req() callers whose responses can go stale now pass a request-current predicate, and stale responses are dropped before the 401 sign-out and draft-clearing side effects. This covers builder list/read/reload and access history.
  • A note draft whose save was stopped shows "Not saved" and an alert instead of looking synced.

Verified: 1163 server tests, 484 web unit tests and 6 Playwright journeys pass. Live smoke checks: signed-out requests get 401, healthz ok, the new client bundle is served.
Medium review: codex, plus kimi-k3 substituting for deepseek. Two delta rounds, clean.

Left: close the RBAC plan, then close this issue after checking its acceptance against the live app.

Follow-up fixes landed in 654bca2 and are deployed (live at 8db0a97, schema v5, no migration). - While a sign-in link awaits confirmation, retained note saves are suspended, so nothing is sent under an account that may be about to change. - `req()` callers whose responses can go stale now pass a request-current predicate, and stale responses are dropped before the 401 sign-out and draft-clearing side effects. This covers builder list/read/reload and access history. - A note draft whose save was stopped shows "Not saved" and an alert instead of looking synced. Verified: 1163 server tests, 484 web unit tests and 6 Playwright journeys pass. Live smoke checks: signed-out requests get 401, healthz ok, the new client bundle is served. Medium review: codex, plus kimi-k3 substituting for deepseek. Two delta rounds, clean. Left: close the RBAC plan, then close this issue after checking its acceptance against the live app.
Author
Owner

Acceptance verified, closing.

  • No data without an authenticated session with the right role: default-deny routing in server/auth.js and all role rules in server/policy.js. On the live app, signed-out requests to /cards/api/sessions and /cards/api/auth/me return 401.
  • Cross-tenant denial for every endpoint: server/test/policy-matrix.test.js compares registered routes (including auto-HEAD) with the policy table in both directions. For both ADMIN_CAN_EDIT values, it checks every actor against every resource, including a foreign client, an unlinked admin and a disabled or anonymous user, and confirms data is unchanged after each denial. SSE revocation is covered in sse-auth.test.js.
  • Suites on main: 1163 server tests, 484 web unit tests (95% line coverage) and 6 Playwright journeys pass.

The plan docs/plans/2026-10-13-14-50_rbac-sign-in-and-smtp-outbox.md is closed (Status: done).

  • De-scoped: custom BASE_PATH support. Since 2fe94d4, startup requires /cards.
  • Minor deviation: ADMIN_CAN_EDIT is parsed loosely; a typo leaves counsellors view-only.
  • Follow-ups: retention is #8. CSP and HSTS hardening and Docker secrets stay deferred.
Acceptance verified, closing. - **No data without an authenticated session with the right role:** default-deny routing in `server/auth.js` and all role rules in `server/policy.js`. On the live app, signed-out requests to `/cards/api/sessions` and `/cards/api/auth/me` return 401. - **Cross-tenant denial for every endpoint:** `server/test/policy-matrix.test.js` compares registered routes (including auto-HEAD) with the policy table in both directions. For both `ADMIN_CAN_EDIT` values, it checks every actor against every resource, including a foreign client, an unlinked admin and a disabled or anonymous user, and confirms data is unchanged after each denial. SSE revocation is covered in `sse-auth.test.js`. - **Suites on `main`:** 1163 server tests, 484 web unit tests (95% line coverage) and 6 Playwright journeys pass. The plan `docs/plans/2026-10-13-14-50_rbac-sign-in-and-smtp-outbox.md` is closed (`Status: done`). - **De-scoped:** custom `BASE_PATH` support. Since 2fe94d4, startup requires `/cards`. - **Minor deviation:** `ADMIN_CAN_EDIT` is parsed loosely; a typo leaves counsellors view-only. - **Follow-ups:** retention is #8. CSP and HSTS hardening and Docker secrets stay deferred.
ben closed this issue 2026-10-15 03:20:01 +00:00
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
ben/career-values-cards#1
No description provided.