From 29a4443d49bacf1309f90df6887f42ad1d2b3b75 Mon Sep 17 00:00:00 2001 From: AJ ONeal Date: Mon, 8 Aug 2016 18:11:25 -0400 Subject: [PATCH] pass test to save account keys --- lib/core.js | 25 +++++++++++++++++++++++-- lib/utils.js | 38 ++++++++++++++++++++++++++++---------- tests/create-account.js | 7 ++++--- 3 files changed, 55 insertions(+), 15 deletions(-) diff --git a/lib/core.js b/lib/core.js index 92fdb1e..6176537 100644 --- a/lib/core.js +++ b/lib/core.js @@ -35,8 +35,11 @@ module.exports.create = function (le) { // Accounts // , accounts: { + // Accounts registerAsync: function (args) { var err; + var copy = utils.merge(args, le); + args = utils.tplCopy(copy); if (!args.email || !args.agreeTos || (parseInt(args.rsaKeySize, 10) < 2048)) { err = new Error( @@ -48,13 +51,26 @@ module.exports.create = function (le) { } return utils.testEmail(args.email).then(function () { + var keypairOpts = { public: true, pem: true }; - return RSA.generateKeypairAsync(args.rsaKeySize, 65537, { public: true, pem: true }).then(function (keypair) { + var promise = le.store.accounts.checkKeypairAsync(args).then(function (keypair) { + return RSA.import(keypair); + }, function (/*err*/) { + return RSA.generateKeypairAsync(args.rsaKeySize, 65537, keypairOpts).then(function (keypair) { + keypair.privateKeyPem = RSA.exportPrivatePem(keypair); + keypair.privateKeyJwk = RSA.exportPrivateJwk(keypair); + return le.store.accounts.setKeypairAsync(args, keypair); + }); + }); + + return promise.then(function (keypair) { // Note: the ACME urls are always fetched fresh on purpose // TODO is this the right place for this? return core.getAcmeUrlsAsync(args).then(function (urls) { args._acmeUrls = urls; + throw new Error("WAIT! Don't go yet!!!"); + return le.acme.registerNewAccountAsync({ email: args.email , newRegUrl: args._acmeUrls.newReg @@ -88,9 +104,11 @@ module.exports.create = function (le) { account.regr = regr; account.accountId = accountId; account.id = accountId; + account.email = args.email; args.account = account; + // TODO move templating to right here? return le.store.accounts.setAsync(args, account).then(function () { return account; }); @@ -100,6 +118,7 @@ module.exports.create = function (le) { }); } + // Accounts , getAsync: function (args) { return core.accounts.checkAsync(args).then(function (account) { if (account) { @@ -110,9 +129,10 @@ module.exports.create = function (le) { }); } + // Accounts , checkAsync: function (args) { var requiredArgs = ['accountId', 'email', 'domains', 'domain']; - if (!requiredArgs.some(function (key) { return -1 !== Object.keys(args).indexOf(key) })) { + if (!requiredArgs.some(function (key) { return -1 !== Object.keys(args).indexOf(key); })) { return PromiseA.reject(new Error( "In order to register or retrieve an account one of '" + requiredArgs.join("', '") + "' must be present" )); @@ -136,6 +156,7 @@ module.exports.create = function (le) { } , certificates: { + // Certificates registerAsync: function (args) { var err; var copy = utils.merge(args, le); diff --git a/lib/utils.js b/lib/utils.js index eaaad17..69af35f 100644 --- a/lib/utils.js +++ b/lib/utils.js @@ -53,28 +53,46 @@ module.exports.merge = function (/*defaults, args*/) { module.exports.tplCopy = function (copy) { var homedir = require('homedir')(); - var tpls = { - hostname: (copy.domains || [])[0] - , server: (copy.server || '').replace('https://', '').replace(/(\/)$/, '') - , conf: copy.configDir - , config: copy.configDir + var tplKeys; + + copy.hostnameGet = function (copy) { + return (copy.domains || [])[0] || copy.domain; }; Object.keys(copy).forEach(function (key) { + var newName; + if (!/Get$/.test(key)) { + return; + } + + newName = key.replace(/Get$/, ''); + copy[newName] = copy[newName] || copy[key](copy); + }); + + tplKeys = Object.keys(copy); + tplKeys.sort(function (a, b) { + return b.length - a.length; + }); + + tplKeys.forEach(function (key) { if ('string' !== typeof copy[key]) { return; } copy[key] = copy[key].replace(homeRe, homedir + path.sep); + }); - Object.keys(tpls).sort(function (a, b) { - return b.length - a.length; - }).forEach(function (tplname) { - if (!tpls[tplname]) { + tplKeys.forEach(function (key) { + if ('string' !== typeof copy[key]) { + return; + } + + tplKeys.forEach(function (tplname) { + if (!copy[tplname]) { // what can't be templated now may be templatable later return; } - copy[key] = copy[key].replace(':' + tplname, tpls[tplname]); + copy[key] = copy[key].replace(':' + tplname, copy[tplname]); }); }); diff --git a/tests/create-account.js b/tests/create-account.js index 32a775f..1c5c888 100644 --- a/tests/create-account.js +++ b/tests/create-account.js @@ -6,12 +6,14 @@ var le = LE.create({ , acme: require('le-acme-core').ACME.create() , store: require('le-store-certbot').create({ configDir: '~/letsencrypt.test/etc/' + , webrootPath: '~/letsencrypt.test/tmp/:hostname' }) +, debug: true }); var testId = Math.round(Date.now() / 1000).toString(); var fakeEmail = 'coolaj86+le.' + testId + '@example.com'; -var testEmail = 'coolaj86+le.' + testId + '@example.com'; +var testEmail = 'coolaj86+le.' + testId + '@gmail.com'; var testAccount; var tests = [ @@ -66,9 +68,8 @@ var tests = [ }); } , function () { - throw new Error('NOT IMPLEMENTED'); return le.core.accounts.registerAsync({ - email: 'coolaj86+le.' + testId + '@example.com' + email: testEmail , agreeTos: true , rsaKeySize: 2048 }).then(function (account) {