Compare commits
4 Commits
Author | SHA1 | Date | |
---|---|---|---|
a9f709ee7b | |||
1b11b637a5 | |||
ad54bf41ea | |||
060ebf1b29 |
8
package-lock.json
generated
8
package-lock.json
generated
@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "@pushrocks/smartacme",
|
||||
"version": "2.0.12",
|
||||
"version": "2.0.14",
|
||||
"lockfileVersion": 1,
|
||||
"requires": true,
|
||||
"dependencies": {
|
||||
@ -1614,9 +1614,9 @@
|
||||
"integrity": "sha1-k2pOMJ7zMKdkXtQUWYbIWuWyCAU="
|
||||
},
|
||||
"luxon": {
|
||||
"version": "1.9.0",
|
||||
"resolved": "https://verdaccio.lossless.one/luxon/-/luxon-1.9.0.tgz",
|
||||
"integrity": "sha512-N1kSwtIEhM/gIRGASXPgi1CwfQZX5VTjndYFjOsZdEEtWij2uSoRrgDGWwViZCUNY9Rwh4UVG/TLcUinHM20cA=="
|
||||
"version": "1.10.0",
|
||||
"resolved": "https://verdaccio.lossless.one/luxon/-/luxon-1.10.0.tgz",
|
||||
"integrity": "sha512-ry3GKh//v3isD6oJN5pFWmdh+3GiScwv9q8VgG6fZ2j1guGOol2vVVdo4GBAWCrcq5RHOqSeipqHBnOu/u024Q=="
|
||||
},
|
||||
"make-error": {
|
||||
"version": "1.3.5",
|
||||
|
@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "@pushrocks/smartacme",
|
||||
"version": "2.0.12",
|
||||
"version": "2.0.14",
|
||||
"private": false,
|
||||
"description": "acme implementation in TypeScript",
|
||||
"main": "dist/index.js",
|
||||
|
13
test/test.ts
13
test/test.ts
@ -11,20 +11,23 @@ tap.test('should create a valid instance of SmartAcme', async () => {
|
||||
smartAcmeInstance = new smartacme.SmartAcme({
|
||||
accountEmail: 'domains@lossless.org',
|
||||
accountPrivateKey: null,
|
||||
mongoDescriptor: {
|
||||
mongoDbName: testQenv.getEnvVarOnDemand('MONGODB_DATABASE'),
|
||||
mongoDbPass: testQenv.getEnvVarOnDemand('MONGODB_PASSWORD'),
|
||||
mongoDbUrl: testQenv.getEnvVarOnDemand('MONGODB_URL')
|
||||
},
|
||||
removeChallenge: async (...args) => {
|
||||
console.log(args);
|
||||
},
|
||||
setChallenge: async (...args) => {
|
||||
console.log(args);
|
||||
},
|
||||
mongoDescriptor: {
|
||||
mongoDbName: testQenv.getEnvVarOnDemand('MONGODB_DATABASE'),
|
||||
mongoDbPass: testQenv.getEnvVarOnDemand('MONGODB_PASSWORD'),
|
||||
mongoDbUrl: testQenv.getEnvVarOnDemand('MONGODB_URL')
|
||||
validateRemoteRequest: async () => {
|
||||
return true;
|
||||
}
|
||||
});
|
||||
await smartAcmeInstance.init();
|
||||
await smartAcmeInstance.getCertificateForDomain('bleu.de');
|
||||
// await smartAcmeInstance.getCertificateForDomain('bleu.de');
|
||||
});
|
||||
|
||||
tap.start();
|
||||
|
9
ts/interfaces/cert.ts
Normal file
9
ts/interfaces/cert.ts
Normal file
@ -0,0 +1,9 @@
|
||||
export type TCertStatus = 'existing' | 'nonexisting' | 'pending' | 'failed';
|
||||
|
||||
export interface ICert {
|
||||
domainName: string;
|
||||
created: number;
|
||||
privateKey: string;
|
||||
publicKey: string;
|
||||
csr: string;
|
||||
}
|
11
ts/interfaces/certremote.ts
Normal file
11
ts/interfaces/certremote.ts
Normal file
@ -0,0 +1,11 @@
|
||||
import { ICert, TCertStatus } from './cert';
|
||||
|
||||
export interface ICertRemoteRequest {
|
||||
secret: string;
|
||||
domainName: string;
|
||||
}
|
||||
|
||||
export interface ICertRemoteResponse {
|
||||
status: TCertStatus;
|
||||
certificate?: ICert;
|
||||
}
|
@ -1 +1,3 @@
|
||||
export * from './accountdata';
|
||||
export * from './cert';
|
||||
export * from './certremote';
|
||||
|
@ -1,34 +1,40 @@
|
||||
import * as plugins from './smartacme.plugins';
|
||||
|
||||
import * as interfaces from './interfaces';
|
||||
|
||||
import { CertManager } from './smartacme.classes.certmanager';
|
||||
|
||||
import { Collection, svDb, unI } from '@pushrocks/smartdata';
|
||||
import { ICert } from './interfaces';
|
||||
|
||||
@plugins.smartdata.Collection(() => {
|
||||
return CertManager.activeDB;
|
||||
})
|
||||
export class Cert extends plugins.smartdata.SmartDataDbDoc<Cert> {
|
||||
export class Cert extends plugins.smartdata.SmartDataDbDoc<Cert> implements interfaces.ICert {
|
||||
@unI()
|
||||
public index: string;
|
||||
|
||||
@svDb()
|
||||
domainName: string;
|
||||
public domainName: string;
|
||||
|
||||
@svDb()
|
||||
created: number;
|
||||
public created: number;
|
||||
|
||||
@svDb()
|
||||
privateKey: string;
|
||||
public privateKey: string;
|
||||
|
||||
@svDb()
|
||||
publicKey: string;
|
||||
public publicKey: string;
|
||||
|
||||
@svDb()
|
||||
csr: string;
|
||||
public csr: string;
|
||||
|
||||
constructor(privateKeyArg: string, publicKeyArg: string, csrArg: string) {
|
||||
constructor(optionsArg: ICert) {
|
||||
super();
|
||||
this.privateKey = privateKeyArg;
|
||||
this.publicKey = publicKeyArg;
|
||||
this.csr = csrArg;
|
||||
this.created = optionsArg.created;
|
||||
this.domainName = optionsArg.domainName;
|
||||
this.privateKey = optionsArg.privateKey;
|
||||
this.publicKey = optionsArg.publicKey;
|
||||
this.csr = optionsArg.csr;
|
||||
}
|
||||
}
|
||||
|
@ -1,5 +1,9 @@
|
||||
import * as plugins from './smartacme.plugins';
|
||||
import { Cert } from './smartacme.classes.cert';
|
||||
import { SmartAcme } from './smartacme.classes.smartacme';
|
||||
|
||||
import * as interfaces from './interfaces';
|
||||
import { ICert } from './interfaces';
|
||||
|
||||
|
||||
export class CertManager {
|
||||
@ -15,16 +19,22 @@ export class CertManager {
|
||||
private mongoDescriptor: plugins.smartdata.IMongoDescriptor;
|
||||
public smartdataDb: plugins.smartdata.SmartdataDb;
|
||||
|
||||
constructor(optionsArg: {
|
||||
public pendingMap: plugins.lik.Stringmap;
|
||||
|
||||
constructor(smartAcmeArg: SmartAcme,optionsArg: {
|
||||
mongoDescriptor: plugins.smartdata.IMongoDescriptor;
|
||||
}) {
|
||||
this.mongoDescriptor = optionsArg.mongoDescriptor;
|
||||
}
|
||||
|
||||
public async init () {
|
||||
// Smartdata DB
|
||||
this.smartdataDb = new plugins.smartdata.SmartdataDb(this.mongoDescriptor);
|
||||
await this.smartdataDb.init();
|
||||
CertManager.activeDB = this.smartdataDb;
|
||||
|
||||
// Pending Map
|
||||
this.pendingMap = new plugins.lik.Stringmap();
|
||||
};
|
||||
|
||||
/**
|
||||
@ -33,6 +43,7 @@ export class CertManager {
|
||||
* @param domainName the domain Name to retrieve the vcertificate for
|
||||
*/
|
||||
public async retrieveCertificate(domainName: string): Promise<Cert> {
|
||||
await this.checkCerts();
|
||||
const existingCertificate: Cert = await Cert.getInstance({
|
||||
name: domainName
|
||||
});
|
||||
@ -51,17 +62,32 @@ export class CertManager {
|
||||
* @param privateKeyArg
|
||||
* @param csrArg
|
||||
*/
|
||||
public async storeCertificate(privateKeyArg: string, publicKeyArg: string, csrArg: string) {
|
||||
const cert = new Cert(privateKeyArg, publicKeyArg, csrArg);
|
||||
public async storeCertificate(optionsArg: ICert) {
|
||||
const cert = new Cert(optionsArg);
|
||||
cert.save();
|
||||
};
|
||||
|
||||
public async deleteCertificate(domainName: string) {
|
||||
public async deleteCertificate(domainNameArg: string) {
|
||||
|
||||
};
|
||||
}
|
||||
|
||||
public async getCertificateStatus(domainNameArg: string): Promise<interfaces.TCertStatus> {
|
||||
const isPending = this.pendingMap.checkString('domainNameArg');
|
||||
if (isPending) {
|
||||
return 'pending';
|
||||
}
|
||||
|
||||
// otherwise lets continue
|
||||
const existingCertificate = this.retrieveCertificate(domainNameArg);
|
||||
if (existingCertificate) {
|
||||
return 'existing';
|
||||
}
|
||||
|
||||
return 'nonexisting';
|
||||
}
|
||||
|
||||
/**
|
||||
* checks all certs for expiration
|
||||
*/
|
||||
checkCerts() {}
|
||||
private async checkCerts() {};
|
||||
}
|
||||
|
@ -1,10 +1,47 @@
|
||||
import * as plugins from './smartacme.plugins';
|
||||
import * as interfaces from './interfaces';
|
||||
import { ICertRemoteResponse } from './interfaces';
|
||||
|
||||
// tslint:disable-next-line: max-classes-per-file
|
||||
export class CertRemoteClient {
|
||||
private remoteUrl: string;
|
||||
private secret: string;
|
||||
|
||||
constructor(optionsArg: {
|
||||
remoteUrl: string;
|
||||
secret: string;
|
||||
}) {
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
@ -1,3 +0,0 @@
|
||||
import * as plugins from './smartacme.plugins';
|
||||
|
||||
export class CertRemoteHandler {}
|
@ -1,6 +1,8 @@
|
||||
import * as plugins from './smartacme.plugins';
|
||||
import { CertManager } from './smartacme.classes.certmanager';
|
||||
import { CertRemoteHandler } from './smartacme.classes.certremotehandler';
|
||||
|
||||
import * as interfaces from './interfaces';
|
||||
import { request } from 'http';
|
||||
|
||||
/**
|
||||
*
|
||||
@ -8,9 +10,10 @@ import { CertRemoteHandler } from './smartacme.classes.certremotehandler';
|
||||
export interface ISmartAcmeOptions {
|
||||
accountPrivateKey?: string;
|
||||
accountEmail: string;
|
||||
mongoDescriptor: plugins.smartdata.IMongoDescriptor;
|
||||
setChallenge: (domainName: string, keyAuthorization: string) => Promise<any>;
|
||||
removeChallenge: (domainName: string) => Promise<any>;
|
||||
mongoDescriptor: plugins.smartdata.IMongoDescriptor;
|
||||
validateRemoteRequest: () => Promise<boolean>;
|
||||
}
|
||||
|
||||
export class SmartAcme {
|
||||
@ -26,26 +29,67 @@ export class SmartAcme {
|
||||
// challenge fullfillment
|
||||
private setChallenge: (domainName: string, keyAuthorization: string) => Promise<any>;
|
||||
private removeChallenge: (domainName: string) => Promise<any>;
|
||||
private validateRemoteRequest: () => Promise<boolean>;
|
||||
|
||||
// certmanager
|
||||
private certmanager: CertManager;
|
||||
private certremoteHandler: CertRemoteHandler;
|
||||
private certremoteHandler: plugins.smartexpress.Handler;
|
||||
|
||||
constructor(optionsArg: ISmartAcmeOptions) {
|
||||
this.options = optionsArg;
|
||||
}
|
||||
|
||||
/**
|
||||
* inits the instance
|
||||
* ```ts
|
||||
* await myCloudlyInstance.init() // does not support options
|
||||
* ```
|
||||
*/
|
||||
public async init() {
|
||||
this.privateKey = this.options.accountPrivateKey || (await plugins.acme.forge.createPrivateKey());
|
||||
this.privateKey =
|
||||
this.options.accountPrivateKey || (await plugins.acme.forge.createPrivateKey());
|
||||
this.setChallenge = this.options.setChallenge;
|
||||
this.removeChallenge = this.options.removeChallenge;
|
||||
|
||||
this.certmanager = new CertManager({
|
||||
// CertMangaer
|
||||
this.certmanager = new CertManager(this, {
|
||||
mongoDescriptor: this.options.mongoDescriptor
|
||||
});
|
||||
await this.certmanager.init();
|
||||
this.certremoteHandler = new CertRemoteHandler();
|
||||
|
||||
// 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
|
||||
this.client = new plugins.acme.Client({
|
||||
directoryUrl: plugins.acme.directory.letsencrypt.staging,
|
||||
accountKey: this.privateKey
|
||||
@ -58,6 +102,8 @@ export class SmartAcme {
|
||||
});
|
||||
}
|
||||
|
||||
public async stop() {};
|
||||
|
||||
public async getCertificateForDomain(domainArg: string) {
|
||||
const domain = domainArg;
|
||||
|
||||
@ -121,8 +167,15 @@ export class SmartAcme {
|
||||
console.log(`Private key:\n${key.toString()}`);
|
||||
console.log(`Certificate:\n${cert.toString()}`);
|
||||
|
||||
this.certmanager.storeCertificate(key.toString(), cert.toString(), csr.toString());
|
||||
}
|
||||
await this.certmanager.storeCertificate({
|
||||
domainName: domainArg,
|
||||
privateKey: key.toString(),
|
||||
publicKey: cert.toString(),
|
||||
csr: csr.toString(),
|
||||
created: Date.now()
|
||||
});
|
||||
|
||||
toStorageObject() {}
|
||||
const newCertificate = await this.certmanager.retrieveCertificate(domainArg);
|
||||
return newCertificate;
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user