gitoria

gitoria

All repositories: gitoria

ReadmeCodePull requestsReleasesTicketsSettings
Main branchmainfe209ffeMerge branch 'w56-owner-members' into mainGitoriamain/lib/app-auth.hl

5.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 { repoFor, orgFor, isInside, 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. }
  69. // Anonymous reads (gitoria#51): ONLY a request with no Authorization header at all, ONLY for GET {api}/pulls/N and GET {api}/statuses/<sha>.
  70. // A public repo answers as for a member; a private or missing repo is the same 404 as today. Any header (even a bad one) takes readerOfRequest. No app rate counter.
  71. static publicReaderOfRequest = (Hybrid route, Hybrid req) => {
  72. if (req.headers['authorization'] != null) { return readerOfRequest(route, req) }
  73. repo = repoOfRequest(route, req)
  74. if (repo == null || !mayRead(null, repo)) { return { error = notFound() } }
  75. return { repo = repo app = null user = null }
  76. }
  77. static userTokenRequest = (Hybrid req, String appMessage = 'a user access token is required') => {
  78. token = bearerOf(req)
  79. if (token != null && token.startsWith('gat_')) { return { error = apiError(403, 'forbidden', appMessage, null) } }
  80. uid = userOfToken(token)
  81. if (uid == null) { return { error = apiError(401, 'unauthorized', 'a user access token is required', null) } }
  82. return { uid = uid }
  83. }
  84. static repoUserRequest = (Hybrid route, Hybrid req, Boolean ownerOnly) => {
  85. who = userTokenRequest(req)
  86. if (who.error != null) { return who }
  87. uid = who.uid
  88. slug = route.params.org == null ? decodeURIComponent(route.params.slug) : route.params.org + '/' + route.params.slug
  89. r = repoFor(slug, uid)
  90. if (r == null) { return { error = apiError(404, 'not_found', 'no such repository', null) } }
  91. if ((ownerOnly && r.owner != uid) || (!ownerOnly && !isInside(uid, r))) { return { error = apiError(403, 'forbidden', 'not allowed', null) } }
  92. return { uid = uid repo = r }
  93. }
  94. static orgUserRequest = (Hybrid route, Hybrid req) => {
  95. who = userTokenRequest(req)
  96. if (who.error != null) { return who }
  97. o = orgFor(route.params.org, who.uid)
  98. if (o == null) { return { error = apiError(404, 'not_found', 'no such organization', null) } }
  99. if (o.owner != who.uid) { return { error = apiError(403, 'forbidden', 'only the organization owner can do this', null) } }
  100. return { uid = who.uid org = o }
  101. }

Branches

Latest commits

  • fe209ffeMerge branch 'w56-owner-members' into mainGitoria
  • fa0754c8|||PR repository owner transfer and members API (#55)mre
  • 8862e537Merge branch 'w55-rename-org' into mainGitoria
  • 014d2b7fOne immutable readers snapshot per move for the left-organization event (#53)mre
  • 1859d506Decide who hears a repo left its organization by its readers before the move (#53)mre
  • 499f25f2Review fixes for the Settings organization move (#53)mre
  • ed3b922b|||PR move a repository into an organization in Settings (#53)mre
  • 74ec2af8Merge branch 'w54-repo-rename-pr' into mainGitoria
  • 9f7ce8d8Preserve rename compatibility and live page titlesmre
  • c3d95b6e|||PR separate names and addresses, rename repositories in Settings (#53)mre
  • ce08920dMerge branch 'w52c-header-phone' into mainGitoria
  • 0bab9e46|||PR header: ident status wraps on phones instead of clippingmre
  • e7af3eceMerge branch 'w52b-approval-followup' into mainGitoria
  • c01e66c6|||PR approvals follow-up: live Merge after status writes, helpers (#52)mre
  • 841da497Merge branch 'w53-pulls-speed-pr' into mainGitoria
  • 80307ae2|||PR pulls page cache: live PR numbers, complete scans onlymre
  • 69141f65|||PR pulls page: cache the git part per repo by branch tipsmre
  • 155861f6Merge branch 'w52-person-approval' into mainGitoria
  • b5435de8|||PR person approvals: second Kunibert round (#52)mre
  • 3a8b2fb8|||PR person approvals: Kunibert review fixes (#52)mre