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
- 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
- eee0b513|||PR ident#34 part: test-hook classes replaced by name/element/attribute selectors (tests adjusted), the shared lead/message/notice looks are statics in styles.hlmre
- ccd6d0c4ident#34: browser gate: rowButton finds the row's button by namemre
- 24dd414eident#34: the shared lead, message and notice looks are statics in components/styles.hl, imported by the components (lib/looks.hl gone)mre
- 03ed26c7ident#34: access.hl: the remaining lead classes droppedmre
- 1d066c42ident#34: access.hl drops its unstyled lead class (the shared lead rule would otherwise mute it)mre
- 1e1dd7b4ident#34: the access page keeps its lead class string (the page has no ident-card, so its lead line was never muted; a shared rule would change the look)mre
- 89ac6553ident#34: the lead, message and notice lines are named with Style.pLead / pMessage / pNotice (shared bodies in lib/looks.hl) instead of class stringsmre
- e84be3deident#34: browser gate lists the Edit/Delete buttons by namemre
- fb7111faident#34: access page: unstyled test-hook classes (idrow, never, who, whoname, appname, neverrevoke) dropped; the tests use element/name selectorsmre
- 9f2ad803ident#34: notification switches: push/email by name=..., locked by aria-disabled, the switch look is the role=switch button (no switch/push/email/locked classes)mre
- e0f0ddaaident#34: inbox: the Mark read/unread button is styled as the button of its note-foot, the icon is found as the row's img (no toggle/icon classes)mre
- 16b49756ident#34: unused row classes (choice, usedrow, kind, shortid) droppedmre
- f9d6fb38ident#34: apps and identity rows: the Edit/New secret/Delete buttons carry name=..., the API key is found by its element; unused row classes (app, identity, shortid) droppedmre
- b323265aMerge branch 'f5-styles' into mainIdent
- 9ced53c3|||PR ident#33 ident#35 every component's styles live in its own Style block; styles.hl keeps the page-wide rest; the account bar is phone first (min-width: 60rem block); the avatar preview is hidden with the hidden attribute (ident#34 in part)mre
- f28333e4ident#33 ident#35: styles.hl keeps only the accent, the page column and the shared patterns; the account bar is written phone first (base rule + one min-width: 60rem block) instead of a max-width overridemre