gitoria

ident

All repositories: gitoria

ReadmeCodePull requestsReleasesTicketsSettings
Commitc3a40ff6c3a40ff6|||PR ident#40 one shell for the app and /start (account part off on /start, #40.1); startshell.hl removed; unused signOut faces gone (#40.4)mrec3a40ff6/lib/invites.hl

7.4 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 { dir, envNumber, envString, countOf, first, merged, validEmail, normEmail, isId, oldestFirst } from './util.hl'
  23. import { checkReturn } from './apps-helpers.hl'
  24. import { isWhole, checkText, acceptedList, usedCount, problemOf } from './invites-helpers.hl'
  25. import { identitiesTable } from './identities.hl'
  26. static invitesTable = new MPackDB(file = dir + '/invites.db', primaryKey = '@id', indexes = ['@app' '!hash'])
  27. static dayMs = envNumber('IDENT_INVITE_DAY_MS', 86400000) // a day (the gate shortens it)
  28. static defaultUses = 1
  29. static maxUses = 1000
  30. static defaultDays = 7
  31. static maxDays = 90
  32. // mails one app may send through ident per 24 h (an app must not turn ident into a mail cannon)
  33. static mailLimit = envNumber('IDENT_INVITE_MAIL_LIMIT', 50)
  34. static publicUrl = envString('IDENT_PUBLIC_URL', null)
  35. // ---- state ----------------------------------------------------------------------------
  36. static stateOf = (rec) => {
  37. if (rec.revoked != null && rec.revoked > 0) { return 'revoked' }
  38. if (usedCount(rec) >= rec.uses) { return 'used' }
  39. if (rec.expires < now()) { return 'expired' }
  40. return 'open'
  41. }
  42. // what the app sees of an invite (never the token)
  43. static inviteRow = (rec) => {
  44. 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 }
  45. }
  46. // ---- the app's calls -------------------------------------------------------------------
  47. static mailedLately = (appId) => {
  48. let n = 0
  49. t0 = now() - dayMs
  50. rows = invitesTable.find('app', appId)
  51. if (countOf(rows) > 0) { for (r of rows) { if (r.mailedAt != null && r.mailedAt > t0) { n = n + 1 } } }
  52. return n
  53. }
  54. // `base` is where the link points (the request's own origin when IDENT_PUBLIC_URL is unset).
  55. // answers { status, error } or { invite (row), url, mail (null | { to, days }) } — the caller mails
  56. static createInvite = (appRec, b, base) => {
  57. p = checkText(b.project, 'project')
  58. if (p.error != null) { return { status = 400 error = p.error } }
  59. r = checkText(b.role, 'role')
  60. if (r.error != null) { return { status = 400 error = r.error } }
  61. rt = checkReturn(b['return'], appRec)
  62. if (rt.error != null) { return { status = 400 error = rt.error } }
  63. let uses = b.uses == null ? defaultUses : b.uses
  64. if (!isWhole(uses) || uses < 1 || uses > maxUses) { return { status = 400 error = 'field uses must be a whole number from 1 to ' + maxUses } }
  65. let days = b.days == null ? defaultDays : b.days
  66. if (!isWhole(days) || days < 1 || days > maxDays) { return { status = 400 error = 'field days must be a whole number from 1 to ' + maxDays } }
  67. let to = null
  68. if (b.email != null) {
  69. to = normEmail(b.email)
  70. if (!validEmail(to)) { return { status = 400 error = 'field email is not an email address' } }
  71. if (mailedLately(appRec.id) >= mailLimit) { return { status = 429 error = 'too many invitation mails from this app in 24 hours' } }
  72. }
  73. token = randomBytes(16)
  74. t = now()
  75. 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 })
  76. return { invite = inviteRow(invitesTable.fetch(id)) url = base + '/invite/' + token mail = to != null ? { to = to days = days } : null }
  77. }
  78. // the invite `id` if the app owns it, else null
  79. static ownInvite = (appRec, id) => {
  80. if (!isId(id)) { return null }
  81. r = invitesTable.fetch(id)
  82. if (r == null || r.app != appRec.id) { return null }
  83. return r
  84. }
  85. // all of the app's invites, oldest first; `project` (optional) narrows it
  86. static listInvites = (appRec, project) => {
  87. out = []
  88. for (r of oldestFirst(invitesTable.find('app', appRec.id))) {
  89. if (project == null || r.project == project) { out.push(inviteRow(r)) }
  90. }
  91. return out
  92. }
  93. // answers { status, error } or { invite (row) }
  94. static revokeInvite = (appRec, id) => {
  95. r = ownInvite(appRec, id)
  96. if (r == null) { return { status = 404 error = 'no such invite' } }
  97. s = stateOf(r)
  98. if (s != 'open') { return { status = 409 error = 'the invite is not open (it is ' + s + ')' } }
  99. invitesTable.update(r.id, merged(r, { revoked = now() }))
  100. return { invite = inviteRow(invitesTable.fetch(r.id)) }
  101. }
  102. // DELETING AN APP (lib/apps.hl deleteApp) drops its invites with it
  103. static dropInvitesOf = (appId) => {
  104. invs = invitesTable.find('app', appId)
  105. if (countOf(invs) > 0) { for (v of invs) { invitesTable.delete(v.id) } }
  106. return null
  107. }
  108. // ---- the person's side (the login flow is lib/apps.hl openInvite / grantInvite) ---------------
  109. // GET /invite/<token>: answers { status, title, error } (an error page) or { invite (record) } — one that can
  110. // still be accepted
  111. static openableInvite = (token) => {
  112. rec = token == null || hlTypeName(token) != 'String' || token.length > 64 ? null : first(invitesTable.find('hash', sha256(token)))
  113. 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.' } }
  114. s = stateOf(rec)
  115. if (s != 'open') { return { status = 410 title = s == 'used' ? 'Invitation already used' : (s == 'expired' ? 'Invitation expired' : 'Invitation withdrawn') error = problemOf(s) } }
  116. return { invite = rec }
  117. }
  118. static inviteById = (id) => { return isId(id) ? invitesTable.fetch(id) : null }
  119. // THE IDENTITY ACCEPTS the invite `rec` (its connection `conn` to the app is made already). Answers { error } or
  120. // { ok }. An identity that already accepted this invite may pass again without taking another use.
  121. static acceptInvite = (rec, identityId, conn) => {
  122. list = acceptedList(rec)
  123. s = stateOf(rec)
  124. if (s == 'revoked') { return { error = problemOf(s) } }
  125. mine = identitiesTable.fetch(identityId).shortId
  126. if (!list.includes(mine) && !(conn.appIdentity != null && list.includes(conn.appIdentity))) {
  127. if (s != 'open') { return { error = problemOf(s) } }
  128. list.push(mine)
  129. invitesTable.update(rec.id, merged(rec, { acceptedBy = list.join(' ') }))
  130. }
  131. return { ok = true }
  132. }

