This commit is contained in:
2026-03-09 14:35:16 +00:00
parent 3a131cb30c
commit 8491c52639
13 changed files with 132 additions and 15 deletions
+23
View File
@@ -0,0 +1,23 @@
import { existsSync, readFileSync } from "fs";
import grabDirNames from "./grab-dir-names";
export default function grabCookieNames() {
const { TURBOCI_DEPLOYMENT_ID_FILE } = grabDirNames();
if (!existsSync(TURBOCI_DEPLOYMENT_ID_FILE)) {
throw new Error(`\`${TURBOCI_DEPLOYMENT_ID_FILE}\` does not exist.`);
}
const deployment_id = readFileSync(TURBOCI_DEPLOYMENT_ID_FILE, "utf-8");
const deplyment_uid_short = deployment_id.split("-").shift();
if (!deplyment_uid_short) {
throw new Error(`Invalid deployment_id`);
}
const auth_key_cookie_name = `turboci-admin-${deplyment_uid_short}-auth-key`;
const csrf_cookie_name = `turboci-admin-${deplyment_uid_short}-csrf`;
return { auth_key_cookie_name, csrf_cookie_name };
}
+2
View File
@@ -8,6 +8,7 @@ export default function grabDirNames() {
TURBOCI_CONFIG_DIR,
"turboci.json",
);
const TURBOCI_DEPLOYMENT_ID_FILE = path.join(TURBOCI_DIR, "deployment_id");
const TURBOCI_SSH_DIR = path.join(TURBOCI_DIR, ".ssh");
const TURBOCI_SSH_KEY_FILE = path.join(TURBOCI_SSH_DIR, "turboci");
@@ -19,5 +20,6 @@ export default function grabDirNames() {
TURBOCI_DIR,
TURBOCI_SSH_DIR,
TURBOCI_SSH_KEY_FILE,
TURBOCI_DEPLOYMENT_ID_FILE,
};
}
+5 -3
View File
@@ -3,13 +3,13 @@ import { ServerResponse } from "http";
import NSQLite from "@moduletrace/nsqlite";
import { NSQLITE_TEST_DB_USERS, NSQLiteTables } from "../db/types";
import { User } from "../types";
import { AppData } from "../data/app-data";
import { setCookie } from "./cookies-actions";
import { EJSON } from "../exports/client-exports";
import encrypt from "@moduletrace/datasquirel/dist/package-shared/functions/dsql/encrypt";
import { APIResponseObject } from "@moduletrace/datasquirel/dist/package-shared/types";
import hashPassword from "@moduletrace/datasquirel/dist/package-shared/functions/dsql/hashPassword";
import dayjs from "dayjs";
import grabCookieNames from "./grab-cookie-names";
type Params = {
res: NextApiResponse | ServerResponse;
@@ -111,9 +111,11 @@ export default async function loginUser({
const expiration_date = dayjs(Date.now()).add(7, "days");
expiration_date.add(7, "days");
const { auth_key_cookie_name, csrf_cookie_name } = grabCookieNames();
setCookie(res, [
{
name: AppData["AuthCookieName"],
name: auth_key_cookie_name,
value: encrypted_payload || "",
options: {
secure: process.env.DOMAIN !== "localhost",
@@ -123,7 +125,7 @@ export default async function loginUser({
},
},
{
name: AppData["AuthCSRFCookieName"],
name: csrf_cookie_name,
value: csrf_k,
options: {
path: "/",
+6 -5
View File
@@ -1,11 +1,11 @@
import { NextApiRequest } from "next";
import { User } from "../types";
import { IncomingMessage } from "http";
import { AppData } from "../data/app-data";
import { getCookie } from "./cookies-actions";
import { APIResponseObject } from "@moduletrace/datasquirel/dist/package-shared/types";
import decrypt from "@moduletrace/datasquirel/dist/package-shared/functions/dsql/decrypt";
import { EJSON } from "../exports/client-exports";
import grabCookieNames from "./grab-cookie-names";
type Params = {
req:
@@ -17,12 +17,13 @@ export default async function userAuth({
req,
}: Params): Promise<APIResponseObject<User>> {
try {
const key = getCookie(req, AppData["AuthCookieName"]);
const { auth_key_cookie_name, csrf_cookie_name } = grabCookieNames();
const key = getCookie(req, auth_key_cookie_name);
if (!key) {
return {
success: false,
msg: `No ${AppData["AuthCookieName"]} found in request object.`,
msg: `No ${auth_key_cookie_name} found in request object.`,
};
}
@@ -36,12 +37,12 @@ export default async function userAuth({
};
}
const csrf = getCookie(req, AppData["AuthCSRFCookieName"]);
const csrf = getCookie(req, csrf_cookie_name);
if (!csrf) {
return {
success: false,
msg: `No ${AppData["AuthCSRFCookieName"]} found in request object.`,
msg: `No ${csrf_cookie_name} found in request object.`,
};
}