Compare commits
6 Commits
Author | SHA1 | Date | |
---|---|---|---|
a1134cf227 | |||
4ee1c4b08c | |||
08c3eaa65f | |||
2717f08476 | |||
f16dbeea32 | |||
a0c0230419 |
9
package-lock.json
generated
9
package-lock.json
generated
@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "@pushrocks/smartacme",
|
"name": "@pushrocks/smartacme",
|
||||||
"version": "2.0.25",
|
"version": "2.0.28",
|
||||||
"lockfileVersion": 1,
|
"lockfileVersion": 1,
|
||||||
"requires": true,
|
"requires": true,
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
@ -170,13 +170,14 @@
|
|||||||
}
|
}
|
||||||
},
|
},
|
||||||
"@pushrocks/smartexpress": {
|
"@pushrocks/smartexpress": {
|
||||||
"version": "3.0.0",
|
"version": "3.0.4",
|
||||||
"resolved": "https://verdaccio.lossless.one/@pushrocks%2fsmartexpress/-/smartexpress-3.0.0.tgz",
|
"resolved": "https://verdaccio.lossless.one/@pushrocks%2fsmartexpress/-/smartexpress-3.0.4.tgz",
|
||||||
"integrity": "sha512-F3x+AFUxkKxb5WgQuIZsfkrNzMz0PaYYIw4gdQbAPn5N/UCEP4eE2OQWXnAcCSYwsXI8hgjEuUOC9gTbCSPDRg==",
|
"integrity": "sha512-sn6yQpTGylvT/t4arCN7te8EUzimhv9JB+73XOwv503u6HokE//nT8eCAhkjprH3hgHpIgvR3iPGI8UBSCJFsw==",
|
||||||
"requires": {
|
"requires": {
|
||||||
"@pushrocks/lik": "^3.0.4",
|
"@pushrocks/lik": "^3.0.4",
|
||||||
"@pushrocks/smartfile": "^6.0.11",
|
"@pushrocks/smartfile": "^6.0.11",
|
||||||
"@pushrocks/smartpromise": "^2.0.5",
|
"@pushrocks/smartpromise": "^2.0.5",
|
||||||
|
"@pushrocks/smartrequest": "^1.1.14",
|
||||||
"@types/express": "^4.16.0",
|
"@types/express": "^4.16.0",
|
||||||
"@types/finalhandler": "0.0.33",
|
"@types/finalhandler": "0.0.33",
|
||||||
"@types/helmet": "0.0.42",
|
"@types/helmet": "0.0.42",
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "@pushrocks/smartacme",
|
"name": "@pushrocks/smartacme",
|
||||||
"version": "2.0.25",
|
"version": "2.0.28",
|
||||||
"private": false,
|
"private": false,
|
||||||
"description": "acme implementation in TypeScript",
|
"description": "acme implementation in TypeScript",
|
||||||
"main": "dist/index.js",
|
"main": "dist/index.js",
|
||||||
@ -29,13 +29,14 @@
|
|||||||
"@pushrocks/smartdata": "^3.1.13",
|
"@pushrocks/smartdata": "^3.1.13",
|
||||||
"@pushrocks/smartdelay": "^2.0.2",
|
"@pushrocks/smartdelay": "^2.0.2",
|
||||||
"@pushrocks/smartdns": "^3.0.8",
|
"@pushrocks/smartdns": "^3.0.8",
|
||||||
"@pushrocks/smartexpress": "^3.0.0",
|
"@pushrocks/smartexpress": "^3.0.4",
|
||||||
|
"@pushrocks/smartlog": "^2.0.9",
|
||||||
"@pushrocks/smartpromise": "^2.0.5",
|
"@pushrocks/smartpromise": "^2.0.5",
|
||||||
"@pushrocks/smartrequest": "^1.1.14",
|
"@pushrocks/smartrequest": "^1.1.14",
|
||||||
"@pushrocks/smartstring": "^3.0.8",
|
"@pushrocks/smartstring": "^3.0.8",
|
||||||
"@pushrocks/smarttime": "^3.0.5",
|
"@pushrocks/smarttime": "^3.0.5",
|
||||||
"@pushrocks/smartunique": "^3.0.1",
|
"@pushrocks/smartunique": "^3.0.1",
|
||||||
"acme-client": "^2.2.2"
|
"acme-client": "2.2.2"
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"@gitzone/tsbuild": "^2.1.4",
|
"@gitzone/tsbuild": "^2.1.4",
|
||||||
|
@ -3,7 +3,6 @@ import { Cert } from './smartacme.classes.cert';
|
|||||||
import { SmartAcme } from './smartacme.classes.smartacme';
|
import { SmartAcme } from './smartacme.classes.smartacme';
|
||||||
|
|
||||||
import * as interfaces from './interfaces';
|
import * as interfaces from './interfaces';
|
||||||
import { ICert } from './interfaces';
|
|
||||||
|
|
||||||
|
|
||||||
export class CertManager {
|
export class CertManager {
|
||||||
@ -35,7 +34,7 @@ export class CertManager {
|
|||||||
|
|
||||||
// Pending Map
|
// Pending Map
|
||||||
this.pendingMap = new plugins.lik.Stringmap();
|
this.pendingMap = new plugins.lik.Stringmap();
|
||||||
};
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* retrieves a certificate
|
* retrieves a certificate
|
||||||
@ -57,12 +56,10 @@ export class CertManager {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* stores the certificate with the
|
* stores the certificate
|
||||||
* @param publicKeyArg
|
* @param optionsArg
|
||||||
* @param privateKeyArg
|
|
||||||
* @param csrArg
|
|
||||||
*/
|
*/
|
||||||
public async storeCertificate(optionsArg: ICert) {
|
public async storeCertificate(optionsArg: interfaces.ICert) {
|
||||||
const cert = new Cert(optionsArg);
|
const cert = new Cert(optionsArg);
|
||||||
await cert.save();
|
await cert.save();
|
||||||
}
|
}
|
||||||
@ -71,14 +68,25 @@ export class CertManager {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public async getCertificateStatus(domainNameArg: string): Promise<interfaces.TCertStatus> {
|
/**
|
||||||
const isPending = this.pendingMap.checkString('domainNameArg');
|
* 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);
|
||||||
if (isPending) {
|
if (isPending) {
|
||||||
return 'pending';
|
return 'pending';
|
||||||
}
|
}
|
||||||
|
|
||||||
// otherwise lets continue
|
// otherwise lets continue
|
||||||
const existingCertificate = this.retrieveCertificate(domainNameArg);
|
const existingCertificate = this.retrieveCertificate(certDomainArg);
|
||||||
if (existingCertificate) {
|
if (existingCertificate) {
|
||||||
return 'existing';
|
return 'existing';
|
||||||
}
|
}
|
||||||
|
@ -1,34 +1,41 @@
|
|||||||
import * as plugins from './smartacme.plugins';
|
import * as plugins from './smartacme.plugins';
|
||||||
import * as interfaces from './interfaces';
|
import * as interfaces from './interfaces';
|
||||||
import { ICertRemoteResponse } from './interfaces';
|
|
||||||
|
|
||||||
// tslint:disable-next-line: max-classes-per-file
|
// tslint:disable-next-line: max-classes-per-file
|
||||||
export class CertRemoteClient {
|
export class CertRemoteClient {
|
||||||
private remoteUrl: string;
|
private remoteUrl: string;
|
||||||
private secret: string;
|
private secret: string;
|
||||||
|
private logger: plugins.smartlog.Smartlog;
|
||||||
|
|
||||||
constructor(optionsArg: {
|
constructor(optionsArg: {
|
||||||
remoteUrl: string;
|
remoteUrl: string;
|
||||||
secret: string;
|
secret: string;
|
||||||
|
logger?: plugins.smartlog.Smartlog;
|
||||||
}) {
|
}) {
|
||||||
this.remoteUrl = optionsArg.remoteUrl;
|
this.remoteUrl = optionsArg.remoteUrl;
|
||||||
this.secret = optionsArg.secret;
|
this.secret = optionsArg.secret;
|
||||||
|
optionsArg.logger
|
||||||
|
? (this.logger = optionsArg.logger)
|
||||||
|
: (this.logger = plugins.smartlog.defaultLogger);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
* @param domainNameArg
|
* @param domainNameArg
|
||||||
*/
|
*/
|
||||||
async getCertificateForDomain(domainNameArg: string): Promise<interfaces.ICert> {
|
public async getCertificateForDomain(domainNameArg: string): Promise<interfaces.ICert> {
|
||||||
let certificate: interfaces.ICert;
|
let certificate: interfaces.ICert;
|
||||||
const doRequestCycle = async (): Promise<interfaces.ICert> => {
|
const doRequestCycle = async (): Promise<interfaces.ICert> => {
|
||||||
const response: ICertRemoteResponse = (await plugins.smartrequest.postJson(this.remoteUrl, {
|
const response: interfaces.ICertRemoteResponse = (await plugins.smartrequest.postJson(
|
||||||
requestBody: <interfaces.ICertRemoteRequest>{
|
this.remoteUrl,
|
||||||
domainName: domainNameArg,
|
{
|
||||||
secret: this.secret
|
requestBody: <interfaces.ICertRemoteRequest>{
|
||||||
|
domainName: domainNameArg,
|
||||||
|
secret: this.secret
|
||||||
|
}
|
||||||
}
|
}
|
||||||
})).body;
|
)).body;
|
||||||
switch(response.status) {
|
switch (response.status as interfaces.TCertStatus) {
|
||||||
case 'pending':
|
case 'pending':
|
||||||
await plugins.smartdelay.delayFor(5000);
|
await plugins.smartdelay.delayFor(5000);
|
||||||
const finalResponse = await doRequestCycle();
|
const finalResponse = await doRequestCycle();
|
||||||
|
@ -15,13 +15,13 @@ export interface ISmartAcmeOptions {
|
|||||||
mongoDescriptor: plugins.smartdata.IMongoDescriptor;
|
mongoDescriptor: plugins.smartdata.IMongoDescriptor;
|
||||||
setChallenge: (domainName: string, keyAuthorization: string) => Promise<any>;
|
setChallenge: (domainName: string, keyAuthorization: string) => Promise<any>;
|
||||||
removeChallenge: (domainName: string) => Promise<any>;
|
removeChallenge: (domainName: string) => Promise<any>;
|
||||||
validateRemoteRequest: () => Promise<boolean>;
|
environment: 'production' | 'integration';
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* class SmartAcme
|
* class SmartAcme
|
||||||
* can be used for setting up communication with an ACME authority
|
* can be used for setting up communication with an ACME authority
|
||||||
*
|
*
|
||||||
* ```ts
|
* ```ts
|
||||||
* const mySmartAcmeInstance = new SmartAcme({
|
* const mySmartAcmeInstance = new SmartAcme({
|
||||||
* // see ISmartAcmeOptions for options
|
* // see ISmartAcmeOptions for options
|
||||||
@ -41,12 +41,44 @@ export class SmartAcme {
|
|||||||
// challenge fullfillment
|
// challenge fullfillment
|
||||||
private setChallenge: (domainName: string, keyAuthorization: string) => Promise<any>;
|
private setChallenge: (domainName: string, keyAuthorization: string) => Promise<any>;
|
||||||
private removeChallenge: (domainName: string) => Promise<any>;
|
private removeChallenge: (domainName: string) => Promise<any>;
|
||||||
private validateRemoteRequest: () => Promise<boolean>;
|
|
||||||
|
|
||||||
// certmanager
|
// certmanager
|
||||||
private certmanager: CertManager;
|
private certmanager: CertManager;
|
||||||
private certmatcher: CertMatcher;
|
private certmatcher: CertMatcher;
|
||||||
private certremoteHandler: plugins.smartexpress.Handler;
|
|
||||||
|
/**
|
||||||
|
* the remote handler to hand the request and response to.
|
||||||
|
*/
|
||||||
|
public certremoteHandler = async (req: plugins.smartexpress.Request, res: plugins.smartexpress.Response) => {
|
||||||
|
const requestBody: interfaces.ICertRemoteRequest = req.body;
|
||||||
|
const certDomain = this.certmatcher.getCertificateDomainNameByDomainName(requestBody.domainName);
|
||||||
|
let status: interfaces.TCertStatus = await this.certmanager.getCertificateStatus(
|
||||||
|
certDomain
|
||||||
|
);
|
||||||
|
let response: interfaces.ICertRemoteResponse;
|
||||||
|
switch (status) {
|
||||||
|
case 'existing':
|
||||||
|
response = {
|
||||||
|
status,
|
||||||
|
certificate: await (await this.certmanager.retrieveCertificate(
|
||||||
|
certDomain
|
||||||
|
)).createSavableObject()
|
||||||
|
};
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
if (status === "nonexisting") {
|
||||||
|
this.getCertificateForDomain(certDomain);
|
||||||
|
status = 'pending';
|
||||||
|
}
|
||||||
|
response = {
|
||||||
|
status
|
||||||
|
};
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
res.status(200);
|
||||||
|
res.send(response);
|
||||||
|
res.end();
|
||||||
|
}
|
||||||
|
|
||||||
constructor(optionsArg: ISmartAcmeOptions) {
|
constructor(optionsArg: ISmartAcmeOptions) {
|
||||||
this.options = optionsArg;
|
this.options = optionsArg;
|
||||||
@ -73,41 +105,15 @@ export class SmartAcme {
|
|||||||
// CertMatcher
|
// CertMatcher
|
||||||
this.certmatcher = new CertMatcher();
|
this.certmatcher = new CertMatcher();
|
||||||
|
|
||||||
// CertRemoteHandler
|
|
||||||
this.certremoteHandler = new plugins.smartexpress.Handler('POST', async (req, res) => {
|
|
||||||
const requestBody: interfaces.ICertRemoteRequest = req.body;
|
|
||||||
const status: interfaces.TCertStatus = await this.certmanager.getCertificateStatus(requestBody.domainName);
|
|
||||||
const existingCertificate = await this.certmanager.retrieveCertificate(
|
|
||||||
requestBody.domainName
|
|
||||||
);
|
|
||||||
let response: interfaces.ICertRemoteResponse;
|
|
||||||
switch (status) {
|
|
||||||
case 'existing':
|
|
||||||
response = {
|
|
||||||
status,
|
|
||||||
certificate: {
|
|
||||||
created: existingCertificate.created,
|
|
||||||
csr: existingCertificate.csr,
|
|
||||||
domainName: existingCertificate.domainName,
|
|
||||||
privateKey: existingCertificate.privateKey,
|
|
||||||
publicKey: existingCertificate.publicKey
|
|
||||||
}
|
|
||||||
};
|
|
||||||
break;
|
|
||||||
default:
|
|
||||||
response = {
|
|
||||||
status
|
|
||||||
};
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
res.status(200);
|
|
||||||
res.send(response);
|
|
||||||
res.end();
|
|
||||||
});
|
|
||||||
|
|
||||||
// ACME Client
|
// ACME Client
|
||||||
this.client = new plugins.acme.Client({
|
this.client = new plugins.acme.Client({
|
||||||
directoryUrl: plugins.acme.directory.letsencrypt.staging,
|
directoryUrl: (() => {
|
||||||
|
if (this.options.environment === 'production') {
|
||||||
|
return plugins.acme.directory.letsencrypt.production;
|
||||||
|
} else {
|
||||||
|
return plugins.acme.directory.letsencrypt.staging;
|
||||||
|
}
|
||||||
|
})(),
|
||||||
accountKey: this.privateKey
|
accountKey: this.privateKey
|
||||||
});
|
});
|
||||||
|
|
||||||
@ -124,7 +130,7 @@ export class SmartAcme {
|
|||||||
|
|
||||||
public async getCertificateForDomain(domainArg: string): Promise<Cert> {
|
public async getCertificateForDomain(domainArg: string): Promise<Cert> {
|
||||||
const certDomain = this.certmatcher.getCertificateDomainNameByDomainName(domainArg);
|
const certDomain = this.certmatcher.getCertificateDomainNameByDomainName(domainArg);
|
||||||
|
await this.certmanager.announceCertificate(certDomain);
|
||||||
const retrievedCertificate = await this.certmanager.retrieveCertificate(certDomain);
|
const retrievedCertificate = await this.certmanager.retrieveCertificate(certDomain);
|
||||||
|
|
||||||
if (retrievedCertificate) {
|
if (retrievedCertificate) {
|
||||||
|
@ -4,13 +4,14 @@ import * as smartdata from '@pushrocks/smartdata';
|
|||||||
import * as smartdelay from '@pushrocks/smartdelay';
|
import * as smartdelay from '@pushrocks/smartdelay';
|
||||||
import * as smartdns from '@pushrocks/smartdns';
|
import * as smartdns from '@pushrocks/smartdns';
|
||||||
import * as smartexpress from '@pushrocks/smartexpress';
|
import * as smartexpress from '@pushrocks/smartexpress';
|
||||||
|
import * as smartlog from '@pushrocks/smartlog';
|
||||||
import * as smartpromise from '@pushrocks/smartpromise';
|
import * as smartpromise from '@pushrocks/smartpromise';
|
||||||
import * as smartrequest from '@pushrocks/smartrequest';
|
import * as smartrequest from '@pushrocks/smartrequest';
|
||||||
import * as smartunique from '@pushrocks/smartunique';
|
import * as smartunique from '@pushrocks/smartunique';
|
||||||
import * as smartstring from '@pushrocks/smartstring';
|
import * as smartstring from '@pushrocks/smartstring';
|
||||||
import * as smarttime from '@pushrocks/smarttime';
|
import * as smarttime from '@pushrocks/smarttime';
|
||||||
|
|
||||||
export { lik, smartdata, smartdelay, smartdns, smartexpress, smartpromise, smartrequest, smartunique, smartstring, smarttime };
|
export { lik, smartdata, smartdelay, smartdns, smartexpress, smartlog, smartpromise, smartrequest, smartunique, smartstring, smarttime };
|
||||||
|
|
||||||
// thirs party scope
|
// thirs party scope
|
||||||
import * as acme from 'acme-client';
|
import * as acme from 'acme-client';
|
||||||
|
Reference in New Issue
Block a user