gitoria
All repositories: gitoria
5.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 { repoFor, orgFor, isInside, 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 }}// Anonymous reads (gitoria#51): ONLY a request with no Authorization header at all, ONLY for GET {api}/pulls/N and GET {api}/statuses/<sha>.// 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.static publicReaderOfRequest = (Hybrid route, Hybrid req) => {if (req.headers['authorization'] != null) { return readerOfRequest(route, req) }repo = repoOfRequest(route, req)if (repo == null || !mayRead(null, repo)) { return { error = notFound() } }return { repo = repo app = null user = null }}static userTokenRequest = (Hybrid req, String appMessage = 'a user access token is required') => {token = bearerOf(req)if (token != null && token.startsWith('gat_')) { return { error = apiError(403, 'forbidden', appMessage, null) } }uid = userOfToken(token)if (uid == null) { return { error = apiError(401, 'unauthorized', 'a user access token is required', null) } }return { uid = uid }}static repoUserRequest = (Hybrid route, Hybrid req, Boolean ownerOnly) => {who = userTokenRequest(req)if (who.error != null) { return who }uid = who.uidslug = route.params.org == null ? decodeURIComponent(route.params.slug) : route.params.org + '/' + route.params.slugr = repoFor(slug, uid)if (r == null) { return { error = apiError(404, 'not_found', 'no such repository', null) } }if ((ownerOnly && r.owner != uid) || (!ownerOnly && !isInside(uid, r))) { return { error = apiError(403, 'forbidden', 'not allowed', null) } }return { uid = uid repo = r }}static orgUserRequest = (Hybrid route, Hybrid req) => {who = userTokenRequest(req)if (who.error != null) { return who }o = orgFor(route.params.org, who.uid)if (o == null) { return { error = apiError(404, 'not_found', 'no such organization', null) } }if (o.owner != who.uid) { return { error = apiError(403, 'forbidden', 'only the organization owner can do this', null) } }return { uid = who.uid org = o }}
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