Skip to content

@mshindi-labs/google — Design Spec ​

Status: Draft v1 · Date: 2026-11-05 · Owner: Mshindi Labs

A server-side TypeScript library that lets multi-tenant SaaS apps send email through each tenant's own Google Workspace accounts via the Gmail API, with no SMTP and no per-tenant secrets in environment variables. Calendar and Sheets are built on the same foundation.


1. Goals and non-goals ​

Goals ​

  1. Each tenant brings its own Google OAuth client (client ID + secret), stored in the host app's database.
  2. The library generates a consent URL; a tenant user (e.g. support@northwind.example) authorizes; the library stores an encrypted refresh token for that account.
  3. Host code sends mail with one call, referencing only IDs — never secrets.
  4. Strict tenant isolation: one tenant can never read, use, or overwrite another tenant's credentials, even with a buggy host or tampered DB rows.
  5. Database-agnostic core; Postgres adapter first.
  6. One package that grows to Calendar and Sheets without redesign.

Non-goals (v1) ​

  • SMTP, IMAP, or reading/modifying inboxes.
  • A platform-owned shared OAuth client, or Workspace domain-wide delegation.
  • Gmail "send as" aliases.
  • Template rendering (host renders HTML/text).
  • A built-in job queue (documented pattern only).
  • App-level authorization ("may user X send as support@?") — host decides.
  • Browser/edge runtimes. Node ≥ 18 only.

2. Invariants ​

Every module must hold these. Breaking one is a bug, not a trade-off.

