This commit is contained in:
Benjamin Toby
2025-03-17 08:48:58 +01:00
parent ec4d0428bb
commit 34f843bc84
20 changed files with 113 additions and 46 deletions
+5 -1
View File
@@ -36,6 +36,7 @@ type Param = {
apiUserID?: string | number;
dbUserId?: string | number;
cleanupTokens?: boolean;
secureCookie?: boolean;
};
/**
@@ -60,6 +61,7 @@ export default async function loginUser({
dbUserId,
debug,
cleanupTokens,
secureCookie,
}: Param): Promise<APILoginFunctionReturn> {
const grabedHostNames = grabHostNames({ userId: user_id || apiUserID });
const { host, port, scheme } = grabedHostNames;
@@ -266,7 +268,9 @@ export default async function loginUser({
}
response?.setHeader("Set-Cookie", [
`${authKeyName}=${encryptedPayload};samesite=strict;path=/;HttpOnly=true;Secure=true`,
`${authKeyName}=${encryptedPayload};samesite=strict;path=/;HttpOnly=true${
secureCookie ? ";Secure=true" : ""
}`,
`${csrfName}=${httpResponse.payload?.csrf_k};samesite=strict;path=/;HttpOnly=true`,
]);
+5 -1
View File
@@ -25,6 +25,7 @@ type Param = {
additionalFields?: string[];
encryptedUserString?: string;
user_id?: string | number;
secureCookie?: boolean;
};
/**
@@ -41,6 +42,7 @@ export default async function reauthUser({
additionalFields,
encryptedUserString,
user_id,
secureCookie,
}: Param): Promise<APILoginFunctionReturn> {
/**
* Check Encryption Keys
@@ -188,7 +190,9 @@ export default async function reauthUser({
const csrfName = cookieNames.csrfCookieName;
response?.setHeader("Set-Cookie", [
`${authKeyName}=${encryptedPayload};samesite=strict;path=/;HttpOnly=true;Secure=true`,
`${authKeyName}=${encryptedPayload};samesite=strict;path=/;HttpOnly=true${
secureCookie ? ";Secure=true" : ""
}`,
`${csrfName}=${httpResponse.payload?.csrf_k};samesite=strict;path=/;HttpOnly=true`,
]);
@@ -31,6 +31,7 @@ type Param = {
additionalFields?: string[];
additionalData?: { [s: string]: string | number };
user_id?: boolean;
secureCookie?: boolean;
};
/**
@@ -49,6 +50,7 @@ export default async function githubAuth({
additionalFields,
user_id,
additionalData,
secureCookie,
}: Param): Promise<FunctionReturn | undefined> {
/**
* Check inputs
@@ -228,10 +230,10 @@ export default async function githubAuth({
const csrfName = `datasquirel_${dsqlUserId}_${database}_csrf`;
response.setHeader("Set-Cookie", [
`${authKeyName}=${encryptedPayload};samesite=strict;path=/;HttpOnly=true;Secure=true`,
`${authKeyName}=${encryptedPayload};samesite=strict;path=/;HttpOnly=true${
secureCookie ? ";Secure=true" : ""
}`,
`${csrfName}=${user.csrf_k};samesite=strict;path=/;HttpOnly=true`,
`dsqluid=${dsqlUserId};samesite=strict;path=/;HttpOnly=true`,
`datasquirel_social_id=${user.social_id};samesite=strict;path=/`,
]);
}
@@ -17,6 +17,7 @@ type Param = {
additionalData?: { [s: string]: string | number };
apiUserID?: string | number;
debug?: boolean;
secureCookie?: boolean;
};
/**
@@ -33,6 +34,7 @@ export default async function googleAuth({
additionalData,
apiUserID,
debug,
secureCookie,
}: Param): Promise<APILoginFunctionReturn> {
const grabedHostNames = grabHostNames({
userId: apiUserID || process.env.DSQL_API_USER_ID,
@@ -192,7 +194,9 @@ export default async function googleAuth({
const csrfName = cookieNames.csrfCookieName;
response?.setHeader("Set-Cookie", [
`${authKeyName}=${encryptedPayload};samesite=strict;path=/;HttpOnly=true;Secure=true`,
`${authKeyName}=${encryptedPayload};samesite=strict;path=/;HttpOnly=true${
secureCookie ? ";Secure=true" : ""
}`,
`${csrfName}=${httpResponse.payload?.csrf_k};samesite=strict;path=/;HttpOnly=true`,
]);
}
@@ -2,6 +2,11 @@ import fs from "fs";
import path from "path";
import EJSON from "../../../utils/ejson";
import { DATASQUIREL_LoggedInUser } from "../../../types";
import debugLog from "../../../utils/logging/debug-log";
function debugFn(log: any, label?: string) {
debugLog({ log, addTime: true, title: "write-auth-files", label });
}
export const grabAuthDirs = () => {
const DSQL_AUTH_DIR = process.env.DSQL_AUTH_DIR;
@@ -43,14 +48,15 @@ export const writeAuthFile = (
}
) => {
initAuthFiles();
try {
const { auth, root } = grabAuthDirs();
const { auth } = grabAuthDirs();
if (cleanup) {
cleanupUserAuthFiles(cleanup.userId);
}
fs.writeFileSync(path.join(auth, name), data);
return true;
} catch (/** @type {any} */ error: any) {
} catch (error: any) {
console.log(`Error writing Auth File: ${error.message}`);
return false;
}
@@ -67,14 +73,16 @@ export const cleanupUserAuthFiles = (userId: string | number) => {
for (let i = 0; i < loginFiles.length; i++) {
const loginFile = loginFiles[i];
const loginFilePath = path.join(auth, loginFile);
try {
const authPayload = EJSON.parse(
fs.readFileSync(loginFilePath, "utf-8")
) as DATASQUIREL_LoggedInUser;
if (authPayload.id == userId) {
fs.unlinkSync(loginFilePath);
}
} catch (error) {}
} catch (error: any) {}
}
return true;
} catch (error: any) {
@@ -90,7 +98,7 @@ export const getAuthFile = (name: string) => {
try {
const authFilePath = path.join(grabAuthDirs().auth, name);
return fs.readFileSync(authFilePath, "utf-8");
} catch (/** @type {any} */ error: any) {
} catch (error: any) {
console.log(`Error getting Auth File: ${error.message}`);
return null;
}
@@ -103,7 +111,7 @@ export const getAuthFile = (name: string) => {
export const deleteAuthFile = (name: string) => {
try {
return fs.rmSync(path.join(grabAuthDirs().auth, name));
} catch (/** @type {any} */ error: any) {
} catch (error: any) {
console.log(`Error deleting Auth File: ${error.message}`);
return null;
}
@@ -117,7 +125,7 @@ export const checkAuthFile = (name: string) => {
try {
return fs.existsSync(path.join(grabAuthDirs().auth, name));
return true;
} catch (/** @type {any} */ error: any) {
} catch (error: any) {
console.log(`Error checking Auth File: ${error.message}`);
return false;
}
+6 -6
View File
@@ -25,13 +25,13 @@ async function grantFullPrivileges({ userId }: { userId: string | null }) {
const datasquirelUserDatabase = datasquirelUserDatabases[i];
const { Database } = datasquirelUserDatabase;
const grantDbPriviledges = await noDatabaseDbHandler(
`GRANT ALL PRIVILEGES ON ${Database}.* TO '${process.env.DSQL_DB_FULL_ACCESS_USERNAME}'@'%' WITH GRANT OPTION`
);
// const grantDbPriviledges = await noDatabaseDbHandler(
// `GRANT ALL PRIVILEGES ON ${Database}.* TO '${process.env.DSQL_DB_FULL_ACCESS_USERNAME}'@'%' WITH GRANT OPTION`
// );
const grantRead = await noDatabaseDbHandler(
`GRANT SELECT ON ${Database}.* TO '${process.env.DSQL_DB_READ_ONLY_USERNAME}'@'%'`
);
// const grantRead = await noDatabaseDbHandler(
// `GRANT SELECT ON ${Database}.* TO '${process.env.DSQL_DB_READ_ONLY_USERNAME}'@'%'`
// );
}
const flushPriviledged = await noDatabaseDbHandler(`FLUSH PRIVILEGES`);
@@ -4,15 +4,19 @@ import path from "path";
type Param = {
user?: DATASQUIREL_LoggedInUser | UserType;
userId?: string | number | null;
appDir?: string;
};
export default function grabDirNames(param?: Param) {
const appDir = process.env.DSQL_APP_DIR;
const schemasDir = process.env.DSQL_DB_SCHEMA_DIR;
const tempDirName = ".tmp";
const appDir = param?.appDir || process.env.DSQL_APP_DIR;
if (!appDir)
throw new Error("Please provide the `DSQL_APP_DIR` env variable.");
const schemasDir =
process.env.DSQL_DB_SCHEMA_DIR ||
path.join(appDir, "jsonData", "dbSchemas");
const tempDirName = ".tmp";
if (!schemasDir)
throw new Error(
"Please provide the `DSQL_DB_SCHEMA_DIR` env variable."
@@ -67,7 +71,14 @@ export default function grabDirNames(param?: Param) {
"docker/mariadb/load-balancer/config/template/nginx.conf"
);
const dockerComposeFile = path.join(appDir, "docker-compose.yml");
const testDockerComposeFile = path.join(appDir, "test.docker-compose.yml");
const siteSetupFile = path.join(appDir, "site-setup.json");
const envFile = path.join(appDir, ".env");
const testEnvFile = path.join(appDir, "test.env");
return {
appDir,
schemasDir,
userDirPath,
mainShemaJSONFilePath,
@@ -86,5 +97,10 @@ export default function grabDirNames(param?: Param) {
userPrivateDbImportZipFileName,
userPrivateDbImportZipFilePath,
dbNginxLoadBalancerConfigFile,
dockerComposeFile,
testDockerComposeFile,
siteSetupFile,
envFile,
testEnvFile,
};
}