gitoria

tickets

All repositories: gitoria

ReadmeCodePull requestsReleasesTicketsSettings
Commit7ed2e5e57ed2e5e5deploy.sh: a gate without a 'passed,' line (check-theme) no longer ends the script silently (grep under pipefail), as gitoria/layoutsmre7ed2e5e5/lib/connections.hl

10.8 KB

  1. // lib/connections.hl — AN APP IS CONNECTED TO ONE PROJECT (ticket tickets.worldapi.org#21; first app: gitoria).
  2. // Statics only. One hl:mpackdb table beside the others (storage/mpackdb/connections.db, UUID keys):
  3. //
  4. // connectionsTable pk @id index nonce, hash, codeHash, project
  5. // { status, nonce, app, label, returnUrl, state, project, createdBy, codeHash, expires, hash, created }
  6. //
  7. // THE FLOW (README "Connecting an app"):
  8. // 1. the app sends the browser to <tickets>/connect?app=gitoria&label=<owner/repo>&return=<url>&state=<opaque>
  9. // → a REQUEST (status 'request', an unguessable nonce) and a redirect to /connect/<nonce> (components/connect.hl).
  10. // 2. the person logs in (ident), picks a project they are ADMIN of — or makes a new one — and confirms.
  11. // The request becomes a connection (status 'waiting') with a ONE-TIME CODE (5 minutes); the page links back to
  12. // `return?code=<code>&state=<state>`.
  13. // 3. the app exchanges the code on the server: POST /api/connect/exchange { code } → { key, project }.
  14. // The connection is 'active'; only the sha256 of the key is stored (`tktc_` + 48 hex, shown once).
  15. // 4. the key acts inside THAT ONE project only (lib/api-helpers.hl apiAuth / actorIn): create tickets, comment, change state.
  16. // Every write names the person: header `X-Tickets-Identity: <ident public id>`; the person is a tickets user
  17. // (they logged in here once and chose a name) and the project's ROLES apply to them as to a token.
  18. // 5. the project page shows "connected to <app>: <label>"; an admin's "Disconnect" deletes the row — the key is dead at once.
  19. // The return URL is the app's: only https on worldapi.org (and its subdomains) — or an origin listed in
  20. // TICKETS_CONNECT_ORIGINS (comma separated, e.g. http://127.0.0.1:8700 for a dev copy). The page shows the host.
  21. import { MPackDB } from 'hl:mpackdb'
  22. import { now } from 'hl:time'
  23. import { randomBytes, sha256 } from 'hl:crypto'
  24. import { env } from 'hl:proc'
  25. import { storageDir, first, countOf, isHex, plainError } from './util.hl'
  26. import { isIdentId, userOfIdentity, nameOfUser, publicUrl } from './users.hl'
  27. import { projectRecord, projectRecords, isAdminOf, createProject, userOk, notAdmin } from './projects.hl'
  28. static connectionsTable = new MPackDB(file = storageDir + '/connections.db', primaryKey = '@id', indexes = ['nonce', 'hash', 'codeHash', 'project'])
  29. static requestTtlMs = 3600000
  30. static codeTtlMs = 300000
  31. static extraOrigins = () => {
  32. v = env('TICKETS_CONNECT_ORIGINS')
  33. out = []
  34. if (v == null || v.trim() == '') { return out }
  35. for (o of v.split(',')) { if (o.trim() != '') { out.push(o.trim()) } }
  36. return out
  37. }
  38. // the characters a return URL may hold: URL-safe, no spaces, no quotes, no '#' (the code goes in the query)
  39. static urlChars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-._~/?&=%+,;:@!$()*'
  40. // the host (with port) of an absolute URL: everything between '://' and the next '/' or '?'
  41. static authorityOf = (url) => {
  42. at = url.indexOf('://')
  43. if (at < 0) { return '' }
  44. rest = url.slice(at + 3)
  45. let end = rest.length
  46. s = rest.indexOf('/')
  47. if (s >= 0 && s < end) { end = s }
  48. q = rest.indexOf('?')
  49. if (q >= 0 && q < end) { end = q }
  50. return rest.slice(0, end)
  51. }
  52. // null when the URL may be an app's return address, else the reason
  53. static returnError = (url) => {
  54. if (url == null || hlTypeName(url) != 'String' || url == '') { return 'the app sent no return address' }
  55. if (url.length > 500) { return 'the return address is too long' }
  56. let i = 0
  57. while (i < url.length) {
  58. if (!urlChars.includes(url[i])) { return 'the return address holds a character that is not allowed' }
  59. i = i + 1
  60. }
  61. for (o of extraOrigins()) {
  62. if (url == o || url.startsWith(o + '/') || url.startsWith(o + '?')) { return null }
  63. }
  64. if (!url.startsWith('https://')) { return 'the return address must be https on worldapi.org' }
  65. host = authorityOf(url)
  66. if (host == '' || host.includes('@') || host.includes(':')) { return 'the return address has an unusual host' }
  67. if (host != 'worldapi.org' && !host.endsWith('.worldapi.org')) { return 'the return address must be on worldapi.org' }
  68. return null
  69. }
  70. static hostOf = (url) => { return authorityOf(url) }
  71. static sepOf = (url) => { return url.includes('?') ? '&' : '?' }
  72. // ---- 1. the request ---------------------------------------------------------------------------
  73. // answers { nonce } or { error }
  74. static createRequest = (app, label, returnUrl, state) => {
  75. a = app == null ? '' : ('' + app).trim()
  76. if (a == '') { return { error = 'the app sent no name' } }
  77. let bad = plainError(a, 60, 'the app name')
  78. if (bad == null) { bad = plainError(label == null ? '' : label, 100, 'the label') }
  79. if (bad == null) { bad = plainError(state == null ? '' : state, 200, 'the state') }
  80. if (bad == null) { bad = returnError(returnUrl) }
  81. if (bad != null) { return { error = bad } }
  82. let nonce = randomBytes(16)
  83. id = connectionsTable.put({ status = 'request' nonce = nonce app = a label = label == null ? '' : label.trim() returnUrl = returnUrl state = state == null ? '' : state project = '' createdBy = '' codeHash = '' hash = '' expires = now() + requestTtlMs created = now() })
  84. if (id == null) { return { error = 'could not store the request: ' + connectionsTable.lastError() } }
  85. return { nonce = nonce }
  86. }
  87. // the open request of a nonce, or null (unknown, used, expired)
  88. static requestOf = (nonce) => {
  89. if (!isHex(nonce, 64)) { return null }
  90. c = first(connectionsTable.find('nonce', nonce))
  91. if (c == null || c.status != 'request' || c.expires < now()) { return null }
  92. return c
  93. }
  94. // what the connect page shows of a request
  95. static requestView = (c) => {
  96. return { app = c.app label = c.label hasLabel = c.label != '' host = hostOf(c.returnUrl) }
  97. }
  98. // the projects a user is an ADMIN of: [{ id, title, slug }]
  99. static adminProjects = (user) => {
  100. out = []
  101. if (user == null) { return out }
  102. for (p of projectRecords()) { if (isAdminOf(p.id, user)) { out.push({ id = p.id title = p.title slug = p.slug }) } }
  103. return out
  104. }
  105. // ---- 2. the confirmation ----------------------------------------------------------------------
  106. // `projectId` '' = make a new project titled `title` (the person becomes its admin). Answers { error, forbidden? } or
  107. // { url (back to the app, with the one-time code), project (title), slug }
  108. static confirmRequest = (nonce, user, projectId, title) => {
  109. c = requestOf(nonce)
  110. if (c == null) { return { error = 'this request is used up or expired — start again from the app' } }
  111. if (!userOk(user)) { return { error = 'log in with ident and choose a display name first' } }
  112. let p = null
  113. if (projectId == null || projectId == '') {
  114. r = createProject(user, title, '', '')
  115. if (r.error != null) { return { error = r.error } }
  116. p = projectRecord(r.project.id)
  117. } else {
  118. p = projectRecord('' + projectId)
  119. if (p == null) { return { error = 'no such project' } }
  120. if (!isAdminOf(p.id, user)) { return notAdmin }
  121. }
  122. // a second connection of the same app and label to the same project replaces the first: its key dies
  123. for (o of connectionsOf(p.id, true)) { if (o.app == c.app && o.label == c.label && o.id != c.id) { connectionsTable.delete(o.id) } }
  124. code = randomBytes(24)
  125. c.status = 'waiting'
  126. c.nonce = ''
  127. c.project = p.id
  128. c.createdBy = user.id
  129. c.codeHash = sha256(code)
  130. c.expires = now() + codeTtlMs
  131. connectionsTable.update(c.id, c)
  132. let url = c.returnUrl + sepOf(c.returnUrl) + 'code=' + code
  133. if (c.state != '') { url = url + '&state=' + encodeURIComponent(c.state) }
  134. return { url = url project = p.title slug = p.slug }
  135. }
  136. // ---- 3. the exchange --------------------------------------------------------------------------
  137. // answers { key, project (slug), title, api } or { error }
  138. static exchangeConnectCode = (code) => {
  139. if (!isHex(code, 200)) { return { error = 'that is not a connection code' } }
  140. c = first(connectionsTable.find('codeHash', sha256(code)))
  141. if (c == null || c.status != 'waiting' || c.expires < now()) { return { error = 'unknown, used or expired code — connect again' } }
  142. p = projectRecord(c.project)
  143. if (p == null) { return { error = 'the project is gone' } }
  144. let key = 'tktc_' + randomBytes(24)
  145. c.status = 'active'
  146. c.hash = sha256(key)
  147. c.codeHash = ''
  148. c.expires = 0
  149. c.connected = now()
  150. connectionsTable.update(c.id, c)
  151. return { key = key project = p.slug title = p.title api = publicUrl + '/api/projects/' + p.slug }
  152. }
  153. // ---- 4. the key -------------------------------------------------------------------------------
  154. static isKeyHeader = (header) => {
  155. if (header == null || hlTypeName(header) != 'String') { return false }
  156. h = header.trim()
  157. return (h.startsWith('Bearer ') || h.startsWith('bearer ')) && h.slice(7).trim().startsWith('tktc_')
  158. }
  159. // `Authorization: Bearer tktc_…` → the ACTIVE connection, or null
  160. static connectionOfKey = (header) => {
  161. if (!isKeyHeader(header)) { return null }
  162. key = header.trim().slice(7).trim()
  163. if (key.length != 53) { return null }
  164. c = first(connectionsTable.find('hash', sha256(key)))
  165. if (c == null || c.status != 'active') { return null }
  166. return c
  167. }
  168. // the person an app write names (`X-Tickets-Identity`): { user } or { error }
  169. static personOf = (identity) => {
  170. if (identity == null || hlTypeName(identity) != 'String' || !isIdentId(identity.trim())) {
  171. return { error = 'a write through a connection names the person: header X-Tickets-Identity: <their ident id>' }
  172. }
  173. u = userOfIdentity(identity.trim())
  174. if (u == null || u.name == '') { return { error = 'that person has not logged in to tickets yet — they log in once and choose a display name' } }
  175. return { user = u }
  176. }
  177. // ---- 5. the project page ----------------------------------------------------------------------
  178. // the connections of a project: [{ id, app, label, by, when }]; `all` = also the waiting ones
  179. static connectionsOf = (projectId, all) => {
  180. out = []
  181. rows = connectionsTable.find('project', projectId)
  182. if (countOf(rows) == 0) { return out }
  183. for (c of rows) { if (all || c.status == 'active') { out.push(c) } }
  184. return out
  185. }
  186. static connectionRows = (projectId) => {
  187. out = []
  188. for (c of connectionsOf(projectId, false)) {
  189. out.push({ id = c.id app = c.app label = c.label hasLabel = c.label != '' by = nameOfUser(c.createdBy) text = c.app + (c.label != '' ? ': ' + c.label : '') })
  190. }
  191. return out
  192. }
  193. // an admin ends a connection; the key is dead at once. answers { connections } or { error, forbidden? }
  194. static disconnect = (projectId, actor, connectionId) => {
  195. p = projectRecord(projectId)
  196. if (p == null) { return { error = 'no such project' } }
  197. if (!userOk(actor)) { return { error = 'log in with ident and choose a display name first' } }
  198. if (!isAdminOf(p.id, actor)) { return notAdmin }
  199. if (connectionId == null || hlTypeName(connectionId) != 'String' || connectionId == '') { return { error = 'no such connection' } }
  200. c = connectionsTable.fetch(connectionId)
  201. if (c == null || c.project != p.id) { return { error = 'no such connection' } }
  202. connectionsTable.delete(c.id)
  203. return { connections = connectionRows(p.id) }
  204. }

