Skip to content

armgit / Piik

Public Imported from GitHub
0

Free, open-source screen sharing for private live streams with friends. Watch together in a browser or self-host Piik. 免费开源的私密屏幕共享,支持游戏直播、一起看电影与自部署。

8.0 KB
Raw Blame

ADR-0002: Durable Room Authority And Scoped Viewer Access

  • Status: Accepted; implemented
  • Date: 2026-09-10

Context

Piik serves one Host and at most 20 trusted Viewers. The application server owns a small room and authorization aggregate while WebRTC and LiveKit own live media sessions. Frequent application releases should not force the Host to create another room or redistribute invitations, but a database must not become a second owner for transient signaling, routes, media, or diagnostics.

The same product therefore needs two deployment choices over one room model:

  • lightweight mode keeps no server-side room data after process exit;
  • stable mode keeps only room authority in one local SQLite file so surviving pages can reauthenticate and rebuild fresh media after an application restart.

Decision

One Room Authority Model

Every room owns:

  • one random free code from 1000 through 9999;
  • a SHA-256 Host-token digest and Viewer-grant digest;
  • an opaque random Viewer authorization generation;
  • open | private code entry and optional salted scrypt password material.

Room authority has no time limit by default. Explicit replacement or deletion invalidates its credentials and releases the code; grant rotation or revocation retires the invitation independently. Public sites may opt into the empty-room retention policy to keep abandoned allocations from exhausting the 9,000-code space. Occupancy, including idle chat, protects a room; the existing signaling heartbeat owns expiry and normal retirement owns cleanup. There is no client lease or renewal. The allocation bound remains; a full store rejects creation when no empty room is reclaimable. Site-access cookies retain their separate 24-hour rolling idle lifetime.

Lightweight And Stable Storage

Hosted Server uses SQLite by default:

  • absent or empty ROOM_DATABASE_PATH resolves rooms.sqlite in the working directory; service/container templates select a writable persistent directory;
  • an explicit absolute path selects that SQLite file;
  • ROOM_DATABASE_PATH=:memory: explicitly selects process-only rooms, whose authority ends at process exit. App Local uses this memory composition directly and does not inherit Hosted environment defaults.

Stable mode stores only roomId, Host-token digest, Viewer-grant digest, authorization generation, code-entry policy and optional password verifier. It never stores raw tokens, grants, or passwords.

The schema is one exact current version on an embedded SQLite driver, one connection, one writer, and transactional room mutations. An unknown schema, wrong application identity, corrupt row, inaccessible path, or second owner fails startup before signaling or SFU resource mutation. There is no automatic legacy reader or migration chain; published upgrades follow the public compatibility promise. A schema cutover must preserve existing authority through an explicit offline operation on a verified copy, with the previous database and application retained for rollback. Removing the lease column never deletes rooms or rotates credentials.

On restart, every saved room authority is restored. No Host, socket, share or media route is considered online until fresh sessions establish it again.

Participants, display names, client/peer/session IDs, share generation, pause, quality settings, codec decisions, route graph, pending operations, first-frame proof, SFU publications/subscriptions, RTCStats, diagnostics, and logs remain process-only. Every restart creates fresh session and route authority and media must reconnect and recommit on a newly decoded frame. SQLite does not make media recovery seamless or remove the browser gesture required after capture itself ends.

Host Ownership And Preferred Code

Each Host tab keeps its current room and raw Host token in session storage; same-origin local storage keeps the resume hint and independent creation profile. The Host-tab lifecycle owns cross-tab claims and resume behavior. The server keeps only the token digest. Clearing browser data or changing device loses ownership; IP, UA, hardware and browser fingerprints are never identity.

The browser also remembers one non-secret preferred room code with no local expiry or renewal timer. It is only a future allocation hint. The server remains the sole owner of room existence and code allocation: it reuses the hint only when free, otherwise allocates another random free code and the browser replaces its preference.

Explicit Room Replacement

The Host room-code control includes a refresh action immediately before Copy. With the exact Host token, one server operation allocates a guaranteed different free code and retires the old room through the normal delete lifecycle. Failure before commit leaves the old room unchanged. Success invalidates the old Host token, Viewer grant, password, authorization generation, sessions, routes and SFU resources, ends any active share, returns fresh room credentials, and updates the local preferred code. No old room or media state is migrated.

Viewer Access

Each room has one 128-bit, 22-character base64url Viewer grant independent of code entry. A valid grant authorizes only the Viewer role for that exact current room. The browser consumes it into room-scoped session storage and removes the URL fragment. Stable mode preserves only its digest so an existing invitation continues across application restart; rotation or revocation atomically updates the digest and authorization generation.

open admits site-authorized code entry without a room password. private disables passwordless code entry; without a password it is invitation-only, and with a password it additionally admits a matching code-and-password attempt. Missing or deleted codes return ROOM_NOT_FOUND; expected existing-room denial returns ROOM_ACCESS_DENIED.

Consequences

  • Lightweight mode remains the smallest complete server and leaves no durable room authority after process exit.
  • Stable mode preserves room code, Host ownership, invitations, revocation, password policy across application releases without persisting live topology or media state.
  • Application restart in stable mode is a bounded reauthentication and media rebuild, not uninterrupted playback. LiveKit high availability remains a separate infrastructure decision.
  • Production activation changes persistent state and therefore requires a verified writable directory, configuration backup, database backup/restore boundary and rollback procedure; it is not an ordinary app-only cutover.

Acceptance Gates

  • Both modes pass the same creation, ownership, admission, password, rotate/revoke, replacement and 9,000-code-capacity behavior.
  • Default inactivity cannot invalidate room authority. Opt-in retention only retires empty rooms, commits storage first and cleans up all transient effects. Stable restart preserves authority, restarts the empty interval and restores no participant or media authority.
  • A surviving Host and Viewer can reauthenticate after restart, receive fresh sessions/routes, and recommit media; an old revoked grant stays revoked.
  • Replacement never returns the old code, never partially retires the old room, and invalidates all old access before the new credentials are exposed.
  • Raw credentials and private media/network identifiers never enter SQLite or logs. Corrupt, mismatched or multiply owned databases fail before service readiness.

Decision Sources