From 009e0dc1fb949b470cca865291db381348eaf06d Mon Sep 17 00:00:00 2001 From: AJ ONeal Date: Mon, 6 May 2019 19:34:17 -0600 Subject: [PATCH] use pre-gen or jit-CSR --- app.js | 156 +++++++++++++++++++++++++++++++-------------------------- 1 file changed, 86 insertions(+), 70 deletions(-) diff --git a/app.js b/app.js index 3ea96c5..45a5024 100644 --- a/app.js +++ b/app.js @@ -138,6 +138,7 @@ accountStuff.privateJwk = privJwk; accountStuff.email = email; accountStuff.acme = acme; + $('.js-create-order').hidden = false; }).catch(function (err) { console.error("A bad thing happened:"); console.error(err); @@ -149,26 +150,7 @@ $('form.js-csr').addEventListener('submit', function (ev) { ev.preventDefault(); ev.stopPropagation(); - var domains = ($('.js-domains').value||'example.com').split(/[, ]+/g); - //var privJwk = JSON.parse($('.js-jwk').innerText).private; - return Keypairs.generate({ - kty: $('input[name="kty"]:checked').value - , namedCurve: $('input[name="ec-crv"]:checked').value - , modulusLength: $('input[name="rsa-len"]:checked').value - }).then(function (pair) { - console.log('domain keypair:', pair); - accountStuff.domainPrivateJwk = pair.private; - return CSR({ jwk: pair.private, domains: domains }).then(function (pem) { - // Verify with https://www.sslshopper.com/csr-decoder.html - accountStuff.csr = pem; - console.log('CSR:'); - console.log(pem); - - console.log('CSR info:'); - console.log(CSR._info(pem)); - $('.js-create-order').hidden = false; - }); - }); + generateCsr(); }); $('form.js-acme-order').addEventListener('submit', function (ev) { @@ -181,64 +163,98 @@ var domains = ($('.js-domains').value||'example.com').split(/[, ]+/g); - return acme.certificates.create({ - accountKeypair: { privateKeyJwk: privJwk } - , account: account - //, domainKeypair: { privateKeyJwk: accountStuff.domainPrivateJwk } - , csr: accountStuff.csr - , email: email - , domains: domains - , agreeToTerms: checkTos - , challenges: { - 'dns-01': { - set: function (opts) { - console.info('dns-01 set challenge:'); - console.info('TXT', opts.dnsHost); - console.info(opts.dnsAuthorization); - return new Promise(function (resolve) { - while (!window.confirm("Did you set the challenge?")) {} - resolve(); - }); + return getDomainPrivkey().then(function () { + return acme.certificates.create({ + accountKeypair: { privateKeyJwk: privJwk } + , account: account + //, domainKeypair: { privateKeyJwk: accountStuff.domainPrivateJwk } + , csr: accountStuff.csr + , email: email + , domains: domains + , agreeToTerms: checkTos + , challenges: { + 'dns-01': { + set: function (opts) { + console.info('dns-01 set challenge:'); + console.info('TXT', opts.dnsHost); + console.info(opts.dnsAuthorization); + return new Promise(function (resolve) { + while (!window.confirm("Did you set the challenge?")) {} + resolve(); + }); + } + , remove: function (opts) { + console.log('dns-01 remove challenge:'); + console.info('TXT', opts.dnsHost); + console.info(opts.dnsAuthorization); + return new Promise(function (resolve) { + while (!window.confirm("Did you delete the challenge?")) {} + resolve(); + }); + } } - , remove: function (opts) { - console.log('dns-01 remove challenge:'); - console.info('TXT', opts.dnsHost); - console.info(opts.dnsAuthorization); - return new Promise(function (resolve) { - while (!window.confirm("Did you delete the challenge?")) {} - resolve(); - }); + , 'http-01': { + set: function (opts) { + console.info('http-01 set challenge:'); + console.info(opts.challengeUrl); + console.info(opts.keyAuthorization); + return new Promise(function (resolve) { + while (!window.confirm("Did you set the challenge?")) {} + resolve(); + }); + } + , remove: function (opts) { + console.log('http-01 remove challenge:'); + console.info(opts.challengeUrl); + console.info(opts.keyAuthorization); + return new Promise(function (resolve) { + while (!window.confirm("Did you delete the challenge?")) {} + resolve(); + }); + } } } - , 'http-01': { - set: function (opts) { - console.info('http-01 set challenge:'); - console.info(opts.challengeUrl); - console.info(opts.keyAuthorization); - return new Promise(function (resolve) { - while (!window.confirm("Did you set the challenge?")) {} - resolve(); - }); - } - , remove: function (opts) { - console.log('http-01 remove challenge:'); - console.info(opts.challengeUrl); - console.info(opts.keyAuthorization); - return new Promise(function (resolve) { - while (!window.confirm("Did you delete the challenge?")) {} - resolve(); - }); - } - } - } - , challengeTypes: [$('input[name="acme-challenge-type"]:checked').value] - }).catch(function (err) { - window.alert("failed! " + err.message || JSON.stringify(err)); + , challengeTypes: [$('input[name="acme-challenge-type"]:checked').value] + }).catch(function (err) { + window.alert("failed! " + err.message || JSON.stringify(err)); + }); }); }); $('.js-generate').hidden = false; } + function getDomainPrivkey() { + if (accountStuff.domainPrivateJwk) { return Promise.resolve(accountStuff.domainPrivateJwk); } + return Keypairs.generate({ + kty: $('input[name="kty"]:checked').value + , namedCurve: $('input[name="ec-crv"]:checked').value + , modulusLength: $('input[name="rsa-len"]:checked').value + }).then(function (pair) { + console.log('domain keypair:', pair); + accountStuff.domainPrivateJwk = pair.private; + return pair.private; + }); + } + + function generateCsr() { + var domains = ($('.js-domains').value||'example.com').split(/[, ]+/g); + //var privJwk = JSON.parse($('.js-jwk').innerText).private; + return getDomainPrivkey().then(function (privJwk) { + accountStuff.domainPrivateJwk = privJwk; + return CSR({ jwk: privJwk, domains: domains }).then(function (pem) { + // Verify with https://www.sslshopper.com/csr-decoder.html + accountStuff.csr = pem; + console.log('CSR:'); + console.log(pem); + + console.log('CSR info:'); + console.log(CSR._info(pem)); + + return pem; + }); + }); + } + window.addEventListener('load', run); }());