Branches

Latest commits

  • 7ed2e5e5deploy.sh: a gate without a 'passed,' line (check-theme) no longer ends the script silently (grep under pipefail), as gitoria/layoutsmre
  • bf9064a3mission 022: tickets on the layouts theme (step 1): shell, styles, gates, audit, E2E; reportmre
  • f3d61711mission 022 WIP: tickets on the layouts theme (dbe3de7): AppHeader/MainMenu/UserMenu/Sidebar/ContentFirst shell, styles reduced to the app's own, check-theme in the gates, 390 audit + real-Chrome walk scriptmre
  • 31cabfccmission 021: E2E script, report, LOG, STATUSmre
  • 6aea8683editor look: .empty rule only for p.empty, files off where tickets has no upload, state note one line, events test scrolls the editor into view; synced md-editor (components d702d76+6f27ad3); gate 377/0, events 52/0mre
  • dce6ffa6Merge branch 'w28-deflake'mre
  • 536092a1tickets (mission 019): de-flake the m016 comment clicks (B: unliked) — calmClick waits until the button stops moving; test only, no app changemre
  • 9c5fd42amission 020: attachments E2E proof (lisbeth Chrome, real-data copy), reportmre
  • 1206404fREADME attachments, report 018, LOG/STATUSmre
  • 3d9e7f3ctests: m017 gate fixes (list count, waits before delete clicks); gate 373/0mre
  • dd41d5bcre-sync components 15be2ac; mission 018mre
  • 420ad1bbtickets (mission 017 WIP, tickets#27): comment attachments — lib/attachments.hl (storage/attachments, limits, type by content, members-only GET /api/attachments/:id, delete drops, edit keeps), multipart POST …/comments (lib/multipart.hl), Respond block in FilesUpload (blocks verbatim from components w27c 1f8d79d), gate m017 checks written (not yet run); report, LOG, STATUSmre
  • a7a1380dmission brief 017mre
  • f83d9c0dtickets (mission 016): report reports/016-comments-block.md, LOG, STATUS, README gate countmre
  • eef80f55tickets (mission 016): comments through the shared Comments block (layouts#2) — edit (author, earlier versions kept), delete (author, asks; leaves one line in the history), like (members, toggle) on the server (lib/comments.hl, append-only tables), faces + API …/comments/:comment/{edit,delete,like}, push commentChanged; every run of comments is a block in place in the history; Markdown fields on the Hybriel MdEditor (shared/md-editor.js retired: it took over the block's <md-editor>); blocks copied verbatim from components.hybriel 9db927d (tools/sync-components.sh); gates browser 351/0 (old 326/0), events 52/0 (old 52/0)mre
  • eb7aeeb9mission brief 016mre
  • e4c34759paths: projects moved into /media/STORAGE/projects/WorldAPI/mre
  • 26efb23freport w6-events (events test)mre
  • c6bfab04mission 015: re-vendor Hybriel 0f396652 (static build), reportmre
  • b7ec5981mission brief 015mre