UNPKG

@facets-cloud/facetsctlv3

Version:
86 lines (85 loc) 3.63 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); __setModuleDefault(result, mod); return result; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.ConfigService = void 0; const node_fs_1 = require("node:fs"); const os = __importStar(require("node:os")); const path = __importStar(require("node:path")); const crypto = __importStar(require("node:crypto")); const CONFIG_FILE_NAME = '.facets.login.json'; class ConfigService { static findConfigFile() { const homeConfigPath = path.join(os.homedir(), CONFIG_FILE_NAME); const cwdConfigPath = path.join(process.cwd(), CONFIG_FILE_NAME); if ((0, node_fs_1.existsSync)(homeConfigPath)) { return homeConfigPath; } if ((0, node_fs_1.existsSync)(cwdConfigPath)) { return cwdConfigPath; } return null; } static saveConfig(config) { const data = this.encryptConfig(config); const primaryPath = path.join(os.homedir(), CONFIG_FILE_NAME); const fallbackPath = path.join(process.cwd(), CONFIG_FILE_NAME); try { this.writeConfigWithPermissions(primaryPath, data); } catch { console.log(`Failed to write config to ${primaryPath}. Trying current working directory.`); this.writeConfigWithPermissions(fallbackPath, data); } } static readConfig(filePath) { const encryptedData = (0, node_fs_1.readFileSync)(filePath, 'utf8'); return this.decryptConfig(encryptedData); } static writeConfigWithPermissions(filePath, data) { const stream = (0, node_fs_1.createWriteStream)(filePath, { mode: 0o600, // Restricting file permissions }); stream.write(data); stream.end(); } static encryptConfig(config) { const algorithm = 'aes-256-cbc'; const key = crypto.randomBytes(32); const iv = crypto.randomBytes(16); const cipher = crypto.createCipheriv(algorithm, key, iv); let encrypted = cipher.update(JSON.stringify(config), 'utf8', 'hex'); encrypted += cipher.final('hex'); return JSON.stringify({ iv: iv.toString('hex'), key: key.toString('hex'), data: encrypted }); } static decryptConfig(encryptedData) { const algorithm = 'aes-256-cbc'; const { iv, key, data } = JSON.parse(encryptedData); const decipher = crypto.createDecipheriv(algorithm, Buffer.from(key, 'hex'), Buffer.from(iv, 'hex')); let decrypted = decipher.update(data, 'hex', 'utf8'); decrypted += decipher.final('utf8'); return JSON.parse(decrypted); } } exports.ConfigService = ConfigService;