2019-01-08 19:45:35 +00:00
|
|
|
import * as plugins from './smartacme.plugins';
|
2019-01-08 23:01:01 +00:00
|
|
|
import * as interfaces from './interfaces';
|
2019-01-08 19:45:35 +00:00
|
|
|
|
2019-01-08 23:01:01 +00:00
|
|
|
// tslint:disable-next-line: max-classes-per-file
|
2019-01-08 19:45:35 +00:00
|
|
|
export class CertRemoteClient {
|
2019-01-08 23:01:01 +00:00
|
|
|
private remoteUrl: string;
|
|
|
|
private secret: string;
|
2019-01-15 22:39:31 +00:00
|
|
|
private logger: plugins.smartlog.Smartlog;
|
2019-01-08 23:01:01 +00:00
|
|
|
|
2019-01-08 19:45:35 +00:00
|
|
|
constructor(optionsArg: {
|
|
|
|
remoteUrl: string;
|
|
|
|
secret: string;
|
2019-01-15 22:39:31 +00:00
|
|
|
logger?: plugins.smartlog.Smartlog;
|
2019-01-08 19:45:35 +00:00
|
|
|
}) {
|
2019-01-08 23:01:01 +00:00
|
|
|
this.remoteUrl = optionsArg.remoteUrl;
|
|
|
|
this.secret = optionsArg.secret;
|
2019-01-15 22:39:31 +00:00
|
|
|
optionsArg.logger
|
|
|
|
? (this.logger = optionsArg.logger)
|
|
|
|
: (this.logger = plugins.smartlog.defaultLogger);
|
2019-01-08 23:01:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-01-15 22:39:31 +00:00
|
|
|
*
|
|
|
|
* @param domainNameArg
|
2019-01-08 23:01:01 +00:00
|
|
|
*/
|
2019-01-15 22:39:31 +00:00
|
|
|
public async getCertificateForDomain(domainNameArg: string): Promise<interfaces.ICert> {
|
2019-01-08 23:01:01 +00:00
|
|
|
let certificate: interfaces.ICert;
|
|
|
|
const doRequestCycle = async (): Promise<interfaces.ICert> => {
|
2019-01-15 22:39:31 +00:00
|
|
|
const response: interfaces.ICertRemoteResponse = (await plugins.smartrequest.postJson(
|
|
|
|
this.remoteUrl,
|
|
|
|
{
|
|
|
|
requestBody: <interfaces.ICertRemoteRequest>{
|
|
|
|
domainName: domainNameArg,
|
|
|
|
secret: this.secret
|
|
|
|
}
|
2019-01-08 23:01:01 +00:00
|
|
|
}
|
2019-01-15 22:39:31 +00:00
|
|
|
)).body;
|
|
|
|
switch (response.status as interfaces.TCertStatus) {
|
2019-01-08 23:01:01 +00:00
|
|
|
case 'pending':
|
|
|
|
await plugins.smartdelay.delayFor(5000);
|
|
|
|
const finalResponse = await doRequestCycle();
|
|
|
|
return finalResponse;
|
|
|
|
case 'existing':
|
|
|
|
return response.certificate;
|
|
|
|
case 'failed':
|
|
|
|
default:
|
|
|
|
console.log(`could not retrieve certificate for ${domainNameArg}`);
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
certificate = await doRequestCycle();
|
|
|
|
return certificate;
|
2019-01-08 19:45:35 +00:00
|
|
|
}
|
|
|
|
}
|