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
@@ -0,0 +1,80 @@
|
||||
package domain
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
// ServiceMailState is what the sending worker reads right before it sends a
|
||||
// service mail that carries a one-time token: the token the delivery is
|
||||
// bound to and the account it is addressed to, as one consistent snapshot.
|
||||
// A zero time means "not set".
|
||||
//
|
||||
// It holds no raw token and no address in a loggable form: the account
|
||||
// address is only the comparison key, used to compare it with the address
|
||||
// stored in the delivery.
|
||||
type ServiceMailState struct {
|
||||
// TokenFound is false when the delivery has no token binding or the
|
||||
// token row does not exist.
|
||||
TokenFound bool
|
||||
TokenPurpose EmailTokenPurpose
|
||||
TokenUserID uuid.UUID
|
||||
TokenRegistrationVersion int64
|
||||
TokenConsumedAt time.Time
|
||||
TokenInvalidatedAt time.Time
|
||||
TokenExpiresAt time.Time
|
||||
AccountRegistrationVersion int64
|
||||
AccountEmailVerified bool
|
||||
// AccountEmailKey is the normalized address of the account now.
|
||||
AccountEmailKey string
|
||||
}
|
||||
|
||||
// CheckServiceMail decides whether a verification or password reset mail may
|
||||
// still be sent at the given time, and returns the reason when it may not.
|
||||
// An empty reason means that the mail is sendable. The checks run in this
|
||||
// order and the first failure decides:
|
||||
//
|
||||
// 1. a token is bound to the delivery and exists;
|
||||
// 2. its purpose is the one the mail kind carries, and it belongs to the
|
||||
// recipient account;
|
||||
// 3. it is not consumed, revoked, or expired;
|
||||
// 4. the account is current: it still has the registration the token was
|
||||
// issued for, and a verification mail is not sent to an address that is
|
||||
// verified already;
|
||||
// 5. the address stored in the delivery is still the address of the
|
||||
// account.
|
||||
//
|
||||
// Publishing consent (C2) and every other product consent play no part: a
|
||||
// service mail is part of the account flow, not a product notification.
|
||||
// A kind without a bearer token is refused with token_missing, because the
|
||||
// sending worker never claims it.
|
||||
//
|
||||
// The check is a snapshot: a token consumed or revoked after it, before the
|
||||
// provider call, still lets the mail go. That is harmless, because the link
|
||||
// is checked again when it is used.
|
||||
func (delivery Delivery) CheckServiceMail(state ServiceMailState, at time.Time) DeliveryCancelReason {
|
||||
purpose, carriesToken := delivery.Kind().TokenPurpose()
|
||||
switch {
|
||||
case !carriesToken || !state.TokenFound:
|
||||
return DeliveryCancelTokenMissing
|
||||
case state.TokenPurpose != purpose:
|
||||
return DeliveryCancelTokenPurposeMismatch
|
||||
case state.TokenUserID != delivery.recipientUserID:
|
||||
return DeliveryCancelTokenOwnerMismatch
|
||||
case !state.TokenConsumedAt.IsZero():
|
||||
return DeliveryCancelTokenConsumed
|
||||
case !state.TokenInvalidatedAt.IsZero():
|
||||
return DeliveryCancelTokenRevoked
|
||||
case !at.Before(state.TokenExpiresAt):
|
||||
return DeliveryCancelTokenExpired
|
||||
case state.TokenRegistrationVersion != state.AccountRegistrationVersion:
|
||||
return DeliveryCancelAccountNotCurrent
|
||||
case delivery.Kind() == MailKindVerification && state.AccountEmailVerified:
|
||||
return DeliveryCancelAccountNotCurrent
|
||||
case state.AccountEmailKey != delivery.recipient.Normalized():
|
||||
return DeliveryCancelRecipientMismatch
|
||||
default:
|
||||
return ""
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user