"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.checkAuthFile = exports.deleteAuthFile = exports.getAuthFile = exports.cleanupUserAuthFiles = exports.writeAuthFile = exports.initAuthFiles = exports.grabAuthDirs = void 0; const fs_1 = __importDefault(require("fs")); const path_1 = __importDefault(require("path")); const ejson_1 = __importDefault(require("../../../utils/ejson")); const debug_log_1 = __importDefault(require("../../../utils/logging/debug-log")); function debugFn(log, label) { (0, debug_log_1.default)({ log, addTime: true, title: "write-auth-files", label }); } const grabAuthDirs = () => { const DSQL_AUTH_DIR = process.env.DSQL_AUTH_DIR; const ROOT_DIR = (DSQL_AUTH_DIR === null || DSQL_AUTH_DIR === void 0 ? void 0 : DSQL_AUTH_DIR.match(/./)) ? DSQL_AUTH_DIR : path_1.default.resolve(process.cwd(), "./.tmp"); const AUTH_DIR = path_1.default.join(ROOT_DIR, "logins"); return { root: ROOT_DIR, auth: AUTH_DIR }; }; exports.grabAuthDirs = grabAuthDirs; const initAuthFiles = () => { var _a; try { const authDirs = (0, exports.grabAuthDirs)(); if (!fs_1.default.existsSync(authDirs.root)) fs_1.default.mkdirSync(authDirs.root, { recursive: true }); if (!fs_1.default.existsSync(authDirs.auth)) fs_1.default.mkdirSync(authDirs.auth, { recursive: true }); return true; } catch (error) { console.log(`Error initializing Auth Files: ${error.message}`); (_a = global.ERROR_CALLBACK) === null || _a === void 0 ? void 0 : _a.call(global, `Error Initializing Auth Files`, error); return false; } }; exports.initAuthFiles = initAuthFiles; /** * # Write Auth Files */ const writeAuthFile = (name, data, cleanup) => { (0, exports.initAuthFiles)(); try { const { auth } = (0, exports.grabAuthDirs)(); if (cleanup) { (0, exports.cleanupUserAuthFiles)(cleanup.userId); } fs_1.default.writeFileSync(path_1.default.join(auth, name), data); return true; } catch (error) { console.log(`Error writing Auth File: ${error.message}`); return false; } }; exports.writeAuthFile = writeAuthFile; /** * # Clean up User Auth Files */ const cleanupUserAuthFiles = (userId) => { (0, exports.initAuthFiles)(); try { const { auth } = (0, exports.grabAuthDirs)(); const loginFiles = fs_1.default.readdirSync(auth); for (let i = 0; i < loginFiles.length; i++) { const loginFile = loginFiles[i]; const loginFilePath = path_1.default.join(auth, loginFile); try { const authPayload = ejson_1.default.parse(fs_1.default.readFileSync(loginFilePath, "utf-8")); if (authPayload.id == userId) { fs_1.default.unlinkSync(loginFilePath); } } catch (error) { } } return true; } catch (error) { console.log(`Error Cleaning up User Auth Files: ${error.message}`); return false; } }; exports.cleanupUserAuthFiles = cleanupUserAuthFiles; /** * # Get Auth Files */ const getAuthFile = (name) => { try { const authFilePath = path_1.default.join((0, exports.grabAuthDirs)().auth, name); return fs_1.default.readFileSync(authFilePath, "utf-8"); } catch (error) { console.log(`Error getting Auth File: ${error.message}`); return null; } }; exports.getAuthFile = getAuthFile; /** * # Delete Auth Files * @param {string} name */ const deleteAuthFile = (name) => { try { return fs_1.default.rmSync(path_1.default.join((0, exports.grabAuthDirs)().auth, name)); } catch (error) { console.log(`Error deleting Auth File: ${error.message}`); return null; } }; exports.deleteAuthFile = deleteAuthFile; /** * # Delete Auth Files * @param {string} name */ const checkAuthFile = (name) => { try { return fs_1.default.existsSync(path_1.default.join((0, exports.grabAuthDirs)().auth, name)); return true; } catch (error) { console.log(`Error checking Auth File: ${error.message}`); return false; } }; exports.checkAuthFile = checkAuthFile;