gitoria

notes

All repositories: gitoria

ReadmeCodePull requestsReleasesTicketsSettings
Commit6140e6a06140e6a0sessions last 99 years (creator 2026-10-23: logins never expire; only sign out ends them)mre6140e6a0/lib/api.hl

5.9 KB

  1. // lib/api.hl — THE FUNCTION ROUTES (notes mission 002, code order): thin wrappers — check the input and the session,
  2. // call the topic (lib/notes.hl, lib/users.hl), answer.
  3. //
  4. // THE LOGIN BUTTON'S RETURN (ident README "How apps use ident"): /login/callback?ident_code=&next= → the user
  5. // (lib/users.hl userOfLoginCode) → the session is signed in → back to `next` (lib/api-helpers.hl safePath). A FAILED
  6. // LOGIN is a page (components/loginfailed.hl): the reason is parked in the session, then → /login/failed.
  7. // These two write the session: project.hl hands them the request and the session store BY REFERENCE (&req, &sessions);
  8. // a by-value copy would lose the write (tickets mission 010, gitoria mission 002).
  9. //
  10. // THE JSON API (ticket notes#2; since notes#4 the page syncs through its faces, components/notes.hl). JSON, the session cookie is the login.
  11. // GET /api/me → { user } (an opaque id of the signed-in user, or null)
  12. // GET /api/notes → { notes: [{ id, edited, gone }] } every note of the user, tombstones included
  13. // GET /api/notes?id=<id> → { id, text, edited, created }
  14. // POST /api/notes { op: 'save', id, local, text, base, at } → { id, edited, text, copy? }
  15. // POST /api/notes { op: 'delete', id, base } → { ok } | { kept, id, edited, text }
  16. // Invalid JSON is refused by lib/jsoncheck.hl before JSON.parse sees it (hybriel #6); unknown/mistyped fields → 400.
  17. import { Response } from 'hl:http1'
  18. import { randomBytes } from 'hl:crypto'
  19. import { userOfLoginCode, userIdOfSession } from './users.hl'
  20. import { metaRows, noteView, pushNote, removeNote } from './notes.hl'
  21. import { jsonErrorAt } from './jsoncheck.hl'
  22. import { reply, isText, isTime, safePath } from './api-helpers.hl'
  23. import { loginAllowed } from './ratelimit.hl'
  24. import { publicUrl } from './users.hl'
  25. static loginFailed = (&req, &sessions, why) => {
  26. let s = req.session
  27. fresh = s == null
  28. if (fresh) { s = sessions.mint() }
  29. s.data.loginError = why
  30. sessions.save(s)
  31. 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' } })
  32. if (fresh) { res.headers['Set-Cookie'] = sessions.cookieHeader(s.id) }
  33. return res
  34. }
  35. // the function route gets the cookie's session as req.session (hybriel #11); none yet → minted here
  36. static loginCallback = (route, &req, &sessions) => {
  37. if (req.method != 'GET') { return loginFailed(&req, &sessions, 'GET only') }
  38. if (!loginAllowed(req.headers['x-client-ip'])) { return new Response('too many login attempts — wait a few minutes', { status = 429 headers = { 'Cache-Control' = 'no-store' 'Content-Type' = 'text/plain; charset=utf-8' 'X-Content-Type-Options' = 'nosniff' 'Retry-After' = '600' } }) }
  39. q = req.query != null ? req.query : {}
  40. code = q.ident_code
  41. if (code == null || code == '') { return loginFailed(&req, &sessions, 'ident sent no login code') }
  42. x = userOfLoginCode(code)
  43. if (x.error != null) { return loginFailed(&req, &sessions, x.error) }
  44. // A NEW SESSION AT EVERY LOGIN (session fixation): a cookie planted before the login is not the logged-in one; the old session
  45. // (if any) is logged out and left behind
  46. old = req.session
  47. if (old != null) {
  48. old.user = null
  49. old.data.tag = null
  50. sessions.save(old)
  51. }
  52. let s = sessions.mint()
  53. s.user = { id = x.user.id }
  54. s.data.tag = randomBytes(16)
  55. s.data.loginError = null
  56. sessions.save(s)
  57. res = new Response('logged in', { status = 302 headers = { 'Location' = safePath(q.next) 'Cache-Control' = 'no-store' 'Content-Type' = 'text/plain; charset=utf-8' 'X-Content-Type-Options' = 'nosniff' } })
  58. res.headers['Set-Cookie'] = sessions.cookieHeader(s.id)
  59. return res
  60. }
  61. static apiMe = (route, req) => {
  62. u = userIdOfSession(req.session)
  63. return reply(200, { user = u })
  64. }
  65. static apiNotes = (route, req) => {
  66. u = userIdOfSession(req.session)
  67. if (u == null) { return reply(401, { error = 'log in with ident first' }) }
  68. q = req.query != null ? req.query : {}
  69. if (req.method == 'GET') {
  70. if (q.id != null) {
  71. v = noteView(u, q.id)
  72. if (v == null) { return reply(404, { error = 'no such note' }) }
  73. return reply(200, v)
  74. }
  75. return reply(200, { notes = metaRows(u) })
  76. }
  77. if (req.method != 'POST') { return reply(405, { error = 'GET or POST only' }) }
  78. // CSRF: a request that names another Origin is refused; the body must be declared JSON (a cross-site form can only send text/plain,
  79. // urlencoded or multipart — a JSON content type needs a CORS preflight, which this server never grants)
  80. origin = req.headers['origin']
  81. if (origin != null && origin != publicUrl) { return reply(403, { error = 'another origin' }) }
  82. ctype = req.headers['content-type']
  83. if (ctype == null || !ctype.toLowerCase().startsWith('application/json')) { return reply(415, { error = 'send Content-Type: application/json' }) }
  84. if (req.body == null || req.body == '') { return reply(400, { error = 'a JSON body is needed' }) }
  85. at = jsonErrorAt(req.body)
  86. if (at >= 0) { return reply(400, { error = 'invalid JSON at character ' + at }) }
  87. b = JSON.parse(req.body)
  88. if (b == null || hlTypeName(b) != 'Hybrid') { return reply(400, { error = 'the body must be a JSON object' }) }
  89. if (b.op == 'save') {
  90. if (!isText(b.id) || !isText(b.local) || !isText(b.text) || !isTime(b.base) || !isTime(b.at)) { return reply(400, { error = 'save needs id, local, text (strings) and base, at (numbers)' }) }
  91. if (b.local.length > 64 || b.id.length > 64) { return reply(400, { error = 'id / local too long' }) }
  92. r = pushNote(u, b.id, b.local, b.text, b.base, b.at, '')
  93. if (r.error != null) { return reply(400, r) }
  94. return reply(200, r)
  95. }
  96. if (b.op == 'delete') {
  97. if (!isText(b.id) || !isTime(b.base)) { return reply(400, { error = 'delete needs id (string) and base (number)' }) }
  98. return reply(200, removeNote(u, b.id, b.base, '', ''))
  99. }
  100. return reply(400, { error = "op must be 'save' or 'delete'" })
  101. }

