gitoria

gitoria

All repositories: gitoria

ReadmeCodePull requestsReleasesTicketsSettings
Commit76e0587a76e0587aWIP gs 2mre76e0587a/lib/api.hl

8.4 KB

  1. // lib/api.hl — THE FUNCTION ROUTES (project.hl wires them): thin wrappers — check the request and the session, call the
  2. // topic, answer. The plumbing (JSON / Markdown / plain answers, redirects, the ssh guard, `?next=`) is lib/api-helpers.hl.
  3. // /api/repos, /api/repos/:slug the public reads (repos.hl; Accept: text/markdown → the Markdown read view)
  4. // /login/callback the login button's return (users.hl)
  5. // /settings/connect, /settings/connected connecting a repo to a tickets project (tickets.hl)
  6. // /__git/keys, /__git/access what the sshd container asks (sshkeys.hl, repos.hl)
  7. // (The git protocol's routes are lib/transport.hl gitTransport.)
  8. // A route that WRITES THE SESSION gets the request and the framework's session store BY REFERENCE (`&req`, `&sessions`,
  9. // from project.hl): copied into a second call, the session inside `req` would be a copy too and the write would be lost.
  10. import { Response } from 'hl:http1'
  11. import { randomBytes } from 'hl:crypto'
  12. import { NL } from './util.hl'
  13. import { userOfLoginCode } from './users.hl'
  14. import { repoRows, repoRow, repoBySlug, ownsRepo, mayPush } from './repos.hl'
  15. import { slugError, repoDocument, listDocument } from './repos-helpers.hl'
  16. import { connectHref, finishConnect } from './tickets.hl'
  17. import { keyLine } from './sshkeys.hl'
  18. import { reply, fail, wantsMarkdown, markdownReply, textReply, goTo, sshGuard, repoOfRequest, safeNext } from './api-helpers.hl'
  19. // ---- the API: reads are public (creating a repo is a web action for now) ------------------------
  20. static apiRepos = (route, req) => {
  21. if (req.method != 'GET') { return fail(405, 'GET only') }
  22. rows = repoRows()
  23. if (wantsMarkdown(req)) { return markdownReply(listDocument(rows)) }
  24. return reply(200, { repos = rows })
  25. }
  26. static apiRepo = (route, req) => {
  27. if (req.method != 'GET') { return fail(405, 'GET only') }
  28. row = repoRow(route.params.slug)
  29. if (row == null) { return fail(404, 'no such repository') }
  30. if (wantsMarkdown(req)) { return markdownReply(repoDocument(row)) }
  31. return reply(200, row)
  32. }
  33. // ---- NOTHING ELSE (mission MG, as ident's notFoundPage): the last route of project.hl answers a path no route knows with the
  34. // shell's 404 page (components/notfound.hl) — status 404, the page rendered by the framework with the request's session and host
  35. static notFoundPage = (route, req, &srv, pageComponent) => {
  36. m = { route = { pattern = route.route.pattern component = pageComponent } kind = 'component' params = { path = req.path } path = req.path }
  37. res = srv.page(m, req.session, srv.hostOf(req.headers['host']), srv.requestHeaders(req.headers))
  38. res.status = 404
  39. return res
  40. }
  41. static notFoundApi = (route, req) => { return fail(404, 'no such endpoint') }
  42. // ---- THE LOGIN BUTTON'S RETURN (ident README "How apps use ident") ----------------------------
  43. // ident sends the browser back with ?ident_code= (and our ?next=); the code is exchanged here (users.hl userOfLoginCode),
  44. // the user goes into THIS browser's session (`req.session`, the cookie's — hybriel #11; minted when it brought none),
  45. // then → `?next=` (api-helpers.hl safeNext). A FAILED LOGIN is a page (components/loginfailed.hl): the reason is parked
  46. // in the session, then → /login/failed.
  47. static loginFailed = (&req, &sessions, why) => {
  48. let s = req.session
  49. fresh = s == null
  50. if (fresh) { s = sessions.mint() }
  51. s.data.loginError = why
  52. sessions.save(s)
  53. 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' } })
  54. if (fresh) { res.headers['Set-Cookie'] = sessions.cookieHeader(s.id) }
  55. return res
  56. }
  57. static loginCallback = (route, &req, &sessions) => {
  58. if (req.method != 'GET') { return loginFailed(&req, &sessions, 'GET only') }
  59. q = req.query != null ? req.query : {}
  60. code = q.ident_code
  61. if (code == null || code == '') { return loginFailed(&req, &sessions, 'ident sent no login code') }
  62. x = userOfLoginCode(code)
  63. if (x.error != null) { return loginFailed(&req, &sessions, x.error) }
  64. // A LOGIN GETS A NEW SESSION ID (session fixation, mission SG): a cookie planted before the login must not become the
  65. // logged-in one. The old session (if any) is logged out and left behind.
  66. old = req.session
  67. if (old != null) {
  68. old.user = null
  69. old.data.tag = null
  70. sessions.save(old)
  71. }
  72. let s = sessions.mint()
  73. s.user = { id = x.user.id }
  74. s.data.tag = randomBytes(16)
  75. s.data.loginError = null
  76. sessions.save(s)
  77. 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) } })
  78. return res
  79. }
  80. // ---- ONE LOGIN FOR THE MAIN ADDRESS AND EVERY REPO ADDRESS ---------------------------------------------------------------------
  81. // The session cookie carries `Domain=.<host>` (project.hl sessionDomain), so <repo>.<host> sees the login of the main address. A browser
  82. // whose session cookie is a HOST-ONLY one of the main address (a cookie from before the Domain was set, or kept from an earlier visit) shows the
  83. // login on the main address and none on a repo address — creator 2026-10-24: "does not keep the state from the main site". The shell calls this
  84. // once per page view of a logged-in session (components/main.hl): the answer re-issues THE SAME session id with the Domain, nothing is signed out,
  85. // nothing is rotated; the browser then holds the one cookie for every address.
  86. static shareSession = (route, &req, &sessions) => {
  87. s = req.session
  88. headers = { 'Cache-Control' = 'no-store' 'X-Content-Type-Options' = 'nosniff' }
  89. if (s != null && s.user != null) { headers['Set-Cookie'] = sessions.cookieHeader(s.id) }
  90. return new Response('', { status = 204 headers = headers })
  91. }
  92. // ---- CONNECT A REPO TO A TICKETS PROJECT (gitoria#18; tickets.hl, components/settings.hl) ----------------------------
  93. // /settings/connect: the owner's click → a fresh nonce parked in the session (bound to the repo) → tickets' /connect.
  94. // /settings/connected: tickets' return with ?code&state → tickets.hl finishConnect (nonce, exchange, key stored per
  95. // repo) → back to /settings. A failure is a note on the settings page.
  96. static connectStart = (route, &req, &sessions) => {
  97. if (req.method != 'GET') { return fail(405, 'GET only') }
  98. repo = repoOfRequest(req)
  99. if (repo == null) { return fail(404, 'no such repository') }
  100. s = req.session
  101. if (s == null || !ownsRepo(repo.slug, s)) { return goTo('/settings') }
  102. nonce = randomBytes(16)
  103. s.data.connectState = repo.slug + '.' + nonce
  104. s.data.connectNote = null
  105. sessions.save(s)
  106. return goTo(connectHref(repo.slug, nonce))
  107. }
  108. static connectBack = (route, &req, &sessions) => {
  109. if (req.method != 'GET') { return fail(405, 'GET only') }
  110. repo = repoOfRequest(req)
  111. if (repo == null) { return fail(404, 'no such repository') }
  112. s = req.session
  113. if (s == null || !ownsRepo(repo.slug, s)) { return goTo('/settings') }
  114. q = req.query != null ? req.query : {}
  115. want = s.data.connectState
  116. s.data.connectState = null
  117. note = finishConnect(repo.slug, want, q)
  118. s.data.connectNote = note == '' ? null : note
  119. sessions.save(s)
  120. return goTo('/settings')
  121. }
  122. // ---- WHAT THE SSHD CONTAINER ASKS (ticket gitoria#7; docker/sshd) — only with the shared secret (api-helpers.hl sshGuard)
  123. // GET /__git/keys?type=<ssh-ed25519>&key=<base64> sshd AuthorizedKeysCommand: the authorized_keys line for a known key
  124. // (sshkeys.hl keyLine), empty for an unknown one
  125. // GET /__git/access?user=<id>&slug=<slug>&write=0|1 gitoria-shell before it starts git: `ok` or 403. Read = every repo (public);
  126. // write = the repo's owner only (repos.hl mayPush — the same rule as the token on HTTPS, transport.hl)
  127. static gitKeys = (route, req) => {
  128. no = sshGuard(req)
  129. if (no != null) { return no }
  130. q = req.query != null ? req.query : {}
  131. return textReply(200, keyLine(q.type, q.key))
  132. }
  133. static gitAccess = (route, req) => {
  134. no = sshGuard(req)
  135. if (no != null) { return no }
  136. q = req.query != null ? req.query : {}
  137. slug = q.slug
  138. if (slug == null || hlTypeName(slug) != 'String' || slugError(slug) != null) { return textReply(404, 'no such repository' + NL) }
  139. repo = repoBySlug(slug)
  140. if (repo == null) { return textReply(404, 'no such repository' + NL) }
  141. if (q.write == '1' && !mayPush(q.user, repo)) { return textReply(403, 'only the owner of this repository can push to it' + NL) }
  142. return textReply(200, 'ok' + NL)
  143. }

