gitoria

gitoria

All repositories: gitoria

ReadmeCodePull requestsReleasesTicketsSettings
Commit63cfa21e63cfa21ereports/G2-repotabs.mdmre63cfa21e/lib/users.hl

6.5 KB

  1. // lib/users.hl — WHO MAY CREATE (ticket gitoria#6; CONCEPT.md "login via ident"). Reading is public;
  2. // creating a repo needs a login through ident. Login is ident's LOGIN BUTTON flow (ident README
  3. // "How apps use ident", way 2): <ident>/login?key=&return=<public url>/login/callback → ?ident_code=
  4. // → the server exchanges it (key + secret) for the per-app identity id. Way 3, the IDENTITY SELECTOR
  5. // (gitoria#14, ident README "The identity selector"), sits beside the button; ident checks its caller's origin
  6. // against the app's registered origins, and a repo address (<slug>.<domain>) is not one — so the selector shows
  7. // on the main address only (login.js) and the button works from every address (it returns through the main one).
  8. //
  9. // usersTable pk @id index !identity { identity, name, created } storage/mpackdb/users.db
  10. // identity = what ident's exchange answers: the identity's public SHORT id since ident#23 (`a68sz`; old 32-hex per-app
  11. // ids are rewritten once by tools/migrate-short-ids.hl) — stays SERVER SIDE, never sent to a page.
  12. // name = the display name asked once at the first login ('' until chosen); it is what
  13. // a repo shows as its owner.
  14. // The session (hl:web) carries `user = { id = <users @id> }` only.
  15. //
  16. // Config (environment, or `.env` beside project.hl — never read or printed by workers):
  17. // IDENT_URL, IDENT_EXCHANGE_URL, IDENT_API_KEY, IDENT_API_SECRET as in tickets
  18. // (GITORIA_PUBLIC_URL and GITORIA_STORAGE: lib/util.hl)
  19. import { MPackDB } from 'hl:mpackdb'
  20. import { now } from 'hl:time'
  21. import { fetch } from 'hl:fetch'
  22. import { envOr, storageDir, publicUrl, scheme, hostPort, firstOf, plainError, isHex } from './util.hl'
  23. static identUrl = envOr('IDENT_URL', 'https://ident.worldapi.org')
  24. static identExchangeUrl = envOr('IDENT_EXCHANGE_URL', identUrl)
  25. static identKey = envOr('IDENT_API_KEY', '')
  26. static identSecret = envOr('IDENT_API_SECRET', '')
  27. static usersTable = new MPackDB(file = storageDir + '/users.db', primaryKey = '@id', indexes = ['!identity'])
  28. static selectorScript = identUrl + '/selector.js'
  29. static callbackUrl = publicUrl.replaceAll('/', '') == '' ? '' : scheme + '://' + hostPort + '/login/callback'
  30. static loginHref = identUrl + '/login?key=' + identKey + '&return=' + encodeURIComponent(callbackUrl)
  31. // an identity id as ident answers it: its public SHORT ID since ident#23 (5 characters like `a68sz`: 2-9 and a-z),
  32. // before that the old per-app id (32 hex) — lower case letters and digits, at most 64 (mission 039; isHex refused `a68sz`)
  33. static isIdentId = (s) => {
  34. if (s == null || hlTypeName(s) != 'String' || s.length == 0 || s.length > 64) { return false }
  35. let i = 0
  36. while (i < s.length) {
  37. let c = s.charCodeAt(i)
  38. if (!((c >= 48 && c <= 57) || (c >= 97 && c <= 122))) { return false }
  39. i = i + 1
  40. }
  41. return true
  42. }
  43. // THE EXCHANGE: POST <ident>/api/exchange { key, secret, code } → { identity } | { error }
  44. // (a failed fetch is an `Error` event, absorbed by project.hl's `on Error`; the fetch then yields null)
  45. static exchangeCode = (code) => {
  46. if (identKey == '' || identSecret == '') { return { error = 'login is not set up on this server (IDENT_API_KEY / IDENT_API_SECRET missing)' } }
  47. if (!isHex(code, 200)) { return { error = 'that is not an ident login code' } }
  48. r = fetch(identExchangeUrl + '/api/exchange', { method = 'POST' json = { key = identKey secret = identSecret code = code } headers = { 'user-agent' = 'gitoria.worldapi.org (ident exchange)' } timeoutMs = 10000 })
  49. if (r == null || r.status == null || r.status == 0) { return { error = 'ident did not answer' } }
  50. j = r.status == 200 ? r.json() : null
  51. if (j == null || j.identity == null || !isIdentId(j.identity)) {
  52. let why = ''
  53. if (r.status != 200) {
  54. e = r.json()
  55. why = e != null && e.error != null ? ': ' + e.error : ''
  56. }
  57. return { error = 'ident refused the login (' + r.status + why + ')' }
  58. }
  59. return { identity = j.identity }
  60. }
  61. // ---- users --------------------------------------------------------------------------------
  62. // a face's trailing `session` is always the server's since hybriel #16 (a peer's extra argument is refused)
  63. static userRecord = (userId) => {
  64. if (userId == null || hlTypeName(userId) != 'String' || userId == '') { return null }
  65. return usersTable.fetch(userId)
  66. }
  67. // the user of an identity id, made at its first login (name '' = not chosen yet)
  68. static ensureUser = (identity) => {
  69. u = firstOf(usersTable.find('identity', identity))
  70. if (u != null) { return u }
  71. id = usersTable.put({ identity = identity name = '' created = now() })
  72. if (id == null) { return null }
  73. return usersTable.fetch(id)
  74. }
  75. // what a page may know about a user: NEVER the identity id
  76. static userInfo = (u) => { return { name = u.name named = u.name != '' } }
  77. static userOfSession = (session) => {
  78. if (session == null || session.user == null) { return null }
  79. return userRecord(session.user.id)
  80. }
  81. static infoOfSession = (session) => {
  82. u = userOfSession(session)
  83. return u == null ? null : userInfo(u)
  84. }
  85. // the display name of an owner, 'someone' for a user without one
  86. static nameOfUser = (userId) => {
  87. u = userRecord(userId)
  88. if (u == null || u.name == '') { return 'someone' }
  89. return u.name
  90. }
  91. // the display name: 1–60 characters, one line, asked ONCE
  92. static setUserName = (userId, name) => {
  93. u = userRecord(userId)
  94. if (u == null) { return { error = 'not logged in' } }
  95. if (u.name != '') { return { error = 'your display name is already set' } }
  96. bad = plainError(name, 60, 'the display name')
  97. if (bad != null) { return { error = bad } }
  98. n = name.trim()
  99. if (n == '') { return { error = 'the display name must not be empty' } }
  100. usersTable.update(u.id, { id = u.id identity = u.identity name = n created = u.created })
  101. after = usersTable.fetch(u.id)
  102. if (after == null || after.name != n) { return { error = 'could not store the name: ' + usersTable.lastError() } }
  103. return { user = after }
  104. }
  105. // A LOGIN (the login button's return, lib/api.hl loginCallback, and the selector's face gitoriaLogin, components/main.hl):
  106. // ident's one-time code → { user } (made at the first login) or { error }. The caller puts the user into the session.
  107. static userOfLoginCode = (code) => {
  108. x = exchangeCode(code)
  109. if (x.error != null) { return { error = x.error } }
  110. u = ensureUser(x.identity)
  111. if (u == null) { return { error = 'could not store the user' } }
  112. return { user = u }
  113. }
  114. // the random tag of a session's login (session.data.tag, set at login): `gitoriaSignedIn` / `gitoriaSignedOut` reach the
  115. // tabs of the session that carries it (project.hl audience)
  116. static tagOf = (session) => { return session != null && session.data != null ? session.data.tag : null }

