gitoria
All repositories: gitoria
8.9 KB
// lib/apps.hl — APPS CONNECTED TO A REPO (gitoria#41, part of the push API gitoria#38; contract docs/apps-api.md §0 and §1).// An app is an outside program the repo's OWNER connected to ONE repo; it acts under its own name, never as a person. Two tables:// appsTable pk @id index !keyHash, repo storage/mpackdb/apps.db// { repo (repos @id, NOT the slug: a moved repo keeps its apps), name, hookUrl, keyHash (sha256 of `gak_` + 40 hex),// secret (64 hex, signs the events — SERVER ONLY, never sent to a page or a log), createdBy (users @id), created,// lastDelivery, lastStatus (unused since gitoria#43: the status shown is the newest delivery, events.hl lastDeliveryOf) } one row per (repo, app name): a re-connect replaces key and secret// codesTable pk @id index !hash storage/mpackdb/appcodes.db { hash (sha256 of the code), repo, name, hookUrl, createdBy, created }// the one-time code of connect flow A: works once, for 5 minutes; only its sha256 is stored// The key and the secret are shown ONCE (at connect / add / rotate); only the key's sha256 is kept. The URL allow-list, the shapes: lib/apps-helpers.hl.// The routes are thin wrappers in lib/api.hl (connect, confirm, exchange); the settings page (components/settings.hl) lists, adds, removes, rotates.import { MPackDB } from 'hl:mpackdb'import { now } from 'hl:time'import { randomBytes, sha256 } from 'hl:crypto'import { storageDir, countOfList, firstOf, plainError, isHex, localStamp, newestFirst } from './util.hl'import { repoBySlug, ownsRepo } from './repos.hl'import { urlAllowed, hostOfUrl, keyShaped, apiOf } from './apps-helpers.hl'import { appsTable, appsOfRepo, appWith } from './apps-table.hl'import { raisePing, pingApp, lastDeliveryOf } from './events.hl'import { dropTokensOfApp } from './app-tokens.hl'static codesTable = new MPackDB(file = storageDir + '/appcodes.db', primaryKey = '@id', indexes = ['!hash'])static codeLifeMs = 300000static maxApps = 20// ---- the owner's list (never the key, never the secret) -----------------------------------------------------static statusText = (a) => {last = lastDeliveryOf(a.id)if (last == null) { return 'no delivery yet' }return last.text + ' · ' + localStamp(last.at)}static rowOfApp = (a) => {return { id = a.id name = a.name host = hostOfUrl(a.hookUrl) created = localStamp(a.created) createdMs = a.created status = statusText(a) }}// the apps of a repo for its owner's page, newest firststatic appRows = (repoId) => {out = []for (a of appsOfRepo(repoId)) { out.push(rowOfApp(a)) }return newestFirst(out)}static appOfName = (repoId, name) => {want = ('' + name).toLowerCase()for (a of appsOfRepo(repoId)) { if (a.name.toLowerCase() == want) { return a } }return null}// the app a key belongs to (like tokens.hl userOfToken): the record or null; anything not shaped like a key is null without a lookupstatic appOfKey = (key) => {if (!keyShaped(key)) { return null }return firstOf(appsTable.find('keyHash', sha256(key)))}// ---- checks shared by the connect flow and the manual way ------------------------------------------------------// the app's name: one line, at most 60 characters → null or the complaintstatic nameError = (name) => {n = name == null ? '' : ('' + name).trim()if (n == '') { return 'give the app a name' }return plainError(n, 60, 'the app name')}// ---- THE HOOK POINT OF gitoria#43 -------------------------------------------------------------------------------// GITORIA#43: called once after an app was connected (connect flow A at the exchange, the manual way at Add): raises the `ping` event for that app// (lib/events.hl; the sender thread delivers it, docs/apps-api.md §4). It only writes a delivery row.static pingAfterConnect = (app, repo) => {raisePing(repo, app, app.createdBy)return null}// ---- storing an app: a new key and secret, shown once -----------------------------------------------------------// (re)connect: the one row of (repo, name) gets a new key and secret (a new row when there is none) → { app, key, secret } or { error }static storeApp = (repo, name, hookUrl, userId) => {n = ('' + name).trim()key = 'gak_' + randomBytes(20, 'hex')secret = randomBytes(32, 'hex')old = appOfName(repo.id, n)let id = nullif (old != null) {appsTable.update(old.id, appWith(old, { hookUrl = hookUrl keyHash = sha256(key) secret = secret createdBy = userId }))id = old.iddropTokensOfApp(old.id)} else {if (appsOfRepo(repo.id).length >= maxApps) { return { error = 'this repository has ' + maxApps + ' apps already — remove one first' } }id = appsTable.put({ repo = repo.id name = n hookUrl = hookUrl keyHash = sha256(key) secret = secret createdBy = userId created = now() lastDelivery = 0 lastStatus = '' })}if (id == null) { return { error = 'could not store the app: ' + appsTable.lastError() } }a = appsTable.fetch(id)pingAfterConnect(a, repo)return { app = a key = key secret = secret }}// the manual way B (Settings → Apps → Add) for the owner → { rows, key, secret, name } or { error }static addApp = (userId, session, slug, name, hook) => {repo = repoBySlug(slug)if (repo == null || userId == null || !ownsRepo(slug, session)) { return { error = 'only the owner of this repository can add an app' } }bad = nameError(name)if (bad != null) { return { error = bad } }if (hook == null || !urlAllowed(('' + hook).trim())) { return { error = 'that receiver address is not allowed (https://, host worldapi.org or one of its subdomains, or a configured HyperConductor address)' } }r = storeApp(repo, name, ('' + hook).trim(), userId)if (r.error != null) { return r }return { rows = appRows(repo.id) name = r.app.name key = r.key secret = r.secret }}// the owner removes an app: the row is gone, so its key is dead at once → { rows } or { error }static removeApp = (userId, session, slug, appId) => {repo = repoBySlug(slug)if (repo == null || userId == null || !ownsRepo(slug, session)) { return { error = 'only the owner of this repository can remove an app' } }a = appId == null || hlTypeName(appId) != 'String' ? null : appsTable.fetch(appId)if (a == null || a.repo != repo.id) { return { error = 'no such app' } }appsTable.delete(a.id)dropTokensOfApp(a.id)return { rows = appRows(repo.id) }}// the owner rotates an app's secret: a new one, shown once → { rows, name, secret } or { error }static rotateSecret = (userId, session, slug, appId) => {repo = repoBySlug(slug)if (repo == null || userId == null || !ownsRepo(slug, session)) { return { error = 'only the owner of this repository can rotate a secret' } }a = appId == null || hlTypeName(appId) != 'String' ? null : appsTable.fetch(appId)if (a == null || a.repo != repo.id) { return { error = 'no such app' } }secret = randomBytes(32, 'hex')appsTable.update(a.id, appWith(a, { secret = secret }))return { rows = appRows(repo.id) name = a.name secret = secret }}// the owner clicks "Send ping" on an app: a `ping` event row for that app (the sender delivers it) → { name } or { error }static sendPing = (userId, session, slug, appId) => {repo = repoBySlug(slug)if (repo == null || userId == null || !ownsRepo(slug, session)) { return { error = 'only the owner of this repository can send a ping' } }a = appId == null || hlTypeName(appId) != 'String' ? null : appsTable.fetch(appId)if (a == null || a.repo != repo.id) { return { error = 'no such app' } }pingApp(repo, a, userId)return { name = a.name }}// ---- connect flow A ----------------------------------------------------------------------------------------------// the owner said Yes on the confirm page: a one-time code for (repo, name, hook) → the code (shown to nobody, it goes in the redirect)static makeCode = (repo, name, hookUrl, userId) => {code = randomBytes(24, 'hex')id = codesTable.put({ hash = sha256(code) repo = repo.id name = ('' + name).trim() hookUrl = hookUrl createdBy = userId created = now() })return id == null ? null : code}// the app's server trades the code for the key, secret and API address (POST /api/apps/exchange). `repo` = the repo the request's address names.// A code works once (it is deleted before anything else) and for 5 minutes, and only on the address of ITS repo → { body } (the contract's JSON) or { notFound }static exchangeCode = (repo, code) => {if (code == null || hlTypeName(code) != 'String' || !isHex(code, 48)) { return { notFound = true } }row = firstOf(codesTable.find('hash', sha256(code)))if (row == null) { return { notFound = true } }codesTable.delete(row.id)if (now() - row.created > codeLifeMs || repo == null || row.repo != repo.id) { return { notFound = true } }r = storeApp(repo, row.name, row.hookUrl, row.createdBy)if (r.error != null) { return { notFound = true } }return { body = { key = r.key secret = r.secret api = apiOf(repo.slug) repo = { id = repo.id full = repo.slug } app = { id = r.app.id name = r.app.name } } }}
Branches
- f1-broken-json
- f2-api-bugs
- gp-gate-ports
- heap-revert
- hl25-records
- ident-chooser-selector
- mainmain branch
- p51-public-pr-reads
- records-1112
- s25-no-session-on-login-error
- v59
- w52-person-approval
- w52b-approval-followup
- w52c-header-phone
- w53-pulls-speed-pr
- w54-repo-rename-pr
- w55-rename-org
- w56-owner-members
- w62-pr-close
- w63-force-push
- w64-org-slash
Latest commits
- bd94cc7elog: #25 cause found (run() leak)mre
- 565cb508log: H25 round 1mre
- 90a0af84log: H25 startedmre
- 87bd53b8mission H25mre
- 71a746cflog: day48 livemre
- bf568e14log: V48 mergedmre
- 09955dedMerge v48: Hybriel set-day48, theme layouts ce4a0bamre
- f2a2b0cevendor: Hybriel set-day48, theme layouts ce4a0bamre
- 5d0834e7log: Apollo model rulemre
- fe8e5bd5log: #25 sample, V48 missionmre
- 8580c5d9log: theme fix from Atlasmre
- 46823d93log: Apollo budget correctionmre
- f6d32341log: Apollo on budget and #25 nextmre
- d7184f53log: batch live, #25 evidencemre
- 5e25e816log: W61 mergedmre
- aafe330dTidy: drop evidence JSON (kept in scratch), collapse blank linesmre
- aca1fda1Merge W61: theme from layouts 17e0f03, Views refer to Style rules (gitoria#37 #27)mre
- 03cb650blog: W61 theme decisionmre
- 248bce85Report final style gates, CSS mappings and 136 whole-image comparisonsmre
- 153c344bRestore organization metadata exposed by the whole-image comparisonmre