gitoria

notes

All repositories: gitoria

ReadmeCodePull requestsReleasesTicketsSettings
Commit8f7a6a958f7a6a95gate: MN phone checks (list header gutter, back gap, delete question)mre8f7a6a95/lib/users.hl

4.7 KB

  1. // lib/users.hl — WHO OWNS NOTES (ticket notes#1; CONCEPT.md "login via ident"). Login is ident's LOGIN BUTTON flow
  2. // (ident README "How apps use ident", way 2): <ident>/login?key=&return=<public url>/login/callback → ?ident_code=
  3. // → the server exchanges it (key + secret) for the per-app identity id. Way 3, the IDENTITY SELECTOR, sits beside the
  4. // button (login.js hands its code to the shell).
  5. //
  6. // usersTable pk @id index !identity { identity, created } storage/mpackdb/users.db
  7. // identity = what ident's exchange answers: the identity's public SHORT id since ident#23 (`a68sz`; old 32-hex per-app
  8. // ids are rewritten once by tools/migrate-short-ids.hl) — stays SERVER SIDE, never sent to a page.
  9. // The session (hl:web) carries `user = { id = <users @id> }` and `data.tag` (the login's random tag, tagOf) only.
  10. // No display name: notes are private. Both logins (lib/api.hl loginCallback, the face notesLogin in components/main.hl)
  11. // go through userOfLoginCode; they write the session themselves.
  12. //
  13. // Config (environment, or `.env` beside project.hl — never read or printed by workers):
  14. // IDENT_URL, IDENT_EXCHANGE_URL, IDENT_API_KEY, IDENT_API_SECRET as in gitoria
  15. // NOTES_PUBLIC_URL the app's address, default https://notes.worldapi.org
  16. // NOTES_STORAGE table directory, default ./storage/mpackdb (lib/util.hl storageDir)
  17. import { MPackDB } from 'hl:mpackdb'
  18. import { now } from 'hl:time'
  19. import { fetch } from 'hl:fetch'
  20. import { envOr, storageDir, firstOf, isHex, isIdentId } from './util.hl'
  21. static identUrl = envOr('IDENT_URL', 'https://ident.worldapi.org')
  22. static identExchangeUrl = envOr('IDENT_EXCHANGE_URL', identUrl)
  23. static identKey = envOr('IDENT_API_KEY', '')
  24. static identSecret = envOr('IDENT_API_SECRET', '')
  25. static publicUrl = envOr('NOTES_PUBLIC_URL', 'https://notes.worldapi.org')
  26. static usersTable = new MPackDB(file = storageDir + '/users.db', primaryKey = '@id', indexes = ['!identity'])
  27. static selectorScript = identUrl + '/selector.js'
  28. static callbackUrl = publicUrl.replaceAll('/', '') == '' ? '' : publicUrl + '/login/callback'
  29. static loginHref = identUrl + '/login?key=' + identKey + '&return=' + encodeURIComponent(callbackUrl)
  30. // THE EXCHANGE: POST <ident>/api/exchange { key, secret, code } → { identity } | { error }
  31. // (a failed fetch is an `Error` event, absorbed by project.hl's `on Error`; the fetch then yields null)
  32. static exchangeCode = (code) => {
  33. if (identKey == '' || identSecret == '') { return { error = 'login is not set up on this server (IDENT_API_KEY / IDENT_API_SECRET missing)' } }
  34. if (!isHex(code, 200)) { return { error = 'that is not an ident login code' } }
  35. r = fetch(identExchangeUrl + '/api/exchange', { method = 'POST' json = { key = identKey secret = identSecret code = code } headers = { 'user-agent' = 'notes.worldapi.org (ident exchange)' } timeoutMs = 10000 })
  36. if (r == null || r.status == null || r.status == 0) { return { error = 'ident did not answer' } }
  37. j = r.status == 200 ? r.json() : null
  38. if (j == null || j.identity == null || !isIdentId(j.identity)) {
  39. let why = ''
  40. if (r.status != 200) {
  41. e = r.json()
  42. why = e != null && e.error != null ? ': ' + e.error : ''
  43. }
  44. return { error = 'ident refused the login (' + r.status + why + ')' }
  45. }
  46. return { identity = j.identity }
  47. }
  48. // ---- users --------------------------------------------------------------------------------
  49. // a face's trailing `session` is always the server's since hybriel #16 (a peer's extra argument is refused)
  50. static userRecord = (userId) => {
  51. if (userId == null || hlTypeName(userId) != 'String' || userId == '') { return null }
  52. return usersTable.fetch(userId)
  53. }
  54. // the user of an identity id, made at its first login
  55. static ensureUser = (identity) => {
  56. u = firstOf(usersTable.find('identity', identity))
  57. if (u != null) { return u }
  58. id = usersTable.put({ identity = identity created = now() })
  59. if (id == null) { return null }
  60. return usersTable.fetch(id)
  61. }
  62. // A LOGIN CODE (the button's ?ident_code= or the selector's) → { user } (made at the first login) | { error }
  63. static userOfLoginCode = (code) => {
  64. x = exchangeCode(code)
  65. if (x.error != null) { return { error = x.error } }
  66. u = ensureUser(x.identity)
  67. if (u == null) { return { error = 'could not store the user' } }
  68. return { user = u }
  69. }
  70. static userOfSession = (session) => {
  71. if (session == null || session.user == null) { return null }
  72. return userRecord(session.user.id)
  73. }
  74. // the users @id of a session, or null (a page may know it: it is not the identity id)
  75. static userIdOfSession = (session) => {
  76. u = userOfSession(session)
  77. return u == null ? null : u.id
  78. }
  79. // the random tag of a session's login (project.hl audience: the login state reaches the tabs of that one session)
  80. static tagOf = (session) => { return session != null && session.data != null ? session.data.tag : null }

Branches

Latest commits

  • 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
  • 1f3198b6tests/cdp.mjs: HL_CPU_THROTTLE=<rate> optional CPU throttling for gate runsmre
  • 39cf9fdfre-vendor Hybriel 5406d146 (hl:web batches 9+10: #72 emits while the socket is closed delivered after reconnect, browser boot linear, faster list appends; contains int 6–14 + m406)mre
  • db07aa84tests: short-id-switch ports from env (NOTES_SWITCH_IDENT_PORT/_PORT), defaults unchangedmre
  • ef9f9feere-vendor Hybriel 6f8a0689 (int 6–11: streaming cursors, #203 index heal + close() fix, fd403 listDir/cursor fds, hl:time browser side, hl:web batches 5+6)mre
  • 5485467cdocker-compose: nofile 65536 (Hybriel int 6+ boot opened >1024 files in the tracker)mre
  • 86ea4a85deploy: backup copies index files first and *.mpack data last (hot-copy skew, hybriel #203)mre
  • edeab20fnotes mission 015: mission, report, LOG, STATUSmre
  • 542a0383notes mission 015: e2e-chrome E2E_HOST (own cookie jar in the shared Chrome)mre