gitoria

gitoria

All repositories: gitoria

ReadmeCodePull requestsReleasesTicketsSettings
Main branchmainfe209ffeMerge branch 'w56-owner-members' into mainGitoriamain/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 title = repo.title != null ? repo.title : '' } app = { id = r.app.id name = r.app.name } } }
  141. }

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