gitoria

tickets

All repositories: gitoria

ReadmeCodePull requestsReleasesTicketsSettings
Branchk40-lists9af8e780|||PR Use keyed list mutations for live ticket updatesmrek40-lists/lib/invites.hl

5.0 KB

  1. import { writerOfSession } from './users.hl'
  2. import { projectRecord } from './projects.hl'
  3. // lib/invites.hl — PEOPLE JOIN A PROJECT THROUGH IDENT'S INVITE SERVICE (ticket #20; ident README "Invites",
  4. // ident#22). An admin asks ident for an invite for one project and a role (mayInvite, createInvite) and shows the
  5. // link; whoever opens it and picks an identity is sent back to /login/callback?ident_code=…&invite=<invite id>.
  6. // lib/api.hl loginCallback then exchanges the code (users.hl), and joinByInvite asks ident who ACCEPTED the invite
  7. // (`identities`) and only then makes that person a member. Statics only, the server realm. A failed fetch is
  8. // absorbed by project.hl's `on Error` (the call then yields null → "ident did not answer").
  9. import { fetch } from 'hl:fetch'
  10. import { identKey, identSecret, identExchangeUrl, callbackUrl } from './users.hl'
  11. import { projectByRef, isAdminOf, roles, addMember } from './projects.hl'
  12. static ua = { 'user-agent' = 'tickets.worldapi.org (ident invites)' }
  13. // only letters, digits and dashes (ident's invite ids)
  14. static isInviteId = (s) => {
  15. if (s == null || hlTypeName(s) != 'String' || s.length == 0 || s.length > 100) { return false }
  16. let i = 0
  17. while (i < s.length) {
  18. c = s.charCodeAt(i)
  19. if (!((c >= 48 && c <= 57) || (c >= 97 && c <= 122) || (c >= 65 && c <= 90) || c == 45 || c == 95)) { return false }
  20. i = i + 1
  21. }
  22. return true
  23. }
  24. static identCall = (path, body) => {
  25. if (identKey == '' || identSecret == '') { return { error = 'invites are not set up on this server (IDENT_API_KEY / IDENT_API_SECRET missing)' } }
  26. let json = { key = identKey secret = identSecret }
  27. for (k of body.keys()) { json[k] = body[k] }
  28. r = fetch(identExchangeUrl + path, { method = 'POST' json = json headers = ua timeoutMs = 10000 })
  29. if (r == null || r.status == null || r.status == 0) { return { error = 'ident did not answer' } }
  30. j = r.json()
  31. if (r.status != 200) { return { error = 'ident refused (' + r.status + (j != null && j.error != null ? ': ' + j.error : '') + ')' } }
  32. if (j == null) { return { error = 'ident answered nothing readable' } }
  33. return { body = j }
  34. }
  35. // may this user invite to project `p` with `role`? null, or the refusal { error, field, forbidden? }
  36. // (the caller then checks its numbers and calls createInvite)
  37. static mayInvite = (p, user, role) => {
  38. if (!isAdminOf(p.id, user)) { return { error = 'only an admin of the project can invite' field = '' forbidden = true } }
  39. if (!roles.includes(role)) { return { error = 'role must be one of: ' + roles.join(', ') field = 'role' } }
  40. return null
  41. }
  42. // an invite link for `projectId` and `role`: { url, id, expires } | { error }. `email` (optional) makes ident mail it.
  43. static createInvite = (projectId, role, uses, days, email) => {
  44. body = { project = projectId role = role return = callbackUrl uses = uses days = days }
  45. if (email != null && email != '') { body.email = email }
  46. r = identCall('/api/invites', body)
  47. if (r.error != null) { return r }
  48. j = r.body
  49. if (j.url == null || j.id == null) { return { error = 'ident answered no invite link' } }
  50. return { url = j.url id = j.id expires = j.expires mailed = j.mailed == true }
  51. }
  52. // did this identity accept this invite? { project (id), role } | { error }
  53. static acceptedInvite = (inviteId, identity) => {
  54. if (!isInviteId(inviteId)) { return { error = 'that is not an invite id' } }
  55. r = identCall('/api/invites/get', { id = inviteId })
  56. if (r.error != null) { return r }
  57. inv = r.body.invite
  58. if (inv == null || inv.project == null || inv.role == null) { return { error = 'ident answered no invite' } }
  59. let ok = false
  60. if (inv.identities != null && countOfIds(inv.identities) > 0) { for (i of inv.identities) { if (i == identity) { ok = true } } }
  61. if (!ok) { return { error = 'this login did not accept that invite' } }
  62. return { project = inv.project role = inv.role }
  63. }
  64. static countOfIds = (list) => {
  65. n = list.length
  66. return n == null ? 0 : n
  67. }
  68. // THE INVITED PERSON JOINS (the login callback brought `invite=<id>`): only when ident says this identity accepted
  69. // the invite does the user become a member, with the invite's role. Answers { project (record) } or { error }
  70. static joinByInvite = (inviteId, identity, userId) => {
  71. a = acceptedInvite(inviteId, identity)
  72. if (a.error != null) { return a }
  73. jp = projectByRef(a.project)
  74. if (jp == null || !roles.includes(a.role)) { return { error = 'the invite is for a project or role that does not exist here' } }
  75. addMember(jp.id, userId, a.role)
  76. return { project = jp }
  77. }
  78. static inviteForSession = (projectId, role, uses, days, email, session) => {
  79. w = writerOfSession(session)
  80. if (w.error != null) { return { error = w.error } }
  81. p = projectRecord('' + projectId)
  82. if (p == null) { return { error = 'no such project' } }
  83. no = mayInvite(p, w.user, '' + role)
  84. if (no != null) { return { error = no.error } }
  85. n = toNumber('' + uses)
  86. d = toNumber('' + days)
  87. if (n == null || n < 1 || n > 1000) { return { error = 'the number of people must be from 1 to 1000' } }
  88. if (d == null || d < 1 || d > 90) { return { error = 'the days must be from 1 to 90' } }
  89. return createInvite(p.id, '' + role, n, d, '' + email)
  90. }

Branches

Latest commits

  • 9af8e780|||PR Use keyed list mutations for live ticket updatesmre
  • 610d8e8aMerge branch 'records-t39' into mainTickets
  • 32bcdf24|||PR records: GO P39 K40mre
  • 4123c6d1Merge branch 'records-t38' into mainTickets
  • cd3641a1|||PR records: day72 deploy live, tickets closedmre
  • 7d44d852Merge branch 'records-t37' into mainTickets
  • 926f7061|||PR records: merges, D36 deploymre
  • bf58534eMerge branch 'l33-final' into mainTickets
  • 759a9ac3|||PR thin login (tickets#46, #45): merge main, route entry cleanupmre
  • 239436d8Merge remote-tracking branch 'origin/main' into l33-finalmre
  • 5822cb3aMerge branch 'o34-clean' into mainTickets
  • 0870653c|||PR offline note from __hlOnline, no polling (tickets#41)mre
  • 14cf96b4Merge branch 'records-t36' into mainTickets
  • 07749fa1|||PR records: day72 + #40 mergedmre
  • ecd7e226|||PR Consolidate login and move handler logic into topic libraries (partial)mre
  • 6dea1c60Merge branch 'r35-day72' into mainTickets
  • 7bcfdc22|||PR drop worker edits to conductor recordsmre
  • 2617b6ee|||PR dialogs: host flag cleared on close, dialog gate (tickets#40)mre
  • 6e701371|||PR record partial day72 dialog validationmre
  • b9fb9a91|||PR bind ticket dialog visibility to member flagsmre