Branches

Latest commits

  • 6140e6a0sessions last 99 years (creator 2026-10-23: logins never expire; only sign out ends them)mre
  • cb7e6469deploy: security gate (tests/security.mjs)mre
  • 774bf34bnotes security: CSRF guard on /api/notes, new session at login, Secure cookie, uploadMax 0, login rate limit, nosniff, face payload check; tests/security.mjs; SN reportmre
  • ef18a68fsync components.hybriel 214c318: MdEditor on a phone, two even toolbar rows of 44 px targets, label 15 px, table head 16 px (E2)mre
  • c37488b1MN report: alignment updatemre
  • 4fb35a0enotes mobile: Delete note and the delete row exactly on the editor's edges; gate asserts 1 pxmre
  • 91736270MN report and logmre
  • 15667425notes: delete confirmation buttons at most 12rem wide on the desktopmre
  • 8f7a6a95gate: MN phone checks (list header gutter, back gap, delete question)mre
  • 90c73ed3notes mobile: back link gapmre
  • 72bd4f83notes mobile: list header on the gutter, delete confirmation as a question + two equal buttons, Delete note under the editor edgemre
  • ba1e73f8notes: a sent edit is done once the server's version is not older, so a second writer's text shows in the first tab; gate: two writers both waysmre
  • c69ef9ebsync components.hybriel 134b4e3: MdEditor builds an editor put into the page at once, redraws a surface put in anew (F5, hybriel#211)mre
  • 2eb783f5sync components 9a: shared Icon (icons/icons.hl), md-editor toolbar icons + single focus ring (components f4-icons ba96735)mre
  • 4dd46dadsync-components: copy icons/icons.hl + README (F4); testsmre
  • c92e4a0ftests/e2e-outside.mjs: real-Chrome E2E, a note pushed from another device while open, then typingmre
  • 0516b4ccmd-editor.hl: verbatim copy of components f3-mdvalue (an older text handed back is kept)mre
  • 38cc11ecmd-editor.hl: verbatim copy of components f3-mdvalue (an outside value change is shown by the editor)mre
  • 3e7e79f5reports/H1-hydrate.mdmre
  • d98775e8tests/browser.mjs: wait for hydration before the clicks after a goto('/') (tables new note, phone sidebar)mre