gitoria

gitoria

All repositories: gitoria

ReadmeCodePull requestsReleasesTicketsSettings
Commita487248ca487248cgitoria#25: remove every cache that hid the page build (page key, Markdown render key, homepage cache, parsed-blocks cache) and the gate that only tested themmrea487248c/lib/api.hl

7.9 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. // Nothing here: hl:web (batch 20) does it. The session cookie carries `Domain=.<host>` (project.hl sessionDomain); among several gitoriasid
  82. // 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
  83. // 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".
  84. // ---- CONNECT A REPO TO A TICKETS PROJECT (gitoria#18; tickets.hl, components/settings.hl) ----------------------------
  85. // /settings/connect: the owner's click → a fresh nonce parked in the session (bound to the repo) → tickets' /connect.
  86. // /settings/connected: tickets' return with ?code&state → tickets.hl finishConnect (nonce, exchange, key stored per
  87. // repo) → back to /settings. A failure is a note on the settings page.
  88. static connectStart = (route, &req, &sessions) => {
  89. if (req.method != 'GET') { return fail(405, 'GET only') }
  90. repo = repoOfRequest(req)
  91. if (repo == null) { return fail(404, 'no such repository') }
  92. s = req.session
  93. if (s == null || !ownsRepo(repo.slug, s)) { return goTo('/settings') }
  94. nonce = randomBytes(16)
  95. s.data.connectState = repo.slug + '.' + nonce
  96. s.data.connectNote = null
  97. sessions.save(s)
  98. return goTo(connectHref(repo.slug, nonce))
  99. }
  100. static connectBack = (route, &req, &sessions) => {
  101. if (req.method != 'GET') { return fail(405, 'GET only') }
  102. repo = repoOfRequest(req)
  103. if (repo == null) { return fail(404, 'no such repository') }
  104. s = req.session
  105. if (s == null || !ownsRepo(repo.slug, s)) { return goTo('/settings') }
  106. q = req.query != null ? req.query : {}
  107. want = s.data.connectState
  108. s.data.connectState = null
  109. note = finishConnect(repo.slug, want, q)
  110. s.data.connectNote = note == '' ? null : note
  111. sessions.save(s)
  112. return goTo('/settings')
  113. }
  114. // ---- WHAT THE SSHD CONTAINER ASKS (ticket gitoria#7; docker/sshd) — only with the shared secret (api-helpers.hl sshGuard)
  115. // GET /__git/keys?type=<ssh-ed25519>&key=<base64> sshd AuthorizedKeysCommand: the authorized_keys line for a known key
  116. // (sshkeys.hl keyLine), empty for an unknown one
  117. // GET /__git/access?user=<id>&slug=<slug>&write=0|1 gitoria-shell before it starts git: `ok` or 403. Read = every repo (public);
  118. // write = the repo's owner only (repos.hl mayPush — the same rule as the token on HTTPS, transport.hl)
  119. static gitKeys = (route, req) => {
  120. no = sshGuard(req)
  121. if (no != null) { return no }
  122. q = req.query != null ? req.query : {}
  123. return textReply(200, keyLine(q.type, q.key))
  124. }
  125. static gitAccess = (route, req) => {
  126. no = sshGuard(req)
  127. if (no != null) { return no }
  128. q = req.query != null ? req.query : {}
  129. slug = q.slug
  130. if (slug == null || hlTypeName(slug) != 'String' || slugError(slug) != null) { return textReply(404, 'no such repository' + NL) }
  131. repo = repoBySlug(slug)
  132. if (repo == null) { return textReply(404, 'no such repository' + NL) }
  133. if (q.write == '1' && !mayPush(q.user, repo)) { return textReply(403, 'only the owner of this repository can push to it' + NL) }
  134. return textReply(200, 'ok' + NL)
  135. }

Branches

Latest commits

  • a487248cgitoria#25: remove every cache that hid the page build (page key, Markdown render key, homepage cache, parsed-blocks cache) and the gate that only tested themmre
  • 12f2f4fegitoria: re-vendor Hybriel set-day27b, a startup fault in on Error exits 1mre
  • 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