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';
|
|
|
|
import { ICertRemoteResponse } 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-08 19:45:35 +00:00
|
|
|
constructor(optionsArg: {
|
|
|
|
remoteUrl: string;
|
|
|
|
secret: string;
|
|
|
|
}) {
|
2019-01-08 23:01:01 +00:00
|
|
|
this.remoteUrl = optionsArg.remoteUrl;
|
|
|
|
this.secret = optionsArg.secret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @param domainNameArg
|
|
|
|
*/
|
|
|
|
async getCertificateForDomain(domainNameArg: string): Promise<interfaces.ICert> {
|
|
|
|
let certificate: interfaces.ICert;
|
|
|
|
const doRequestCycle = async (): Promise<interfaces.ICert> => {
|
|
|
|
const response: ICertRemoteResponse = (await plugins.smartrequest.postJson(this.remoteUrl, {
|
|
|
|
requestBody: <interfaces.ICertRemoteRequest>{
|
|
|
|
domainName: domainNameArg,
|
|
|
|
secret: this.secret
|
|
|
|
}
|
|
|
|
})).body;
|
|
|
|
switch(response.status) {
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|