UNPKG

@smartdcc/dccboxed-keystore

Version:
133 lines 5.46 kB
"use strict"; /* * Created on Thu Aug 04 2022 * * Copyright (c) 2022 Smart DCC Limited * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see <http://www.gnu.org/licenses/>. */ Object.defineProperty(exports, "__esModule", { value: true }); exports.BoxedKeyStore = exports.defaultBackingFile = void 0; const promises_1 = require("node:fs/promises"); const node_path_1 = require("node:path"); const tmp_1 = require("tmp"); const certificateMetadata_1 = require("./certificateMetadata"); const certificateSearch_1 = require("./certificateSearch"); const db_1 = require("./db"); exports.defaultBackingFile = (0, node_path_1.resolve)(__dirname, '..', 'keystore.json'); class BoxedKeyStore extends db_1.KeyStoreDB { get temporyFile() { return this._temporyFile; } /** * Creates a new caching key store * * @param boxedAddress ip address of a DCC Boxed instance * @param backingDB backing readonly database * @param localFile local file to cache updates to, if undefined a temporary file is created * @param _temporyFile */ constructor(boxedAddress, backingDB, localFile, _temporyFile, headers) { super(localFile); this.boxedAddress = boxedAddress; this.backingDB = backingDB; this._temporyFile = _temporyFile; this.headers = headers; } /** * Wrap constructor for async operations. * * @param boxedAddress ip address of a DCC Boxed instance * @param localFile local file to cache updates to, if undefined a temporary file is created * @param backingFile backing readonly database file, if undefined a default backing file is used * @returns */ static async new(boxedAddress, localFile, backingFile, headers) { let tmp_flag = false; if (typeof localFile !== 'string') { localFile = (0, tmp_1.tmpNameSync)({ postfix: '.json' }); tmp_flag = true; } const instance = new BoxedKeyStore(boxedAddress, await db_1.KeyStoreDB.new(backingFile ?? exports.defaultBackingFile), localFile, tmp_flag ? localFile : undefined, headers); await instance.db.load(); return instance; } async query(options) { let x = await super.query(options); if (x) { return x; } x = await this.backingDB.query(options); if (x) { return x; } if (options.lookup === 'certificate') { if ((0, db_1.queryOptionsHasEUI)(options)) { let keyUsage; switch (options.keyUsage) { case certificateMetadata_1.KeyUsage.digitalSignature: keyUsage = "DS" /* CertificateUsage['Digital Signing'] */; break; case certificateMetadata_1.KeyUsage.keyAgreement: keyUsage = "KA" /* CertificateUsage['Key Agreement'] */; break; default: return null; } /* role is only valid for org certs which are not currently supported to be queried */ //const role: { CertificateRole?: number } = {} if (typeof options.role === 'number') { return null; // role.CertificateRole = options.role } const sr = { CertificateUsage: keyUsage, CertificateStatus: "I" /* CertificateStatus['In use'] */, // ...role, q: { CertificateSubjectAltName: (0, certificateMetadata_1.normaliseEUI)(options.eui) .toString() .replace(/(.{2})(?!$)/g, '$1-'), }, }; const qrs = await (0, certificateSearch_1.search)(sr, this.boxedAddress, this.headers); if (qrs.length >= 1) { for (const qr of qrs) { await super.push({ certificate: qr.x509 }); } return qrs.map(({ meta, x509 }) => ({ ...meta, certificate: x509 })); } } else { const r = await (0, certificateSearch_1.query)(options.serial.toString(16), this.boxedAddress, this.headers); if (r) { await super.push({ certificate: r.x509 }); return { ...r.meta, certificate: r.x509 }; } } } return null; } /** * deletes any temporary files created */ async cleanup() { if (typeof this.temporyFile === 'string') { await (0, promises_1.rm)(this.temporyFile); } } } exports.BoxedKeyStore = BoxedKeyStore; //# sourceMappingURL=boxedKeyStore.js.map