Skip to content

A modern and easy to use dashboard. 80+ integrations. 20K+ icons built in. Authentication out of the box. No YAML, drag and drop configuration.

6.6 KB
Raw Blame
import { getCustomWidgetRequiredSecretKinds } from "../core/secret-requirements";
import { customWidgetAuthenticationHeadersSchema } from "../core/request-schema";
import type { CustomWidgetAuthenticationHeader } from "../core/request-schema";
import { getCustomWidgetHeaderSecretKind } from "../core/schema-types";
import { isCustomWidgetHeaderSecretValue } from "../core/request-header-policy";
import type { CustomWidgetHttpRequest, CustomWidgetHttpResponse } from "./request-types";
import { CustomWidgetDomainError } from "./errors";
import { assertCustomWidgetPathScope, resolveSameOriginTarget } from "./network-policy";

type HeaderSetter = Pick<Headers, "set">;

export const AUTH_HANDLERS: Record<
  string,
  (headers: HeaderSetter, url: URL, apiKey: string, headerName?: string) => void
> = {
  bearer: (headers, _url, apiKey) => {
    headers.set("Authorization", `Bearer ${apiKey}`);
  },
  apiKeyHeader: (headers, _url, apiKey, headerName) => {
    headers.set(headerName ?? "X-API-Key", apiKey);
  },
  apiKeyQuery: (_headers, url, apiKey, headerName) => {
    url.searchParams.set(headerName ?? "api_key", apiKey);
  },
};

export function buildSecretMap(secrets: Array<{ kind: string; value: string }>): Record<string, string> {
  const map: Record<string, string> = {};
  for (const secret of secrets) {
    map[secret.kind] = secret.value;
  }
  return map;
}

export function getCustomWidgetAuthConfigurationError(auth: CustomWidgetHttpRequest["auth"]): string | null {
  if (auth?.type === "headers") {
    if (Object.keys(auth.headers ?? {}).length > 0 || Object.keys(auth.query ?? {}).length > 0 || auth.body) {
      return "Custom widget authentication headers must use their declared credential slots";
    }
    const declarations = customWidgetAuthenticationHeadersSchema.safeParse(auth.headerSlots);
    if (!declarations.success) return "Custom widget authentication header declarations are invalid";
    const required = new Set<string>(declarations.data.map(({ slot }) => getCustomWidgetHeaderSecretKind(slot)));
    const supplied = new Set<string>();
    for (const secret of auth.secrets) {
      if (!required.has(secret.kind) || supplied.has(secret.kind)) {
        return "Custom widget credentials do not match the declared authentication headers";
      }
      if (!isCustomWidgetHeaderSecretValue(secret.value)) return "Custom widget authentication header value is invalid";
      supplied.add(secret.kind);
    }
    if ([...required].some((kind) => !auth.secrets.some((secret) => secret.kind === kind && secret.value.trim()))) {
      return "Custom widget source credentials are missing; complete secure source configuration before running requests";
    }
    return null;
  }
  if (
    !auth ||
    (auth.type !== "basic" && auth.type !== "bearer" && auth.type !== "apiKeyHeader" && auth.type !== "apiKeyQuery")
  )
    return null;
  const secretMap = buildSecretMap(auth.secrets);
  if (getCustomWidgetRequiredSecretKinds(auth.type).some((kind) => !secretMap[kind]?.trim())) {
    return "Custom widget source credentials are missing; complete secure source configuration before running requests";
  }
  return null;
}

function assertAuthCredentials(auth: CustomWidgetHttpRequest["auth"]): void {
  const message = getCustomWidgetAuthConfigurationError(auth);
  if (message) throw new CustomWidgetDomainError({ code: "BAD_REQUEST", message });
}

export function applyAuth(
  headers: HeaderSetter,
  url: URL,
  authType: string,
  secrets: Array<{ kind: string; value: string }>,
  headerName?: string | null,
  headerSlots?: CustomWidgetAuthenticationHeader[],
): void {
  assertAuthCredentials({ type: authType, secrets, headerSlots });
  const secretMap = buildSecretMap(secrets);

  if (authType === "headers") {
    for (const { name, slot } of headerSlots ?? []) {
      const value = secretMap[getCustomWidgetHeaderSecretKind(slot)];
      if (value !== undefined) headers.set(name, value);
    }
    return;
  }

  if (authType === "basic" && secretMap.username && secretMap.password) {
    const credentials = Buffer.from(`${secretMap.username}:${secretMap.password}`).toString("base64");
    headers.set("Authorization", `Basic ${credentials}`);
    return;
  }

  const handler = AUTH_HANDLERS[authType];
  if (handler && secretMap.apiKey) {
    handler(headers, url, secretMap.apiKey, headerName ?? undefined);
  }
}

export function applyBodyAuth(body: string | undefined, auth: CustomWidgetHttpRequest["auth"]): string | undefined {
  if (!auth?.body) return body;
  if (!body) throw new CustomWidgetDomainError({ code: "BAD_REQUEST", message: "Authenticated JSON body is required" });
  let value: unknown;
  try {
    value = JSON.parse(body) as unknown;
  } catch {
    throw new CustomWidgetDomainError({ code: "BAD_REQUEST", message: "Authenticated JSON body is invalid" });
  }
  if (value === null || typeof value !== "object" || Array.isArray(value))
    throw new CustomWidgetDomainError({ code: "BAD_REQUEST", message: "Authenticated JSON body must be an object" });
  const record = value as Record<string, unknown>;
  if (auth.body.type === "jsonField") {
    record[auth.body.name] = auth.body.value;
  } else {
    const current = record[auth.body.name];
    if (current !== undefined && !Array.isArray(current))
      throw new CustomWidgetDomainError({
        code: "BAD_REQUEST",
        message: "Authenticated JSON parameter must be an array",
      });
    record[auth.body.name] = [auth.body.value, ...(current ?? [])];
  }
  return JSON.stringify(record);
}

export async function performAuthenticatedRequest(
  input: CustomWidgetHttpRequest,
  signal: AbortSignal,
  perform: (input: CustomWidgetHttpRequest, signal: AbortSignal) => Promise<CustomWidgetHttpResponse>,
): Promise<CustomWidgetHttpResponse> {
  const target = resolveSameOriginTarget(input.baseUrl, input.targetUrl);
  if (input.pathPrefix !== undefined) assertCustomWidgetPathScope(target, input.pathPrefix);
  if (input.resolveConnectionAsync) {
    const connection = await withinDeadline(input.resolveConnectionAsync, signal);
    resolveSameOriginTarget(connection.baseUrl, target);
    input = { ...input, ...connection, targetUrl: target };
  }
  assertAuthCredentials(input.auth);
  return perform(input, signal);
}

async function withinDeadline<T>(callback: () => Promise<T>, signal: AbortSignal): Promise<T> {
  signal.throwIfAborted();
  let abort: (() => void) | undefined;
  const aborted = new Promise<never>((_resolve, reject) => {
    abort = () => reject(signal.reason);
    signal.addEventListener("abort", abort, { once: true });
  });
  try {
    return await Promise.race([callback(), aborted]);
  } finally {
    if (abort) signal.removeEventListener("abort", abort);
  }
}