gitoria

gitoria

All repositories: gitoria

ReadmeCodePull requestsReleasesTicketsSettings
Branchf2-api-bugs26117255|||PR apps API: review comments on files with spaces in their namemref2-api-bugs/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

  • 26117255|||PR apps API: review comments on files with spaces in their namemre
  • b8796cecMerge branch 'f1-broken-json' into mainGitoria
  • 2f2959c5|||PR apps API: broken JSON body gives 400 invalid_json, not 500mre
  • f7c6ca07Merge branch 'ident-chooser-selector' into mainGitoria
  • 691abb8e|||PR tests: find ident identity choice without the removed .choose classmre
  • 16a3530fMerge branch 'heap-revert' into mainGitoria
  • 220a80e0|||PR Remove temporary /__heap route and heap helper; logmre
  • 13f0c342Merge branch 'hl25-records' into mainGitoria
  • a002ecb7|||PR records: HL25 report, heap helper, logmre
  • 2492a55cTemporary keyed /__heap route for the live memory snapshotmre
  • fefaedd2log: tracker PR-only, HL25 startedmre
  • 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