#Invariant
I1Client secrets, refresh tokens, and PKCE verifiers are never stored, logged, thrown, emitted, or returned in plaintext.
I2Every read of tenant-owned data takes tenantId and fails closed on mismatch. The only exception is the operator tools rewrapAll / verifyAll (§5.4), which never return plaintext and run under a separate privileged role.
I3Ciphertext is cryptographically bound to its tenant, record, and field (AAD). Moving a ciphertext to another row makes it undecryptable.
I4The OAuth callback derives tenant, actor, and intent only from server-stored state.
I5The redirect URI is fixed at construction time; never taken from request input.
I6Access tokens live in process memory only.
I7A send is attempted at most once unless the failure provably happened before Google received it, or Google answered 429.
I8One (tenantId, organizerEmail, idempotencyKey) yields at most one calendar event, and its invitations are sent once.
I9Only calendar events stamped mshindiGoogle: "v1" with the same mshindiTenant are read or changed.
I10append and batch are retried only when Google answered 429 / rate-limit 403, or the failure provably happened before the request reached Google (ENOTFOUND, EAI_AGAIN, ECONNREFUSED, ERR_TLS_*, CERT_*); any other ambiguous failure throws SheetsWriteOutcomeUnknownError after exactly one Google call.
I11A service-account private key is never logged, thrown, emitted or returned, and never reaches a @googleapis/* module: only minted access tokens do.

3. Package layout ​

Single npm package, subpath exports, same tooling as gsheets (tsup, ESM + CJS, semantic-release, commitlint, prettier).

@mshindi-labs/google
├── /core       createGoogle, OAuth flow, crypto, store + key-provider interfaces, errors, events
├── /gmail      createGmail(google) → send()
├── /postgres   createPostgresStore(query), migrations
├── /testing    InMemoryStore, LocalKeyProvider, CaptureTransport, FakeCalendar, FakeSheets, sheetsHttpError
├── /calendar   createCalendar(google) → availability, suggestSlots, create/update/cancel/getEvent
└── /sheets     createSheets(google) → open() → values, rows, append, batch

Dependencies

PackageKindUsed by
google-auth-librarydependencycore (OAuth2 client, ID token verification)
@googleapis/gmailoptional peer/gmail
nodemailer (MailComposer only)optional peer/gmail
html-to-textoptional peer/gmail (plain-text fallback)
@googleapis/calendar (>=14.0.0 <21.0.0)optional peer/calendar
@googleapis/sheets (>=12.0.0 <19.0.0)optional peer/sheets

Importing /gmail without its peers throws a clear install-instructions error.

Retirement of @mshindi-labs/gsheets: /sheets replaces it with a redesigned, tenant-aware API and no compatibility shim. After /sheets ships, gsheets is marked npm deprecate ("Moved to @mshindi-labs/google/sheets") and its repo is archived. No forwarding release. See docs/superpowers/specs/2026-11-16-sheets-design.md §10.


4. Domain model &ZeroWidthSpace;

Tenant (host-owned, opaque string id)
 └── GoogleOAuthClient           0..1 per tenant
      └── GoogleConnection       0..n — one Google account authorized inside the tenant
           └── granted scopes    grows via incremental authorization
GoogleOAuthState                 short-lived, one per in-flight consent
GoogleAuditEvent                 optional persistence of emitted events

4.1 GoogleOAuthClient &ZeroWidthSpace;

FieldNotes
tenantIdPK
clientIdValidated: ends with .apps.googleusercontent.com
clientSecretEncryptedValue — write-only
secretHintLast 4 chars, for UI display
hostedDomainOptional Workspace domain, sent as hd and enforced on callback
createdBy, createdAt, updatedBy, updatedAtAudit

Rules:

  • Re-saving with the same clientId re-encrypts the secret; connections stay active.
  • Saving a different clientId marks every connection in the tenant revoked (reason client_changed) in the same transaction. Refresh tokens are bound to the client that issued them.

4.2 GoogleConnection &ZeroWidthSpace;

FieldNotes
idUUID, generated by the library (needed before encryption for AAD)
tenantId
googleSubStable Google account ID from the ID token. Unique with tenantId.
emailVerified email from the ID token, lower-cased
displayNameOptional default From name
scopesGranted scope set (from token response)
refreshTokenEncryptedValue
statusactive · revoked · error
statusReasone.g. invalid_grant, client_changed, disconnected
connectedBy, connectedAt, updatedAt, lastUsedAtAudit

Re-authorizing the same Google account in the same tenant updates the existing row (new token, merged scopes, status back to active) rather than creating a duplicate.

4.3 GoogleOAuthState &ZeroWidthSpace;

FieldNotes
stateHashSHA-256 of the random state value (PK). The raw value only exists in the URL.
tenantId, actorIdWho started the flow
intendedEmailOptional; enforced on callback
connectionIdSet when adding scopes to an existing connection
scopesRequested scopes
codeVerifierEncryptedValue (PKCE)
expiresAtnow + 10 min

5. Encryption &ZeroWidthSpace;

5.1 Scheme &ZeroWidthSpace;

Envelope encryption, per value:

  1. Generate a random 256-bit data encryption key (DEK).
  2. Encrypt plaintext with AES-256-GCM, random 96-bit IV, with AAD (5.2).
  3. Wrap the DEK with the key encryption key (KEK) via KeyProvider.wrap.
  4. Store the envelope; discard the plaintext DEK.
ts
type EncryptedValue = {
  v: 1; // format version
  kid: string; // KEK id + version, e.g. "gcp-kms:projects/.../cryptoKeyVersions/3"
  wdek: string; // wrapped DEK, base64url
  iv: string; // base64url
  ct: string; // ciphertext, base64url
  tag: string; // GCM auth tag, base64url
};

Stored as jsonb in Postgres.

5.2 Associated data (binds ciphertext to its place) &ZeroWidthSpace;

mshindi-google/v1|tenant=<tenantId>|record=<oauth_client|connection|oauth_state>:<id>|field=<name>

Decryption with any other AAD fails (GCM tag mismatch) → CredentialIntegrityError. The same string is passed to KMS as encryption context / AAD where supported.

5.3 KeyProvider &ZeroWidthSpace;

ts
interface KeyProvider {
  readonly currentKeyId: string;
  wrap(
    dek: Uint8Array,
    aad: string,
  ): Promise<{ kid: string; wdek: Uint8Array }>;
  unwrap(kid: string, wdek: Uint8Array, aad: string): Promise<Uint8Array>;
}

Shipped implementations:

ProviderUse
GcpKmsKeyProviderRecommended production. Uses workload identity → zero secrets in env.
AwsKmsKeyProviderProduction on AWS.
StaticKeyProvider({ keys: { [kid]: Uint8Array }, current })Small deploys; master key(s) from env or a secret manager. Supports several keys for rotation.
LocalKeyProvider (/testing)Dev/test. Throws if NODE_ENV === 'production' unless allowInProduction: true.

KMS providers make one KMS call per unwrap. Because access tokens are cached (§7), a connection's refresh token is decrypted at most about once an hour per process, so no DEK cache is needed in v1.

5.4 Rotation and integrity tools &ZeroWidthSpace;

ts
rewrapAll(opts: { store, keyProvider, fromKeyId?: string, batchSize?: number }):
  Promise<{ rewrapped: number; failed: Array<{ record: string; error: string }> }>;

verifyAll(opts: { store, keyProvider }):
  Promise<{ ok: number; failed: Array<{ record: string; error: string }> }>;

rewrapAll unwraps each DEK with its old kid and re-wraps it under currentKeyId. The secret ciphertext itself is untouched. verifyAll performs a full decrypt of every value and reports tampered, moved, or orphaned records. Both are idempotent and resumable.


6. OAuth flow &ZeroWidthSpace;

6.1 Construction &ZeroWidthSpace;

ts
import { createGoogle } from "@mshindi-labs/google/core";

const google = createGoogle({
  store, // CredentialStore
  keyProvider, // KeyProvider
  callbackUrl: "https://app.example.com/api/google/callback", // fixed, HTTPS required outside localhost
  onEvent: (e) => audit.write(e), // optional
  logger, // optional; receives redacted messages only
});

Every tenant registers the same callbackUrl as an authorized redirect URI in its Google client.

6.2 Configure a tenant's client &ZeroWidthSpace;

ts
await google.clients.configure({
  tenantId,
  actorId,
  clientId,
  clientSecret,
  hostedDomain: "northwind.example", // optional
});

await google.clients.get(tenantId);
// → { clientId, secretHint: 'x7Qa', hostedDomain, updatedAt } — never the secret
ts
const { url, expiresAt } = await google.oauth.createAuthUrl({
  tenantId,
  actorId,
  services: ["gmail"], // → scopes via the service registry
  intendedEmail: "support@northwind.example", // optional but recommended
  connectionId, // optional: add scopes to an existing connection
});

The library:

  1. Loads the tenant's client (fails with OAuthClientNotConfiguredError if absent).
  2. Resolves scopes: openid email + each service's scopes.
  3. Generates state (32 random bytes, base64url) and PKCE code_verifier.
  4. Stores GoogleOAuthState (hashed state, encrypted verifier, 10-minute TTL).
  5. Builds the URL with: response_type=code, access_type=offline, prompt=consent, include_granted_scopes=true, code_challenge_method=S256, login_hint=<intendedEmail>, hd=<hostedDomain>, fixed redirect_uri.

Service scope registry (v1):

ServiceScopes
(always)openid, email
gmailhttps://www.googleapis.com/auth/gmail.send
calendarhttps://www.googleapis.com/auth/calendar.events.owned, https://www.googleapis.com/auth/calendar.events.freebusy

6.4 Handle callback &ZeroWidthSpace;

ts
// host route: GET /api/google/callback
const result = await google.oauth.handleCallback({
  code: req.query.code,
  state: req.query.state,
  error: req.query.error,
});
// → { connection: ConnectionView, tenantId, actorId, created: boolean }

Steps (any failure → typed error, the state is consumed regardless):

  1. Consume state atomically: delete-and-return by sha256(state) where not expired. Missing or expired → InvalidOAuthStateError.
  2. If error is present (e.g. access_denied) → OAuthConsentDeniedError.
  3. Load the tenant's client using tenantId from the state row.
  4. Exchange code with the decrypted PKCE verifier and fixed redirect_uri.
  5. Verify the ID token (google-auth-library verifyIdToken): signature, aud = tenant client ID, iss, exp, email_verified === true.
  6. If hostedDomain is set: the ID token's hd claim must equal it.
  7. If intendedEmail is set: the normalized email must equal it → otherwise UnexpectedAccountError (the new token is revoked at Google immediately).
  8. Check that the granted scopes include every requested service scope. The user may untick boxes on the consent screen → MissingScopeError (token revoked).
  9. No refresh_token returned → NoRefreshTokenError (should not happen with prompt=consent, but handled).
  10. Upsert the connection by (tenantId, googleSub), encrypt the refresh token, merge scopes.
  11. Emit connection.created or connection.scopes_added.

The host maps errors to a user-facing page. Error messages never contain codes, tokens, or secrets.

6.5 Connections API &ZeroWidthSpace;

ts
google.connections.list(tenantId): Promise<ConnectionView[]>;
google.connections.get(tenantId, connectionId): Promise<ConnectionView>;
google.connections.disconnect({ tenantId, connectionId, actorId }): Promise<void>;

ConnectionView contains no ciphertext. disconnect calls https://oauth2.googleapis.com/revoke, then deletes the encrypted token and sets status=revoked, statusReason=disconnected. If revocation at Google fails, the local token is still deleted and the event records googleRevoke: 'failed'.


7. Token lifecycle &ZeroWidthSpace;

  • Access tokens: kept in an in-process LRU keyed by connectionId, until 60 s before expiry. Never persisted.
  • Refresh: on a cache miss, decrypt the refresh token, refresh via google-auth-library, cache the access token, update lastUsedAt. Concurrent callers for the same connection share one in-flight refresh (single-flight).
  • No cross-instance lock: Google refresh tokens do not rotate on use, so parallel refreshes on different instances are safe.
  • invalid_grant (user revoked, admin removed app, password reset under certain policies, 6 months unused, the OAuth client was deleted): mark the connection revoked with reason invalid_grant, evict the cache, emit connection.revoked, throw ConnectionRevokedError. No retry.
  • Known Google limits to document for tenants:
    • A client in Testing publishing status issues refresh tokens that expire after 7 days. Use Internal (Workspace) or publish the app.
    • Google caps live refresh tokens per account per client. Reconnecting many times silently invalidates the oldest ones.

8. Gmail service &ZeroWidthSpace;

8.1 API &ZeroWidthSpace;

ts
import { createGmail } from "@mshindi-labs/google/gmail";

const gmail = createGmail(google, {
  blockedAttachmentExtensions: ["exe", "bat", "js"], // optional
  transport, // optional; default = Gmail API
});

const { messageId, threadId } = await gmail.send({
  tenantId,
  connectionId,
  actorId, // for audit
  fromName: "Northwind Support", // optional; overrides connection.displayName
  to: ["jane@customer.example"], // string | { name, address }, also cc/bcc
  cc,
  bcc,
  replyTo: "team@northwind.example",
  subject: "Your order has shipped",
  html: "<p>…</p>",
  text: "…", // optional; generated from html if omitted
  attachments: [
    {
      filename: "invoice.pdf",
      content: buffer,
      contentType: "application/pdf",
    },
  ],
  headers: { "X-App-Order-Id": "order_123" },
  thread: { threadId, inReplyTo, references }, // optional: reply in an existing thread
});

8.2 Send pipeline &ZeroWidthSpace;

  1. Load the connection with tenantId (I2). Not active → ConnectionRevokedError.
  2. Check the scope gmail.send is granted → otherwise MissingScopeError (the host can call createAuthUrl({ connectionId, services: ['gmail'] })).
  3. Validate input:
    • Every header value and address: reject CR, LF, and NUL → InvalidMessageError.
    • Addresses parsed and validated. At least one recipient across to/cc/bcc.
    • Custom headers must match ^X-[A-Za-z0-9-]+$. Reserved headers (From, Sender, Bcc, Return-Path, DKIM-*, Received, …) are rejected.
    • fromName sanitized (control characters stripped, length capped).
    • Attachment filenames: basename only, control characters and path separators removed, extension checked against the blocklist.
  4. Build MIME with Nodemailer MailComposer. From is always "<fromName>" <connection.email>. text is generated with html-to-text if missing.
  5. Size check: total encoded size over Gmail's limit (25 MB attachments) → MessageTooLargeError before any network call.
  6. Send: users.messages.send with userId: 'me'. Messages over 5 MB use the media upload endpoint; smaller ones use the base64url raw field. threadId is passed when replying.
  7. Emit message.sent (or message.failed), update lastUsedAt.

8.3 Retries (I7) &ZeroWidthSpace;

ConditionBehavior
Connection error before the request was written (DNS, ECONNREFUSED, TLS handshake)Retry, max 3, exponential backoff with full jitter
HTTP 429 / 403 rateLimitExceeded / userRateLimitExceededRetry, max 3, honor Retry-After; then RateLimitedError { retryAfter }
403 dailyLimitExceeded / sending-limit errorsDailyQuotaExceededError, no retry
401 after one forced token refreshConnectionRevokedError path (§7)
Timeout or reset after the request was sent, 5xxNo retry → SendOutcomeUnknownError (the message may have been sent)
400 invalid recipient / messageInvalidMessageError

8.4 Bulk sends (documented pattern, not library code) &ZeroWidthSpace;

The docs show a pg-boss queue: one job per recipient, with an idempotency key such as billing:renewal-reminder:<subscriptionId>. The worker records messageId against the key and skips the job if it already has one. SendOutcomeUnknownError routes the job to manual review instead of retrying. Per-connection concurrency of 1–2 stays under Gmail's per-user rate limits.


9. Store interface &ZeroWidthSpace;

ts
interface CredentialStore {
  // OAuth clients
  getOAuthClient(tenantId: string): Promise<OAuthClientRecord | null>;
  upsertOAuthClient(
    rec: OAuthClientRecord,
    opts: { revokeConnections: boolean },
  ): Promise<void>;

  // Connections
  getConnection(
    tenantId: string,
    connectionId: string,
  ): Promise<ConnectionRecord | null>;
  findConnectionBySub(
    tenantId: string,
    googleSub: string,
  ): Promise<ConnectionRecord | null>;
  listConnections(tenantId: string): Promise<ConnectionRecord[]>;
  upsertConnection(rec: ConnectionRecord): Promise<void>;
  updateConnectionStatus(
    tenantId: string,
    connectionId: string,
    status: ConnectionStatus,
    reason: string,
  ): Promise<void>;
  touchConnection(
    tenantId: string,
    connectionId: string,
    at: Date,
  ): Promise<void>;

  // OAuth state
  insertOAuthState(rec: OAuthStateRecord): Promise<void>;
  consumeOAuthState(
    stateHash: string,
    now: Date,
  ): Promise<OAuthStateRecord | null>; // atomic
  deleteExpiredOAuthStates(now: Date): Promise<number>;

  // Key rotation
  iterateEncryptedValues(batchSize: number): AsyncIterable<EncryptedValueRef>;
  replaceEncryptedValue(
    ref: EncryptedValueRef,
    value: EncryptedValue,
  ): Promise<void>;

  // Optional audit persistence
  insertAuditEvent?(event: GoogleEvent): Promise<void>;
}

Every method that takes tenantId must include it in its WHERE clause. The library also re-checks record.tenantId === tenantId after every read (belt and braces).

A shared contract test suite (runStoreContractTests(makeStore)) in /testing must pass for every adapter, including the in-memory one.


10. Postgres adapter &ZeroWidthSpace;

ts
import { createPostgresStore, migrations } from "@mshindi-labs/google/postgres";

type Query = <T = unknown>(
  sql: string,
  params: unknown[],
) => Promise<{ rows: T[] }>;

const store = createPostgresStore({
  query: (sql, params) => pool.query(sql, params), // pg; wrappers for postgres.js / Drizzle / Prisma in docs
  schema: "public", // optional
  tablePrefix: "google_", // optional
});

All SQL is parameterized. Identifiers (schema, prefix) are validated against ^[a-z_][a-z0-9_]*$ at construction.

10.1 Migration 0001_init.sql &ZeroWidthSpace;

sql
create table google_oauth_clients (
  tenant_id       text primary key,
  client_id       text not null,
  client_secret   jsonb not null,
  secret_hint     text not null,
  hosted_domain   text,
  created_by      text not null,
  created_at      timestamptz not null default now(),
  updated_by      text not null,
  updated_at      timestamptz not null default now()
);

create table google_connections (
  id              uuid primary key,
  tenant_id       text not null references google_oauth_clients(tenant_id) on delete cascade,
  google_sub      text not null,
  email           text not null,
  display_name    text,
  scopes          text[] not null,
  refresh_token   jsonb,                       -- null after disconnect
  status          text not null check (status in ('active','revoked','error')),
  status_reason   text,
  connected_by    text not null,
  connected_at    timestamptz not null default now(),
  updated_at      timestamptz not null default now(),
  last_used_at    timestamptz,
  unique (tenant_id, google_sub)
);
create index on google_connections (tenant_id);

create table google_oauth_states (
  state_hash      bytea primary key,
  tenant_id       text not null,
  actor_id        text not null,
  intended_email  text,
  connection_id   uuid,
  scopes          text[] not null,
  code_verifier   jsonb not null,
  expires_at      timestamptz not null
);
create index on google_oauth_states (expires_at);

create table google_audit_events (
  id              bigserial primary key,
  tenant_id       text not null,
  type            text not null,
  actor_id        text,
  connection_id   uuid,
  payload         jsonb not null,
  occurred_at     timestamptz not null default now()
);
create index on google_audit_events (tenant_id, occurred_at desc);

Consume state atomically:

sql
delete from google_oauth_states
where state_hash = $1 and expires_at > $2
returning *;

10.2 Optional row-level security (docs) &ZeroWidthSpace;

Policies on each table: tenant_id = current_setting('app.tenant_id'). The host sets app.tenant_id per transaction. Key rotation (rewrapAll) runs as a separate role with BYPASSRLS.


11. Events and audit &ZeroWidthSpace;

ts
type GoogleEvent =
  | {
      type: "oauth_client.configured" | "oauth_client.rotated";
      tenantId;
      actorId;
      clientIdChanged: boolean;
      at;
    }
  | {
      type: "connection.created" | "connection.scopes_added";
      tenantId;
      actorId;
      connectionId;
      email;
      scopes;
      at;
    }
  | { type: "connection.revoked"; tenantId; connectionId; reason; at }
  | {
      type: "connection.disconnected";
      tenantId;
      actorId;
      connectionId;
      googleRevoke: "ok" | "failed";
      at;
    }
  | {
      type: "message.sent";
      tenantId;
      actorId;
      connectionId;
      messageId;
      threadId;
      to;
      cc;
      bcc;
      subject;
      attachmentCount;
      at;
    }
  | {
      type: "message.failed";
      tenantId;
      actorId;
      connectionId;
      errorCode;
      to;
      cc;
      bcc;
      subject;
      at;
    }
  | {
      type: "calendar_event.created" | "calendar_event.cancelled";
      tenantId;
      actorId;
      connectionId;
      eventId;
      attendees;
      title;
      start;
      end;
      at;
    }
  | {
      type: "calendar_event.updated";
      tenantId;
      actorId;
      connectionId;
      eventId;
      attendees;
      title;
      start;
      end;
      changed;
      at;
    }
  | {
      type: "calendar_event.failed";
      tenantId;
      actorId;
      connectionId;
      operation: "create" | "update" | "cancel";
      errorCode;
      at;
    };
  • No message bodies, attachment contents, tokens, codes, or secrets in any event.
  • redactRecipients: true (global or per-send) replaces addresses with sha256(lowercase(address)); for calendar events this applies to attendees.
  • Calendar reads (getEvent, getAvailability, suggestSlots) and input validation errors raised before the token fetch emit nothing. In a crash or lost-response corner combined with concurrency, calendar_event.created may be emitted twice or not at all; invitations are still sent once (I8).
  • onEvent failures are caught and logged; they never fail a send. If the store has insertAuditEvent, events are also written there.

12. Errors &ZeroWidthSpace;

All errors extend GoogleLibError { code: string; tenantId?; connectionId? }. Messages are safe to show to admins and never include secret material; cause is redacted before attaching.

CodeClass
oauth_client_not_configuredOAuthClientNotConfiguredError
invalid_oauth_stateInvalidOAuthStateError
oauth_consent_deniedOAuthConsentDeniedError
unexpected_accountUnexpectedAccountError
no_refresh_tokenNoRefreshTokenError
missing_scopeMissingScopeError { required: string[] }
connection_not_foundConnectionNotFoundError
connection_revokedConnectionRevokedError { reason }
credential_integrityCredentialIntegrityError
invalid_messageInvalidMessageError { field }
message_too_largeMessageTooLargeError { bytes, limit }
rate_limitedRateLimitedError { retryAfter }
daily_quota_exceededDailyQuotaExceededError
send_outcome_unknownSendOutcomeUnknownError
invalid_eventInvalidEventError { field }
event_not_foundEventNotFoundError
event_not_managedEventNotManagedError
event_cancelledEventCancelledError
event_conflictEventConflictError
conferencing_unavailableConferencingUnavailableError
service_account_rejectedServiceAccountRejectedError { clientEmail }
google_apiGoogleApiError { status, reason } (fallback)

13. Testing &ZeroWidthSpace;

/testing exports &ZeroWidthSpace;

  • InMemoryStore — full CredentialStore.
  • LocalKeyProvider — random or fixed key; refuses production unless overridden.
  • CaptureTransport — gmail.send builds real MIME and stores { raw, parsed: { from, to, subject, html, text, attachments } } in memory; exposes messages, clear(), find(predicate). Use it in host tests and on staging.
  • runStoreContractTests(makeStore).
  • FakeGoogleOAuth — local stand-in for the token, revoke, and certificate endpoints for end-to-end flow tests.
  • FakeCalendar and FakeSheets — in-memory CalendarTransport and SheetsTransport (see the Calendar and Sheets guides); sheetsHttpError(status, reason, message) builds a Google-shaped HTTP error for FakeSheets.failNext.

Required library test coverage &ZeroWidthSpace;

  • Crypto: round-trip; tampered ct/tag/iv/wdek fails; ciphertext moved to another tenant, record, or field fails; key rotation round-trip.
  • OAuth: state single-use, expiry, replay, cross-tenant; PKCE verifier mismatch; ID token aud/email_verified/hd/intended-email checks; partial scope grant.
  • Token: single-flight refresh; invalid_grant → revoked + event; cache expiry.
  • MIME: header injection attempts (CR/LF in name, subject, address, filename, custom header); reserved header rejection; generated text part; size limit.
  • Retries: each row of the table in §8.3.
  • Redaction: no secret appears in any thrown error, event, or logger call (fuzzed with known secret strings).
  • Postgres adapter against a real Postgres (Testcontainers) in CI.

14. Threat model summary &ZeroWidthSpace;

ThreatMitigation
DB dump / backup leakEnvelope encryption; KEK outside the DB (KMS)
SQL injection or DB write access swapping tokens between tenantsAAD binding (I3), tenant re-check (I2), optional RLS
CSRF / login-CSRF on OAuth callbackServer-side single-use hashed state bound to actor and tenant (I4)
Authorization code interceptionPKCE S256, fixed HTTPS redirect URI (I5)
Wrong account connected (personal Gmail instead of support@)ID token verification + intendedEmail + hd checks; the token is revoked on mismatch
Over-privileged tokensgmail.send + openid email only; scope check per service
Email header injection via user-supplied dataCR/LF rejection, header allowlist, MailComposer
Secrets in logs or error trackersRedaction layer; tested by fuzz (§13)
Duplicate emails to recipientsI7 retry rules; idempotency-key queue pattern
Ex-employee's mailbox still sendinginvalid_grant → revoked; admin disconnect(); audit trail
Compromised host serverOut of scope: it can call send legitimately. Limited by KMS IAM and audit.

15. Delivery phases &ZeroWidthSpace;

  1. Core: crypto, KeyProviders, store interface, InMemoryStore, errors, events.
  2. OAuth: clients, createAuthUrl, handleCallback, connections, token cache.
  3. Gmail: validation, MIME, send, retries, CaptureTransport.
  4. Postgres: adapter, migration, contract tests in CI, RLS docs.
  5. Ops: rewrapAll, verifyAll, state cleanup, the pg-boss docs example.
  6. Docs: tenant setup guide (create a GCP project → OAuth client "Internal" → add redirect URI → paste ID/secret), host integration guide (Next.js + Express examples).
  7. Calendar: /calendar (availability, slot suggestions, idempotent create with Google Meet, reschedule, cancel, RSVP status). Done; see docs/guides/calendar-scheduling.md and docs/superpowers/specs/2026-11-05-calendar-design.md.
  8. Sheets: /sheets (tenant-aware values, rows, safe append, atomic batch, FakeSheets), replacing gsheets. Done; see docs/guides/sheets.md and docs/superpowers/specs/2026-11-16-sheets-design.md.
  9. Later: more DB adapters.

16. Open items &ZeroWidthSpace;

  • Allowed domains per tenant: should the tenant client config allow several Workspace domains (e.g. a group with two domains) instead of one hostedDomain?
  • State cleanup: is a host-scheduled deleteExpiredOAuthStates call enough, or should the library clean up opportunistically on each createAuthUrl?
  • Audit retention: should the library offer a retention helper for google_audit_events, or leave that to the host?
  • Licensing: MIT, like gsheets?

Released under the MIT License.