import { GMAIL_SCOPES } from "./google/gmailApi.js"; import { MICROSOFT_MAIL_SCOPES } from "./microsoft/graphMailApi.js"; export type AdminMailConnectionProvider = "google_mail" | "microsoft_mail"; export type AdminConnectionDefinition = { readonly provider: AdminMailConnectionProvider; readonly capability: "email"; readonly oauth: true; readonly requiredScopes: readonly string[]; readonly healthMessageKey: string; }; export const ADMIN_MAIL_CONNECTION_REGISTRY = Object.freeze({ google_mail: Object.freeze({ provider: "google_mail", capability: "email", oauth: true, requiredScopes: GMAIL_SCOPES, healthMessageKey: "admin.sales.email.connections.health.google", }), microsoft_mail: Object.freeze({ provider: "microsoft_mail", capability: "email", oauth: true, requiredScopes: MICROSOFT_MAIL_SCOPES, healthMessageKey: "admin.sales.email.connections.health.microsoft", }), } satisfies Record); export type AdminOAuthCredentialBundle = { readonly accessToken: string; readonly refreshToken: string; readonly expiresAt: string; readonly scopes: readonly string[]; }; export function normalizeAdminOAuthCredentialBundle( provider: AdminMailConnectionProvider, input: AdminOAuthCredentialBundle ): AdminOAuthCredentialBundle { const accessToken = normalizeSecret(input.accessToken, "accessToken"); const refreshToken = normalizeSecret(input.refreshToken, "refreshToken"); const expiresAt = normalizeExpiry(input.expiresAt); const scopes = normalizeScopes(input.scopes); assertAdminConnectionScopes(provider, scopes); return Object.freeze({ accessToken, refreshToken, expiresAt, scopes }); } export function assertAdminConnectionScopes( provider: AdminMailConnectionProvider, grantedScopes: readonly string[] ): void { const granted = new Set(normalizeScopes(grantedScopes)); const missing = ADMIN_MAIL_CONNECTION_REGISTRY[provider].requiredScopes.filter( (scope) => !granted.has(scope) ); if (missing.length > 0) { throw new AdminConnectionContractError("missing_required_scopes", { provider, missingScopes: missing, }); } } export class AdminConnectionContractError extends Error { readonly code: "invalid_credential_bundle" | "missing_required_scopes"; readonly details: Readonly>; constructor( code: "invalid_credential_bundle" | "missing_required_scopes", details: Readonly> = {} ) { super(code); this.name = "AdminConnectionContractError"; this.code = code; this.details = details; } } function normalizeSecret(value: string, field: string): string { const normalized = value.trim(); if (normalized.length === 0 || normalized.length > 16_384) { throw new AdminConnectionContractError("invalid_credential_bundle", { field, }); } return normalized; } function normalizeExpiry(value: string): string { const timestamp = Date.parse(value); if (!Number.isFinite(timestamp)) { throw new AdminConnectionContractError("invalid_credential_bundle", { field: "expiresAt", }); } return new Date(timestamp).toISOString(); } function normalizeScopes(scopes: readonly string[]): readonly string[] { const normalized = [ ...new Set( scopes.map((scope) => scope.trim()).filter((scope) => scope.length > 0) ), ].sort(); if (normalized.length === 0 || normalized.some((scope) => scope.length > 512)) { throw new AdminConnectionContractError("invalid_credential_bundle", { field: "scopes", }); } return Object.freeze(normalized); }