-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathemails.go
513 lines (448 loc) · 18.2 KB
/
emails.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
package emails
import (
"encoding/base64"
"fmt"
"html/template"
"net/url"
"os"
"path/filepath"
"strings"
"time"
"github.com/sendgrid/sendgrid-go/helpers/mail"
"github.com/trisacrypto/directory/pkg/utils/sentry"
)
// Compile all email templates into a top-level global variable.
var templates map[string]*template.Template
func init() {
templates = make(map[string]*template.Template)
for _, name := range AssetNames() {
data := MustAssetString(name)
templates[name] = template.Must(template.New(name).Parse(data))
}
}
//===========================================================================
// Template Contexts
//===========================================================================
const (
UnknownDate = "unknown date"
DateFormat = "Monday, January 2, 2006"
UnspecifiedOrganization = "Unknown Legal Name"
)
// VerifyContactData to complete the verify contact email templates.
type VerifyContactData struct {
Name string // Used to address the email
Token string // The unique token needed to verify the email
VID string // The ID of the VASP/Registration
BaseURL string // The URL of the verify contact endpoint to build the VerifyContactURL
DirectoryID string // The registered directory to build a URL accessible by the BFF
}
// VerifyContactURL composes the link to verify the contact from the context. If the
// link is not able to be composed, the function returns an empty string and logs an
// error because without the link the email is fairly useless.
func (d VerifyContactData) VerifyContactURL() *url.URL {
var (
link *url.URL
err error
)
if d.BaseURL != "" {
if link, err = url.Parse(d.BaseURL); err != nil {
sentry.Error(nil).Err(err).Msg("could not include verify contact link in email, could not parse verify contact base url")
return nil
}
} else {
sentry.Error(nil).Msg("could not include verify contact link in email, no verify contact base url")
return nil
}
params := link.Query()
params.Set("vaspID", d.VID)
params.Set("token", d.Token)
params.Set("registered_directory", d.DirectoryID)
link.RawQuery = params.Encode()
return link
}
func (d VerifyContactData) VerifyContactURLUnencoded() template.HTML {
url := d.VerifyContactURL()
return template.HTML(url.String())
}
// ReviewRequestData to complete review request email templates.
type ReviewRequestData struct {
VID string // The ID of the VASP/Registration
Token string // The unique token needed to review the registration
Request string // The review request data as a nicely formatted JSON or YAML string
RegisteredDirectory string // The directory name for the review request
Attachment []byte // Data to attach to the email
BaseURL string // The URL of the admin review endpoint to build the AdminReviewURL
}
// AdminReviewURL composes a link to the VASP detail in the admin UI. If the base url is
// missing or can't be parsed, it logs an warning and returns empty string.
// The AdminReviewURL is useful, but it is not a critical error.
func (d ReviewRequestData) AdminReviewURL() string {
var (
link *url.URL
err error
)
if d.BaseURL != "" {
if link, err = url.Parse(d.BaseURL); err != nil {
sentry.Warn(nil).Err(err).Msg("could not include admin review link in email, could not parse admin base url")
return ""
}
} else {
sentry.Warn(nil).Msg("could not include admin review link in email, no admin base url")
return ""
}
return link.ResolveReference(&url.URL{Path: d.VID}).String()
}
// RejectRegistrationData to complete reject registration email templates.
type RejectRegistrationData struct {
Name string // Used to address the email
VID string // The ID of the VASP/Registration
Organization string // The name of the organization (if it exists)
CommonName string // The common name assigned to the cert
RegisteredDirectory string // The directory name for the certificates being issued
Reason string // A description of why the registration request was rejected
}
// DeliverCertsData to complete deliver certs email templates.
type DeliverCertsData struct {
Name string // Used to address the email
VID string // The ID of the VASP/Registration
Organization string // The name of the organization (if it exists)
CommonName string // The common name assigned to the cert
SerialNumber string // The serial number of the certificate
Endpoint string // The expected endpoint for the TRISA service
RegisteredDirectory string // The directory name for the certificates being issued
}
// ExpiresAdminNotificationData to complete expires admin notification email templates.
type ExpiresAdminNotificationData struct {
VID string // The ID of the VASP/Registration
Organization string // The name of the organization (if it exists)
CommonName string // The common name assigned to the cert
SerialNumber string // The serial number of the certificate
Endpoint string // The expected endpoint for the TRISA service
RegisteredDirectory string // The directory name for the certificates being issued
Expiration time.Time // The timestamp that the certificates expire
Reissuance time.Time // The timestamp the certificates will be reissued
BaseURL string // The URL of the admin review endpoint to build the AdminReviewURL
}
// AdminReviewURL composes a link to the VASP detail in the admin UI. If the base url is
// missing or can't be parsed, it logs an warning and returns empty string.
// The AdminReviewURL is useful, but it is not a critical error.
func (d ExpiresAdminNotificationData) AdminReviewURL() string {
var (
link *url.URL
err error
)
if d.BaseURL != "" {
if link, err = url.Parse(d.BaseURL); err != nil {
sentry.Warn(nil).Err(err).Msg("could not include admin review link in email, could not parse admin base url")
return ""
}
} else {
sentry.Warn(nil).Msg("could not include admin review link in email, no admin base url")
return ""
}
return link.ResolveReference(&url.URL{Path: d.VID}).String()
}
// ExpirationDate formats the expiration date for rendering in the email.
func (d ExpiresAdminNotificationData) ExpirationDate() string {
if d.Expiration.IsZero() {
return UnknownDate
}
return d.Expiration.Format(DateFormat)
}
// ReissueDate formats the reissuance date for rendering in the email.
func (d ExpiresAdminNotificationData) ReissueDate() string {
if d.Reissuance.IsZero() {
return UnknownDate
}
return d.Reissuance.Format(DateFormat)
}
// ReissuanceReminderData to complete reissue reminder email templates.
type ReissuanceReminderData struct {
Name string // Used to address the email
VID string // The ID of the VASP/Registration
Organization string // The name of the organization (if it exists)
CommonName string // The common name assigned to the cert
SerialNumber string // The serial number of the certificate
Endpoint string // The expected endpoint for the TRISA service
RegisteredDirectory string // The directory name for the certificates being issued
Expiration time.Time // The timestamp that the certificates expire
Reissuance time.Time // The timestamp the certificates will be reissued
}
// ExpirationDate formats the expiration date for rendering in the email.
func (d ReissuanceReminderData) ExpirationDate() string {
if d.Expiration.IsZero() {
return UnknownDate
}
return d.Expiration.Format(DateFormat)
}
// ReissueDate formats the reissuance date for rendering in the email.
func (d ReissuanceReminderData) ReissueDate() string {
if d.Reissuance.IsZero() {
return UnknownDate
}
return d.Reissuance.Format(DateFormat)
}
// ReissuanceStartedData to complete reissue reminder email templates.
type ReissuanceStartedData struct {
Name string // Used to address the email
VID string // The ID of the VASP/Registration
Organization string // The name of the organization (if it exists)
CommonName string // The common name assigned to the cert
Endpoint string // The expected endpoint for the TRISA service
RegisteredDirectory string // The directory name for the certificates being issued
WhisperURL string // Secure one-time whisper link for password retrieval
}
// ReissuanceAdminNotificationData to complete reissuance admin notification email templates.
type ReissuanceAdminNotificationData struct {
VID string // The ID of the VASP/Registration
Organization string // The name of the organization (if it exists)
CommonName string // The common name assigned to the cert
SerialNumber string // The serial number of the certificate
Endpoint string // The expected endpoint for the TRISA service
RegisteredDirectory string // The directory name for the certificate that was issued
Expiration time.Time // The timestamp when the certificate expires
Reissuance time.Time // The timestamp when the certificate was reissued
BaseURL string // The URL of the admin review endpoint to build the AdminReviewURL
}
// AdminReviewURL composes a link to the VASP detail in the admin UI. If the base url is
// missing or can't be parsed, it logs an warning and returns empty string.
// The AdminReviewURL is useful, but it is not a critical error.
func (d ReissuanceAdminNotificationData) AdminReviewURL() string {
var (
link *url.URL
err error
)
if d.BaseURL != "" {
if link, err = url.Parse(d.BaseURL); err != nil {
sentry.Warn(nil).Err(err).Msg("could not include admin review link in email, could not parse admin base url")
return ""
}
} else {
sentry.Warn(nil).Msg("could not include admin review link in email, no admin base url")
return ""
}
return link.ResolveReference(&url.URL{Path: d.VID}).String()
}
// ExpirationDate formats the expiration date for rendering in the email.
func (d ReissuanceAdminNotificationData) ExpirationDate() string {
if d.Expiration.IsZero() {
return UnknownDate
}
return d.Expiration.Format(DateFormat)
}
// ReissueDate formats the reissuance date for rendering in the email.
func (d ReissuanceAdminNotificationData) ReissueDate() string {
if d.Reissuance.IsZero() {
return UnknownDate
}
return d.Reissuance.Format(DateFormat)
}
//===========================================================================
// Email Builders
//===========================================================================
// VerifyContactEmail creates a new verify contact email, ready for sending by rendering
// the text and html templates with the supplied data then constructing a sendgrid email.
func VerifyContactEmail(sender, senderEmail, recipient, recipientEmail string, data VerifyContactData) (message *mail.SGMailV3, err error) {
var text, html string
if text, html, err = Render("verify_contact", data); err != nil {
return nil, err
}
return mail.NewSingleEmail(
mail.NewEmail(sender, senderEmail),
VerifyContactRE,
mail.NewEmail(recipient, recipientEmail),
text,
html,
), nil
}
// ReviewRequestEmail creates a new review request email, ready for sending by rendering
// the text and html templates with the supplied data then constructing a sendgrid email.
func ReviewRequestEmail(sender, senderEmail, recipient, recipientEmail string, data ReviewRequestData) (message *mail.SGMailV3, err error) {
var text, html string
if text, html, err = Render("review_request", data); err != nil {
return nil, err
}
message = mail.NewSingleEmail(
mail.NewEmail(sender, senderEmail),
ReviewRequestRE,
mail.NewEmail(recipient, recipientEmail),
text,
html,
)
if err = AttachJSON(message, data.Attachment, fmt.Sprintf("%s.json", data.VID)); err != nil {
// Log the error but do not stop sending the message
sentry.Error(nil).Err(err).Msg("could not attach JSON data to review request email")
}
return message, nil
}
// RejectRegistrationEmail creates a new reject registration email, ready for sending by
// rendering the text and html templates with the supplied data then constructing a
// sendgrid email.
func RejectRegistrationEmail(sender, senderEmail, recipient, recipientEmail string, data RejectRegistrationData) (message *mail.SGMailV3, err error) {
var text, html string
if text, html, err = Render("reject_registration", data); err != nil {
return nil, err
}
return mail.NewSingleEmail(
mail.NewEmail(sender, senderEmail),
RejectRegistrationRE,
mail.NewEmail(recipient, recipientEmail),
text,
html,
), nil
}
// DeliverCertsEmail creates a new deliver certs email, ready for sending by rendering
// the text and html templates with the supplied data, loading the attachment from disk
// then constructing a sendgrid email.
func DeliverCertsEmail(sender, senderEmail, recipient, recipientEmail, attachmentPath string, data DeliverCertsData) (message *mail.SGMailV3, err error) {
var text, html string
if text, html, err = Render("deliver_certs", data); err != nil {
return nil, err
}
message = mail.NewSingleEmail(
mail.NewEmail(sender, senderEmail),
DeliverCertsRE,
mail.NewEmail(recipient, recipientEmail),
text,
html,
)
// Add attachment from a file on disk.
if err = LoadAttachment(message, attachmentPath); err != nil {
return nil, err
}
return message, nil
}
// ExpiresAdminNotificationEmail creates a new certs expired admin notification email,
// ready for sending by rendering the text and html templates with the supplied data.
func ExpiresAdminNotificationEmail(sender, senderEmail, recipient, recipientEmail string, data ExpiresAdminNotificationData) (message *mail.SGMailV3, err error) {
var text, html string
if text, html, err = Render("expires_admin_notification", data); err != nil {
return nil, err
}
message = mail.NewSingleEmail(
mail.NewEmail(sender, senderEmail),
ExpiresAdminNotificationRE,
mail.NewEmail(recipient, recipientEmail),
text,
html,
)
return message, nil
}
// ReissuanceReminderEmail creates a new reissuance reminder email, ready for sending by
// rendering the text and html templates with the supplied data.
func ReissuanceReminderEmail(sender, senderEmail, recipient, recipientEmail string, data ReissuanceReminderData) (message *mail.SGMailV3, err error) {
var text, html string
if text, html, err = Render("reissuance_reminder", data); err != nil {
return nil, err
}
message = mail.NewSingleEmail(
mail.NewEmail(sender, senderEmail),
ReissuanceReminderRE,
mail.NewEmail(recipient, recipientEmail),
text,
html,
)
return message, nil
}
// ReissuanceStartedEmail creates a new reissuance started email, ready for sending by
// rendering the text and html templates with the supplied data.
func ReissuanceStartedEmail(sender, senderEmail, recipient, recipientEmail string, data ReissuanceStartedData) (message *mail.SGMailV3, err error) {
var text, html string
if text, html, err = Render("reissuance_started", data); err != nil {
return nil, err
}
message = mail.NewSingleEmail(
mail.NewEmail(sender, senderEmail),
ReissuanceStartedRE,
mail.NewEmail(recipient, recipientEmail),
text,
html,
)
return message, nil
}
// ReissuanceAdminNotificationEmail creates a new certs reissuance admin notification email,
// ready for sending by rendering the text and html templates with the supplied data.
func ReissuanceAdminNotificationEmail(sender, senderEmail, recipient, recipientEmail string, data ReissuanceAdminNotificationData) (message *mail.SGMailV3, err error) {
var text, html string
if text, html, err = Render("reissuance_admin_notification", data); err != nil {
return nil, err
}
message = mail.NewSingleEmail(
mail.NewEmail(sender, senderEmail),
ReissuanceAdminNotificationRE,
mail.NewEmail(recipient, recipientEmail),
text,
html,
)
return message, nil
}
//===========================================================================
// Template Builders
//===========================================================================
// Render returns the text and html executed templates for the specified name and data.
// Ensure that the extension is not supplied to the render method.
func Render(name string, data interface{}) (text, html string, err error) {
if text, err = render(name+".txt", data); err != nil {
return "", "", err
}
if html, err = render(name+".html", data); err != nil {
return "", "", err
}
return text, html, nil
}
func render(name string, data interface{}) (_ string, err error) {
var (
ok bool
t *template.Template
)
if t, ok = templates[name]; !ok {
return "", fmt.Errorf("could not find %q in templates", name)
}
buf := &strings.Builder{}
if err = t.Execute(buf, data); err != nil {
return "", err
}
return buf.String(), nil
}
// LoadAttachment onto email from a file on disk.
func LoadAttachment(message *mail.SGMailV3, attachmentPath string) (err error) {
// Read and encode the attachment data
var data []byte
if data, err = os.ReadFile(attachmentPath); err != nil {
return err
}
encoded := base64.StdEncoding.EncodeToString(data)
// Create the attachment
// TODO: detect mimetype rather than assuming zip
attach := mail.NewAttachment()
attach.SetContent(encoded)
attach.SetType("application/zip")
attach.SetFilename(filepath.Base(attachmentPath))
attach.SetDisposition("attachment")
message.AddAttachment(attach)
return nil
}
// AttachJSON by marshaling the specified data into human-readable data and encode and
// attach it to the email as a file.
func AttachJSON(message *mail.SGMailV3, data []byte, filename string) (err error) {
// Encode the data to attach to the email
encoded := base64.StdEncoding.EncodeToString(data)
// Create the attachment
attach := mail.NewAttachment()
attach.SetContent(encoded)
attach.SetType("application/json")
attach.SetFilename(filename)
attach.SetDisposition("attachment")
message.AddAttachment(attach)
return nil
}
//===========================================================================
// Testing Interfaces
//===========================================================================
type AdminReview interface {
AdminReviewURL() string
}
type FutureReissuer interface {
ExpirationDate() string
ReissueDate() string
}