feat(mail): send service identity mail through a leased worker
This commit is contained in:
1 parent
204c6bae62
commit
d21fd49a42
58 files changed
+7577
-164
No files matched your search
@@ -57,12 +57,39 @@ func ParseMailKind(value string) (MailKind, error) {
|
||||
}
|
||||
}
|
||||
|
||||
// ServiceMailKinds lists the kinds of service identity mail, the kinds the
|
||||
// sending worker claims and sends. Notifications are not among them until
|
||||
// their sending is connected (TASK-462). The delivery metrics of due mail,
|
||||
// the sending worker, and the due index of the deliveries table follow this
|
||||
// list; changing it changes the predicate of deliveries_due_idx too.
|
||||
func ServiceMailKinds() []MailKind {
|
||||
return []MailKind{MailKindVerification, MailKindPasswordReset}
|
||||
}
|
||||
|
||||
// IsServiceMail reports a kind of service identity mail.
|
||||
func (kind MailKind) IsServiceMail() bool { return kind.CarriesBearerToken() }
|
||||
|
||||
// CarriesBearerToken reports kinds whose body holds a one-time bearer token
|
||||
// in a link. Their stored bodies are secrets until the token expires.
|
||||
func (kind MailKind) CarriesBearerToken() bool {
|
||||
return kind == MailKindVerification || kind == MailKindPasswordReset
|
||||
}
|
||||
|
||||
// TokenPurpose returns the email token purpose that the link of a mail of
|
||||
// this kind carries, and false for a kind without a bearer token.
|
||||
func (kind MailKind) TokenPurpose() (EmailTokenPurpose, bool) {
|
||||
switch kind {
|
||||
case MailKindVerification:
|
||||
return EmailTokenPurposeEmailVerification, true
|
||||
case MailKindPasswordReset:
|
||||
return EmailTokenPurposePasswordReset, true
|
||||
case MailKindNotification:
|
||||
return "", false
|
||||
default:
|
||||
return "", false
|
||||
}
|
||||
}
|
||||
|
||||
// MailContentHash is SHA-256 over the template version, subject, text part,
|
||||
// and HTML part, each encoded as a netstring ("<len>:<part>,"), so moving
|
||||
// bytes from one part to the next changes the digest. The mail templates and
|
||||
@@ -98,6 +125,9 @@ type mailContentValue struct {
|
||||
text string
|
||||
html string
|
||||
hash [MailContentHashSize]byte
|
||||
// redacted is set for the content of a terminal token mail whose bodies
|
||||
// were cleared; only the subject and the hash remain.
|
||||
redacted bool
|
||||
}
|
||||
|
||||
// NewMailContent validates a rendering and computes its hash. The template
|
||||
@@ -134,6 +164,30 @@ func RestoreMailContent(kind MailKind, templateVersion, subject, text, html stri
|
||||
return content, nil
|
||||
}
|
||||
|
||||
// RestoreRedactedMailContent rebuilds the content of a token mail whose bodies
|
||||
// were cleared after it reached a terminal status. Nothing can be checked
|
||||
// against the hash any more, so the stored hash is kept as it is; the content
|
||||
// has no bodies and can never be sent.
|
||||
func RestoreRedactedMailContent(kind MailKind, templateVersion, subject string, hash []byte) (MailContent, error) {
|
||||
const location = "domain.RestoreRedactedMailContent"
|
||||
if _, err := ParseMailKind(string(kind)); err != nil || !kind.CarriesBearerToken() {
|
||||
return MailContent{}, invalidMailContent("only a token mail has redacted content")
|
||||
}
|
||||
if !mailTemplateVersionPattern.MatchString(templateVersion) || !strings.HasPrefix(templateVersion, string(kind)+".v") ||
|
||||
strings.TrimSpace(subject) == "" || utf8.RuneCountInString(subject) > maxMailSubjectRunes {
|
||||
return MailContent{}, fmt.Errorf("%s - stored version or subject is invalid: %w", location, ErrInvalidMailContent)
|
||||
}
|
||||
if len(hash) != MailContentHashSize {
|
||||
return MailContent{}, fmt.Errorf("%s - stored hash has the wrong size: %w", location, ErrInvalidMailContent)
|
||||
}
|
||||
value := &mailContentValue{kind: kind, templateVersion: templateVersion, subject: subject, redacted: true}
|
||||
copy(value.hash[:], hash)
|
||||
return MailContent{value: value}, nil
|
||||
}
|
||||
|
||||
// Redacted reports content whose bodies were cleared after the mail ended.
|
||||
func (content MailContent) Redacted() bool { return content.read().redacted }
|
||||
|
||||
func validateMailContent(kind MailKind, templateVersion, subject, text, html string) error {
|
||||
switch {
|
||||
case !mailTemplateVersionPattern.MatchString(templateVersion) || !strings.HasPrefix(templateVersion, string(kind)+".v"):
|
||||
|
||||
Reference in new issue
Block a user