iterate.com / onboarding

git clone https://git.iterate.com/repos/onboarding

onboarding / cloud-deliveries.tsmain at b65083a

// cloud-deliveries.ts — WHERE iterate Cloud IS, AND WHAT COUNTS AS iterate's OWN EVENT. iterate's
// mind (iterate.iterate.com) signs its events with the key it publishes, and they land on its
// context on this mind once the owner let it in: `/minds/<iterate's id>`, or
// `/minds/iterate.iterate.com` under a rule by hostname. `iterateContextOf` takes both, and
// `signedByIterate` tests the signer by its id. Such an event reaches `processMindEvent`.

/** WHERE iterate Cloud IS: its origin, an `https:` address with no path. This constant is the one
 *  place in the mind that names it. No config of the stack does: the code that makes a secret
 *  names the Cloud (`client: { cloud: "google", origin: ITERATE_CLOUD }`), and the secret keeps
 *  that origin for every later request (accounts.md). Every file of this template that names the
 *  Cloud imports it from here. */
export const ITERATE_CLOUD = "https://cloud.iterate.com";

/** The mind that is iterate Cloud, when it signs as a mind: by its id, the hash of its genesis
 *  record, which its log at `ITERATE_HOSTNAME` proves. The id decides: a hostname can move to
 *  another mind, the id cannot. */
// allow-high-entropy-next-line: a public mind id, the SHA-256 of iterate.iterate.com's genesis record
export const ITERATE_MIND = "7780b12cd895879475d1b5d01974de09b23b6b9a15d518b534bd7f314b11ac95";

/** Where iterate's mind is: its control-plane hostname, an address and a label. */
export const ITERATE_HOSTNAME = "iterate.iterate.com";

/** An event as `processEvent` and `processMindEvent` hand it, as far as this file reads it. */
export type CloudDeliveryEvent = {
  path: string;
  source?: { signer?: { principal: { actor?: string; kind?: string } } };
};

/** `/integrations/<provider>/<account>`, and no segment more or less. */
const ACCOUNT_PATH = /^\/integrations\/([^/]+)\/([^/]+)$/;
/** iterate's context on this mind: by its id, where it lands its events, and by its hostname,
 *  where a sender in the hostname form lands them under a rule by hostname
 *  (core/auth-worker/src/minds.ts). The platform stamps iterate's id as the signer in both. */
const ITERATE_CONTEXTS = [`/minds/${ITERATE_MIND}`, `/minds/${ITERATE_HOSTNAME}`];

/** The path below iterate's context on this mind, in either form (`/cloud`,
 *  `/integrations/<provider>/<account>`), or `/` for the context itself; null for any other
 *  path. Every reader of iterate's events asks this, never one form alone. */
export function iterateContextOf(path: string): string | null {
  for (const prefix of ITERATE_CONTEXTS)
    if (path === prefix || path.startsWith(`${prefix}/`)) return path.slice(prefix.length) || "/";
  return null;
}

/** Whether iterate's mind signed `event`, by its id. */
export function signedByIterate(event: CloudDeliveryEvent): boolean {
  const principal = event.source?.signer?.principal;
  return principal?.kind === "mind" && principal.actor === ITERATE_MIND;
}

/** The provider, the account and the mind's own context for it, when `event` is iterate Cloud's
 *  delivery in either form; else null. */
export function cloudDeliveryOf(
  event: CloudDeliveryEvent,
): { provider: string; account: string; context: string } | null {
  const principal = event.source?.signer?.principal;
  const below = iterateContextOf(event.path);
  // `below` is never empty: iterate's context itself is `/`
  const signed = below
    ? signedByIterate(event)
    : !principal?.kind && principal?.actor === "iterate-cloud";
  if (!signed) return null;
  const context = below || event.path;
  const [, provider, account] = ACCOUNT_PATH.exec(context) || [];
  return provider && account ? { provider, account, context } : null;
}