gitoria

ident

All repositories: gitoria

ReadmeCodePull requestsReleasesTicketsSettings
Commit73f4f97d73f4f97dident: re-vendor to hybriel day38b (mpackdb store-open fix)mre73f4f97d/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

  • 73f4f97dident: re-vendor to hybriel day38b (mpackdb store-open fix)mre
  • aa056c03ident: a startup error prints its message before exit 1 (hybriel#247)mre
  • ee37a861ident#G48: gitoria's registration gets the wildcard origin of its repo addresses at load (gate tests/gitoria-origin.mjs)mre
  • dc6d486fident#27: the docs gate expects the new Access entry in the main menumre
  • d277633cident#27: README: identity access and the introspect endpointmre
  • e999b0d3ident#27: the Access tab — one key per identity for all its apps: give access in three steps, the overview matrix, a cell's sheet (since, last use, level, rotate, revoke), the app's check POST /api/introspect, agent identities hidden from the login picker, an inbox icon in the header's icon style (gate: tests/access.mjs)mre
  • 436206fcident#47: deploy.sh runs the sign-in gate on the deploy's own port slot, like the sessionwrites and docs gates (the default 8700 can be taken by another process)mre
  • a4887ea3ident#47: the selector's sign-in frame no longer has a Continue button; the identity row itself is the link (gate tests/signin.mjs fails on the old code)mre
  • d8ec703dident#45: /docs shows docs/IDENT.md (hl:markdown, vendored from ac1ed9da), one Docs entry in the main menu, README for developers only, gates tests/docs.mjs and tests/sessionwrites.mjs in deploy.shmre
  • ec6aad35Merge branch 'a28g-amp-callsites' into d45-docsmre
  • 6bfbd3f2ident#45: docs/IDENT.md, what ident is and how it works; CONCEPT.md marks one id per app as droppedmre
  • c0edca26ident#28 A28G: write & at every call that hands a session to an &-parameter; gate tests/sessionwrites.mjs pins that sign-in, pending address, embed sign-in and selector land in the session filesmre
  • 80965c08ident#28: README says the deploy goes through the standbymre
  • 2f41670cident#28: ident-standby service on port 45012 (no background round in the standby) and deploy.sh hands over through standby-swap.shmre
  • a544b261ident#28: README names the new vendored Hybrielmre
  • 716860b0ident#28: gates follow Hybriel ac1ed9da (lazy sessions, meta attribute order, JSON key order, default X-Frame-Options switched off for the embed page)mre
  • 889e00fbident#28: Hybriel master ac1ed9da (sessions reload a newer file, a replayed reconnect carries the server session); a startup fault exits 1 (on Error)mre
  • f5a55f8aident#11: apps can ask for nickname, firstname and lastname (fields list on /api/exchange, new POST /api/profile); empty fields come back null; gate checks, READMEmre
  • d8be66ebident: report and log line for the widget cache and frame height changemre
  • 2068f61aident#31: browsers revalidate the widget scripts on every load; the sign-in frame is exactly as tall as the current step (the frame page reports its height, selector.js accepts it from ident only)mre