import * as plugins from '../smartacme.plugins.js'; import type { ICertManager } from '../interfaces/certmanager.js'; import { SmartacmeCert } from '../smartacme.classes.cert.js'; /** * MongoDB-backed certificate manager using EasyStore from smartdata. */ export class MongoCertManager implements ICertManager { private db: plugins.smartdata.SmartdataDb; private store: plugins.smartdata.EasyStore>; /** * @param mongoDescriptor MongoDB connection settings */ constructor(mongoDescriptor: plugins.smartdata.IMongoDescriptor) { this.db = new plugins.smartdata.SmartdataDb(mongoDescriptor); // Use a single EasyStore document to hold all certs keyed by domainName this.store = new plugins.smartdata.EasyStore>( 'smartacme-certs', this.db, ); } public async init(): Promise { await this.db.init(); } public async retrieveCertificate(domainName: string): Promise { const data = await this.store.readKey(domainName); return data ? new SmartacmeCert(data) : null; } public async storeCertificate(cert: SmartacmeCert): Promise { // write plain object for persistence await this.store.writeKey(cert.domainName, { ...cert }); } public async deleteCertificate(domainName: string): Promise { await this.store.deleteKey(domainName); } public async close(): Promise { await this.db.close(); } /** * Wipe all certificates from the persistent store (for integration testing) */ public async wipe(): Promise { // clear all keys in the easy store await this.store.wipe(); } }