gitoria

gitoria

All repositories: gitoria

ReadmeCodePull requestsReleasesTicketsSettings
Commitb554cd5fb554cd5flogmreb554cd5f/lib/app-auth.hl

2.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 { appsTable } from './apps-table.hl'
  10. import { rowOfToken } from './app-tokens.hl'
  11. import { limitCheck } from './limits.hl'
  12. // the credential of `Authorization: Bearer <x>` (the scheme in any case) or null
  13. static bearerOf = (req) => {
  14. h = req.headers['authorization']
  15. if (h == null || hlTypeName(h) != 'String') { return null }
  16. t = h.trim()
  17. if (t.length < 8 || t.slice(0, 7).toLowerCase() != 'bearer ') { return null }
  18. return t.slice(7).trim()
  19. }
  20. static unauthorized = () => {
  21. return apiError(401, 'unauthorized', 'missing, unknown or expired token: get a new one with POST /apps/token', null)
  22. }
  23. static notFound = () => {
  24. return apiError(404, 'not_found', 'no such repository', null)
  25. }
  26. static tooMany = (wait) => {
  27. res = apiError(429, 'rate_limited', 'too many requests: wait ' + wait + ' s', { retryAfter = wait })
  28. res.headers['Retry-After'] = '' + wait
  29. return res
  30. }
  31. // the live token → { app, repo (records) } or null; the app must still exist and still belong to the token's repo
  32. static appOfToken = (token) => {
  33. row = rowOfToken(token)
  34. if (row == null) { return null }
  35. app = appsTable.fetch(row.app)
  36. if (app == null || app.repo != row.repo) { return null }
  37. return { app = app repoId = row.repo }
  38. }
  39. static appOfRequest = (route, req) => {
  40. found = appOfToken(bearerOf(req))
  41. if (found == null) { return { error = unauthorized() } }
  42. repo = repoOfRequest(route, req)
  43. if (repo == null || repo.id != found.repoId) { return { error = notFound() } }
  44. // 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)
  45. wait = limitCheck(found.app.id, req.method)
  46. if (wait > 0) { return { error = tooMany(wait) } }
  47. return { app = found.app repo = repo }
  48. }

Branches

Latest commits

  • 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
  • 4d779b3bAdd create-api gatemre
  • 88563122Add POST /api/repos for user tokensmre
  • b00860e3Create repositories with member checks in createRepoInmre