"use strict"; // Copyright (c) Microsoft Corporation. All rights reserved. // Licensed under the MIT license. 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; }; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.installCaCertificate = exports.ensureCertificatesAreInstalled = void 0; const child_process_1 = require("child_process"); const path_1 = __importDefault(require("path")); const defaults = __importStar(require("./defaults")); const generate_1 = require("./generate"); const uninstall_1 = require("./uninstall"); const verify_1 = require("./verify"); const defaults_1 = require("./defaults"); const office_addin_usage_data_1 = require("office-addin-usage-data"); /* global console process __dirname */ function getInstallCommand(caCertificatePath, machine = false) { switch (process.platform) { case "win32": { const script = path_1.default.resolve(__dirname, "..\\scripts\\install.ps1"); return `powershell -ExecutionPolicy Bypass -File "${script}" ${machine ? "LocalMachine" : "CurrentUser"} "${caCertificatePath}"`; } case "darwin": { // macOS const prefix = machine ? "sudo " : ""; const keychainFile = machine ? "/Library/Keychains/System.keychain" : "~/Library/Keychains/login.keychain-db"; return `${prefix}security add-trusted-cert -d -r trustRoot -k ${keychainFile} '${caCertificatePath}'`; } case "linux": { const script = path_1.default.resolve(__dirname, "../scripts/install_linux.sh"); return `sudo sh '${script}' '${caCertificatePath}'`; } default: throw new office_addin_usage_data_1.ExpectedError(`Platform not supported: ${process.platform}`); } } function ensureCertificatesAreInstalled(daysUntilCertificateExpires = defaults.daysUntilCertificateExpires, domains = defaults.domain, machine = false) { return __awaiter(this, void 0, void 0, function* () { try { const areCertificatesValid = (0, verify_1.verifyCertificates)(); if (areCertificatesValid) { console.log(`You already have trusted access to https://localhost.\nCertificate: ${defaults.localhostCertificatePath}\nKey: ${defaults.localhostKeyPath}`); } else { yield (0, uninstall_1.uninstallCaCertificate)(false, false); (0, uninstall_1.deleteCertificateFiles)(defaults.certificateDirectory); yield (0, generate_1.generateCertificates)(defaults.caCertificatePath, defaults.localhostCertificatePath, defaults.localhostKeyPath, daysUntilCertificateExpires, domains); yield installCaCertificate(defaults.caCertificatePath, machine); } defaults_1.usageDataObject.reportSuccess("ensureCertificatesAreInstalled()"); } catch (err) { defaults_1.usageDataObject.reportException("ensureCertificatesAreInstalled()", err); throw err; } }); } exports.ensureCertificatesAreInstalled = ensureCertificatesAreInstalled; function installCaCertificate(caCertificatePath = defaults.caCertificatePath, machine = false) { return __awaiter(this, void 0, void 0, function* () { const command = getInstallCommand(caCertificatePath, machine); try { console.log(`Installing CA certificate "Developer CA for Microsoft Office Add-ins"...`); // If the certificate is already installed by another instance skip it. if (!(0, verify_1.isCaCertificateInstalled)()) { (0, child_process_1.execSync)(command, { stdio: "pipe" }); } console.log(`You now have trusted access to https://localhost.\nCertificate: ${defaults.localhostCertificatePath}\nKey: ${defaults.localhostKeyPath}`); } catch (error) { throw new Error(`Unable to install the CA certificate. ${error.stderr.toString()}`); } }); } exports.installCaCertificate = installCaCertificate; //# sourceMappingURL=install.js.map