gitoria

gitoria

All repositories: gitoria

ReadmeCodePull requestsReleasesTicketsSettings
Commit21aa57ca21aa57calog: parkmre21aa57ca/lib/apps.hl

8.9 KB

  1. // 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).
  2. // 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:
  3. // appsTable pk @id index !keyHash, repo storage/mpackdb/apps.db
  4. // { repo (repos @id, NOT the slug: a moved repo keeps its apps), name, hookUrl, keyHash (sha256 of `gak_` + 40 hex),
  5. // secret (64 hex, signs the events — SERVER ONLY, never sent to a page or a log), createdBy (users @id), created,
  6. // 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
  7. // codesTable pk @id index !hash storage/mpackdb/appcodes.db { hash (sha256 of the code), repo, name, hookUrl, createdBy, created }
  8. // the one-time code of connect flow A: works once, for 5 minutes; only its sha256 is stored
  9. // 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.
  10. // The routes are thin wrappers in lib/api.hl (connect, confirm, exchange); the settings page (components/settings.hl) lists, adds, removes, rotates.
  11. import { MPackDB } from 'hl:mpackdb'
  12. import { now } from 'hl:time'
  13. import { randomBytes, sha256 } from 'hl:crypto'
  14. import { storageDir, countOfList, firstOf, plainError, isHex, localStamp, newestFirst } from './util.hl'
  15. import { repoBySlug, ownsRepo } from './repos.hl'
  16. import { urlAllowed, hostOfUrl, keyShaped, apiOf } from './apps-helpers.hl'
  17. import { appsTable, appsOfRepo, appWith } from './apps-table.hl'
  18. import { raisePing, pingApp, lastDeliveryOf } from './events.hl'
  19. import { dropTokensOfApp } from './app-tokens.hl'
  20. static codesTable = new MPackDB(file = storageDir + '/appcodes.db', primaryKey = '@id', indexes = ['!hash'])
  21. static codeLifeMs = 300000
  22. static maxApps = 20
  23. // ---- the owner's list (never the key, never the secret) -----------------------------------------------------
  24. static statusText = (a) => {
  25. last = lastDeliveryOf(a.id)
  26. if (last == null) { return 'no delivery yet' }
  27. return last.text + ' · ' + localStamp(last.at)
  28. }
  29. static rowOfApp = (a) => {
  30. return { id = a.id name = a.name host = hostOfUrl(a.hookUrl) created = localStamp(a.created) createdMs = a.created status = statusText(a) }
  31. }
  32. // the apps of a repo for its owner's page, newest first
  33. static appRows = (repoId) => {
  34. out = []
  35. for (a of appsOfRepo(repoId)) { out.push(rowOfApp(a)) }
  36. return newestFirst(out)
  37. }
  38. static appOfName = (repoId, name) => {
  39. want = ('' + name).toLowerCase()
  40. for (a of appsOfRepo(repoId)) { if (a.name.toLowerCase() == want) { return a } }
  41. return null
  42. }
  43. // the app a key belongs to (like tokens.hl userOfToken): the record or null; anything not shaped like a key is null without a lookup
  44. static appOfKey = (key) => {
  45. if (!keyShaped(key)) { return null }
  46. return firstOf(appsTable.find('keyHash', sha256(key)))
  47. }
  48. // ---- checks shared by the connect flow and the manual way ------------------------------------------------------
  49. // the app's name: one line, at most 60 characters → null or the complaint
  50. static nameError = (name) => {
  51. n = name == null ? '' : ('' + name).trim()
  52. if (n == '') { return 'give the app a name' }
  53. return plainError(n, 60, 'the app name')
  54. }
  55. // ---- THE HOOK POINT OF gitoria#43 -------------------------------------------------------------------------------
  56. // 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
  57. // (lib/events.hl; the sender thread delivers it, docs/apps-api.md §4). It only writes a delivery row.
  58. static pingAfterConnect = (app, repo) => {
  59. raisePing(repo, app, app.createdBy)
  60. return null
  61. }
  62. // ---- storing an app: a new key and secret, shown once -----------------------------------------------------------
  63. // (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 }
  64. static storeApp = (repo, name, hookUrl, userId) => {
  65. n = ('' + name).trim()
  66. key = 'gak_' + randomBytes(20, 'hex')
  67. secret = randomBytes(32, 'hex')
  68. old = appOfName(repo.id, n)
  69. let id = null
  70. if (old != null) {
  71. appsTable.update(old.id, appWith(old, { hookUrl = hookUrl keyHash = sha256(key) secret = secret createdBy = userId }))
  72. id = old.id
  73. dropTokensOfApp(old.id)
  74. } else {
  75. if (appsOfRepo(repo.id).length >= maxApps) { return { error = 'this repository has ' + maxApps + ' apps already — remove one first' } }
  76. id = appsTable.put({ repo = repo.id name = n hookUrl = hookUrl keyHash = sha256(key) secret = secret createdBy = userId created = now() lastDelivery = 0 lastStatus = '' })
  77. }
  78. if (id == null) { return { error = 'could not store the app: ' + appsTable.lastError() } }
  79. a = appsTable.fetch(id)
  80. pingAfterConnect(a, repo)
  81. return { app = a key = key secret = secret }
  82. }
  83. // the manual way B (Settings → Apps → Add) for the owner → { rows, key, secret, name } or { error }
  84. static addApp = (userId, session, slug, name, hook) => {
  85. repo = repoBySlug(slug)
  86. if (repo == null || userId == null || !ownsRepo(slug, session)) { return { error = 'only the owner of this repository can add an app' } }
  87. bad = nameError(name)
  88. if (bad != null) { return { error = bad } }
  89. 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)' } }
  90. r = storeApp(repo, name, ('' + hook).trim(), userId)
  91. if (r.error != null) { return r }
  92. return { rows = appRows(repo.id) name = r.app.name key = r.key secret = r.secret }
  93. }
  94. // the owner removes an app: the row is gone, so its key is dead at once → { rows } or { error }
  95. static removeApp = (userId, session, slug, appId) => {
  96. repo = repoBySlug(slug)
  97. if (repo == null || userId == null || !ownsRepo(slug, session)) { return { error = 'only the owner of this repository can remove an app' } }
  98. a = appId == null || hlTypeName(appId) != 'String' ? null : appsTable.fetch(appId)
  99. if (a == null || a.repo != repo.id) { return { error = 'no such app' } }
  100. appsTable.delete(a.id)
  101. dropTokensOfApp(a.id)
  102. return { rows = appRows(repo.id) }
  103. }
  104. // the owner rotates an app's secret: a new one, shown once → { rows, name, secret } or { error }
  105. static rotateSecret = (userId, session, slug, appId) => {
  106. repo = repoBySlug(slug)
  107. if (repo == null || userId == null || !ownsRepo(slug, session)) { return { error = 'only the owner of this repository can rotate a secret' } }
  108. a = appId == null || hlTypeName(appId) != 'String' ? null : appsTable.fetch(appId)
  109. if (a == null || a.repo != repo.id) { return { error = 'no such app' } }
  110. secret = randomBytes(32, 'hex')
  111. appsTable.update(a.id, appWith(a, { secret = secret }))
  112. return { rows = appRows(repo.id) name = a.name secret = secret }
  113. }
  114. // the owner clicks "Send ping" on an app: a `ping` event row for that app (the sender delivers it) → { name } or { error }
  115. static sendPing = (userId, session, slug, appId) => {
  116. repo = repoBySlug(slug)
  117. if (repo == null || userId == null || !ownsRepo(slug, session)) { return { error = 'only the owner of this repository can send a ping' } }
  118. a = appId == null || hlTypeName(appId) != 'String' ? null : appsTable.fetch(appId)
  119. if (a == null || a.repo != repo.id) { return { error = 'no such app' } }
  120. pingApp(repo, a, userId)
  121. return { name = a.name }
  122. }
  123. // ---- connect flow A ----------------------------------------------------------------------------------------------
  124. // 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)
  125. static makeCode = (repo, name, hookUrl, userId) => {
  126. code = randomBytes(24, 'hex')
  127. id = codesTable.put({ hash = sha256(code) repo = repo.id name = ('' + name).trim() hookUrl = hookUrl createdBy = userId created = now() })
  128. return id == null ? null : code
  129. }
  130. // 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.
  131. // 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 }
  132. static exchangeCode = (repo, code) => {
  133. if (code == null || hlTypeName(code) != 'String' || !isHex(code, 48)) { return { notFound = true } }
  134. row = firstOf(codesTable.find('hash', sha256(code)))
  135. if (row == null) { return { notFound = true } }
  136. codesTable.delete(row.id)
  137. if (now() - row.created > codeLifeMs || repo == null || row.repo != repo.id) { return { notFound = true } }
  138. r = storeApp(repo, row.name, row.hookUrl, row.createdBy)
  139. if (r.error != null) { return { notFound = true } }
  140. 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 } } }
  141. }

Branches

Latest commits

  • 21aa57calog: parkmre
  • e9f950a8reports: G25c soakmre
  • a7783686log: deploy 9f41ffcmre
  • 9f41ffc1log: W30b mergedmre
  • bef635c1Merge branch 'w30b'mre
  • 15b996a5Org list: keyed rows by @id, newest first, concat instead of hand loopmre
  • c4f6d9e3reports: G25c growth huntmre
  • b88e39f1log: privacy cleanup donemre
  • bfd7826ddocs: neutral example organisation namemre
  • 01b71c2edocs: neutral wording in missions, reports and logmre
  • 3eeea733log: 2026-11-09 PR flow plan and membersmre
  • e7c2ff1fdocs: neutral name for the apps-api reviewmre
  • 120b7abdlog: step 3 queuedmre
  • 358474bflog: P50 livemre
  • 477d7bdcMerge branch 'p50'mre
  • 00efc06fP50 reportmre
  • e3f85afblog: commit mention slipmre
  • 4d1a4cf4log: #25 numbersmre
  • 1af0a60ddocs: H25 run() correctionmre
  • eefa2dbcRepo setting: refuse direct pushes to the main branch (pre-receive over HTTPS and ssh)mre