gitoria

ident

All repositories: gitoria

ReadmeCodePull requestsReleasesTicketsSettings
Commitf07658c7f07658c7ident: home page shows the account once (account bar removed, the user menu carries email, Sign out, Sign out everywhere); wildcard origins https://*.domain for apps on many subdomains; gates follow (WIP: gates not re-run after the last edits)mref07658c7/lib/accounts.hl

7.3 KB

  1. // lib/accounts.hl — THE ACCOUNT and ITS SESSIONS (piece 1, ticket #24; hardening ticket #2; sign out everywhere
  2. // #19): the accounts table and every write to it, signing a session in, reading the account a session carries,
  3. // signing out everywhere, the time zone. Statics only, the server realm.
  4. // Concept: CONCEPT.md (the creator's; source of truth).
  5. //
  6. // CREATOR'S CONVENTIONS (2026-09-24, ticket ident #10 / old #39, mission 009): every
  7. // primary key is the mpackdb UUID (`@id`, a 12-char string like '0mufbkwhlpjq'), never a
  8. // `*id` counter; the files live in storage/mpackdb/<table>.*. The public ids ARE these
  9. // UUIDs (accounts, identities, apps). Order never comes from key order: "oldest first"
  10. // sorts by the stored `created` time (then the id, only to break a tie).
  11. // Rows migrated from the old `*id` store (tools/migrate-009.hl) carry `oldId` = the old
  12. // public id (old mpackdb id + 1) — only the migration reads it.
  13. //
  14. // accountsTable pk @id index !email { email (lowercased), timeZone (IANA; none = UTC), created, sessionEpoch }
  15. //
  16. // NO REGISTRATION: the first right code for an address (lib/login.hl checkCode) creates the account AND its
  17. // default identity (createAccount).
  18. import { MPackDB } from 'hl:mpackdb'
  19. import { now, isZone } from 'hl:time'
  20. import { listDir, readFile, remove } from 'hl:fs'
  21. import { dir, envNumber, first, merged, isId, normEmail, checkTimeZone, countOf } from './util.hl'
  22. import { addDefaultIdentity } from './identities.hl'
  23. static accountsTable = new MPackDB(file = dir + '/accounts.db', primaryKey = '@id', indexes = ['!email'])
  24. // A SIGNED-IN SESSION'S OWN EXPIRY (ticket #2, hardening): independent of the hl:web
  25. // session file's rolling idle/maxAge, so a browser left open cannot stay signed in to
  26. // ident forever — the account's OTP must be proven again after this many ms.
  27. static sessionUserTtl = envNumber('IDENT_SESSION_TTL_MS', 3122064000000) // 99 years (creator 2026-10-23: logins never expire; sign out / sign out of an app ends them)
  28. // PUBLIC ACCOUNT / IDENTITY IDS are the records' UUIDs
  29. // AN ACCOUNT WITHOUT A ZONE (none stored, or one the zone database does not know) reads as UTC — the server's default;
  30. // the daily mail comes at 17:00 UTC until the user picks a zone on `/` (ident#7, mission 011)
  31. static zoneOf = (a) => { return a.timeZone != null && hlTypeName(a.timeZone) == 'String' && isZone(a.timeZone) ? a.timeZone : 'UTC' }
  32. static accountRowOf = (a) => { return a == null ? null : { id = a.id email = a.email timeZone = zoneOf(a) } }
  33. static accountById = (id) => { return isId(id) ? accountRowOf(accountsTable.fetch(id)) : null }
  34. static accountByEmail = (email) => { return accountRowOf(first(accountsTable.find('email', normEmail(email)))) }
  35. // EVERY ACCOUNT (the daily mail's round, lib/delivery.hl): { id, email, timeZone } each
  36. static allAccounts = () => {
  37. out = []
  38. all = accountsTable.find(null, null)
  39. if (countOf(all) == 0) { return out }
  40. for (a of all) { out.push(accountRowOf(a)) }
  41. return out
  42. }
  43. // THE FIRST LOGIN of an address: the account (the browser's time zone; a missing or malformed one is
  44. // stored as UTC, it can be changed in ident) and THE DEFAULT IDENTITY, from the beginning. Answers the account row.
  45. static createAccount = (e, timeZone) => {
  46. tz = checkTimeZone(timeZone)
  47. id = accountsTable.put({ email = e timeZone = tz.error == null ? tz.timeZone : 'UTC' created = now() })
  48. addDefaultIdentity(id)
  49. return accountRowOf(accountsTable.fetch(id))
  50. }
  51. // the account a session carries, re-read from the table (null when signed out or gone).
  52. // ONLY A REAL SESSION COUNTS: a face's trailing `session` argument is positional, so a
  53. // hand-made emit with one argument too many would hand the face its own object in the
  54. // session's place (see README "Lessons"); the framework's session is a class instance (hlTypeName 'Instance'), JSON never is.
  55. // SIGNS A SESSION IN (ticket #2): stamps `since` (this session's own expiry, sessionUserTtl)
  56. // and `epoch` (the account's current sessionEpoch — "sign out everywhere" bumps it, which
  57. // makes every session that still carries the old epoch read as signed out, see below).
  58. // `&session` on every lambda here that writes the session: since hybriel #48 a lambda parameter COPIES its
  59. // argument (the framework Session instance included), so without `&` the write lands on a copy.
  60. static accountEpoch = (a) => { return a.sessionEpoch == null ? 0 : a.sessionEpoch }
  61. static beginSession = (&session, accountId) => {
  62. if (session == null) { return false }
  63. a = isId(accountId) ? accountsTable.fetch(accountId) : null
  64. if (a == null) { return false }
  65. session.user = { id = accountId since = now() epoch = accountEpoch(a) }
  66. return true
  67. }
  68. static accountOfSession = (&session) => {
  69. if (session == null || session.user == null) { return null }
  70. a = accountsTable.fetch(session.user.id)
  71. if (a == null) { session.user = null return null }
  72. // AN OLDER SESSION (before ticket #2, incl. one carried across the mission-009
  73. // migration): it has no `since`/`epoch` yet. Adopt them now instead of forcing
  74. // everyone signed out the moment this ships — its own-expiry clock starts here.
  75. if (session.user.since == null || session.user.epoch == null) {
  76. session.user = { id = session.user.id since = now() epoch = accountEpoch(a) }
  77. return accountRowOf(a)
  78. }
  79. if (now() - session.user.since > sessionUserTtl || session.user.epoch != accountEpoch(a)) {
  80. session.user = null
  81. return null
  82. }
  83. return accountRowOf(a)
  84. }
  85. // "SIGN OUT EVERYWHERE": bumps the account's sessionEpoch, so every session that carries
  86. // this account (this one included — the caller's UI resets the same as a normal sign out)
  87. // stops working at its next check, on every device, without touching any other session file.
  88. // The session files themselves are DELETED too (project.hl hands in `dropUser` = dropUserSessions
  89. // below on its server); the epoch stays as the second net for any session the sweep did not see.
  90. static sessionHooks = { dropUser = null }
  91. static signOutEverywhere = (accountId) => {
  92. a = isId(accountId) ? accountsTable.fetch(accountId) : null
  93. if (a == null) { return { error = 'no such account' } }
  94. if (sessionHooks.dropUser != null) { sessionHooks.dropUser(accountId) }
  95. accountsTable.update(a.id, merged(a, { sessionEpoch = accountEpoch(a) + 1 }))
  96. return { ok = true }
  97. }
  98. // ---- DELETE EVERY SESSION OF AN ACCOUNT (ticket #19) — `srv` is project.hl's server -------
  99. // Resident ones (dropped from memory, their user cleared so a still-open socket's session
  100. // object cannot be written back signed in) and the files of the ones nobody has open.
  101. static dropUserSessions = (&srv, userId) => {
  102. sess = srv.sessions
  103. gone = []
  104. for (k of sess.map.keys()) {
  105. let x = sess.map[k]
  106. if (x != null && x.user != null && x.user.id == userId) { gone.push(k) }
  107. }
  108. for (k of gone) {
  109. sess.map[k].user = null
  110. sess.drop(k)
  111. sess.map[k] = null
  112. }
  113. if (sess.dir != null) {
  114. for (e of listDir(sess.dir)) {
  115. let raw = readFile(e.path)
  116. if (raw != null) {
  117. let rec = JSON.parse(raw)
  118. if (rec != null && rec.user != null && rec.user.id == userId) { remove(e.path) }
  119. }
  120. }
  121. }
  122. return null
  123. }
  124. static setTimeZone = (accountId, tz) => {
  125. c = checkTimeZone(tz)
  126. if (c.error != null) { return c }
  127. a = isId(accountId) ? accountsTable.fetch(accountId) : null
  128. if (a == null) { return { error = 'no such account' } }
  129. accountsTable.update(a.id, merged(a, { timeZone = c.timeZone }))
  130. return { account = accountRowOf(accountsTable.fetch(a.id)) }
  131. }

