gitoria

ident

All repositories: gitoria

ReadmeCodePull requestsReleasesTicketsSettings
Commit4ca2ed204ca2ed20404 page in the shell (viewport, status 404, way home); phone: account actions live in the user menu so the open menu covers nothing; mobile gate; signoutall waits for a cold startmre4ca2ed20/lib/invites.hl

7.5 KB

  1. // lib/invites.hl — THE INVITE SERVICE (ticket ident#22). Statics only, the server realm.
  2. // An app (key + secret) asks ident for an INVITE for its project and role and gets a link
  3. // `<ident>/invite/<token>`. Whoever opens the link joins with ident: the login button's
  4. // own flow (lib/apps.hl openInvite / grantInvite) — email → code if signed out, the identity choice if
  5. // signed in — and ident sends the browser back to the app's `return` URL with `ident_code` (the identity)
  6. // AND `invite=<invite id>`. The app's server then asks POST /api/invites/get which identity
  7. // accepted the invite (compare it with the one the exchange gave). Concept: CONCEPT.md is
  8. // silent on invites; the ticket is the spec.
  9. // This file holds the invites table and every write to it; the checks: lib/invites-helpers.hl.
  10. //
  11. // invitesTable pk @id index @app, !hash
  12. // { app (app @id), hash = sha256(token), project, role, returnUrl, uses, acceptedBy
  13. // ('a b c': the identities' short ids that accepted it (invites accepted before ident#23 hold the old per-app id), space separated),
  14. // expires, revoked (0 or the time), mailedAt (0 or the time), created }
  15. //
  16. // Single use by default (`uses`, up to 1000), 7 days by default (`days`, up to 90). The link
  17. // (token) is shown once, in the answer of the create call; only its sha256 is stored.
  18. // A state is one of: revoked, used (all uses taken), expired, open.
  19. import { MPackDB } from 'hl:mpackdb'
  20. import { now } from 'hl:time'
  21. import { randomBytes, sha256 } from 'hl:crypto'
  22. import { env } from 'hl:proc'
  23. import { dir, envNumber, countOf, first, merged, validEmail, normEmail, isId, oldestFirst } from './util.hl'
  24. import { checkReturn } from './apps-helpers.hl'
  25. import { isWhole, checkText, acceptedList, usedCount, problemOf } from './invites-helpers.hl'
  26. import { identitiesTable } from './identities.hl'
  27. static invitesTable = new MPackDB(file = dir + '/invites.db', primaryKey = '@id', indexes = ['@app' '!hash'])
  28. static dayMs = envNumber('IDENT_INVITE_DAY_MS', 86400000) // a day (the gate shortens it)
  29. static defaultUses = 1
  30. static maxUses = 1000
  31. static defaultDays = 7
  32. static maxDays = 90
  33. // mails one app may send through ident per 24 h (an app must not turn ident into a mail cannon)
  34. static mailLimit = envNumber('IDENT_INVITE_MAIL_LIMIT', 50)
  35. static publicUrl = env('IDENT_PUBLIC_URL') != null && env('IDENT_PUBLIC_URL') != '' ? env('IDENT_PUBLIC_URL') : null
  36. // ---- state ----------------------------------------------------------------------------
  37. static stateOf = (rec) => {
  38. if (rec.revoked != null && rec.revoked > 0) { return 'revoked' }
  39. if (usedCount(rec) >= rec.uses) { return 'used' }
  40. if (rec.expires < now()) { return 'expired' }
  41. return 'open'
  42. }
  43. // what the app sees of an invite (never the token)
  44. static inviteRow = (rec) => {
  45. return { id = rec.id project = rec.project role = rec.role state = stateOf(rec) uses = rec.uses used = usedCount(rec) identities = acceptedList(rec) expires = rec.expires created = rec.created }
  46. }
  47. // ---- the app's calls -------------------------------------------------------------------
  48. static mailedLately = (appId) => {
  49. let n = 0
  50. t0 = now() - dayMs
  51. rows = invitesTable.find('app', appId)
  52. if (countOf(rows) > 0) { for (r of rows) { if (r.mailedAt != null && r.mailedAt > t0) { n = n + 1 } } }
  53. return n
  54. }
  55. // `base` is where the link points (the request's own origin when IDENT_PUBLIC_URL is unset).
  56. // answers { status, error } or { invite (row), url, mail (null | { to, days }) } — the caller mails
  57. static createInvite = (appRec, b, base) => {
  58. p = checkText(b.project, 'project')
  59. if (p.error != null) { return { status = 400 error = p.error } }
  60. r = checkText(b.role, 'role')
  61. if (r.error != null) { return { status = 400 error = r.error } }
  62. rt = checkReturn(b['return'], appRec)
  63. if (rt.error != null) { return { status = 400 error = rt.error } }
  64. let uses = b.uses == null ? defaultUses : b.uses
  65. if (!isWhole(uses) || uses < 1 || uses > maxUses) { return { status = 400 error = 'field uses must be a whole number from 1 to ' + maxUses } }
  66. let days = b.days == null ? defaultDays : b.days
  67. if (!isWhole(days) || days < 1 || days > maxDays) { return { status = 400 error = 'field days must be a whole number from 1 to ' + maxDays } }
  68. let to = null
  69. if (b.email != null) {
  70. to = normEmail(b.email)
  71. if (!validEmail(to)) { return { status = 400 error = 'field email is not an email address' } }
  72. if (mailedLately(appRec.id) >= mailLimit) { return { status = 429 error = 'too many invitation mails from this app in 24 hours' } }
  73. }
  74. token = randomBytes(16)
  75. t = now()
  76. id = invitesTable.put({ app = appRec.id hash = sha256(token) project = p.text role = r.text returnUrl = b['return'] uses = uses acceptedBy = '' expires = t + days * dayMs revoked = 0 mailedAt = to != null ? t : 0 created = t })
  77. return { invite = inviteRow(invitesTable.fetch(id)) url = base + '/invite/' + token mail = to != null ? { to = to days = days } : null }
  78. }
  79. // the invite `id` if the app owns it, else null
  80. static ownInvite = (appRec, id) => {
  81. if (!isId(id)) { return null }
  82. r = invitesTable.fetch(id)
  83. if (r == null || r.app != appRec.id) { return null }
  84. return r
  85. }
  86. // all of the app's invites, oldest first; `project` (optional) narrows it
  87. static listInvites = (appRec, project) => {
  88. out = []
  89. for (r of oldestFirst(invitesTable.find('app', appRec.id))) {
  90. if (project == null || r.project == project) { out.push(inviteRow(r)) }
  91. }
  92. return out
  93. }
  94. // answers { status, error } or { invite (row) }
  95. static revokeInvite = (appRec, id) => {
  96. r = ownInvite(appRec, id)
  97. if (r == null) { return { status = 404 error = 'no such invite' } }
  98. s = stateOf(r)
  99. if (s != 'open') { return { status = 409 error = 'the invite is not open (it is ' + s + ')' } }
  100. invitesTable.update(r.id, merged(r, { revoked = now() }))
  101. return { invite = inviteRow(invitesTable.fetch(r.id)) }
  102. }
  103. // DELETING AN APP (lib/apps.hl deleteApp) drops its invites with it
  104. static dropInvitesOf = (appId) => {
  105. invs = invitesTable.find('app', appId)
  106. if (countOf(invs) > 0) { for (v of invs) { invitesTable.delete(v.id) } }
  107. return null
  108. }
  109. // ---- the person's side (the login flow is lib/apps.hl openInvite / grantInvite) ---------------
  110. // GET /invite/<token>: answers { status, title, error } (an error page) or { invite (record) } — one that can
  111. // still be accepted
  112. static openableInvite = (token) => {
  113. rec = token == null || hlTypeName(token) != 'String' || token.length > 64 ? null : first(invitesTable.find('hash', sha256(token)))
  114. if (rec == null) { return { status = 404 title = 'Unknown invitation' error = 'This invitation link is not valid. Check that you copied all of it, or ask whoever invited you for a new one.' } }
  115. s = stateOf(rec)
  116. if (s != 'open') { return { status = 410 title = s == 'used' ? 'Invitation already used' : (s == 'expired' ? 'Invitation expired' : 'Invitation withdrawn') error = problemOf(s) } }
  117. return { invite = rec }
  118. }
  119. static inviteById = (id) => { return isId(id) ? invitesTable.fetch(id) : null }
  120. // THE IDENTITY ACCEPTS the invite `rec` (its connection `conn` to the app is made already). Answers { error } or
  121. // { ok }. An identity that already accepted this invite may pass again without taking another use.
  122. static acceptInvite = (rec, identityId, conn) => {
  123. list = acceptedList(rec)
  124. s = stateOf(rec)
  125. if (s == 'revoked') { return { error = problemOf(s) } }
  126. mine = identitiesTable.fetch(identityId).shortId
  127. if (!list.includes(mine) && !(conn.appIdentity != null && list.includes(conn.appIdentity))) {
  128. if (s != 'open') { return { error = problemOf(s) } }
  129. list.push(mine)
  130. invitesTable.update(rec.id, merged(rec, { acceptedBy = list.join(' ') }))
  131. }
  132. return { ok = true }
  133. }

