gitoria

gitoria

All repositories: gitoria

ReadmeCodePull requestsReleasesTicketsSettings
Commitbef635c1bef635c1Merge branch 'w30b'mrebef635c1/lib/app-auth.hl

3.6 KB

  1. // lib/app-auth.hl — WHO IS THE APP BEHIND AN API REQUEST (gitoria#42; contract docs/apps-api.md §2 and §6). ONE helper for every route of the apps API:
  2. // appOfRequest(route, req) → { app, repo } the connected app and the repo its token is bound to
  3. // or { error } the contract's error answer (a Response), to be returned as it is:
  4. // 401 unauthorized no Bearer token, a token that is unknown, expired, or whose app was removed or re-connected
  5. // 429 rate_limited the app is over its minute (limits.hl), with Retry-After
  6. // 404 not_found the token is good but the request's address names another repo (or none): the same as a repo that does not exist
  7. // and keyOfRequest / tokenOfRequest read the Bearer header. Statics only.
  8. import { apiError, repoOfRequest } from './api-helpers.hl'
  9. import { userOfToken } from './tokens.hl'
  10. import { mayRead, mayPush } from './repos.hl'
  11. import { appsTable } from './apps-table.hl'
  12. import { rowOfToken } from './app-tokens.hl'
  13. import { limitCheck } from './limits.hl'
  14. // the credential of `Authorization: Bearer <x>` (the scheme in any case) or null
  15. static bearerOf = (req) => {
  16. h = req.headers['authorization']
  17. if (h == null || hlTypeName(h) != 'String') { return null }
  18. t = h.trim()
  19. if (t.length < 8 || t.slice(0, 7).toLowerCase() != 'bearer ') { return null }
  20. return t.slice(7).trim()
  21. }
  22. static appTokenRequired = (req) => {
  23. token = bearerOf(req)
  24. if (token == null || !token.startsWith('gtr_')) { return null }
  25. if (userOfToken(token) == null) { return unauthorized() }
  26. return apiError(403, 'app_token_required', 'this route needs an app token (gat_); user tokens (gtr_) can read pulls, reviews, threads, comments and statuses and merge with POST /pulls/N/merge', null)
  27. }
  28. static unauthorized = () => {
  29. return apiError(401, 'unauthorized', 'missing, unknown or expired token: get a new one with POST /apps/token', null)
  30. }
  31. static notFound = () => {
  32. return apiError(404, 'not_found', 'no such repository', null)
  33. }
  34. static tooMany = (wait) => {
  35. res = apiError(429, 'rate_limited', 'too many requests: wait ' + wait + ' s', { retryAfter = wait })
  36. res.headers['Retry-After'] = '' + wait
  37. return res
  38. }
  39. // the live token → { app, repo (records) } or null; the app must still exist and still belong to the token's repo
  40. static appOfToken = (token) => {
  41. row = rowOfToken(token)
  42. if (row == null) { return null }
  43. app = appsTable.fetch(row.app)
  44. if (app == null || app.repo != row.repo) { return null }
  45. return { app = app repoId = row.repo }
  46. }
  47. static appOfRequest = (route, req) => {
  48. no = appTokenRequired(req)
  49. if (no != null) { return { error = no } }
  50. found = appOfToken(bearerOf(req))
  51. if (found == null) { return { error = unauthorized() } }
  52. repo = repoOfRequest(route, req)
  53. if (repo == null || repo.id != found.repoId) { return { error = notFound() } }
  54. // the rate limit (gitoria#47, limits.hl): counted per app, after the token and the repo are good (a bad token costs the app nothing)
  55. wait = limitCheck(found.app.id, req.method)
  56. if (wait > 0) { return { error = tooMany(wait) } }
  57. return { app = found.app repo = repo }
  58. }
  59. // Member reads follow apiMergePull: valid user token, visible repo and push permission, no app rate counter.
  60. static readerOfRequest = (route, req) => {
  61. token = bearerOf(req)
  62. if (token == null || !token.startsWith('gtr_')) { return appOfRequest(route, req) }
  63. uid = userOfToken(token)
  64. if (uid == null) { return { error = apiError(401, 'unauthorized', 'a user access token is required', null) } }
  65. repo = repoOfRequest(route, req)
  66. if (repo == null || !mayRead(uid, repo) || !mayPush(uid, repo)) { return { error = notFound() } }
  67. return { repo = repo app = null user = uid }
  68. }

Branches

Latest commits

  • 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
  • 01b71c2edocs: neutral wording in missions, reports and logmre
  • 3eeea733log: 2026-11-09 PR flow plan and membersmre
  • e7c2ff1fdocs: neutral name for the apps-api reviewmre
  • 120b7abdlog: step 3 queuedmre
  • 358474bflog: P50 livemre
  • 477d7bdcMerge branch 'p50'mre
  • 00efc06fP50 reportmre
  • e3f85afblog: commit mention slipmre
  • 4d1a4cf4log: #25 numbersmre
  • 1af0a60ddocs: H25 run() correctionmre
  • eefa2dbcRepo setting: refuse direct pushes to the main branch (pre-receive over HTTPS and ssh)mre
  • 8c8ba2f1log: PR flow announcedmre
  • fa00cd35log: Kunibert pings provenmre
  • 3a68cfaclog: components decisionmre
  • 87de8e31log: Kunibert connected to 9 app reposmre