Outlook_Addin_LLM/node_modules/office-addin-dev-certs/lib/install.js

79 lines
4.5 KiB
JavaScript
Raw Normal View History

// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license.
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());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.installCaCertificate = exports.ensureCertificatesAreInstalled = void 0;
const child_process_1 = require("child_process");
const path = require("path");
const defaults = 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 process, console, __dirname */
function getInstallCommand(caCertificatePath, machine = false) {
switch (process.platform) {
case "win32": {
const script = path.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.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