gitoria

tickets

All repositories: gitoria

ReadmeCodePull requestsReleasesTicketsSettings
Commit506a4efd506a4efdmission 028: #94, identity rulemre506a4efd/lib/invites.hl

4.3 KB

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

Branches

Latest commits

  • 506a4efdmission 028: #94, identity rulemre
  • 78c7f48eRefine mpackdb holdmre
  • 3d7d89c8Record mpackdb hold until day38mre
  • f1bf7195#51 done; my subscription idmre
  • 9870b945status: #51 proof greenmre
  • e92173c6log: #51 live push proofmre
  • 72d87041log: Hermes push livemre
  • 1d69bf7dTickets conductor: park until Hermes pushmre
  • 80d8cfd1Tickets conductor: #51 livemre
  • 61641dd9Complete subscriptions deployment evidencemre
  • c980856cRecord successful subscriptions deployment and live checksmre
  • 4b4710adMerge branch 's51-subscriptions'mre
  • a731ef7eTickets conductor: S51 reviewed, deploy stepmre
  • b487b05aRecord verified direct push protocol handoffmre
  • 74e34cc3Verify encrypted push secrets and direct push retry semanticsmre
  • a1ac5cd0Push subscription notices directly to signed HyperConductor chat endpointsmre
  • d662ad27Remove superseded waker service for direct chat push protocolmre
  • c0ac927aTickets conductor: S51 redo for push protocolmre
  • ff2e8701Finalize subscription build verification and handoff reportmre
  • 07c6daeeTickets conductor: push allowlist URLsmre