Branches

Latest commits

  • 4ca2ed20404 page in the shell (viewport, status 404, way home); phone: account actions live in the user menu so the open menu covers nothing; mobile gate; signoutall waits for a cold startmre
  • 9baad745tests: browser gate waits for the reloaded document (offline reload race under CPU throttle); HL_CPU_THROTTLE knobmre
  • b96796bdtests: wait for hydration before real clicks after a navigation (H2)mre
  • 9722964fre-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
  • c9b196c9tests/iplimit: wait for the welcome form itself after B's first login (identities list renders before it); mission 017 reportmre
  • 72e0f587ident#26: identity page with "Used in" (app, since, last use, Notif. chip, empty state); lastLogin recorded at exchange; gates + lisbeth E2E; mission 016 reportmre
  • 94576fbcident#26 WIP: identity page /identity/:iid with "Used in" (app, since, last use, Notif. chip), lastLogin recorded at exchange, "Used in…" link on the identities list; apps gate 4b, e2e-usedin.mjsmre
  • 349cb502tests/invites: wait for hydration on the code page before entering the code (native GET submit raced the welcome form); mission 015 reportmre
  • d1bf4720tests: no hard-coded ports — every gate's ports and fixture origins come from <GATE>_PORT / _CHROME env (defaults unchanged; Bob/fixture origins follow, selector cookie name too)mre
  • 6280c92fre-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
  • 147744bftests/migration: the fixture app origin follows MIGRATION_GATE_TESTAPP_PORT (oldstore-009 OLD_TESTAPP)mre
  • 48cd36bctests/migration: ports from env (MIGRATION_GATE_PORT, MIGRATION_GATE_TESTAPP_PORT); 8395/8396 were taken by a foreign servermre
  • 3d9b2fd8docker-compose: nofile 65536 (Hybriel int 6+ boot opened >1024 files in the tracker)mre
  • 19b7e3c3deploy: backup copies index files first and *.mpack data last (hot-copy skew, hybriel #203)mre
  • c8a596f4re-vendor Hybriel 66c3db71 (web batch 4); e2e: main menu marked after in-place sign-in is a normal check; report 014mre
  • 8f3dc1ectests: delivery allow-list for the stopped server's network errors uses the gate port, not 8700mre
  • 9cc8eddftests: notify and iplimit gate Chrome ports from env (NOTIFY_GATE_CHROME, IPLIMIT_GATE_CHROME_A/B)mre
  • d6da2d02tests: delivery and migration gate ports from env (DELIVERY_GATE_PORT, DELIVERY_GATE_CHROME, MIGRATION_GATE_CHROME) — hard-coded 8700-8719 collided with a worker during deploymre
  • 7276a008ident mission 013: re-vendor Hybriel master f9614007 (#200, #202 mpackdb copy fix); gates and e2e rerunmre
  • e13e30c3ident mission 013: report, mission, repro, LOGmre