From 223a47c997cec04ad588f8188a3d369cf2e49bf5 Mon Sep 17 00:00:00 2001 From: Phil Kunz Date: Sun, 12 Aug 2018 01:35:14 +0200 Subject: [PATCH] fix(core): now creating certs all right --- package-lock.json | 8 ++++ package.json | 1 + test/test.ts | 1 + ts/smartacme.classes.smartacme.ts | 70 ++++++++++++++++++++++++------- ts/smartacme.plugins.ts | 4 +- 5 files changed, 69 insertions(+), 15 deletions(-) diff --git a/package-lock.json b/package-lock.json index 3779b8f..99322e0 100644 --- a/package-lock.json +++ b/package-lock.json @@ -69,6 +69,14 @@ "ansi-256-colors": "^1.1.0" } }, + "@pushrocks/smartdelay": { + "version": "2.0.2", + "resolved": "https://registry.npmjs.org/@pushrocks/smartdelay/-/smartdelay-2.0.2.tgz", + "integrity": "sha512-4xf6tMKwZcxBynKgXrM4SQKgeASfRvx43LUmR5DkStp26ZHAsarCXUdKJS6y8QIPygEOTOCP8we97JAcCzBuMg==", + "requires": { + "@pushrocks/smartpromise": "^2.0.5" + } + }, "@pushrocks/smartfile": { "version": "6.0.6", "resolved": "https://registry.npmjs.org/@pushrocks/smartfile/-/smartfile-6.0.6.tgz", diff --git a/package.json b/package.json index 230e04d..dc8d992 100644 --- a/package.json +++ b/package.json @@ -23,6 +23,7 @@ }, "homepage": "https://gitlab.com/umbrellazone/smartacme#README", "dependencies": { + "@pushrocks/smartdelay": "^2.0.2", "@pushrocks/smartpromise": "^2.0.5", "acme-v2": "^1.2.0", "rsa-compat": "^1.5.1" diff --git a/test/test.ts b/test/test.ts index a425c8a..418e38e 100644 --- a/test/test.ts +++ b/test/test.ts @@ -8,6 +8,7 @@ tap.test('should create a valid instance of SmartAcme' , async () => { smartAcmeInstance = new smartacme.SmartAcme(); await smartAcmeInstance.init() console.log(smartAcmeInstance.directoryUrls); + await smartAcmeInstance.getCertificateForDomain('bleu.de'); }) tap.start(); \ No newline at end of file diff --git a/ts/smartacme.classes.smartacme.ts b/ts/smartacme.classes.smartacme.ts index 8d64542..368bf94 100644 --- a/ts/smartacme.classes.smartacme.ts +++ b/ts/smartacme.classes.smartacme.ts @@ -15,33 +15,75 @@ const acme = require('acme-v2').ACME.create({ }, // don't try to validate challenges locally - skipChallengeTest: false + skipChallengeTest: true }); import { KeyPair } from './smartacme.classes.keypair'; +import * as plugins from './smartacme.plugins'; +const rsa = require('rsa-compat').RSA; export class SmartAcme { - keyPair: KeyPair; + domainKeyPair: KeyPair; + accountKeyPair: KeyPair; + accountData: any; directoryUrls: any; async init() { // get directory url this.directoryUrls = await acme.init('https://acme-staging-v02.api.letsencrypt.org/directory'); - // create keyPair - this.keyPair = await KeyPair.generateFresh(); + // create keyPairs + this.domainKeyPair = await KeyPair.generateFresh(); + this.accountKeyPair = await KeyPair.generateFresh(); // get account - const registrationData = await acme.accounts.create({ - email: 'domains@lossless.org', // valid email (server checks MX records) - accountKeypair: this.keyPair.rsaKeyPair, - agreeToTerms: async tosUrl => { - return tosUrl; - } - }).catch(e => { - console.log(e); - }); + const registrationData = await acme.accounts + .create({ + email: 'domains@lossless.org', // valid email (server checks MX records) + accountKeypair: this.accountKeyPair.rsaKeyPair, + agreeToTerms: async tosUrl => { + return tosUrl; + } + }) + .catch(e => { + console.log(e); + }); + this.accountData = registrationData; + } - console.log(registrationData); + async getCertificateForDomain(domain) { + const result = await acme.certificates + .create({ + domainKeypair: this.domainKeyPair.rsaKeyPair, + accountKeypair: this.accountKeyPair.rsaKeyPair, + domains: ['bleu.de'], + challengeType: 'dns-01', + + setChallenge: async (hostname, key, val, cb) => { + console.log('set challenge'); + console.log(hostname); + //console.log(key); + //console.log(val); + const dnsKey = rsa.utils.toWebsafeBase64( + require('crypto') + .createHash('sha256') + .update(val) + .digest('base64') + ); + + console.log(dnsKey); + await plugins.smartdelay.delayFor(20000); + console.log('ready!'); + cb(); + }, // return Promise + removeChallenge: async (hostname, key) => { + console.log('removing challenge'); + return; + } // return Promise + }) + .catch(e => { + console.log(e); + }); // returns Promise + console.log(result); } } diff --git a/ts/smartacme.plugins.ts b/ts/smartacme.plugins.ts index 869640d..199efdf 100644 --- a/ts/smartacme.plugins.ts +++ b/ts/smartacme.plugins.ts @@ -1,5 +1,7 @@ import * as smartpromise from '@pushrocks/smartpromise'; +import * as smartdelay from '@pushrocks/smartdelay'; export { - smartpromise + smartpromise, + smartdelay } \ No newline at end of file