50 lines
1.5 KiB
TypeScript
50 lines
1.5 KiB
TypeScript
import { NSQLITE_TURBOCI_ADMIN_USERS } from "@/src/db/types";
|
|
import userAuth from "@/src/utils/user-auth";
|
|
import NSQLite from "@moduletrace/nsqlite";
|
|
import { APIResponseObject } from "@moduletrace/datasquirel/dist/package-shared/types";
|
|
import type { NextApiRequest, NextApiResponse } from "next";
|
|
import { APIReqObject } from "@/src/types";
|
|
import { createReadStream, existsSync } from "fs";
|
|
|
|
export default async function handler(
|
|
req: NextApiRequest,
|
|
res: NextApiResponse<APIResponseObject>,
|
|
) {
|
|
try {
|
|
const { singleRes: user } = await userAuth({ req });
|
|
|
|
if (!user?.id || !user.super_admin) {
|
|
return res.json({
|
|
success: false,
|
|
msg: "Unauthorized",
|
|
});
|
|
}
|
|
|
|
const { user_id } = req.query as APIReqObject;
|
|
|
|
const target_user_res =
|
|
await NSQLite.select<NSQLITE_TURBOCI_ADMIN_USERS>({
|
|
table: "users",
|
|
targetId: user_id,
|
|
});
|
|
|
|
const target_user = target_user_res.singleRes;
|
|
|
|
if (!target_user?.username) {
|
|
throw new Error(`Couldn't grab user`);
|
|
}
|
|
|
|
const file_path = `/home/${target_user.username}/.ssh/${target_user.username}`;
|
|
|
|
if (!existsSync(file_path)) {
|
|
throw new Error(`No Private SSH key file found`);
|
|
}
|
|
|
|
const read_stream = createReadStream(file_path);
|
|
|
|
read_stream.pipe(res);
|
|
} catch (error: any) {
|
|
return res.json({ success: false, msg: error.message });
|
|
}
|
|
}
|