Skip to content

Self-hosted, plugin-based server management.

3.0 KB
Raw Blame
import express, { type Request, type Response } from "express";
import { AuthManager } from "../../utils/auth-manager.js";
import {
  listSshAuthProviders,
  listSshAuthTypeOwners,
} from "../../hosts/connect/auth-provider-registry.js";
import { ensureCoreSshAuthProviders } from "../../hosts/connect/core-providers.js";

const router = express.Router();
const authenticateJWT = AuthManager.getInstance().createAuthMiddleware();

export interface SshAuthProviderSummary {
  type: string;
  labelKey: string;
  descriptionKey?: string;
  pluginId: string;
  fields: unknown[];
  credentialType: boolean;
  needsUserInteraction: boolean;
  supportsBackground: boolean;
  /** Offered in Quick Connect, for a host that is never saved. */
  quickConnect: boolean;
  available: boolean;
  /** Set when the type belongs to a plugin that is disabled or missing. */
  missingPlugin?: { id: string; name: string };
}

/**
 * A plugin names its keys relative to its own locales, so they are sent
 * qualified with its namespace. Core never has to hold a plugin's strings.
 */
function qualifyKey(
  pluginId: string,
  key: string | undefined,
): string | undefined {
  if (!key || pluginId === "core" || key.includes(":")) return key;
  return `${pluginId}:${key}`;
}

function summarizeSshAuthProviders(): SshAuthProviderSummary[] {
  ensureCoreSshAuthProviders();
  const summaries: SshAuthProviderSummary[] = listSshAuthProviders().map(
    (provider) => ({
      type: provider.type,
      labelKey: qualifyKey(provider.pluginId, provider.labelKey) ?? "",
      descriptionKey: qualifyKey(provider.pluginId, provider.descriptionKey),
      pluginId: provider.pluginId,
      fields: provider.fields ?? [],
      credentialType: !!provider.credentialType,
      needsUserInteraction: !!provider.needsUserInteraction,
      supportsBackground: provider.supportsBackground !== false,
      quickConnect: !!provider.quickConnect,
      available: true,
    }),
  );

  const known = new Set(summaries.map((summary) => summary.type));
  for (const owner of listSshAuthTypeOwners()) {
    if (known.has(owner.type)) continue;
    summaries.push({
      type: owner.type,
      labelKey: "",
      pluginId: owner.pluginId,
      fields: [],
      credentialType: false,
      needsUserInteraction: false,
      supportsBackground: false,
      quickConnect: false,
      available: false,
      missingPlugin: { id: owner.pluginId, name: owner.pluginName },
    });
  }
  return summaries;
}

/**
 * @openapi
 * /ssh-auth/providers:
 *   get:
 *     summary: List SSH auth types
 *     description: Every SSH auth type the server can connect with, from core and from plugins, plus types declared by plugins that are disabled so the host editor can explain them.
 *     tags:
 *       - SSH Auth
 *     responses:
 *       200:
 *         description: Auth type list.
 *       401:
 *         description: Not authenticated.
 */
router.get("/providers", authenticateJWT, (_req: Request, res: Response) => {
  res.json({ providers: summarizeSshAuthProviders() });
});

export default router;