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 title = repo.title != null ? repo.title : '' } app = { id = r.app.id name = r.app.name } } }}
Branches
Latest commits
- fe209ffeMerge branch 'w56-owner-members' into mainGitoria
- fa0754c8|||PR repository owner transfer and members API (#55)mre
- 8862e537Merge branch 'w55-rename-org' into mainGitoria
- 014d2b7fOne immutable readers snapshot per move for the left-organization event (#53)mre
- 1859d506Decide who hears a repo left its organization by its readers before the move (#53)mre
- 499f25f2Review fixes for the Settings organization move (#53)mre
- ed3b922b|||PR move a repository into an organization in Settings (#53)mre
- 74ec2af8Merge branch 'w54-repo-rename-pr' into mainGitoria
- 9f7ce8d8Preserve rename compatibility and live page titlesmre
- c3d95b6e|||PR separate names and addresses, rename repositories in Settings (#53)mre
- ce08920dMerge branch 'w52c-header-phone' into mainGitoria
- 0bab9e46|||PR header: ident status wraps on phones instead of clippingmre
- e7af3eceMerge branch 'w52b-approval-followup' into mainGitoria
- c01e66c6|||PR approvals follow-up: live Merge after status writes, helpers (#52)mre
- 841da497Merge branch 'w53-pulls-speed-pr' into mainGitoria
- 80307ae2|||PR pulls page cache: live PR numbers, complete scans onlymre
- 69141f65|||PR pulls page: cache the git part per repo by branch tipsmre
- 155861f6Merge branch 'w52-person-approval' into mainGitoria
- b5435de8|||PR person approvals: second Kunibert round (#52)mre
- 3a8b2fb8|||PR person approvals: Kunibert review fixes (#52)mre