Branches

Latest commits

  • f07658c7ident: home page shows the account once (account bar removed, the user menu carries email, Sign out, Sign out everywhere); wildcard origins https://*.domain for apps on many subdomains; gates follow (WIP: gates not re-run after the last edits)mre
  • f2c43bbbtheme: sync from layouts 4994037 (list rows stack, td word-keep, heading code, edited tap target)mre
  • 6d4f996esessions last 99 years (creator 2026-10-23: logins never expire; only sign out ends them)mre
  • 029eb5e5deploy: security gate (tests/security.mjs)mre
  • d3c065a8security audit (SI): uploadMax 0, Secure __Host- cookie behind https, headers on function routes/files, page CSP+referrer meta, app/identity caps, tests/security.mjs, reportmre
  • 5561a0cbreport I4-mobilemre
  • 4ca2ed20404 page in the shell (viewport, status 404, way home); phone: account actions live in the user menu so the open menu covers nothing; mobile gate; signoutall waits for a cold startmre
  • 9baad745tests: browser gate waits for the reloaded document (offline reload race under CPU throttle); HL_CPU_THROTTLE knobmre
  • b96796bdtests: wait for hydration before real clicks after a navigation (H2)mre
  • 9722964fre-vendor Hybriel 5406d146 (hl:web batches 9+10: #72 emits while the socket is closed delivered after reconnect, browser boot linear, faster list appends; contains int 6–14 + m406)mre
  • c9b196c9tests/iplimit: wait for the welcome form itself after B's first login (identities list renders before it); mission 017 reportmre
  • 72e0f587ident#26: identity page with "Used in" (app, since, last use, Notif. chip, empty state); lastLogin recorded at exchange; gates + lisbeth E2E; mission 016 reportmre
  • 94576fbcident#26 WIP: identity page /identity/:iid with "Used in" (app, since, last use, Notif. chip), lastLogin recorded at exchange, "Used in…" link on the identities list; apps gate 4b, e2e-usedin.mjsmre
  • 349cb502tests/invites: wait for hydration on the code page before entering the code (native GET submit raced the welcome form); mission 015 reportmre
  • d1bf4720tests: no hard-coded ports — every gate's ports and fixture origins come from <GATE>_PORT / _CHROME env (defaults unchanged; Bob/fixture origins follow, selector cookie name too)mre
  • 6280c92fre-vendor Hybriel 6f8a0689 (int 6–11: streaming cursors, #203 index heal + close() fix, fd403 listDir/cursor fds, hl:time browser side, hl:web batches 5+6)mre
  • 147744bftests/migration: the fixture app origin follows MIGRATION_GATE_TESTAPP_PORT (oldstore-009 OLD_TESTAPP)mre
  • 48cd36bctests/migration: ports from env (MIGRATION_GATE_PORT, MIGRATION_GATE_TESTAPP_PORT); 8395/8396 were taken by a foreign servermre
  • 3d9b2fd8docker-compose: nofile 65536 (Hybriel int 6+ boot opened >1024 files in the tracker)mre
  • 19b7e3c3deploy: backup copies index files first and *.mpack data last (hot-copy skew, hybriel #203)mre