gitoria
All repositories: gitoria
3.6 KB
// 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:// appOfRequest(route, req) → { app, repo } the connected app and the repo its token is bound to// or { error } the contract's error answer (a Response), to be returned as it is:// 401 unauthorized no Bearer token, a token that is unknown, expired, or whose app was removed or re-connected// 429 rate_limited the app is over its minute (limits.hl), with Retry-After// 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// and keyOfRequest / tokenOfRequest read the Bearer header. Statics only.import { apiError, repoOfRequest } from './api-helpers.hl'import { userOfToken } from './tokens.hl'import { mayRead, mayPush } from './repos.hl'import { appsTable } from './apps-table.hl'import { rowOfToken } from './app-tokens.hl'import { limitCheck } from './limits.hl'// the credential of `Authorization: Bearer <x>` (the scheme in any case) or nullstatic bearerOf = (req) => {h = req.headers['authorization']if (h == null || hlTypeName(h) != 'String') { return null }t = h.trim()if (t.length < 8 || t.slice(0, 7).toLowerCase() != 'bearer ') { return null }return t.slice(7).trim()}static appTokenRequired = (req) => {token = bearerOf(req)if (token == null || !token.startsWith('gtr_')) { return null }if (userOfToken(token) == null) { return unauthorized() }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)}static unauthorized = () => {return apiError(401, 'unauthorized', 'missing, unknown or expired token: get a new one with POST /apps/token', null)}static notFound = () => {return apiError(404, 'not_found', 'no such repository', null)}static tooMany = (wait) => {res = apiError(429, 'rate_limited', 'too many requests: wait ' + wait + ' s', { retryAfter = wait })res.headers['Retry-After'] = '' + waitreturn res}// the live token → { app, repo (records) } or null; the app must still exist and still belong to the token's repostatic appOfToken = (token) => {row = rowOfToken(token)if (row == null) { return null }app = appsTable.fetch(row.app)if (app == null || app.repo != row.repo) { return null }return { app = app repoId = row.repo }}static appOfRequest = (route, req) => {no = appTokenRequired(req)if (no != null) { return { error = no } }found = appOfToken(bearerOf(req))if (found == null) { return { error = unauthorized() } }repo = repoOfRequest(route, req)if (repo == null || repo.id != found.repoId) { return { error = notFound() } }// 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)wait = limitCheck(found.app.id, req.method)if (wait > 0) { return { error = tooMany(wait) } }return { app = found.app repo = repo }}// Member reads follow apiMergePull: valid user token, visible repo and push permission, no app rate counter.static readerOfRequest = (route, req) => {token = bearerOf(req)if (token == null || !token.startsWith('gtr_')) { return appOfRequest(route, req) }uid = userOfToken(token)if (uid == null) { return { error = apiError(401, 'unauthorized', 'a user access token is required', null) } }repo = repoOfRequest(route, req)if (repo == null || !mayRead(uid, repo) || !mayPush(uid, repo)) { return { error = notFound() } }return { repo = repo app = null user = uid }}
Branches
- f1-broken-json
- f2-api-bugs
- gp-gate-ports
- heap-revert
- hl25-records
- ident-chooser-selector
- mainmain branch
- p51-public-pr-reads
- records-1112
- s25-no-session-on-login-error
- v59
- w52-person-approval
- w52b-approval-followup
- w52c-header-phone
- w53-pulls-speed-pr
- w54-repo-rename-pr
- w55-rename-org
- w56-owner-members
- w62-pr-close
- w63-force-push
- w64-org-slash
Latest commits
- e7c2ff1fdocs: neutral name for the apps-api reviewmre
- 120b7abdlog: step 3 queuedmre
- 358474bflog: P50 livemre
- 477d7bdcMerge branch 'p50'mre
- 00efc06fP50 reportmre
- e3f85afblog: commit mention slipmre
- 4d1a4cf4log: #25 numbersmre
- 1af0a60ddocs: H25 run() correctionmre
- eefa2dbcRepo setting: refuse direct pushes to the main branch (pre-receive over HTTPS and ssh)mre
- 8c8ba2f1log: PR flow announcedmre
- fa00cd35log: Kunibert pings provenmre
- 3a68cfaclog: components decisionmre
- 87de8e31log: Kunibert connected to 9 app reposmre
- c22aff69log: PR-only rollout startedmre
- 25309981log: STOP and paused workers 2026-11-08mre
- 5286803alog: worker briefs and reports of 2026-11-08mre
- 85c86605log: 2026-11-08 Arachne on list shapemre
- cb634512log: 2026-11-08 keyed lists livemre
- 07c98d4aLists: keyed rows with @id, remove(id) and one whole-list assignment instead of hand-rebuilt loopsmre
- 0db0d81elog: 2026-11-08 day52 live, W30 startedmre