UNPKG

@denis-kalinin/dev-certs

Version:

Managing certificates on nodejs http server

105 lines 4.59 kB
// 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.generateCertificates = void 0; const fs = require("fs"); const fsExtra = require("fs-extra"); const mkcert = require("mkcert"); const path = require("path"); const defaults = require("./defaults"); const verify_1 = require("./verify"); /* global console */ /* Generate operation will check if there is already valid certificate installed. if yes, then this operation will be no op. else, new certificates are generated and installed if --install was provided. */ function generateCertificates(pkiConfig) { return __awaiter(this, void 0, void 0, function* () { const localPath = defaults.getLocalPath(pkiConfig.cert.fileName); try { fsExtra.ensureDirSync(path.dirname(defaults.caCertificatePath)); fsExtra.ensureDirSync(path.dirname(`${localPath}.crt`)); fsExtra.ensureDirSync(path.dirname(`${localPath}.key`)); } catch (err) { throw new Error(`Unable to create the directory.\n${err}`); } const caCertPath = path.join(defaults.certificateDirectory, defaults.caCertificateFileName); const caKeyPath = path.join(defaults.certificateDirectory, defaults.caKeyFileName); let caExists = false; try { caExists = fs.existsSync(caCertPath) && fs.existsSync(caKeyPath); } catch (err) { caExists = false; } let caCertKey = undefined; if (caExists) { try { caCertKey = (0, verify_1.validateCertificateAndKey)(caCertPath, caKeyPath); } catch (err) { caExists = false; } } if (!caCertKey) { const ca = pkiConfig.ca; const caCertificateInfo = { countryCode: ca.countryCode, locality: ca.locality, organization: ca.organization, state: ca.state, validityDays: ca.validityDays, }; try { const caCertificate = yield mkcert.createCA(caCertificateInfo); caCertKey = { certificate: caCertificate.cert, key: caCertificate.key }; } catch (err) { throw new Error(`Unable to generate the CA certificate.\n${err}`); } } const localhostCertificateInfo = { caCert: caCertKey.certificate, caKey: caCertKey.key, domains: pkiConfig.cert.domains, validityDays: pkiConfig.cert.validityDays, }; let localhostCertificate; try { localhostCertificate = yield mkcert.createCert(localhostCertificateInfo); } catch (err) { throw new Error(`Unable to generate the localhost certificate.\n${err}`); } try { if (!caExists) { fs.writeFileSync(defaults.caCertificatePath, caCertKey.certificate); const caCertificateKeyPath = path.join(defaults.certificateDirectory, "ca.key"); fs.writeFileSync(caCertificateKeyPath, caCertKey.key); } fs.writeFileSync(`${localPath}.crt`, localhostCertificate.cert); fs.writeFileSync(`${localPath}.key`, localhostCertificate.key); } catch (err) { throw new Error(`Unable to write generated certificates.\n${err}`); } if (defaults.caCertificatePath) { console.log(`The developer certificates have been generated in ${defaults.certificateDirectory}`); } else { console.log("The developer certificates have been generated."); } }); } exports.generateCertificates = generateCertificates; //# sourceMappingURL=generate.js.map