smartacme/ts/smartacme.classes.certremoteclient.ts

57 lines
1.8 KiB
TypeScript
Raw Permalink Normal View History

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-17 00:15:22 +00:00
const responseBody: interfaces.ICertRemoteResponse = (await plugins.smartrequest.postJson(
2019-01-15 22:39:31 +00:00
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;
2019-01-17 00:15:22 +00:00
switch (responseBody.status as interfaces.TCertStatus) {
2019-01-08 23:01:01 +00:00
case 'pending':
2019-01-18 00:34:59 +00:00
this.logger.log('info', `request for ${domainNameArg} still pending!`);
2019-01-08 23:01:01 +00:00
await plugins.smartdelay.delayFor(5000);
const finalResponse = await doRequestCycle();
return finalResponse;
case 'existing':
2019-01-17 00:15:22 +00:00
this.logger.log('ok', `got certificate for ${domainNameArg}`);
return responseBody.certificate;
2019-01-08 23:01:01 +00:00
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
}
}