feat(identity): add server sessions and audited mail recovery

This commit is contained in:
TakuyaYagam1 committed 2026-10-07 11:03:46 +03:00
1 parent 20c03288d6
commit 4ee53d9a74
58 files changed
+5359 -567

No files matched your search

@@ -28,6 +28,11 @@ type Querier interface {
// ends every earlier token, and deletes the registration C1 of the earlier
// registrant like UpdateUserRegistration does.
AdvanceUserRegistration(ctx context.Context, arg AdvanceUserRegistrationParams) (AdvanceUserRegistrationRow, error)
// Authorizes one retry of a terminal unknown delivery. The update and append-
// only decision share one statement and lock the current row, so a second or
// concurrent operator decision cannot schedule another send for the same
// unknown attempt. The provider account must still match the delivery's pin.
AuthorizeUnknownDeliveryRetry(ctx context.Context, arg AuthorizeUnknownDeliveryRetryParams) (int64, error)
// Cancels a job that has not finished. A job whose handler transaction is
// in progress holds its row lock, so the statement waits for it: after the
// handler committed the job is done and nothing is canceled; otherwise the
@@ -40,7 +45,7 @@ type Querier interface {
// application clock.
CancelUnreadableDelivery(ctx context.Context, arg CancelUnreadableDeliveryParams) (int64, error)
// Claims the most overdue delivery that the sending worker may send, in one
// statement: a pending or waiting_quota delivery of the service mail kinds
// statement: a pending, waiting_quota, or scheduled unknown delivery of the service mail kinds
// (the literals match the predicate of deliveries_due_idx, so an idle poll
// reads only that small index) whose next_attempt_at has passed, pinned to the
// configured provider account (or to rebind_provider before its first
@@ -54,10 +59,15 @@ type Querier interface {
// stay on the application clock that wrote them. Neither the version nor
// updated_at changes: a claim is not a change of the delivery.
// previous_claimed_until is the deadline of an abandoned lease, NULL for a
// delivery nobody held; since_first_send_seconds is how long ago, by the
// database clock, its first request started, -1 when none did. now is the
// application clock. Deliveries that are unknown or wait for an unknown quota
// reset have no due time here and wait for an operator.
// delivery nobody held; since_first_send_seconds always measures from the
// original first request by the database clock, and is -1 when none started.
// manual_retry_pending is true until the first provider call of the latest
// manual authorization starts. send_start_unsettled means a provider call
// started at the current attempt_count but no result was recorded afterward.
// has_uncertain_attempt keeps an earlier unknown result visible even if a
// later confirmed refusal moved the current delivery back to pending.
// now is the application clock. Unknown deliveries without a scheduled retry
// and quota waits with an unknown reset wait for an operator.
ClaimDueDelivery(ctx context.Context, arg ClaimDueDeliveryParams) (ClaimDueDeliveryRow, error)
// Claims up to batch_limit jobs of the listed kinds in one statement: jobs
// whose available_at has passed, and running jobs whose lease expired
@@ -163,7 +173,10 @@ type Querier interface {
// The database clock, which stamps the effective time of a policy version, so
// "the version in force now" is judged against the same clock.
GetDatabaseTime(ctx context.Context) (pgtype.Timestamptz, error)
GetDeliveryByID(ctx context.Context, id pgtype.UUID) (Delivery, error)
GetDeliveryByID(ctx context.Context, id pgtype.UUID) (GetDeliveryByIDRow, error)
// Reads only the metadata needed by the local operator retry interface. It
// intentionally does not select the address, token, subject, or message body.
GetDeliveryRetrySnapshot(ctx context.Context, id pgtype.UUID) (GetDeliveryRetrySnapshotRow, error)
// Aggregates the deliveries by kind and status for the metrics, at most once
// per metrics interval. It reads only kind, status, and next_attempt_at,
// never an address, a body, or an ID. No index covers status, so it scans
@@ -406,11 +419,13 @@ type Querier interface {
LockPendingOutboxEvents(ctx context.Context, batchLimit int32) ([]LockPendingOutboxEventsRow, error)
// Locks the active registration C1 while its pending registration is applied.
LockRegistrationConsentRecord(ctx context.Context, arg LockRegistrationConsentRecordParams) (LockRegistrationConsentRecordRow, error)
// Notes that the first request of a claimed delivery is about to start, by the
// database clock, and clears the count of worker failures: the claim and the
// preparation succeeded, so the failures are no longer consecutive. The provider keeps the idempotency key for a window counted
// from this moment, so a later claim can tell whether repeating the request is
// still safe. It never moves.
// Notes that a request is about to start, by the database clock, and clears
// the count of worker failures. For an operator-authorized send, this sets
// its single-use marker without overwriting the original first-send time. The
// marker captures the current attempt_count so a lost result can be recovered
// as unknown instead of silently retried. When require_dedup_window is true,
// the age check is repeated here after quota work and the row lock, immediately
// before the network call; manual starts never reset that original window.
MarkDeliverySendStarted(ctx context.Context, arg MarkDeliverySendStartedParams) (int64, error)
// Records that the provider refused a send because the quota of a window is
// used up: the usage rises to at least the configured limit, so the local
@@ -553,6 +568,10 @@ type Querier interface {
// never started on a lease that another worker may take over. now is the
// application clock.
StartScheduledJob(ctx context.Context, arg StartScheduledJobParams) (int64, error)
// Stops a due automatic retry of an unknown delivery without recording a
// fictitious provider attempt, for example when the 23-hour safety cutoff is
// reached after the claim but before the provider call.
StopUnknownDeliveryRetry(ctx context.Context, arg StopUnknownDeliveryRetryParams) (int64, error)
// Records activity on an active session and reports whether it is active.
// The row is written only when last_seen_at is older than touch_cutoff, so
// frequent requests do not rewrite it. last_seen_at never moves back, and a