gitoria

gitoria

All repositories: gitoria

ReadmeCodePull requestsReleasesTicketsSettings
Commitb1dcb580b1dcb580gitoria#46: statuses keyed by app + context, required = app + context, old rows still readmreb1dcb580/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

  • b1dcb580gitoria#46: statuses keyed by app + context, required = app + context, old rows still readmre
  • 9678a8f3gitoria#46: contract v1.4, statuses keyed by app + contextmre
  • bdcbbaa9gitoria#47: rate limits, status read route, statuses + required on the pull (merge w47-limits)mre
  • 772c401fgitoria#45: W45 report, logmre
  • b6b793afgitoria#45: PR page with reviews, threads, conversation, Review again (merge w45-pr-page)mre
  • fb9e7554gitoria#45: browser gate: a /pulls title links to the PR pagemre
  • f7198131gitoria#45: tests/pr-page.mjs gatemre
  • 483141d0gitoria#45: Settings shows the Apps heading only to the ownermre
  • e2eff8f6gitoria#45: the PR page /pulls/N with reviews, threads, verdicts, conversation, comment, reply, resolve and Review againmre
  • 7a602839gitoria#45: raiseEvent can send to one app only; people's comments, replies and resolving in reviews.hlmre
  • 5de7f578gitoria#47: rate limits per app (429, Retry-After), status read route, statuses and required on the pull, one JSON check for every write routemre
  • 19edc1f5gitoria#44: contract v1.3.1, what outdated meansmre
  • 7c2277ccgitoria#38: worker reports W40–W46, logmre
  • 1c3fa842gitoria#45 #47: worker briefsmre
  • 0a46bf7dgitoria#47: apps API contract v1.3 (status read route, statuses + required on the pull)mre
  • 3373f7c3gitoria#38: push API contract v1.2.1, reviews, worker briefs and logmre
  • 34143d2cgitoria#46: closing brace of appComments lost in the mergemre
  • 54927c9cgitoria#46: PR statuses and required checks before merge (merge w46-status)mre
  • 187e1871gitoria#46: badges without decorative bordersmre
  • 6b022798gitoria#46: tests/status.mjs in GATESmre