Skip to content

Self-hosted, plugin-based server management.

1.0 KB
Raw Blame
import type { NextFunction, Request, Response } from "express";
import type { AuthenticatedRequest } from "../../../types/index.js";
import { DataCrypto } from "../../utils/data-crypto.js";

export function applyHostEnrollmentDefaults(
  hostData: Record<string, unknown>,
): Record<string, unknown> {
  return {
    connectionType: "ssh",
    port: 22,
    authType: "none",
    enableSsh: true,
    ...hostData,
  };
}

export function requireHostEnrollmentAccessForPath(
  req: Request,
  res: Response,
  next: NextFunction,
): void {
  if (req.path !== "/enroll") {
    next();
    return;
  }

  const authReq = req as AuthenticatedRequest;
  if (!authReq.apiKeyId) {
    res.status(401).json({
      error: "Host enrollment requires an API key",
      code: "API_KEY_REQUIRED",
    });
    return;
  }

  if (!DataCrypto.canUserAccessData(authReq.userId)) {
    res.status(423).json({
      error: "User data is locked. Sign in before enrolling hosts.",
      code: "DATA_LOCKED",
    });
    return;
  }

  next();
}