gitoria

gitoria

All repositories: gitoria

ReadmeCodePull requestsReleasesTicketsSettings
Commit0a46bf7d0a46bf7dgitoria#47: apps API contract v1.3 (status read route, statuses + required on the pull)mre0a46bf7d/lib/app-auth.hl

2.0 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. // 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
  6. // and keyOfRequest / tokenOfRequest read the Bearer header. Statics only.
  7. import { apiError, repoOfRequest } from './api-helpers.hl'
  8. import { appsTable } from './apps-table.hl'
  9. import { rowOfToken } from './app-tokens.hl'
  10. // the credential of `Authorization: Bearer <x>` (the scheme in any case) or null
  11. static bearerOf = (req) => {
  12. h = req.headers['authorization']
  13. if (h == null || hlTypeName(h) != 'String') { return null }
  14. t = h.trim()
  15. if (t.length < 8 || t.slice(0, 7).toLowerCase() != 'bearer ') { return null }
  16. return t.slice(7).trim()
  17. }
  18. static unauthorized = () => {
  19. return apiError(401, 'unauthorized', 'missing, unknown or expired token: get a new one with POST /apps/token', null)
  20. }
  21. static notFound = () => {
  22. return apiError(404, 'not_found', 'no such repository', null)
  23. }
  24. // the live token → { app, repo (records) } or null; the app must still exist and still belong to the token's repo
  25. static appOfToken = (token) => {
  26. row = rowOfToken(token)
  27. if (row == null) { return null }
  28. app = appsTable.fetch(row.app)
  29. if (app == null || app.repo != row.repo) { return null }
  30. return { app = app repoId = row.repo }
  31. }
  32. static appOfRequest = (route, req) => {
  33. found = appOfToken(bearerOf(req))
  34. if (found == null) { return { error = unauthorized() } }
  35. repo = repoOfRequest(route, req)
  36. if (repo == null || repo.id != found.repoId) { return { error = notFound() } }
  37. return { app = found.app repo = repo }
  38. }

Branches

Latest commits

  • 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
  • c6162f00gitoria#46: status badges on /pulls, Required before merge in Settingsmre
  • e5a0b64egitoria#46: PUT statuses/<sha>, required contexts block the merge on the servermre
  • 26a140e3gitoria#44: reviews, inline threads, replies, conversation for apps (merge w44-reviews)mre
  • 888dbf87gitoria#44: unknown API paths answer the contract's JSON 404 and sit above the page routesmre
  • ea587d90gitoria#44: reviews with inline threads, replies, resolve and the PR conversation for apps (lib/reviews.hl, tests/reviews.mjs)mre
  • 49533a50gitoria#43: signed events to connected apps (merge w43-events)mre
  • 1a1e87d5gitoria#43: the sender never writes the apps row; Settings shows the newest deliverymre
  • cd442066gitoria#43: app-auth unit test reads the apps table from its new filemre
  • 5dccbe38gitoria#43: in-flight rows are left alone by the other process; gate in deploy.sh; READMEmre
  • 8c8805f3merge main (gitoria#42), delivery log and redelivery routes, sweep fixmre
  • 5a996ea8gitoria#42: short-lived app tokens, read-only git fetch with them (merge w42-app-token)mre
  • afdb0cbfgitoria#43: rename the moved helper (name clash in the browser build)mre
  • 15e01e94gitoria#43: tests/events.mjs and its probemre
  • 77829b73gitoria#43: signed events to connected apps (deliveries table, sender timer, pull_request events, ping)mre