Add API key host enrollment endpoint (#1029)

This commit is contained in:
ZacharyZcR
2026-07-13 22:59:36 +08:00
committed by GitHub
parent 033dd38344
commit 1c18620629
5 changed files with 240 additions and 1 deletions
@@ -0,0 +1,46 @@
import type { NextFunction, Request, Response } from "express";
import type { AuthenticatedRequest } from "../../../types/index.js";
import { SimpleDBOps } from "../../utils/simple-db-ops.js";
export function applyHostEnrollmentDefaults(
hostData: Record<string, unknown>,
): Record<string, unknown> {
return {
connectionType: "ssh",
port: 22,
authType: "none",
enableTerminal: true,
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 (!SimpleDBOps.isUserDataUnlocked(authReq.userId)) {
res.status(423).json({
error: "User data is locked. Sign in before enrolling hosts.",
code: "DATA_LOCKED",
});
return;
}
next();
}