From 2f2d0783d7327a35578e35bdba55f977d5595ef7 Mon Sep 17 00:00:00 2001 From: AJ ONeal Date: Wed, 16 Dec 2015 03:23:34 +0000 Subject: [PATCH] updates --- README.md | 6 +++++- example/letsencrypt.js | 17 ++++++++++------- lib/get-certificate.js | 18 +++++++++++++----- lib/register-new-account.js | 31 +++++++++++++++++++++++++------ 4 files changed, 53 insertions(+), 19 deletions(-) diff --git a/README.md b/README.md index e464755..8becccb 100644 --- a/README.md +++ b/README.md @@ -60,8 +60,12 @@ LeCore.getAcmeUrls( // record to disk (or db) LeCore.getCertificate( - { domainPrivateKeyPem: domainPrivateKeyPem + { newAuthzUrl: urls.newAuthz + , newCertUrl: urls.newCert + + , domainPrivateKeyPem: domainPrivateKeyPem , accountPrivateKeyPem: accountPrivateKeyPem + , setChallenge: challengeStore.set , removeChallenge: challengeStore.remove } diff --git a/example/letsencrypt.js b/example/letsencrypt.js index 28f1adf..532f53f 100644 --- a/example/letsencrypt.js +++ b/example/letsencrypt.js @@ -63,9 +63,8 @@ function runDemo() { , accountPrivateKeyPem: accountPrivateKeyPem , agreeToTerms: function (tosUrl, done) { - // agree to these exact terms - console.log('[tosUrl]'); - console.log(tosUrl); + // agree to the exact version of these terms + console.log('[tosUrl]:', tosUrl); done(null, tosUrl); } } @@ -74,15 +73,19 @@ function runDemo() { // Note: you should save the registration // record to disk (or db) console.log('[regr]'); - console.log(regr); + console.log(err || regr); console.log('Registering New Certificate'); LeCore.getCertificate( - { domainPrivateKeyPem: domainPrivateKeyPem + { newAuthzUrl: acmeUrls.newAuthz + , newCertUrl: acmeUrls.newCert + + , domainPrivateKeyPem: domainPrivateKeyPem , accountPrivateKeyPem: accountPrivateKeyPem + , domains: domains + , setChallenge: challengeStore.set , removeChallenge: challengeStore.remove - , domains: domains } , function (err, certs) { @@ -90,7 +93,7 @@ function runDemo() { certStore.set(domains[0], certs, function () { console.log('[certs]'); - console.log(certs); + console.log(err || certs); }); diff --git a/lib/get-certificate.js b/lib/get-certificate.js index f63cd81..235a9c5 100644 --- a/lib/get-certificate.js +++ b/lib/get-certificate.js @@ -19,10 +19,18 @@ module.exports.create = function (deps) { var state={ validatedDomains:[] , validAuthorizationUrls:[] - , newAuthorizationUrl: options.newAuthorizationUrl || options.newAuthz - , newCertificateUrl: options.newCertificateUrl || options.newCert + , newAuthzUrl: options.newAuthzUrl + , newCertUrl: options.newCertUrl }; + console.log('state'); + console.log(state); + if (!options.newAuthzUrl) { + return handleErr(new Error("options.newAuthzUrl must be the authorization url")); + } + if (!options.newCertUrl) { + return handleErr(new Error("options.newCertUrl must be the new certificate url")); + } if (!options.accountPrivateKeyPem) { return handleErr(new Error("options.accountPrivateKeyPem must be an ascii private key pem")); } @@ -64,7 +72,7 @@ module.exports.create = function (deps) { function getChallenges(domain) { state.domain=domain; - state.acme.post(state.newAuthorizationUrl, { + state.acme.post(state.newAuthzUrl, { resource:'new-authz', identifier:{ type:'dns', @@ -90,7 +98,7 @@ module.exports.create = function (deps) { } state.authorizationUrl=res.headers.location; - state.newCertificateUrl=links.next; + state.newCertUrl=links.next; authz=JSON.parse(body); @@ -156,7 +164,7 @@ module.exports.create = function (deps) { function getCertificate() { var csr=generateCsr(state.certPrivateKey, state.validatedDomains); log('Requesting certificate...'); - state.acme.post(state.newCertificateUrl, { + state.acme.post(state.newCertUrl, { resource:'new-cert', csr:csr, authorizations:state.validAuthorizationUrls diff --git a/lib/register-new-account.js b/lib/register-new-account.js index 4b5ff05..0c4ad69 100644 --- a/lib/register-new-account.js +++ b/lib/register-new-account.js @@ -22,8 +22,8 @@ module.exports.create = function (deps) { cb(new Error("options.agreeToTerms must be function (tosUrl, fn => (err, true))")); return; } - if (!options.newReg) { - cb(new Error("options.newReg must be the a new registration url")); + if (!options.newRegUrl) { + cb(new Error("options.newRegUrl must be the a new registration url")); return; } if (!options.email) { @@ -38,7 +38,7 @@ module.exports.create = function (deps) { register(); function register() { - state.acme.post(options.newReg, { + state.acme.post(options.newRegUrl, { resource:'new-reg', contact:['mailto:'+options.email] }, getTerms); @@ -77,7 +77,7 @@ module.exports.create = function (deps) { request.get(state.termsUrl, getAgreement); }); } else { - cb(); + cb(null, null); } } @@ -100,11 +100,30 @@ module.exports.create = function (deps) { resource:'reg', agreement:state.termsUrl }, function(err, res, body) { + var data; + if (err || Math.floor(res.statusCode/100)!==2) { return handleErr(err, 'Couldn\'t POST agreement back to server', body); - } else { - cb(null, body); } + + data = body; + // handle for node and browser + if ('string' === typeof body) { + try { + data = JSON.parse(body); + } catch(e) { + // ignore + } + } else { + // might be a buffer + data = body.toString('utf8'); + if (!(data.length > 10)) { + // probably json + data = body; + } + } + + cb(null, data); }); }