Branches

Latest commits

  • 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
  • 2b01515ctests/ticketskit: a failed start kills the tickets server (own process group); stop() kills the groupmre
  • 109cfc5ctests/ticketskit: keep the rotated session cookie of the tickets login (tickets ST2 session rotation)mre
  • 7eb6be27sessions last 99 years (creator 2026-10-23: logins never expire; only sign out ends them)mre
  • b3911918reports/MG-mobile.md: 404 fixed, rebased gatesMG
  • 9da65b42tests/browser.mjs: 404 checks (host, no hydration wait)MG
  • 00517fafgitoria: shell 404 page for unknown paths (catch-all route, status 404, JSON 404 under /api), 390 gate checksMG
  • d9e977f0reports/MG-mobile.md, LOGMG
  • 2f2725actests/browser.mjs: commit-row check (one row, centred date)MG
  • 4df1ea01tests/browser.mjs: MG phone checks (list first, no dead strip, one branch heading, tab grid, commit rows, PR title row, ticket columns, wrapping commands)MG
  • c52424e9gitoria mobile: repo list before the forms, 3x2 tab grid, no duplicate branch heading, ordered commit rows, aligned ticket rows, pull title on its own row, wrapping clone commands, no dead strip under formsMG
  • 3a95252dG3: cache parsed README blocks by sha256 of the text (bounded LRU), reportmre