Branches

Latest commits

  • 63cfa21ereports/G2-repotabs.mdmre
  • 9b0516fdgitoria#22: mark the tab underline functional (check-theme), chain E2E: serve/measure split, bring the lisbeth tab to frontmre
  • 19bc1df8gitoria#22: all repo pages under ONE layout level (components/repo.hl, hl:web D-CHAIN): a tab click replaces only the page in the slot; head and tab nav stay the same DOM nodesmre
  • 40658417tests/browser.mjs: wait for the page to be live (hydrated) before clicking Merge — a click on the server-rendered button before the client mounted was lost (flaky 'Clashing change' under load)mre
  • 48a6154bre-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
  • e972e8d7tests: short-id-switch (GITORIA_SWITCH_IDENT_PORT/_PORT) and ssh tunnel (GITORIA_GATE_TUNNEL_PORT) ports from env, defaults unchangedmre
  • f06f5349re-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
  • c7d3cc87docker-compose: nofile 65536 (Hybriel int 6+ boot opened >1024 files in the tracker)mre
  • f0e7ff8cdeploy: backup copies index files first and *.mpack data last (hot-copy skew, hybriel #203)mre
  • fe5f707cgitoria 010: report, mission, comment on why current = true staysmre
  • 9705ca86gitoria 010: gate: menu marker in the server HTMLmre
  • 8cff663agitoria 010: main-menu.hl from layouts 0bcc629 (route marker)mre
  • 359c79f7gitoria 010: re-vendor Hybriel master f9614007 (plugins whole, bin/ git-ignored)mre
  • 6bd7fb3cgitoria 009: theme re-copied from layouts 76f99f6 (menu marks the current page), current = true on the menu item, e2e marker checks, reportmre
  • 44de915cidentkit: refuse a taken ident port, cold-start deadline, abort on dead ident, clear no-code error (as notes)mre
  • 2bf5343egitoria 007: theme re-copied from layouts dbe3de7 (44 px taps), redundant tap overrides dropped, crumbs class, real-browser walk test, reportmre
  • 18843648deploy.sh: a gate without a 'passed,' line (check-theme) no longer ends the script silently (grep under pipefail) — print check-theme's summary, as layouts doesmre
  • e6dff0cbgitoria#23: report 005, STATUS, LOGmre
  • 7f992d62gitoria#23: phone first on the shared theme — brand, branch and pull links 44 px, readme code 14 px; gate block "phone first" (15 pages at 390 + the three dropdowns, fails 20 on old), dropdown-aware login/logout clicks, check-theme in GATESmre
  • a2a840a5gitoria#23 WIP (quota stop): theme from layouts, styles inherit it, shell on the theme's header/menus/sidebar, phone measurement script; gates not runmre