gitoria

gitoria

All repositories: gitoria

ReadmeCodePull requestsReleasesTicketsSettings
Commitfd4fc028fd4fc028Exercise member status reads with a populated check statemrefd4fc028/lib/app-auth.hl

3.5 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. return token != null && token.startsWith('gtr_') ? apiError(401, 'unauthorized', '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) : null
  25. }
  26. static unauthorized = () => {
  27. return apiError(401, 'unauthorized', 'missing, unknown or expired token: get a new one with POST /apps/token', null)
  28. }
  29. static notFound = () => {
  30. return apiError(404, 'not_found', 'no such repository', null)
  31. }
  32. static tooMany = (wait) => {
  33. res = apiError(429, 'rate_limited', 'too many requests: wait ' + wait + ' s', { retryAfter = wait })
  34. res.headers['Retry-After'] = '' + wait
  35. return res
  36. }
  37. // the live token → { app, repo (records) } or null; the app must still exist and still belong to the token's repo
  38. static appOfToken = (token) => {
  39. row = rowOfToken(token)
  40. if (row == null) { return null }
  41. app = appsTable.fetch(row.app)
  42. if (app == null || app.repo != row.repo) { return null }
  43. return { app = app repoId = row.repo }
  44. }
  45. static appOfRequest = (route, req) => {
  46. no = appTokenRequired(req)
  47. if (no != null) { return { error = no } }
  48. found = appOfToken(bearerOf(req))
  49. if (found == null) { return { error = unauthorized() } }
  50. repo = repoOfRequest(route, req)
  51. if (repo == null || repo.id != found.repoId) { return { error = notFound() } }
  52. // 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)
  53. wait = limitCheck(found.app.id, req.method)
  54. if (wait > 0) { return { error = tooMany(wait) } }
  55. return { app = found.app repo = repo }
  56. }
  57. // Member reads follow apiMergePull: valid user token, visible repo and push permission, no app rate counter.
  58. static readerOfRequest = (route, req) => {
  59. token = bearerOf(req)
  60. if (token == null || !token.startsWith('gtr_')) { return appOfRequest(route, req) }
  61. uid = userOfToken(token)
  62. if (uid == null) { return { error = apiError(401, 'unauthorized', 'a user access token is required', null) } }
  63. repo = repoOfRequest(route, req)
  64. if (repo == null || !mayRead(uid, repo) || !mayPush(uid, repo)) { return { error = notFound() } }
  65. return { repo = repo app = null user = uid }
  66. }

Branches

Latest commits

  • fd4fc028Exercise member status reads with a populated check statemre
  • 536ca6f9Allow repository members to read pull workflow APIs with user tokensmre
  • 705dd834W49 missionmre
  • b554cd5flogmre
  • f4c9440ddeploy.sh: tar --hard-dereference for the live data backupmre
  • 1a687b15Merge branch 'w48-member-merge'mre
  • 166c775fW48 report, logmre
  • 1b4623f0Members merge pull requests: API route, button on /pulls and /pulls/N, one shared merge pathmre
  • 1075c38cW48 mission, logmre
  • 17bdd0d6V37/V39 missions, logmre
  • 146de5f8Re-vendor gitoria to Hybriel set-day39 (51e921d7)mre
  • e874e44dtests/status-keyed: app token lives 60 s so it outlives the gate's two restartsmre
  • 270b8d61deploy.sh: DEPLOY_STOP_START=1 stops, waits for the old pid, then starts (mpackdb freeze, replaces the standby swap)mre
  • 3f9235b8project.hl: drop the dummy /pulls/:n component route (hybriel#294 is fixed in day37)mre
  • c90daf19Re-vendor gitoria to Hybriel set-day37 (f6b3d45b)mre
  • 03b5bb08W39 report to reports/, drop scratch repro, mission + logmre
  • 154d333fMerge w39-create-api: POST /api/repos with a user token (#39)mre
  • 149dd512Rewrite W39 reportmre
  • e5385075Expect 401 for POST /api/repos without a tokenmre
  • 1a76055cDocument repository creation APImre