/** * with-oauth-lifecycle.ts * * Centralizes the OAuth reauth/ACK contract shared by every IMAP handler. * * The contract (see issue #472): * - If the account already requires reauth, skip all IMAP traffic entirely. * - Credential resolution AND the per-handler work both run inside one * try/catch so that a revoked OAuth token is caught regardless of where it * surfaces (token mint vs. first IMAP command). * - An account storing no credential at all is terminal, not transient: the * account is flipped to the state that describes it and the event is ACKed. * Retrying instead fenced the account's FIFO group for the message's whole * visibility timeout, blocking the sync that follows the credential the * user was still typing (issue #1120). * - On a terminal auth failure (RefreshTokenError reauth-required, or * MailConnectionError auth), flip the account to reauth_required and return * WITHOUT rethrowing — this ACKs the SQS message so it is not retried. * An IMAP refusal is stored with it, because not every refusal a mail server * hands back is one re-authenticating clears: "SmtpClientAuthentication is * disabled" needs a tenant setting changed, and an account card that only * ever says "Re-authentication required" sends the user around a loop. * - On transient / config / network errors, rethrow so SQS retries with * backoff (let-it-crash). * * Tokens must NEVER appear in logs — only accountId / errorKind / errorCode. */ import type { AccountItem } from "@remit/data-ports"; import type { Logger } from "@remit/logger"; import { type AccountCredentialsDeps, type ConnectionStateValue, type CredentialResolution } from "@remit/mailbox-service/account-credentials"; import { type MailCredentials } from "@remit/mailbox-service/types"; export type { ConnectionStateValue }; export interface OAuthLifecycleDeps extends AccountCredentialsDeps { /** * Persist the account's connectionState, and with it the sentence the * account card shows for the failure. `lastError` carries the server's own * words when the failure came with any; it is absent when nothing was said, * as for a credential that was never stored or a token simply revoked, and * the stored reason is then cleared rather than left to describe an older * failure the account has moved on from. */ updateConnectionState: (accountId: string, state: ConnectionStateValue, lastError?: string) => Promise; /** * Resolve credentials for the account. Defaults to * resolveConnectionCredentials; overridable for testing. Kept as the ONLY * authType branch in the codebase (see account-credentials.ts). */ resolveCredentials?: (account: AccountItem, deps: AccountCredentialsDeps) => Promise; } /** * Run `work` for an account under the shared OAuth reauth/ACK contract. * * `work` receives the resolved MailCredentials. Both credential resolution and * `work` run inside the same try/catch. */ export declare const withOAuthLifecycle: (deps: OAuthLifecycleDeps, account: AccountItem, log: Logger, work: (credentials: MailCredentials) => Promise) => Promise;