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;
|
|
|
|
|
|
|
|
|
|
|
|
// =========
|
|
|
|
// INSTANCE
|
|
|
|
// =========
|
|
|
|
private mongoDescriptor: plugins.smartdata.IMongoDescriptor;
|
|
|
|
public smartdataDb: plugins.smartdata.SmartdataDb;
|
|
|
|
|
2019-01-08 23:01:01 +00:00
|
|
|
public pendingMap: plugins.lik.Stringmap;
|
|
|
|
|
|
|
|
constructor(smartAcmeArg: SmartAcme,optionsArg: {
|
2019-01-08 19:45:35 +00:00
|
|
|
mongoDescriptor: plugins.smartdata.IMongoDescriptor;
|
|
|
|
}) {
|
|
|
|
this.mongoDescriptor = optionsArg.mongoDescriptor;
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
this.pendingMap = new plugins.lik.Stringmap();
|
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 domainName the domain Name to retrieve the vcertificate for
|
|
|
|
*/
|
|
|
|
public async retrieveCertificate(domainName: string): Promise<Cert> {
|
2019-01-08 23:01:01 +00:00
|
|
|
await this.checkCerts();
|
2019-01-08 19:45:35 +00:00
|
|
|
const existingCertificate: Cert = await Cert.getInstance({
|
2019-01-12 23:06:00 +00:00
|
|
|
domainName
|
2019-01-08 19:45:35 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
if(existingCertificate) {
|
|
|
|
return existingCertificate;
|
|
|
|
} else {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-01-15 22:39:31 +00:00
|
|
|
* stores the certificate
|
|
|
|
* @param optionsArg
|
2019-01-08 19:45:35 +00:00
|
|
|
*/
|
2019-01-14 01:46:36 +00:00
|
|
|
public async storeCertificate(optionsArg: interfaces.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();
|
2019-01-17 00:15:22 +00:00
|
|
|
this.pendingMap.removeString(optionsArg.domainName);
|
2019-01-13 18:40:32 +00:00
|
|
|
}
|
2019-01-08 19:45:35 +00:00
|
|
|
|
2019-01-08 23:01:01 +00:00
|
|
|
public async deleteCertificate(domainNameArg: string) {
|
2019-01-08 19:45:35 +00:00
|
|
|
|
2019-01-08 23:01:01 +00:00
|
|
|
}
|
|
|
|
|
2019-01-15 22:39:31 +00:00
|
|
|
/**
|
|
|
|
* announce a certificate as being in the process of being retrieved
|
|
|
|
*/
|
|
|
|
public async announceCertificate (domainNameArg: string) {
|
|
|
|
this.pendingMap.addString(domainNameArg);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* gets the status of a certificate by certDomain name
|
|
|
|
* @param certDomainArg
|
|
|
|
*/
|
|
|
|
public async getCertificateStatus(certDomainArg: string): Promise<interfaces.TCertStatus> {
|
|
|
|
const isPending = this.pendingMap.checkString(certDomainArg);
|
2019-01-08 23:01:01 +00:00
|
|
|
if (isPending) {
|
|
|
|
return 'pending';
|
|
|
|
}
|
|
|
|
|
|
|
|
// otherwise lets continue
|
2019-01-16 01:34:47 +00:00
|
|
|
const existingCertificate = await this.retrieveCertificate(certDomainArg);
|
2019-01-08 23:01:01 +00:00
|
|
|
if (existingCertificate) {
|
|
|
|
return 'existing';
|
|
|
|
}
|
|
|
|
|
|
|
|
return 'nonexisting';
|
|
|
|
}
|
2019-01-08 19:45:35 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* checks all certs for expiration
|
|
|
|
*/
|
2019-01-17 00:15:22 +00:00
|
|
|
private async checkCerts() {
|
|
|
|
// TODO
|
|
|
|
};
|
2019-01-08 19:45:35 +00:00
|
|
|
}
|