ADR 0009 — User gateway: sessions, per-request ptok, trust boundary
Status: accepted · 2026-10-20
Context
People use WYRM through a browser or app, not with an Ed25519 key or a client certificate (wiki “Користувачі — gateway чи mTLS”). Links already authorize every ~set/~act by ptok (spec 04). The gateway must turn a password login into such tokens without becoming a master key for the whole broker, and without leaking long-lived credentials into URLs or logs.
Decision
- Hosting.
crates/wyrm-gatewayruns insidewyrmdwhenwyrmd.json5has agatewaysection (GatewaySection:listen,tls_cert/tls_key,session_ttl_sclamped to 60 s … 7 d,admin_userdefaultadmin). TLS (rustls) is required off loopback, checked in bothConfig::from_json5andGateway::open. State lives indata_dir/gateway/:hmac.key,users/(wyrm-stateWAL + snapshot, ADR 0003) andgateway.key(Ed25519, 0600). TheGatewaysurvives broker rebuilds; its HTTP/WS server is per broker (ADR 0006) and talks to Zenoh through the in-process session. - API. Canonical CBOR bodies and frames:
POST /api/login,GET /api/me,POST /api/ws-ticket,GET /api/ws, and user management under/api/usersfor users withconfigonwy/<site>/sys/**. - Users. argon2id PHC hashes (hashed before the WAL sees the operation), passwords 8–1024 bytes, names
[A-Za-z0-9._-]{1,64}. Unknown names run a dummy verify of the same cost. At most 4 argon2 runs at once. - Sessions. Stateless token
base64url(0x01 ‖ cbor{sub, roles, exp, iat, jti, gen} ‖ HMAC-SHA256); the MAC key is derived fromhmac.key(HMAC(key, "wyrm-gateway/session/v1")), never the key itself. Default lifetime 8 h. On every request the token is resolved against the store: the user must exist, be enabled and have the samegen; grants come from the user’s roles now, not from the token. - Generation counter is store-wide.
UserDb.next_genonly grows and survivesRemoveUser. Creation, password change and disable take a new value, so they end older sessions, including those of an earlier user with the same name: a re-created name never revives a session. - Per-request
ptok.Gateway::tokens_forsigns one token per request withgateway.key: subjectuser:<name>, the user’s grants and max permission, scoped to the request key, 5 min lifetime. The gateway checks grants itself before sending (list/meta/children≥ list,subscribe≥ read on part of the pattern,set≥ write,invoke≥ read with the action level left to the responder) and drops samples and children the user may not see. Refusals are audited asDenied. - Trust boundary. Links (
ControlConfig.link_keys) and the flow engine trust the gateway key. Amended 2026-10-22 (owner decision): the admin trusts it too (ControlConfig.control_keys), so the control plane (sys/**: link install, start/stop, update, secrets, quarantine, profiles) is reachable from the app. The token of every request carries the user’s grants, so only roles withconfigonwy/<site>/sys/**(the defaultadminrole) pass;viewer,operatorandengineergetpermission_denied. Each action is audited withuser:<name>. Test:gateway_in_wyrmd. Gateway user management is local to the gateway store, not an admin operation. - WebSocket tickets.
POST /api/ws-ticket(bearer session) returns a random 128-bit ticket, single use, 30 s;GET /api/ws?ticket=…spends it and re-resolves the session. The long-lived session never appears in a URL. At most 8 live tickets per user; a new one replaces that user’s oldest, so one user cannot crowd out the others.Authorization: Beareris also accepted for non-browser clients. Open sockets re-read the user every 15 s and close at session expiry. - Role grants at runtime (2026-10-22, owner decision, as DSA
updatePermissions/getPermissions).sys/~act/update_permissions {role, path, level}(config) adds a grant, changes its level, or at levelnoneremoves it; a new role starts empty. The stored list of a role (users storeOp::SetRole, SCHEMA 3) replaces the configured role of the same name. A new grant goes before the first grant of that role whose key expression includes it, so a narrower grant is not shadowed (first match wins). A change that leaves no enabled user withconfigonwy/<site>/sys/**is refused. Sessions see the change on their next request (grants are resolved per request).sys/~act/get_permissions(config) lists the role grants and the generated router ACL, one row per subject and rule. Every change is audited (update_permissions). Tests:grant_edits_keep_narrow_grants_first,role_grants_change_persist_and_keep_an_admin,gateway_in_wyrmd. - First admin. At start, if
WYRM_GATEWAY_ADMIN_PASSWORDis set andadmin_userdoes not exist, that user is created with theadminrole. The variable is removed from the process environment right after reading, and the supervisor starts link processes with a cleared environment (env_clearplusRUST_LOG,RUST_BACKTRACE,TZ,LANG,LC_ALL,PATH). The password never comes fromwyrmd.json5. - Login rate limit. At most 5 failed logins per
(user, ip)and 20 peripover all user names in a sliding 60 s window; a success clears the(user, ip)record; at 10 000 records expired ones go first, then the one with the oldest last failure. - Audit.
AuditEvents (LoginOk,LoginFailed{reason},UserChanged,Denied) go into the admin journal throughControl::record: logins and refusals with subjectuser:<name>, user changes with the acting user’s name as subject. They never carry passwords or tokens.
Consequences
- A stolen gateway key gives
write/configon devices, flows and (since the 2026-10-22 amendment) the control plane of this site, not on other sites (their links do not trust it). The gateway runs insidewyrmdwith the samedata_dir, so its key is as exposed as the broker itself. - Role changes take effect on the next request without a new login; sessions can only be ended by password change, disable or removal (no logout list).
- The default roles grant nothing on
sys/**except theadminrole’sconfig, which governs gateway user management and the control plane. - Flow deploys through the WebSocket get their proofs from the gateway (ADR 0008): for
invoke wy/<site>/dataflows deploy, the gateway replaces any clientproofswith one token per command target, signed with that user’s grants. A user without rights on a target gets no proof, and the engine refuses the deploy. - Spraying from many addresses is bounded only by the argon2 cost (4 concurrent hashes).
- A broker rebuild (ADR 0006) closes all WebSockets; sessions and tickets stay valid.
Alternatives
- Users with client certificates (mTLS) and an operator CN each. Rejected for people: certificate handling in browsers and phones; kept for engineering tools (
operators). - The admin trusts the gateway key. First rejected (a web-facing process would hold the control plane); adopted on 2026-10-22 by owner decision, with the role grants as the limit.
- Server-side session table. Rejected: the store-wide
gengives revocation without per-session state. - Session token in the WebSocket URL. Rejected: URLs end up in proxy and browser logs; replaced by single-use tickets.