feat(identity): add server sessions and audited mail recovery
This commit is contained in:
1 parent
20c03288d6
commit
4ee53d9a74
58 files changed
+5359
-567
No files matched your search
@@ -141,7 +141,95 @@ func (repository deliveryRepository) Get(ctx context.Context, id uuid.UUID) (dom
|
||||
if err != nil {
|
||||
return domain.Delivery{}, fmt.Errorf("deliveryRepository.Get - select delivery: %w", mapDeliveryError(err))
|
||||
}
|
||||
return deliveryFromRow(row)
|
||||
return deliveryFromRow(sqlc.Delivery{
|
||||
ID: row.ID,
|
||||
SourceEventID: row.SourceEventID,
|
||||
RecipientUserID: row.RecipientUserID,
|
||||
RecipientEmail: row.RecipientEmail,
|
||||
Kind: row.Kind,
|
||||
TemplateVersion: row.TemplateVersion,
|
||||
Subject: row.Subject,
|
||||
TextBody: row.TextBody,
|
||||
HtmlBody: row.HtmlBody,
|
||||
ContentSha256: row.ContentSha256,
|
||||
Provider: row.Provider,
|
||||
ProviderAccount: row.ProviderAccount,
|
||||
IdempotencyKey: row.IdempotencyKey,
|
||||
Status: row.Status,
|
||||
AttemptCount: row.AttemptCount,
|
||||
NextAttemptAt: row.NextAttemptAt,
|
||||
LastAttemptAt: row.LastAttemptAt,
|
||||
ProviderMessageID: row.ProviderMessageID,
|
||||
Version: row.Version,
|
||||
CreatedAt: row.CreatedAt,
|
||||
UpdatedAt: row.UpdatedAt,
|
||||
EmailTokenID: row.EmailTokenID,
|
||||
CancelReason: row.CancelReason,
|
||||
ClaimOwner: row.ClaimOwner,
|
||||
ClaimedUntil: row.ClaimedUntil,
|
||||
FirstSendStartedAt: row.FirstSendStartedAt,
|
||||
FailureCount: row.FailureCount,
|
||||
BodyRedactedAt: row.BodyRedactedAt,
|
||||
})
|
||||
}
|
||||
|
||||
func (repository deliveryRepository) GetRetrySnapshot(ctx context.Context, id uuid.UUID) (outbound.DeliveryRetrySnapshot, error) {
|
||||
const location = "deliveryRepository.GetRetrySnapshot"
|
||||
row, err := repository.queries.GetDeliveryRetrySnapshot(ctx, pgUUID(id))
|
||||
if errors.Is(err, pgx.ErrNoRows) {
|
||||
return outbound.DeliveryRetrySnapshot{}, fmt.Errorf("%s - select delivery: %w", location, outbound.ErrDeliveryNotFound)
|
||||
}
|
||||
if err != nil {
|
||||
return outbound.DeliveryRetrySnapshot{}, fmt.Errorf("%s - select delivery: %w", location, mapDeliveryError(err))
|
||||
}
|
||||
if _, err := domain.NewMailProviderAccount(row.Provider, row.ProviderAccount); err != nil {
|
||||
return outbound.DeliveryRetrySnapshot{}, fmt.Errorf("%s - decode provider account: %w", location, err)
|
||||
}
|
||||
status, err := domain.ParseDeliveryStatus(row.Status)
|
||||
if err != nil {
|
||||
return outbound.DeliveryRetrySnapshot{}, fmt.Errorf("%s - decode status: %w", location, err)
|
||||
}
|
||||
return outbound.DeliveryRetrySnapshot{
|
||||
ID: uuid.UUID(row.ID.Bytes),
|
||||
Provider: row.Provider,
|
||||
ProviderAccount: row.ProviderAccount,
|
||||
Status: status,
|
||||
AttemptCount: row.AttemptCount,
|
||||
NextAttemptAt: zeroableTime(row.NextAttemptAt),
|
||||
FirstSendStartedAt: zeroableTime(row.FirstSendStartedAt),
|
||||
ManualRetryStartedAt: zeroableTime(row.ManualRetryStartedAt),
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (repository deliveryRepository) AuthorizeUnknownRetry(
|
||||
ctx context.Context,
|
||||
id uuid.UUID,
|
||||
account domain.MailProviderAccount,
|
||||
actor domain.MailRetryActor,
|
||||
acknowledgeDuplicateRisk bool,
|
||||
at time.Time,
|
||||
minimumAge time.Duration,
|
||||
) error {
|
||||
const location = "deliveryRepository.AuthorizeUnknownRetry"
|
||||
if id == uuid.Nil || account.IsZero() || actor == "" || !acknowledgeDuplicateRisk || at.IsZero() || minimumAge < 0 {
|
||||
return fmt.Errorf("%s - invalid authorization request: %w", location, outbound.ErrDeliveryRetryNotAllowed)
|
||||
}
|
||||
updated, err := repository.queries.AuthorizeUnknownDeliveryRetry(ctx, sqlc.AuthorizeUnknownDeliveryRetryParams{
|
||||
Actor: string(actor),
|
||||
DuplicateRiskAcknowledged: acknowledgeDuplicateRisk,
|
||||
DeliveryID: pgUUID(id),
|
||||
Provider: account.Provider(),
|
||||
ProviderAccount: account.Account(),
|
||||
MinimumAgeSeconds: minimumAge.Seconds(),
|
||||
Now: pgTime(at),
|
||||
})
|
||||
if err != nil {
|
||||
return fmt.Errorf("%s - authorize retry: %w", location, mapDeliveryError(err))
|
||||
}
|
||||
if updated == 0 {
|
||||
return fmt.Errorf("%s - current delivery state is not eligible: %w", location, outbound.ErrDeliveryRetryNotAllowed)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (repository deliveryRepository) ListAttempts(ctx context.Context, id uuid.UUID) ([]domain.DeliveryAttempt, error) {
|
||||
@@ -265,6 +353,33 @@ func (repository deliveryRepository) Cancel(ctx context.Context, next domain.Del
|
||||
return repository.updateState(ctx, "deliveryRepository.Cancel", next, true)
|
||||
}
|
||||
|
||||
func (repository deliveryRepository) StopUnknownRetry(ctx context.Context, owner uuid.UUID, next domain.Delivery) error {
|
||||
const location = "deliveryRepository.StopUnknownRetry"
|
||||
if next.IsZero() || next.Status() != domain.DeliveryStatusUnknown || !next.NextAttemptAt().IsZero() || owner == uuid.Nil || next.Version() <= 1 {
|
||||
return fmt.Errorf("%s - state was not built by domain.Delivery.StopAutomaticRetry: %w", location, domain.ErrInvalidDelivery)
|
||||
}
|
||||
updated, err := repository.queries.StopUnknownDeliveryRetry(ctx, sqlc.StopUnknownDeliveryRetryParams{
|
||||
Now: pgTime(next.UpdatedAt()),
|
||||
ID: pgUUID(next.ID()),
|
||||
LeaseOwner: pgUUID(owner),
|
||||
})
|
||||
if err != nil {
|
||||
return fmt.Errorf("%s - stop retry: %w", location, mapDeliveryError(err))
|
||||
}
|
||||
if updated == 0 {
|
||||
return fmt.Errorf("%s - expected version %d: %w", location, next.Version()-1, outbound.ErrDeliveryVersionConflict)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (repository deliveryRepository) ScheduleUnknownRetry(ctx context.Context, owner uuid.UUID, next domain.Delivery) error {
|
||||
const location = "deliveryRepository.ScheduleUnknownRetry"
|
||||
if owner == uuid.Nil || next.IsZero() || next.Status() != domain.DeliveryStatusUnknown || next.NextAttemptAt().IsZero() || next.Version() <= 1 {
|
||||
return fmt.Errorf("%s - state was not built by domain.Delivery.ScheduleUnknownRetry: %w", location, domain.ErrInvalidDelivery)
|
||||
}
|
||||
return repository.updateState(ctx, location, next, true)
|
||||
}
|
||||
|
||||
// updateState stores a state change without an attempt over the version
|
||||
// before it. releaseClaim ends the sending worker's lease with the change.
|
||||
func (repository deliveryRepository) updateState(ctx context.Context, location string, next domain.Delivery, releaseClaim bool) error {
|
||||
@@ -363,9 +478,13 @@ func (repository deliveryRepository) ClaimDue(ctx context.Context, claim outboun
|
||||
return outbound.ClaimedDelivery{}, fmt.Errorf("%s - decode claimed delivery: %w", location, unreadable)
|
||||
}
|
||||
claimed := outbound.ClaimedDelivery{
|
||||
Delivery: delivery,
|
||||
PreviousClaimedUntil: zeroableTime(row.PreviousClaimedUntil),
|
||||
FailureCount: row.FailureCount,
|
||||
Delivery: delivery,
|
||||
PreviousClaimedUntil: zeroableTime(row.PreviousClaimedUntil),
|
||||
ManualRetryPending: row.ManualRetryPending.Bool,
|
||||
ManualRetryCycleStarted: row.ManualRetryCycleStarted,
|
||||
SendStartUnsettled: row.SendStartUnsettled,
|
||||
HasUncertainAttempt: row.HasUncertainAttempt,
|
||||
FailureCount: row.FailureCount,
|
||||
}
|
||||
if row.SinceFirstSendSeconds >= 0 {
|
||||
claimed.FirstSendStarted = true
|
||||
@@ -385,11 +504,17 @@ func (repository deliveryRepository) LockClaimed(ctx context.Context, id, owner
|
||||
return true, nil
|
||||
}
|
||||
|
||||
func (repository deliveryRepository) MarkSendStarted(ctx context.Context, id, owner uuid.UUID) error {
|
||||
if _, err := repository.queries.MarkDeliverySendStarted(ctx, sqlc.MarkDeliverySendStartedParams{ID: pgUUID(id), LeaseOwner: pgUUID(owner)}); err != nil {
|
||||
return fmt.Errorf("deliveryRepository.MarkSendStarted - mark send started: %w", mapDeliveryError(err))
|
||||
func (repository deliveryRepository) MarkSendStarted(ctx context.Context, id, owner uuid.UUID, requireDedupWindow bool, safeWindow time.Duration) (bool, error) {
|
||||
started, err := repository.queries.MarkDeliverySendStarted(ctx, sqlc.MarkDeliverySendStartedParams{
|
||||
ID: pgUUID(id),
|
||||
LeaseOwner: pgUUID(owner),
|
||||
RequireDedupWindow: requireDedupWindow,
|
||||
RetryWindowSeconds: safeWindow.Seconds(),
|
||||
})
|
||||
if err != nil {
|
||||
return false, fmt.Errorf("deliveryRepository.MarkSendStarted - mark send started: %w", mapDeliveryError(err))
|
||||
}
|
||||
return nil
|
||||
return started == 1, nil
|
||||
}
|
||||
|
||||
func (repository deliveryRepository) RecordFailure(ctx context.Context, id, owner uuid.UUID, at, nextAttemptAt time.Time, maxFailures int32, countFailure bool) (bool, error) {
|
||||
|
||||
Reference in new issue
Block a user