gitoria
All repositories: gitoria
7.9 KB
// lib/api.hl — THE FUNCTION ROUTES (project.hl wires them): thin wrappers — check the request and the session, call the// topic, answer. The plumbing (JSON / Markdown / plain answers, redirects, the ssh guard, `?next=`) is lib/api-helpers.hl.// /api/repos, /api/repos/:slug the public reads (repos.hl; Accept: text/markdown → the Markdown read view)// /login/callback the login button's return (users.hl)// /settings/connect, /settings/connected connecting a repo to a tickets project (tickets.hl)// /__git/keys, /__git/access what the sshd container asks (sshkeys.hl, repos.hl)// (The git protocol's routes are lib/transport.hl gitTransport.)// A route that WRITES THE SESSION gets the request and the framework's session store BY REFERENCE (`&req`, `&sessions`,// from project.hl): copied into a second call, the session inside `req` would be a copy too and the write would be lost.import { Response } from 'hl:http1'import { randomBytes } from 'hl:crypto'import { NL } from './util.hl'import { userOfLoginCode } from './users.hl'import { repoRows, repoRow, repoBySlug, ownsRepo, mayPush } from './repos.hl'import { slugError, repoDocument, listDocument } from './repos-helpers.hl'import { connectHref, finishConnect } from './tickets.hl'import { keyLine } from './sshkeys.hl'import { reply, fail, wantsMarkdown, markdownReply, textReply, goTo, sshGuard, repoOfRequest, safeNext } from './api-helpers.hl'// ---- the API: reads are public (creating a repo is a web action for now) ------------------------static apiRepos = (route, req) => {if (req.method != 'GET') { return fail(405, 'GET only') }rows = repoRows()if (wantsMarkdown(req)) { return markdownReply(listDocument(rows)) }return reply(200, { repos = rows })}static apiRepo = (route, req) => {if (req.method != 'GET') { return fail(405, 'GET only') }row = repoRow(route.params.slug)if (row == null) { return fail(404, 'no such repository') }if (wantsMarkdown(req)) { return markdownReply(repoDocument(row)) }return reply(200, row)}// ---- NOTHING ELSE (mission MG, as ident's notFoundPage): the last route of project.hl answers a path no route knows with the// shell's 404 page (components/notfound.hl) — status 404, the page rendered by the framework with the request's session and hoststatic notFoundPage = (route, req, &srv, pageComponent) => {m = { route = { pattern = route.route.pattern component = pageComponent } kind = 'component' params = { path = req.path } path = req.path }res = srv.page(m, req.session, srv.hostOf(req.headers['host']), srv.requestHeaders(req.headers))res.status = 404return res}static notFoundApi = (route, req) => { return fail(404, 'no such endpoint') }// ---- THE LOGIN BUTTON'S RETURN (ident README "How apps use ident") ----------------------------// ident sends the browser back with ?ident_code= (and our ?next=); the code is exchanged here (users.hl userOfLoginCode),// the user goes into THIS browser's session (`req.session`, the cookie's — hybriel #11; minted when it brought none),// then → `?next=` (api-helpers.hl safeNext). A FAILED LOGIN is a page (components/loginfailed.hl): the reason is parked// in the session, then → /login/failed.static loginFailed = (&req, &sessions, why) => {let s = req.sessionfresh = s == nullif (fresh) { s = sessions.mint() }s.data.loginError = whysessions.save(s)res = new Response('login failed: ' + why, { status = 302 headers = { 'Location' = '/login/failed' 'Cache-Control' = 'no-store' 'Content-Type' = 'text/plain; charset=utf-8' 'X-Content-Type-Options' = 'nosniff' } })if (fresh) { res.headers['Set-Cookie'] = sessions.cookieHeader(s.id) }return res}static loginCallback = (route, &req, &sessions) => {if (req.method != 'GET') { return loginFailed(&req, &sessions, 'GET only') }q = req.query != null ? req.query : {}code = q.ident_codeif (code == null || code == '') { return loginFailed(&req, &sessions, 'ident sent no login code') }x = userOfLoginCode(code)if (x.error != null) { return loginFailed(&req, &sessions, x.error) }// A LOGIN GETS A NEW SESSION ID (session fixation, mission SG): a cookie planted before the login must not become the// logged-in one. The old session (if any) is logged out and left behind.old = req.sessionif (old != null) {old.user = nullold.data.tag = nullsessions.save(old)}let s = sessions.mint()s.user = { id = x.user.id }s.data.tag = randomBytes(16)s.data.loginError = nullsessions.save(s)res = new Response('logged in', { status = 302 headers = { 'Location' = safeNext(q.next) 'Cache-Control' = 'no-store' 'Content-Type' = 'text/plain; charset=utf-8' 'X-Content-Type-Options' = 'nosniff' 'Set-Cookie' = sessions.cookieHeader(s.id) } })return res}// ---- ONE LOGIN FOR THE MAIN ADDRESS AND EVERY REPO ADDRESS ---------------------------------------------------------------------// Nothing here: hl:web (batch 20) does it. The session cookie carries `Domain=.<host>` (project.hl sessionDomain); among several gitoriasid// cookies the signed-in one wins and is re-issued with the Domain (the stray host-only copy expires), and a signed-in cookie is re-issued when its// attributes change (a login from before the Domain was set moves to it on its next request) and otherwise once a day. Gate: tests/browser.mjs "gitoria#24".// ---- CONNECT A REPO TO A TICKETS PROJECT (gitoria#18; tickets.hl, components/settings.hl) ----------------------------// /settings/connect: the owner's click → a fresh nonce parked in the session (bound to the repo) → tickets' /connect.// /settings/connected: tickets' return with ?code&state → tickets.hl finishConnect (nonce, exchange, key stored per// repo) → back to /settings. A failure is a note on the settings page.static connectStart = (route, &req, &sessions) => {if (req.method != 'GET') { return fail(405, 'GET only') }repo = repoOfRequest(req)if (repo == null) { return fail(404, 'no such repository') }s = req.sessionif (s == null || !ownsRepo(repo.slug, s)) { return goTo('/settings') }nonce = randomBytes(16)s.data.connectState = repo.slug + '.' + nonces.data.connectNote = nullsessions.save(s)return goTo(connectHref(repo.slug, nonce))}static connectBack = (route, &req, &sessions) => {if (req.method != 'GET') { return fail(405, 'GET only') }repo = repoOfRequest(req)if (repo == null) { return fail(404, 'no such repository') }s = req.sessionif (s == null || !ownsRepo(repo.slug, s)) { return goTo('/settings') }q = req.query != null ? req.query : {}want = s.data.connectStates.data.connectState = nullnote = finishConnect(repo.slug, want, q)s.data.connectNote = note == '' ? null : notesessions.save(s)return goTo('/settings')}// ---- WHAT THE SSHD CONTAINER ASKS (ticket gitoria#7; docker/sshd) — only with the shared secret (api-helpers.hl sshGuard)// GET /__git/keys?type=<ssh-ed25519>&key=<base64> sshd AuthorizedKeysCommand: the authorized_keys line for a known key// (sshkeys.hl keyLine), empty for an unknown one// GET /__git/access?user=<id>&slug=<slug>&write=0|1 gitoria-shell before it starts git: `ok` or 403. Read = every repo (public);// write = the repo's owner only (repos.hl mayPush — the same rule as the token on HTTPS, transport.hl)static gitKeys = (route, req) => {no = sshGuard(req)if (no != null) { return no }q = req.query != null ? req.query : {}return textReply(200, keyLine(q.type, q.key))}static gitAccess = (route, req) => {no = sshGuard(req)if (no != null) { return no }q = req.query != null ? req.query : {}slug = q.slugif (slug == null || hlTypeName(slug) != 'String' || slugError(slug) != null) { return textReply(404, 'no such repository' + NL) }repo = repoBySlug(slug)if (repo == null) { return textReply(404, 'no such repository' + NL) }if (q.write == '1' && !mayPush(q.user, repo)) { return textReply(403, 'only the owner of this repository can push to it' + NL) }return textReply(200, 'ok' + NL)}
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
- c21c084egitoria: write the standby service out in full so the boot gate can read the main servicemre
- e307d69agitoria: say what is per process during the hand-overmre
- f0cbe6a6gitoria: standby service on port 45014 and a deploy that hands over through it without a gapmre
- 727cd083gitoria#25: README: vendor notes for Hybriel c1b1010f and the page cachemre
- 61b72dedgitoria#25: re-vendor Hybriel master c1b1010f (W149 page cache), key the repo Readme page by slug and main commit, extend the readme-cache gatemre
- 5b0b4b9ereadme-cache gate: use the security gate's ports when no own ports are givenmre
- bc8f20d9gitoria#25: re-vendor Hybriel c09538bb with the W141 render cache, cache the homepage per commit, add the readme-cache gatemre
- 9feebbd3Re-vendor Hybriel to ded47c13 (W145: Firefox start script fix)mre
- 28ed41e5gitoria#24: one sign-in comes from hl:web batch 20, the /session/share workaround is gonemre
- 3b2669e4gs: drop the unbuildable legacy check, fix pwa fetchmre
- 2a2fdd77gs: C2 check before cookie experimentsmre
- 76e0587aWIP gs 2mre
- a80da7bdWIP gs: security gate for lazy sessions, g24 browser checksmre
- 23fb05d8re-vendor Hybriel aaa39e69 (hl:web batch 20: login emit and Sessions.mint, tab follows a login, signed-in session cookie wins among duplicates and is re-issued with the Domain, W85 faster render, lazy sessions)mre
- a0727ca3ident#31: the selector's sign-in returns to the page the user started on (return attribute = the login callback with next), gate checkmre
- a06975f9gitoria: a gap between your name and Log out in the phone header (they touched); gate checks itmre
- 32ba9d59gitoria: leave the host-only cookie experiment out of the browser gate (gitoria#24 is a separate mission)mre
- 43ad2bcfgitoria header: the selector alone plus your name and Log out as before the theme missions; tests follow (WIP: host-only cookie experiment fails)mre
- ac2c4e3fident selector back in the header (creator 2026-10-24): selector top right signed in and out on phone and desktop, no second Log in button, new gate checks (WIP: gates not re-run after the last edits)mre
- b279f536theme: sync from layouts 4994037 (list rows stack, td word-keep, heading code, edited tap target)mre