iterate.com / kernel

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

kernel / core / auth-worker / src / env.tsmain at 949894f

// env.ts — THE AUTH WORKER'S BINDINGS, as the stack declares them (core/alchemy), and its config.
// The boundary and the secrets Worker call it; it calls no other Worker. What it reads of the
// world it reads over the public internet: an outside client's document, and the description of
// an app of the mind (./asking-app.ts).
import { controlPlaneOriginOf, zoneOf } from "iterate/mind-config";
import { authConfigOf } from "./config.ts";
import { honouredNext } from "./key-rules.ts";

// THE BINDINGS are declared on `Cloudflare.Env`, which the OAuth library's types default to, as
// `wrangler types` declares them. `Env` is the same type by name.
declare global {
  namespace Cloudflare {
    interface Env {
      /** JSON: the config that every Worker of the mind gets (iterate/mind-config). */
      ITERATE: string;
      /** Generated by the stack and bound as a secret: the config's `signingKey` (./config.ts).
       *  Never returned or logged. */
      ITERATE__SIGNING_KEY: string;
      /** The people (./users.ts): `users` and `identities`, from ../migrations. */
      DB: D1Database;
      /** The OAuth library's store: clients, grants, tokens, consent handles. Its name is the one
       *  the library reads. */
      OAUTH_KV: KVNamespace;
      /** Cloudflare Email Sending: how a mailed sign-in code leaves (./email-sign-in.ts). */
      SEND_EMAIL: SendEmail;
    }
  }
}

export type Env = Cloudflare.Env;

/** The Worker's config, which `mindConfigOf` parses once for each `env` object: every request of
 *  an isolate gets the same one. */
export const configOf = (env: Env) => authConfigOf(env);

/** The issuer's origin, `https://<controlPlaneHostname>`, where every page and endpoint of this Worker
 *  is. The boundary sends it every request there that is not for the API, the MCP server or the
 *  secret pages. */
export const issuerOf = (env: Env) => controlPlaneOriginOf(configOf(env));

/** The mind's Dash (`https://dash.<zone>/`), where the control-plane root and returning sign-ins
 *  with no `next` URL go. A control plane at `dash.<zone>` itself falls back to the sessions page
 *  (./http.ts). */
export function dashOf(env: Env) {
  const config = configOf(env);
  return `https://dash.${zoneOf(config, config.controlPlaneHostname)}/`;
}

/** The mind's onboarding wizard (`https://onboarding.<zone>/`), where a new person continues setup
 *  after adding their first passkey when no explicit `next` URL was passed. */
export function onboardingOf(env: Env) {
  const config = configOf(env);
  return `https://onboarding.${zoneOf(config, config.controlPlaneHostname)}/`;
}

/** An explicit destination under this mind for first-passkey enrolment, or `null` when `next` is
 *  missing, invalid, or the bare control-plane root (`/` or `https://<controlPlaneHostname>/`),
 *  which is a placeholder with no page of its own and should fall back to `onboardingOf(env)`. */
export function explicitFirstPasskeyNext(raw: string | null, env: Env): string | null {
  const honoured = honouredNext(raw, configOf(env));
  return honoured && honoured !== `${issuerOf(env)}/` ? honoured : null;
}