gitoria
All repositories: gitoria
8.6 KB
// users.hl — WHO MAY CREATE (ticket gitoria#6; CONCEPT.md "login via ident"). Reading is public;// creating a repo needs a login through ident. Login is ident's LOGIN BUTTON flow (ident README// "How apps use ident", way 2): <ident>/login?key=&return=<public url>/login/callback → ?ident_code=// → the server exchanges it (key + secret) for the per-app identity id. Way 3, the IDENTITY SELECTOR// (gitoria#14, ident README "The identity selector"), sits beside the button; ident checks its caller's origin// against the app's registered origins, and a repo address (<slug>.<domain>) is not one — so the selector shows// on the main address only (login.js) and the button works from every address (it returns through the main one).//// usersTable pk @id index !identity { identity, name, created } storage/mpackdb/users.db// identity = what ident's exchange answers: the identity's public SHORT id since ident#23 (`a68sz`; old 32-hex per-app// ids are rewritten once by tools/migrate-short-ids.hl) — stays SERVER SIDE, never sent to a page.// name = the display name asked once at the first login ('' until chosen); it is what// a repo shows as its owner.// The session (hl:web) carries `user = { id = <users @id> }` only.//// Config (environment, or `.env` beside project.hl — never read or printed by workers):// IDENT_URL, IDENT_EXCHANGE_URL, IDENT_API_KEY, IDENT_API_SECRET as in tickets// GITORIA_PUBLIC_URL the main address, default https://gitoria.worldapi.org; repos live at// <scheme>://<slug>.<host[:port] of this url>// GITORIA_STORAGE table directory, default ./storage/mpackdbimport { MPackDB } from 'hl:mpackdb'import { env } from 'hl:proc'import { now } from 'hl:time'import { fetch } from 'hl:fetch'static envOr = (name, fallback) => {let v = env(name)return v != null && v.trim() != '' ? v.trim() : fallback}static identUrl = envOr('IDENT_URL', 'https://ident.worldapi.org')static identExchangeUrl = envOr('IDENT_EXCHANGE_URL', identUrl)static identKey = envOr('IDENT_API_KEY', '')static identSecret = envOr('IDENT_API_SECRET', '')static publicUrl = envOr('GITORIA_PUBLIC_URL', 'https://gitoria.worldapi.org')static storageDir = envOr('GITORIA_STORAGE', './storage/mpackdb')static usersTable = new MPackDB(file = storageDir + '/users.db', primaryKey = '@id', indexes = ['!identity'])static countOfList = (list) => {if (list == null) { return 0 }let n = list.lengthreturn n == null ? 0 : n}static firstOf = (list) => { return countOfList(list) > 0 ? list[0] : null }// ---- the addresses: main = <scheme>://<hostport>, a repo = <scheme>://<slug>.<hostport> ---------static schemeEnd = publicUrl.indexOf('://')static scheme = publicUrl.slice(0, schemeEnd)static hostPort = publicUrl.slice(schemeEnd + 3).replaceAll('/', '')static hostOnly = hostPort.split(':')[0]static repoOrigin = (slug) => { return scheme + '://' + slug + '.' + hostPort }static selectorScript = identUrl + '/selector.js'static callbackUrl = publicUrl.replaceAll('/', '') == '' ? '' : scheme + '://' + hostPort + '/login/callback'static loginHref = identUrl + '/login?key=' + identKey + '&return=' + encodeURIComponent(callbackUrl)// only lowercase hex (ident's one-time codes are 48 hex)static isHex = (s, max) => {if (s == null || hlTypeName(s) != 'String' || s.length == 0 || s.length > max) { return false }let i = 0while (i < s.length) {let c = s.charCodeAt(i)if (!((c >= 48 && c <= 57) || (c >= 97 && c <= 102))) { return false }i = i + 1}return true}// an identity id as ident answers it: its public SHORT ID since ident#23 (5 characters like `a68sz`: 2-9 and a-z),// before that the old per-app id (32 hex) — lower case letters and digits, at most 64 (mission 039; isHex refused `a68sz`)static isIdentId = (s) => {if (s == null || hlTypeName(s) != 'String' || s.length == 0 || s.length > 64) { return false }let i = 0while (i < s.length) {let c = s.charCodeAt(i)if (!((c >= 48 && c <= 57) || (c >= 97 && c <= 122))) { return false }i = i + 1}return true}// THE EXCHANGE: POST <ident>/api/exchange { key, secret, code } → { identity } | { error }// (a failed fetch is an `Error` event, absorbed by project.hl's `on Error`; the fetch then yields null)static exchangeCode = (code) => {if (identKey == '' || identSecret == '') { return { error = 'login is not set up on this server (IDENT_API_KEY / IDENT_API_SECRET missing)' } }if (!isHex(code, 200)) { return { error = 'that is not an ident login code' } }let r = fetch(identExchangeUrl + '/api/exchange', { method = 'POST' json = { key = identKey secret = identSecret code = code } headers = { 'user-agent' = 'gitoria.worldapi.org (ident exchange)' } timeoutMs = 10000 })if (r == null || r.status == null || r.status == 0) { return { error = 'ident did not answer' } }let j = r.status == 200 ? r.json() : nullif (j == null || j.identity == null || !isIdentId(j.identity)) {let why = ''if (r.status != 200) {let e = r.json()why = e != null && e.error != null ? ': ' + e.error : ''}return { error = 'ident refused the login (' + r.status + why + ')' }}return { identity = j.identity }}// the session cookie's Domain: every <slug>.<host> shares the login. A host without a dot (localhost) or// an IP address gets a host-only cookie. GITORIA_COOKIE_DOMAIN overrides ('-' = host-only).static domainFor = (h) => {let given = env('GITORIA_COOKIE_DOMAIN')if (given != null && given != '') { return given == '-' ? '' : given }if (!h.includes('.')) { return '' }let last = h.charCodeAt(h.length - 1)if (last >= 48 && last <= 57) { return '' }return '.' + h}// THE REPO OF A REQUEST HOST (gitoria#16): hl:web hands a page component that declares `host = null` the// request's Host without port (hybriel#74). '' = the main address (or any other host) → the repo list;// else the label before .<hostOnly> (a slug that fails slugError, e.g. x.y, is "No such repository").static slugOfHost = (h) => {if (h == null || hlTypeName(h) != 'String') { return '' }let x = h.toLowerCase()if (x == hostOnly) { return '' }let tail = '.' + hostOnlyreturn x.endsWith(tail) ? x.slice(0, x.length - tail.length) : ''}// ---- users --------------------------------------------------------------------------------// a face's trailing `session` is always the server's since hybriel #16 (a peer's extra argument is refused)static userRecord = (userId) => {if (userId == null || hlTypeName(userId) != 'String' || userId == '') { return null }return usersTable.fetch(userId)}// the user of an identity id, made at its first login (name '' = not chosen yet)static ensureUser = (identity) => {let u = firstOf(usersTable.find('identity', identity))if (u != null) { return u }let id = usersTable.put({ identity = identity name = '' created = now() })if (id == null) { return null }return usersTable.fetch(id)}// what a page may know about a user: NEVER the identity idstatic userInfo = (u) => { return { name = u.name named = u.name != '' } }static userOfSession = (session) => {if (session == null || session.user == null) { return null }return userRecord(session.user.id)}static infoOfSession = (session) => {let u = userOfSession(session)return u == null ? null : userInfo(u)}// the display name of an owner, 'someone' for a user without onestatic nameOfUser = (userId) => {let u = userRecord(userId)if (u == null || u.name == '') { return 'someone' }return u.name}// a short one-line text: trimmed, at most `max` characters, no control characters → null or the complaintstatic plainError = (s, max, what) => {if (s == null || hlTypeName(s) != 'String') { return what + ' must be text' }let t = s.trim()if (t.length > max) { return what + ' is too long (at most ' + max + ' characters)' }let i = 0while (i < t.length) {let c = t.charCodeAt(i)if (c < 32 || c == 127) { return what + ' must be one line without control characters' }i = i + 1}return null}// the display name: 1–60 characters, one line, asked ONCEstatic setUserName = (userId, name) => {let u = userRecord(userId)if (u == null) { return { error = 'not logged in' } }if (u.name != '') { return { error = 'your display name is already set' } }let bad = plainError(name, 60, 'the display name')if (bad != null) { return { error = bad } }let n = name.trim()if (n == '') { return { error = 'the display name must not be empty' } }usersTable.update(u.id, { id = u.id identity = u.identity name = n created = u.created })let after = usersTable.fetch(u.id)if (after == null || after.name != n) { return { error = 'could not store the name: ' + usersTable.lastError() } }return { user = after }}
Branches
- mainmain branch
Latest commits
- 205d5fe4gitoria: Hybriel master ff51cf46; ssh keys/tokens no double rows (session sync); gates follow #20mre
- 9b27cb26gitoria#21: installable app (manifest, service worker, offline start page), own iconmre
- 68dcb603deploy.sh: back up live storage/.sessions/.env before every deploy (newest 5 kept)mre
- e2deed6dgitoria#20: "Add code" only on the Code page of an empty repository, no collapsiblemre
- 8bb97ffddeploy.sh: never send .git or .gitignore to Byrodinmre
- fd981932State of 2026-09-27; bin/ no longer tracked (Hybriel commit is in README)mre
- 4a2d7125initial commitmre