smartacme/ts/smartacme.classes.certmanager.ts

78 lines
2.1 KiB
TypeScript
Raw Normal View History

2022-09-27 13:40:55 +00:00
import * as plugins from './smartacme.plugins.js';
import { SmartacmeCert } from './smartacme.classes.cert.js';
2022-09-27 13:40:55 +00:00
import { SmartAcme } from './smartacme.classes.smartacme.js';
2019-01-08 23:01:01 +00:00
2022-09-27 13:40:55 +00:00
import * as interfaces from './interfaces/index.js';
2019-01-08 19:45:35 +00:00
export class SmartacmeCertManager {
2019-01-08 19:45:35 +00:00
// =========
// STATIC
// =========
public static activeDB: plugins.smartdata.SmartdataDb;
2019-02-06 08:47:33 +00:00
2019-01-08 19:45:35 +00:00
// =========
// INSTANCE
// =========
private mongoDescriptor: plugins.smartdata.IMongoDescriptor;
public smartdataDb: plugins.smartdata.SmartdataDb;
public interestMap: plugins.lik.InterestMap<string, SmartacmeCert>;
2019-01-08 23:01:01 +00:00
2019-02-06 08:47:33 +00:00
constructor(
smartAcmeArg: SmartAcme,
optionsArg: {
mongoDescriptor: plugins.smartdata.IMongoDescriptor;
}
) {
2019-01-08 19:45:35 +00:00
this.mongoDescriptor = optionsArg.mongoDescriptor;
}
2019-02-06 08:47:33 +00:00
public async init() {
2019-01-08 23:01:01 +00:00
// Smartdata DB
2019-01-08 19:45:35 +00:00
this.smartdataDb = new plugins.smartdata.SmartdataDb(this.mongoDescriptor);
await this.smartdataDb.init();
SmartacmeCertManager.activeDB = this.smartdataDb;
2019-01-08 23:01:01 +00:00
// Pending Map
2020-08-12 16:36:06 +00:00
this.interestMap = new plugins.lik.InterestMap((certName) => certName);
2019-01-13 20:40:40 +00:00
}
2019-01-08 19:45:35 +00:00
/**
* retrieves a certificate
* @returns the Cert class or null
* @param certDomainNameArg the domain Name to retrieve the vcertificate for
2019-01-08 19:45:35 +00:00
*/
public async retrieveCertificate(certDomainNameArg: string): Promise<SmartacmeCert> {
const existingCertificate: SmartacmeCert = await SmartacmeCert.getInstance<SmartacmeCert>({
2020-08-12 16:36:06 +00:00
domainName: certDomainNameArg,
2019-01-08 19:45:35 +00:00
});
2019-02-06 08:47:33 +00:00
if (existingCertificate) {
2019-01-08 19:45:35 +00:00
return existingCertificate;
} else {
return null;
}
}
/**
2019-01-15 22:39:31 +00:00
* stores the certificate
2019-02-06 08:47:33 +00:00
* @param optionsArg
2019-01-08 19:45:35 +00:00
*/
public async storeCertificate(optionsArg: plugins.tsclass.network.ICert) {
const cert = new SmartacmeCert(optionsArg);
2019-01-13 18:40:32 +00:00
await cert.save();
const interest = this.interestMap.findInterest(cert.domainName);
if (interest) {
interest.fullfillInterest(cert);
interest.markLost();
2019-01-08 23:01:01 +00:00
}
}
2019-01-08 19:45:35 +00:00
public async deleteCertificate(certDomainNameArg: string) {
const cert: SmartacmeCert = await SmartacmeCert.getInstance<SmartacmeCert>({
2020-08-12 16:36:06 +00:00
domainName: certDomainNameArg,
});
await cert.delete();
2019-02-06 08:47:33 +00:00
}
2019-01-08 19:45:35 +00:00
}