gitoria

gitoria

All repositories: gitoria

ReadmeCodePull requestsReleasesTicketsSettings
Commit2ed094412ed09441Use layouts theme 17e0f03 for W61 compatibilitymre2ed09441/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

  • 2ed09441Use layouts theme 17e0f03 for W61 compatibilitymre
  • b8609a91Merge branch 'main' into w61-stylemre
  • 4639caf7Report W61 theme startup blocker and day39 Style probemre
  • 81f0a0bbREADME: two caches were restored by 90b4f72; log W62mre
  • 4a3f557eImport layouts theme ebd00b6 for W61 (startup blocked)mre
  • ab12a257Merge W62: #25 commit page measurements and proc repromre
  • 41323e49gitoria#25: measure commit request costs and reproduce blocking process waitsmre
  • 53ca42b2log: W60 merged, W61 W62 startedmre
  • 80870a33mission W61: style passmre
  • 560fd208Merge W60: helpers, bound forms, login in topics, two form fixes (gitoria#28 #32 #33)mre
  • ac5ba6a4log: W60, #25 measuredmre
  • 220f6342W60: record corrected form proof, full AE and remaining gapsmre
  • 7e31cac5gitoria#28: restrict reply form layout to reply formsmre
  • ae373cfdgitoria#28: keep each reply draft in its thread componentmre
  • eeeebd5agitoria: insert a newly created organization oncemre
  • 34d2d5cagitoria: clear the bound public-key textarea after creationmre
  • 119e54c0W60: restore conductor-owned tracking filesmre
  • 24b479e7W60: record final gates, Chrome failures and AE evidencemre
  • 5e11184cW60: record partial validation, framework gaps and cleanupmre
  • 20fe13fagitoria#33: centralize login and tickets connection statemre