gitoria

gitoria

All repositories: gitoria

ReadmeCodePull requestsReleasesTicketsSettings
Commitafdb0cbfafdb0cbfgitoria#43: rename the moved helper (name clash in the browser build)mreafdb0cbf/lib/apps.hl

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

Branches

Latest commits

  • afdb0cbfgitoria#43: rename the moved helper (name clash in the browser build)mre
  • 15e01e94gitoria#43: tests/events.mjs and its probemre
  • 77829b73gitoria#43: signed events to connected apps (deliveries table, sender timer, pull_request events, ping)mre
  • 1997e77dgitoria#40: stable PR numbers, refs/pull/N/head, ssh pushes reach the app (merge w40-pr-number)mre
  • 9364fb16gitoria#40: ssh pushes call /__git/pushed before and after (ensurePulls, notifyPush, syncPulls); gate over the sshd imagemre
  • fdb9ed28gitoria#41: repo owners can connect an app (merge w41-apps-connect)mre
  • 2ef23df0gitoria#40: the browser gate reads the title without the #Nmre
  • 9af07ecegitoria#41: connect an app to a repo (apps table, connect flow, manual add, allow-list, settings)mre
  • b9137ff0gitoria#40: pull requests are stored with a number, refs/pull/N/head, syncPulls and pullObject; /pulls shows #Nmre
  • 06d622daown image tag gitoria-runtime:debian12 for gitoria and standby (shared tag lost git)mre
  • 20caa33agitoria#37: media queries in the language's @media block (minWidth = '60rem') in list, codebrowser and repomre
  • c4175e6dgitoria: a startup error prints its message before exit 1 (hybriel#247)mre
  • c50d17efKeep function-route not-found style in shared stylesheetmre
  • e42d78d3Preserve ticket state CSS and document function-route style gapmre
  • c6b7154eWIP gitoria#26: move styles into nested component blocksmre
  • 77a2c31cgitoria#31: plain declarations instead of needless let in lib/ and tools/ (103 let removed, 137 kept because reassigned); no other changemre
  • 4b98ab62gitoria: an ident access key (idt_) works as the git password; read key clones only, bad key 401, ident down 503 (ident#27); gate tests/ident-access-gitoria.mjs in deploy.shmre
  • 1185f686gitoria#36: a failed return from tickets shows a note on the settings page and leaves one log line per outcome; no code is exchangedmre
  • 5d2c385agitoria: the sign-in callback keeps the session it finds and signs it in (same cookie, nothing signed out; ident#28); security gate 71+5 failing → 76 passed; deploy.sh runs tests/private.mjs toomre
  • 241ef3f9deploy.sh: rebuild the sshd image before up -d (GO34c)mre