gitoria

ident

All repositories: gitoria

ReadmeCodePull requestsReleasesTicketsSettings
Commit51e387f251e387f2|||PR ident#40 the code step is one component (components/codestep.hl) placed by home.hl and embed.hl; its verify face, setCode, browserZone and verify call exist once (#40.2)mre51e387f2/lib/mail.hl

3.7 KB

  1. // lib/mail.hl — THE MAILER. One hl:smtp Mailer, configured from `.env` beside project.hl
  2. // (SMTP_HOST, SMTP_PORT, SMTP_USER, SMTP_PASSWORD, SMTP_FROM; the real environment
  3. // outranks the file). A send is non-blocking: the SMTP conversation runs on the
  4. // mailer's thread and its outcome arrives on the event loop (logged below).
  5. //
  6. // IDENT_MAIL_SINK names a file that receives the mail INSTEAD of the SMTP server: one
  7. // line `<address> <code>` is APPENDED per code (invitations: `<sink>.invites`, notification mails: `<sink>.mail`). The dev server and the gate use it, so
  8. // no real mail is sent until SMTP is configured for mail.byrod.in (see README).
  9. //
  10. // Handlers belong to instances: project.hl constructs this file once (`mail = new Mail()`).
  11. import { Mailer } from 'hl:smtp'
  12. import { envString } from './util.hl'
  13. import { writeFile, readFile, exists } from 'hl:fs'
  14. static NL = '
  15. '
  16. // An unconfigured dev setup gets the placeholder host 127.0.0.1 (the Mailer must exist:
  17. // `mailer.deliver()` and the `on mailer.…` handlers below); with the sink set nothing is ever
  18. // sent through it. SMTP_FROM's default is a real sender, not a placeholder — kept (mission 035).
  19. static smtpHost = envString('SMTP_HOST', null)
  20. static mailer = new Mailer(smtpHost != null ? smtpHost : '127.0.0.1', toNumber(envString('SMTP_PORT', '587')), {
  21. user = envString('SMTP_USER', null)
  22. pass = envString('SMTP_PASSWORD', null)
  23. sender = envString('SMTP_FROM', '[email protected]')
  24. })
  25. static sink = envString('IDENT_MAIL_SINK', null)
  26. static sendCode = (email, code) => {
  27. if (sink != null) {
  28. before = exists(sink) ? readFile(sink) : ''
  29. writeFile(sink, before + email + ' ' + code + NL)
  30. return 0
  31. }
  32. if (smtpHost == null) {
  33. console.warn('mail: NOT SENT to ' + email + ' — neither IDENT_MAIL_SINK nor SMTP_HOST is set')
  34. return 0
  35. }
  36. return mailer.send({
  37. to = email
  38. subject = 'Your ident login code: ' + code
  39. text = 'Hi,
  40. your one-time login code for worldapi is ' + code + '
  41. It is valid for 10 minutes and works once. If you did not ask for it, ignore this mail.
  42. '
  43. })
  44. }
  45. // AN INVITATION MAIL (ident#22). With the sink set, the line `<address> <link>` goes to
  46. // `<sink>.invites` (a second file: the code reader of the gates keeps its own format).
  47. static sendInvite = (email, link, appName, project, role, days) => {
  48. if (sink != null) {
  49. f = sink + '.invites'
  50. before = exists(f) ? readFile(f) : ''
  51. writeFile(f, before + email + ' ' + link + NL)
  52. return 0
  53. }
  54. if (smtpHost == null) {
  55. console.warn('mail: invitation NOT SENT to ' + email + ' — neither IDENT_MAIL_SINK nor SMTP_HOST is set')
  56. return 0
  57. }
  58. return mailer.send({
  59. to = email
  60. subject = 'You are invited to ' + project + ' (' + appName + ')'
  61. text = 'Hi,
  62. you are invited to ' + project + ' in ' + appName + ' (role: ' + role + ').
  63. Open this link to join with ident:
  64. ' + link + '
  65. The link works for ' + days + ' days. If you were not expecting it, ignore this mail.
  66. '
  67. })
  68. }
  69. // A NOTIFICATION MAIL (ident#7, lib/delivery.hl: the daily mail at 17:00 and urgent ones at once). With the sink set,
  70. // one JSON line `{ to, subject, text }` per mail goes to `<sink>.mail` (the gates read it; never real mail).
  71. static sendNotificationMail = (email, subject, text) => {
  72. if (sink != null) {
  73. f = sink + '.mail'
  74. before = exists(f) ? readFile(f) : ''
  75. writeFile(f, before + JSON.stringify({ to = email subject = subject text = text }) + NL)
  76. return 0
  77. }
  78. if (smtpHost == null) {
  79. console.warn('mail: notification mail NOT SENT to ' + email + ' — neither IDENT_MAIL_SINK nor SMTP_HOST is set')
  80. return 0
  81. }
  82. return mailer.send({ to = email subject = subject text = text })
  83. }
  84. mailer.deliver()
  85. on mailer.sent(r) { console.log('mail: sent', r.id, r.to, r.message) }
  86. on mailer.failed(r) { console.warn('mail: FAILED', r.id, r.to, r.stage, r.code, r.message) }

Branches

Latest commits

  • 51e387f2|||PR ident#40 the code step is one component (components/codestep.hl) placed by home.hl and embed.hl; its verify face, setCode, browserZone and verify call exist once (#40.2)mre
  • c28bddeeMerge branch 'f2-shell' into mainIdent
  • 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