gitoria

gitoria

All repositories: gitoria

ReadmeCodePull requestsReleasesTicketsSettings
Commit5a996ea85a996ea8gitoria#42: short-lived app tokens, read-only git fetch with them (merge w42-app-token)mre5a996ea8/lib/app-tokens.hl

3.0 KB

  1. // lib/app-tokens.hl — THE SHORT-LIVED TOKENS OF CONNECTED APPS (gitoria#42, part of the push API gitoria#38; contract docs/apps-api.md §2).
  2. // An app trades its long-lived key (`gak_`, apps.hl) for a token (`gat_` + 40 hex) that lives one hour and is used for every API call and for
  3. // git fetch (read-only). Only the sha256 of a token is stored (like tokens.hl):
  4. // appTokensTable pk @id index !hash, app, repo storage/mpackdb/apptokens.db
  5. // { hash (sha256 of the token), app (apps @id), repo (repos @id, the app's repo), created, expires (ms) }
  6. // A token is bound to its app and so to its repo. Old tokens keep working until they expire. Expired rows are swept when a new token is issued
  7. // (no timer). Removing an app and re-connecting it (a new key) call dropTokensOfApp: its tokens die at once.
  8. // GITORIA_APP_TOKEN_LIFE_MS shortens the life — ONLY for the gate (tests/app-token.mjs); the default and the contract are one hour.
  9. import { MPackDB } from 'hl:mpackdb'
  10. import { now, timestamp } from 'hl:time'
  11. import { randomBytes, sha256 } from 'hl:crypto'
  12. import { storageDir, envOr, countOfList, firstOf, isHex } from './util.hl'
  13. static appTokensTable = new MPackDB(file = storageDir + '/apptokens.db', primaryKey = '@id', indexes = ['!hash', 'app', 'repo'])
  14. static lifeMs = toNumber(envOr('GITORIA_APP_TOKEN_LIFE_MS', '3600000')) != null ? toNumber(envOr('GITORIA_APP_TOKEN_LIFE_MS', '3600000')) : 3600000
  15. static tokenShaped = (t) => {
  16. return t != null && hlTypeName(t) == 'String' && t.length == 44 && t.startsWith('gat_') && isHex(t.slice(4), 40)
  17. }
  18. // the rows past their expiry are deleted (one pass over the table; called when a token is issued)
  19. static sweepExpired = () => {
  20. all = appTokensTable.find(null, null)
  21. if (countOfList(all) == 0) { return 0 }
  22. let n = 0
  23. for (t of all) {
  24. if (t.expires <= now()) {
  25. appTokensTable.delete(t.id)
  26. n = n + 1
  27. }
  28. }
  29. return n
  30. }
  31. // a new token for an app → { token, expires (ISO, UTC) } or { error }
  32. static issueToken = (a) => {
  33. sweepExpired()
  34. token = 'gat_' + randomBytes(20, 'hex')
  35. expires = now() + lifeMs
  36. id = appTokensTable.put({ hash = sha256(token) app = a.id repo = a.repo created = now() expires = expires })
  37. if (id == null) { return { error = 'could not store the token: ' + appTokensTable.lastError() } }
  38. return { token = token expires = timestamp(expires) }
  39. }
  40. // the live token's row { app (apps @id), repo (repos @id) } or null (not shaped like a token, unknown, expired); an expired row is deleted here too
  41. static rowOfToken = (token) => {
  42. if (!tokenShaped(token)) { return null }
  43. t = firstOf(appTokensTable.find('hash', sha256(token)))
  44. if (t == null) { return null }
  45. if (t.expires <= now()) {
  46. appTokensTable.delete(t.id)
  47. return null
  48. }
  49. return { app = t.app repo = t.repo }
  50. }
  51. // every token of an app is dead (the app was removed, or re-connected with a new key)
  52. static dropTokensOfApp = (appId) => {
  53. all = appTokensTable.find('app', appId)
  54. if (countOfList(all) == 0) { return 0 }
  55. let n = 0
  56. for (t of all) {
  57. appTokensTable.delete(t.id)
  58. n = n + 1
  59. }
  60. return n
  61. }

Branches

Latest commits

  • 5a996ea8gitoria#42: short-lived app tokens, read-only git fetch with them (merge w42-app-token)mre
  • b5d696fegitoria#42: app tokens (gat_, one hour), appOfRequest, app token as the git read passwordmre
  • 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
  • 821cdbb1gitoria#34b: styles, create-repo tool, docs, push/ssh gates sign in through the selector, shots scriptmre