ident
All repositories: gitoria
4.6 KB
// lib/util.hl — SMALL SHARED HELPERS of ident (mission 009, code order): the storage directory and env numbers,// lists and records, ids, text checks, the email shape, oldest-first order. Each one exists once; the topic files// import what they use. Statics only.import { env } from 'hl:proc'import { isZone } from 'hl:time'// IDENT_STORAGE names another table DIRECTORY (the gates run on their own). Relative// paths resolve against the ENTRY SCRIPT's directory — tools must pass an absolute one.static dir = env('IDENT_STORAGE') != null ? env('IDENT_STORAGE') : './storage/mpackdb'static envNumber = (name, fallback) => {v = env(name)if (v == null || v == '') { return fallback }n = toNumber(v)return n == null ? fallback : n}// an empty list written to hl:mpackdb comes back as an empty hybrid (`.length` null)static countOf = (list) => {if (list == null) { return 0 }n = list.lengthreturn n == null ? 0 : n}static first = (list) => { return countOf(list) > 0 ? list[0] : null }static str = (v) => { return v == null ? '' : v }static isList = (v) => { return v != null && hlTypeName(v) == 'Hybrid' && v.length != null }static isObject = (v) => { return v != null && hlTypeName(v) == 'Hybrid' && v.length == null }// A PUBLIC ID is an mpackdb UUID: a non-empty string (a number is never one — an old// numeric id from before mission 009 finds nothing)static isId = (v) => { return v != null && hlTypeName(v) == 'String' && v.length > 0 && v.length <= 64 }// every record is written as a whole (hl:mpackdb's update replaces it)static merged = (rec, changes) => {out = {}for (k of rec.keys()) { out[k] = rec[k] }for (k of changes.keys()) { out[k] = changes[k] }return out}// OLDEST FIRST: by the stored `created`, a tie by the id (never by key order)static olderThan = (a, b) => {ca = a.created == null ? 0 : a.createdcb = b.created == null ? 0 : b.createdif (ca != cb) { return ca < cb }return a.id < b.id // strings order by code point (hybriel#2)}// a found list, oldest first (insertion sort by hand: no list sort(), hybriel #6)static oldestFirst = (list) => {out = []if (countOf(list) == 0) { return out }for (x of list) { out.push(x) }let i = 1while (i < out.length) {let cur = out[i]let j = i - 1while (j >= 0 && olderThan(cur, out[j])) {out[j + 1] = out[j]j = j - 1}out[j + 1] = curi = i + 1}return out}// ---- text checks ------------------------------------------------------------------------// no control characters (a field is one line of text)static hasControl = (s) => {let i = 0while (i < s.length) {let c = s.charCodeAt(i)if (c < 32 || c == 127) { return true }i = i + 1}return false}// the characters a URL may hold (a return URL, a notification's icon / link)static urlChars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-._~:/?&=%+,;@!$()*[]'// AN EMAIL ADDRESS: lowercased and trimmed, then a plain whitelist shapestatic normEmail = (email) => { return email == null ? '' : ('' + email).trim().toLowerCase() }static validEmail = (email) => {if (email == null || email.length < 3 || email.length > 200) { return false }at = email.indexOf('@')if (at < 1 || at != email.lastIndexOf('@') || at == email.length - 1) { return false }if (!email.slice(at + 1, email.length).includes('.')) { return false }// a whitelist: the language has no escapes to name a CR or a tab withok = 'abcdefghijklmnopqrstuvwxyz0123456789.-_+@'let i = 0while (i < email.length) {if (!ok.includes(email[i])) { return false }i = i + 1}return true}// A TIME ZONE is an IANA name as the browser reports it (`Europe/Vienna`, `UTC`,// `America/Argentina/Buenos_Aires`, `Etc/GMT+5`). The server checks the shape, then asks hl:time's zone database// (hybriel#5, `isZone`: the system's tz files); the page checks the name against the browser's own list// (Intl.supportedValuesOf) before it sends it.static zoneChars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789/_+-'static checkTimeZone = (tz) => {if (tz == null || hlTypeName(tz) != 'String') { return { error = 'the time zone must be a string' } }t = tz.trim()if (t == '') { return { error = 'the time zone is empty' } }if (t.length > 64) { return { error = 'the time zone name is too long' } }let i = 0while (i < t.length) {if (!zoneChars.includes(t[i])) { return { error = 'that is not a time zone name (like Europe/Vienna)' } }i = i + 1}if (t[0] == '/' || t[t.length - 1] == '/' || t.includes('//')) { return { error = 'that is not a time zone name (like Europe/Vienna)' } }if (!isZone(t)) { return { error = 'unknown time zone: ' + t } }return { timeZone = t }}
Branches
Latest commits
- dc6d486fident#27: the docs gate expects the new Access entry in the main menumre
- d277633cident#27: README: identity access and the introspect endpointmre
- e999b0d3ident#27: the Access tab — one key per identity for all its apps: give access in three steps, the overview matrix, a cell's sheet (since, last use, level, rotate, revoke), the app's check POST /api/introspect, agent identities hidden from the login picker, an inbox icon in the header's icon style (gate: tests/access.mjs)mre
- 436206fcident#47: deploy.sh runs the sign-in gate on the deploy's own port slot, like the sessionwrites and docs gates (the default 8700 can be taken by another process)mre
- a4887ea3ident#47: the selector's sign-in frame no longer has a Continue button; the identity row itself is the link (gate tests/signin.mjs fails on the old code)mre
- d8ec703dident#45: /docs shows docs/IDENT.md (hl:markdown, vendored from ac1ed9da), one Docs entry in the main menu, README for developers only, gates tests/docs.mjs and tests/sessionwrites.mjs in deploy.shmre
- ec6aad35Merge branch 'a28g-amp-callsites' into d45-docsmre
- 6bfbd3f2ident#45: docs/IDENT.md, what ident is and how it works; CONCEPT.md marks one id per app as droppedmre
- c0edca26ident#28 A28G: write & at every call that hands a session to an &-parameter; gate tests/sessionwrites.mjs pins that sign-in, pending address, embed sign-in and selector land in the session filesmre
- 80965c08ident#28: README says the deploy goes through the standbymre
- 2f41670cident#28: ident-standby service on port 45012 (no background round in the standby) and deploy.sh hands over through standby-swap.shmre
- a544b261ident#28: README names the new vendored Hybrielmre
- 716860b0ident#28: gates follow Hybriel ac1ed9da (lazy sessions, meta attribute order, JSON key order, default X-Frame-Options switched off for the embed page)mre
- 889e00fbident#28: Hybriel master ac1ed9da (sessions reload a newer file, a replayed reconnect carries the server session); a startup fault exits 1 (on Error)mre
- 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