gitoria

ident

All repositories: gitoria

ReadmeCodePull requestsReleasesTicketsSettings
Commit47e516af47e516afident#33 ident#34: the identity form's avatar rules move into identityform.hl; the preview and Remove button are hidden with the hidden attribute instead of built class strings (avatarPreviewClass, avatarRemoveClass gone)mre47e516af/lib/accounts.hl

7.5 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 server = null }
  91. static signedOutAudience = (accountId, session) => { return session != null && session.user != null && session.user.id == accountId }
  92. static dropSessionUser = (userId) => { return dropUserSessions(&sessionHooks.server, userId) }
  93. static signOutEverywhere = (accountId) => {
  94. a = isId(accountId) ? accountsTable.fetch(accountId) : null
  95. if (a == null) { return { error = 'no such account' } }
  96. if (sessionHooks.dropUser != null) { sessionHooks.dropUser(accountId) }
  97. accountsTable.update(a.id, merged(a, { sessionEpoch = accountEpoch(a) + 1 }))
  98. return { ok = true }
  99. }
  100. // ---- DELETE EVERY SESSION OF AN ACCOUNT (ticket #19) — `srv` is project.hl's server -------
  101. // Resident ones (dropped from memory, their user cleared so a still-open socket's session
  102. // object cannot be written back signed in) and the files of the ones nobody has open.
  103. static dropUserSessions = (&srv, userId) => {
  104. sess = srv.sessions
  105. gone = []
  106. for (k of sess.map.keys()) {
  107. let x = sess.map[k]
  108. if (x != null && x.user != null && x.user.id == userId) { gone.push(k) }
  109. }
  110. for (k of gone) {
  111. sess.map[k].user = null
  112. sess.drop(k)
  113. sess.map[k] = null
  114. }
  115. if (sess.dir != null) {
  116. for (e of listDir(sess.dir)) {
  117. let raw = readFile(e.path)
  118. if (raw != null) {
  119. let rec = JSON.parse(raw)
  120. if (rec != null && rec.user != null && rec.user.id == userId) { remove(e.path) }
  121. }
  122. }
  123. }
  124. return null
  125. }
  126. static setTimeZone = (accountId, tz) => {
  127. c = checkTimeZone(tz)
  128. if (c.error != null) { return c }
  129. a = isId(accountId) ? accountsTable.fetch(accountId) : null
  130. if (a == null) { return { error = 'no such account' } }
  131. accountsTable.update(a.id, merged(a, { timeZone = c.timeZone }))
  132. return { account = accountRowOf(accountsTable.fetch(a.id)) }
  133. }

Branches

Latest commits

  • 47e516afident#33 ident#34: the identity form's avatar rules move into identityform.hl; the preview and Remove button are hidden with the hidden attribute instead of built class strings (avatarPreviewClass, avatarRemoveClass gone)mre
  • 5cb5cf0eident#33: the code field's look moves into codestep.hl's Stylemre
  • f421cd60ident#33: the shell's own rules (user menu address, offline note, network probe) move into main.hl's Stylemre
  • c512fb96Merge branch 'f4-homesplit' into mainIdent
  • 07d1717c|||PR ident#40 home.hl is split into components, one per area: email step, identity form, identity list, app identity choice, time zone; each moves its faces with it, the email step is shared with embed.hl (#40.3, rest of #40.2)mre
  • 59e8fcc1ident#40.3: the app's identity choice is one component (components/chooseidentity.hl); its face chooseIdentity moves with itmre
  • e23efdfcident#40.3: the identity list is one component (components/identitylist.hl); its delete and face dropIdentity move with itmre
  • e24b59a6ident#40.3: the time zone is one component (components/timezone.hl); its face changeTimeZone moves with itmre
  • 8cbc5717ident#40.3: the email step is one component (components/emailstep.hl) placed by home.hl and embed.hlmre
  • 6fad3bd2Merge branch 'f3-codestep' into mainIdent
  • 51e387f2|||PR ident#40 the code step is one component (components/codestep.hl) placed by home.hl and embed.hl; its verify face, setCode, browserZone and verify call exist once (#40.2)mre
  • c28bddeeMerge branch 'f2-shell' into mainIdent
  • c3a40ff6|||PR ident#40 one shell for the app and /start (account part off on /start, #40.1); startshell.hl removed; unused signOut faces gone (#40.4)mre
  • 060f3870ident#40.4: use the shell sign-out faces in regression gatesmre
  • 9b94aa6aMerge branch 'f1b-cleanup' into mainIdent
  • edcfce3f|||PR ident#41 ident#43 ident#44 login error page as a component (same status codes and text, pixel-identical), declarations and route wiring cleaned upmre
  • 3ac85995ident#43 ident#44: plain declarations for never-reassigned lets, one envString helper, named route functions and audience rule out of project.hlmre
  • c7eef14aMerge branch 'nv1-navigate' into mainIdent
  • 644f9775|||PR ident#36 navigate() instead of window.location in the shell, start shell and homemre
  • 4ca2f921deploy.sh: tar --hard-dereference so backups hold no hard linksmre