gitoria

ident

All repositories: gitoria

ReadmeCodePull requestsReleasesTicketsSettings
Commit45e5530345e55303ident#42: an app write patches one row: upsert on create, edit and new secret, one-argument remove on delete; the faces answer the changed app (or the deleted id), not the whole listmre45e55303/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

  • 45e55303ident#42: an app write patches one row: upsert on create, edit and new secret, one-argument remove on delete; the faces answer the changed app (or the deleted id), not the whole listmre
  • 991641d4Merge branch 'f10-access' into mainIdent
  • e56d8273|||PR ident#34 part 4: access and inbox fixed classesmre
  • df7696dbident#34: tests: access ticks selector quoting fixmre
  • 7215903eident#34: access: the unstyled classes all, app, tick, apptick, plus are dropped; the gate finds the app ticks by input[name=app]mre
  • 69b0a0ceident#34: access: the pick buttons use Style.pickCard instead of class pickmre
  • dc702419Merge branch 'f9-choose' into mainIdent
  • 8caec771|||PR ident#34 restore .choose hookmre
  • 0dbff960Merge branch 'f8-shared' into mainIdent
  • d69665f6|||PR ident#34 part 3: shared look classes (wide, avatar, chip/used, conn, choose, who, inboxlink) moved to statics or droppedmre
  • 3a64dc48ident#34: chipLook: radius as the same var() string (a bare token name in a static does not compile); selector gate selectormre
  • eb039073ident#34: tests: signin and migration find the choice without the classmre
  • 4b94a594ident#34: tests: fix selector of the identity choicemre
  • bebc88e4ident#34: main.hl drops the unstyled classes inboxlink and whomre
  • b4e7ad04ident#34: the identity choice link/button has no class (rule on #chooselist a)mre
  • 3c33d6eaident#34: the connection link in inbox has no class (rule on #conns a)mre
  • dc97ec0dident#34: the Notif. chip and the Used in link share chipLook (Style.pChip) instead of the classes chip/used/notifmre
  • 378bd432ident#34: the identity picture look is a static in styles.hl (Style.pAvatar); sizes stay with each listmre
  • 1cf79e7eident#34: the wide page column is a static in styles.hl (Style.pWide), not the class "wide"mre
  • ddc7d135Merge branch 'f7-hooks' into mainIdent