Branches

Latest commits

  • c3a40ff6|||PR ident#40 one shell for the app and /start (account part off on /start, #40.1); startshell.hl removed; unused signOut faces gone (#40.4)mre
  • 060f3870ident#40.4: use the shell sign-out faces in regression gatesmre
  • 9b94aa6aMerge branch 'f1b-cleanup' into mainIdent
  • edcfce3f|||PR ident#41 ident#43 ident#44 login error page as a component (same status codes and text, pixel-identical), declarations and route wiring cleaned upmre
  • 3ac85995ident#43 ident#44: plain declarations for never-reassigned lets, one envString helper, named route functions and audience rule out of project.hlmre
  • c7eef14aMerge branch 'nv1-navigate' into mainIdent
  • 644f9775|||PR ident#36 navigate() instead of window.location in the shell, start shell and homemre
  • 4ca2f921deploy.sh: tar --hard-dereference so backups hold no hard linksmre
  • 23971fb7ident#49: media queries in the language's @media formmre
  • 73f4f97dident: re-vendor to hybriel day38b (mpackdb store-open fix)mre
  • aa056c03ident: a startup error prints its message before exit 1 (hybriel#247)mre
  • ee37a861ident#G48: gitoria's registration gets the wildcard origin of its repo addresses at load (gate tests/gitoria-origin.mjs)mre
  • dc6d486fident#27: the docs gate expects the new Access entry in the main menumre
  • d277633cident#27: README: identity access and the introspect endpointmre
  • e999b0d3ident#27: the Access tab — one key per identity for all its apps: give access in three steps, the overview matrix, a cell's sheet (since, last use, level, rotate, revoke), the app's check POST /api/introspect, agent identities hidden from the login picker, an inbox icon in the header's icon style (gate: tests/access.mjs)mre
  • 436206fcident#47: deploy.sh runs the sign-in gate on the deploy's own port slot, like the sessionwrites and docs gates (the default 8700 can be taken by another process)mre
  • a4887ea3ident#47: the selector's sign-in frame no longer has a Continue button; the identity row itself is the link (gate tests/signin.mjs fails on the old code)mre
  • d8ec703dident#45: /docs shows docs/IDENT.md (hl:markdown, vendored from ac1ed9da), one Docs entry in the main menu, README for developers only, gates tests/docs.mjs and tests/sessionwrites.mjs in deploy.shmre
  • ec6aad35Merge branch 'a28g-amp-callsites' into d45-docsmre
  • 6bfbd3f2ident#45: docs/IDENT.md, what ident is and how it works; CONCEPT.md marks one id per app as droppedmre