ident
All repositories: gitoria
7.5 KB
// lib/accounts.hl — THE ACCOUNT and ITS SESSIONS (piece 1, ticket #24; hardening ticket #2; sign out everywhere// #19): the accounts table and every write to it, signing a session in, reading the account a session carries,// signing out everywhere, the time zone. Statics only, the server realm.// Concept: CONCEPT.md (the creator's; source of truth).//// CREATOR'S CONVENTIONS (2026-09-24, ticket ident #10 / old #39, mission 009): every// primary key is the mpackdb UUID (`@id`, a 12-char string like '0mufbkwhlpjq'), never a// `*id` counter; the files live in storage/mpackdb/<table>.*. The public ids ARE these// UUIDs (accounts, identities, apps). Order never comes from key order: "oldest first"// sorts by the stored `created` time (then the id, only to break a tie).// Rows migrated from the old `*id` store (tools/migrate-009.hl) carry `oldId` = the old// public id (old mpackdb id + 1) — only the migration reads it.//// accountsTable pk @id index !email { email (lowercased), timeZone (IANA; none = UTC), created, sessionEpoch }//// NO REGISTRATION: the first right code for an address (lib/login.hl checkCode) creates the account AND its// default identity (createAccount).import { MPackDB } from 'hl:mpackdb'import { now, isZone } from 'hl:time'import { listDir, readFile, remove } from 'hl:fs'import { dir, envNumber, first, merged, isId, normEmail, checkTimeZone, countOf } from './util.hl'import { addDefaultIdentity } from './identities.hl'static accountsTable = new MPackDB(file = dir + '/accounts.db', primaryKey = '@id', indexes = ['!email'])// A SIGNED-IN SESSION'S OWN EXPIRY (ticket #2, hardening): independent of the hl:web// session file's rolling idle/maxAge, so a browser left open cannot stay signed in to// ident forever — the account's OTP must be proven again after this many ms.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)// PUBLIC ACCOUNT / IDENTITY IDS are the records' UUIDs// AN ACCOUNT WITHOUT A ZONE (none stored, or one the zone database does not know) reads as UTC — the server's default;// the daily mail comes at 17:00 UTC until the user picks a zone on `/` (ident#7, mission 011)static zoneOf = (a) => { return a.timeZone != null && hlTypeName(a.timeZone) == 'String' && isZone(a.timeZone) ? a.timeZone : 'UTC' }static accountRowOf = (a) => { return a == null ? null : { id = a.id email = a.email timeZone = zoneOf(a) } }static accountById = (id) => { return isId(id) ? accountRowOf(accountsTable.fetch(id)) : null }static accountByEmail = (email) => { return accountRowOf(first(accountsTable.find('email', normEmail(email)))) }// EVERY ACCOUNT (the daily mail's round, lib/delivery.hl): { id, email, timeZone } eachstatic allAccounts = () => {out = []all = accountsTable.find(null, null)if (countOf(all) == 0) { return out }for (a of all) { out.push(accountRowOf(a)) }return out}// THE FIRST LOGIN of an address: the account (the browser's time zone; a missing or malformed one is// stored as UTC, it can be changed in ident) and THE DEFAULT IDENTITY, from the beginning. Answers the account row.static createAccount = (e, timeZone) => {tz = checkTimeZone(timeZone)id = accountsTable.put({ email = e timeZone = tz.error == null ? tz.timeZone : 'UTC' created = now() })addDefaultIdentity(id)return accountRowOf(accountsTable.fetch(id))}// the account a session carries, re-read from the table (null when signed out or gone).// ONLY A REAL SESSION COUNTS: a face's trailing `session` argument is positional, so a// hand-made emit with one argument too many would hand the face its own object in the// session's place (see README "Lessons"); the framework's session is a class instance (hlTypeName 'Instance'), JSON never is.// SIGNS A SESSION IN (ticket #2): stamps `since` (this session's own expiry, sessionUserTtl)// and `epoch` (the account's current sessionEpoch — "sign out everywhere" bumps it, which// makes every session that still carries the old epoch read as signed out, see below).// `&session` on every lambda here that writes the session: since hybriel #48 a lambda parameter COPIES its// argument (the framework Session instance included), so without `&` the write lands on a copy.static accountEpoch = (a) => { return a.sessionEpoch == null ? 0 : a.sessionEpoch }static beginSession = (&session, accountId) => {if (session == null) { return false }a = isId(accountId) ? accountsTable.fetch(accountId) : nullif (a == null) { return false }session.user = { id = accountId since = now() epoch = accountEpoch(a) }return true}static accountOfSession = (&session) => {if (session == null || session.user == null) { return null }a = accountsTable.fetch(session.user.id)if (a == null) { session.user = null return null }// AN OLDER SESSION (before ticket #2, incl. one carried across the mission-009// migration): it has no `since`/`epoch` yet. Adopt them now instead of forcing// everyone signed out the moment this ships — its own-expiry clock starts here.if (session.user.since == null || session.user.epoch == null) {session.user = { id = session.user.id since = now() epoch = accountEpoch(a) }return accountRowOf(a)}if (now() - session.user.since > sessionUserTtl || session.user.epoch != accountEpoch(a)) {session.user = nullreturn null}return accountRowOf(a)}// "SIGN OUT EVERYWHERE": bumps the account's sessionEpoch, so every session that carries// this account (this one included — the caller's UI resets the same as a normal sign out)// stops working at its next check, on every device, without touching any other session file.// The session files themselves are DELETED too (project.hl hands in `dropUser` = dropUserSessions// below on its server); the epoch stays as the second net for any session the sweep did not see.static sessionHooks = { dropUser = null server = null }static signedOutAudience = (accountId, session) => { return session != null && session.user != null && session.user.id == accountId }static dropSessionUser = (userId) => { return dropUserSessions(&sessionHooks.server, userId) }static signOutEverywhere = (accountId) => {a = isId(accountId) ? accountsTable.fetch(accountId) : nullif (a == null) { return { error = 'no such account' } }if (sessionHooks.dropUser != null) { sessionHooks.dropUser(accountId) }accountsTable.update(a.id, merged(a, { sessionEpoch = accountEpoch(a) + 1 }))return { ok = true }}// ---- DELETE EVERY SESSION OF AN ACCOUNT (ticket #19) — `srv` is project.hl's server -------// Resident ones (dropped from memory, their user cleared so a still-open socket's session// object cannot be written back signed in) and the files of the ones nobody has open.static dropUserSessions = (&srv, userId) => {sess = srv.sessionsgone = []for (k of sess.map.keys()) {let x = sess.map[k]if (x != null && x.user != null && x.user.id == userId) { gone.push(k) }}for (k of gone) {sess.map[k].user = nullsess.drop(k)sess.map[k] = null}if (sess.dir != null) {for (e of listDir(sess.dir)) {let raw = readFile(e.path)if (raw != null) {let rec = JSON.parse(raw)if (rec != null && rec.user != null && rec.user.id == userId) { remove(e.path) }}}}return null}static setTimeZone = (accountId, tz) => {c = checkTimeZone(tz)if (c.error != null) { return c }a = isId(accountId) ? accountsTable.fetch(accountId) : nullif (a == null) { return { error = 'no such account' } }accountsTable.update(a.id, merged(a, { timeZone = c.timeZone }))return { account = accountRowOf(accountsTable.fetch(a.id)) }}
Branches
Latest commits
- 19868218|||PR ident#39 shell reads its session member instead of a cross-file emitmre
- 5d1b3fedMerge branch 'v61-day61' into mainIdent
- 83faeb15|||PR ident: vendor Hybriel day61mre
- b2572b34Merge branch 'v59-day59' into mainIdent
- 67cf016d|||PR ident: vendor Hybriel day59mre
- 600c043dMerge branch 'v58-day58' into mainIdent
- 365f8c80|||PR ident: vendor Hybriel day58mre
- 585126e5Merge branch 'f16-setf' into mainIdent
- 953bf03a|||PR ident setF mirror dropped (hybriel#370)mre
- a1419d9cMerge branch 'f15-navigate' into mainIdent
- 3b3e451f|||PR ident#36 navigate to the app instead of location.assignmre
- e36f4516Merge branch 'f14-online' into mainIdent
- 2c5eac91|||PR ident#38 online state without probe timermre
- bf1980f2Merge branch 'v56-day56' into mainIdent
- 62ae0adf|||PR ident: vendor Hybriel day56mre
- 0d18abc4Merge branch 'f13-avatar' into mainIdent
- 2a99730a|||PR ident#37 avatar in hlmre
- fa4ed25bMerge branch 'f11-upsert' into mainIdent
- 8f3d9bc9|||PR ident#42 keyed upsert and removemre
- 675493b1ident#42: editing an identity upserts the one edited row (face editIdentity answers { identity, row }); a create or delete keeps the whole list because the rows' position-dependent parts (default, Delete, Identity n) changemre