smartacme/ts/smartacme.classes.certmanager.ts

78 lines
2.0 KiB
TypeScript
Raw Normal View History

2019-01-08 19:45:35 +00:00
import * as plugins from './smartacme.plugins';
import { Cert } from './smartacme.classes.cert';
2019-01-08 23:01:01 +00:00
import { SmartAcme } from './smartacme.classes.smartacme';
import * as interfaces from './interfaces';
2019-01-08 19:45:35 +00:00
export class CertManager {
// =========
// 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, Cert>;
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();
CertManager.activeDB = this.smartdataDb;
2019-01-08 23:01:01 +00:00
// Pending Map
2020-02-21 10:48:08 +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<Cert> {
2019-01-08 19:45:35 +00:00
const existingCertificate: Cert = await Cert.getInstance({
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) {
2019-01-12 12:44:18 +00:00
const cert = new Cert(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: Cert = await Cert.getInstance({
domainName: certDomainNameArg
});
await cert.delete();
2019-02-06 08:47:33 +00:00
}
2019-01-08 19:45:35 +00:00
}