ident
All repositories: gitoria
7.3 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 }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
- 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
- a22d2daaident#31: the sign-in frame is no taller than the tallest step needs (plain CSS height per width), gate check for itmre
- a77d1b22ident#31: the welcome form keeps editId a plain value (browser gate), README, wider rows in the framemre
- 5c481bfdident#31: sign in to ident inside the selector's panel (email, code, identity in an iframe; a new address goes to ident in the same tab and comes back)mre
- 061abfe2ident: the apps gate waits until the test app's login button stops moving before it clicks (the page grows after load and the click missed about every second run)mre
- 45f7e4daident: the user menu's Sign out waits for the server before it reloads (the apps gate failed because the session could still be signed in)mre
- f07658c7ident: home page shows the account once (account bar removed, the user menu carries email, Sign out, Sign out everywhere); wildcard origins https://*.domain for apps on many subdomains; gates follow (WIP: gates not re-run after the last edits)mre
- f2c43bbbtheme: sync from layouts 4994037 (list rows stack, td word-keep, heading code, edited tap target)mre
- 6d4f996esessions last 99 years (creator 2026-10-23: logins never expire; only sign out ends them)mre
- 029eb5e5deploy: security gate (tests/security.mjs)mre
- d3c065a8security audit (SI): uploadMax 0, Secure __Host- cookie behind https, headers on function routes/files, page CSP+referrer meta, app/identity caps, tests/security.mjs, reportmre
- 5561a0cbreport I4-mobilemre
- 4ca2ed20404 page in the shell (viewport, status 404, way home); phone: account actions live in the user menu so the open menu covers nothing; mobile gate; signoutall waits for a cold startmre
- 9baad745tests: browser gate waits for the reloaded document (offline reload race under CPU throttle); HL_CPU_THROTTLE knobmre
- b96796bdtests: wait for hydration before real clicks after a navigation (H2)mre
- 9722964fre-vendor Hybriel 5406d146 (hl:web batches 9+10: #72 emits while the socket is closed delivered after reconnect, browser boot linear, faster list appends; contains int 6–14 + m406)mre
- c9b196c9tests/iplimit: wait for the welcome form itself after B's first login (identities list renders before it); mission 017 reportmre
- 72e0f587ident#26: identity page with "Used in" (app, since, last use, Notif. chip, empty state); lastLogin recorded at exchange; gates + lisbeth E2E; mission 016 reportmre