pass test to save account keys

This commit is contained in:
AJ ONeal 2016-08-08 18:11:25 -04:00
parent aa6656e723
commit 29a4443d49
3 changed files with 55 additions and 15 deletions

View File

@ -35,8 +35,11 @@ module.exports.create = function (le) {
// Accounts // Accounts
// //
, accounts: { , accounts: {
// Accounts
registerAsync: function (args) { registerAsync: function (args) {
var err; var err;
var copy = utils.merge(args, le);
args = utils.tplCopy(copy);
if (!args.email || !args.agreeTos || (parseInt(args.rsaKeySize, 10) < 2048)) { if (!args.email || !args.agreeTos || (parseInt(args.rsaKeySize, 10) < 2048)) {
err = new Error( err = new Error(
@ -48,13 +51,26 @@ module.exports.create = function (le) {
} }
return utils.testEmail(args.email).then(function () { 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 // Note: the ACME urls are always fetched fresh on purpose
// TODO is this the right place for this? // TODO is this the right place for this?
return core.getAcmeUrlsAsync(args).then(function (urls) { return core.getAcmeUrlsAsync(args).then(function (urls) {
args._acmeUrls = urls; args._acmeUrls = urls;
throw new Error("WAIT! Don't go yet!!!");
return le.acme.registerNewAccountAsync({ return le.acme.registerNewAccountAsync({
email: args.email email: args.email
, newRegUrl: args._acmeUrls.newReg , newRegUrl: args._acmeUrls.newReg
@ -88,9 +104,11 @@ module.exports.create = function (le) {
account.regr = regr; account.regr = regr;
account.accountId = accountId; account.accountId = accountId;
account.id = accountId; account.id = accountId;
account.email = args.email;
args.account = account; args.account = account;
// TODO move templating to right here?
return le.store.accounts.setAsync(args, account).then(function () { return le.store.accounts.setAsync(args, account).then(function () {
return account; return account;
}); });
@ -100,6 +118,7 @@ module.exports.create = function (le) {
}); });
} }
// Accounts
, getAsync: function (args) { , getAsync: function (args) {
return core.accounts.checkAsync(args).then(function (account) { return core.accounts.checkAsync(args).then(function (account) {
if (account) { if (account) {
@ -110,9 +129,10 @@ module.exports.create = function (le) {
}); });
} }
// Accounts
, checkAsync: function (args) { , checkAsync: function (args) {
var requiredArgs = ['accountId', 'email', 'domains', 'domain']; 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( return PromiseA.reject(new Error(
"In order to register or retrieve an account one of '" + requiredArgs.join("', '") + "' must be present" "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: {
// Certificates
registerAsync: function (args) { registerAsync: function (args) {
var err; var err;
var copy = utils.merge(args, le); var copy = utils.merge(args, le);

View File

@ -53,28 +53,46 @@ module.exports.merge = function (/*defaults, args*/) {
module.exports.tplCopy = function (copy) { module.exports.tplCopy = function (copy) {
var homedir = require('homedir')(); var homedir = require('homedir')();
var tpls = { var tplKeys;
hostname: (copy.domains || [])[0]
, server: (copy.server || '').replace('https://', '').replace(/(\/)$/, '') copy.hostnameGet = function (copy) {
, conf: copy.configDir return (copy.domains || [])[0] || copy.domain;
, config: copy.configDir
}; };
Object.keys(copy).forEach(function (key) { 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]) { if ('string' !== typeof copy[key]) {
return; return;
} }
copy[key] = copy[key].replace(homeRe, homedir + path.sep); copy[key] = copy[key].replace(homeRe, homedir + path.sep);
});
Object.keys(tpls).sort(function (a, b) { tplKeys.forEach(function (key) {
return b.length - a.length; if ('string' !== typeof copy[key]) {
}).forEach(function (tplname) { return;
if (!tpls[tplname]) { }
tplKeys.forEach(function (tplname) {
if (!copy[tplname]) {
// what can't be templated now may be templatable later // what can't be templated now may be templatable later
return; return;
} }
copy[key] = copy[key].replace(':' + tplname, tpls[tplname]); copy[key] = copy[key].replace(':' + tplname, copy[tplname]);
}); });
}); });

View File

@ -6,12 +6,14 @@ var le = LE.create({
, acme: require('le-acme-core').ACME.create() , acme: require('le-acme-core').ACME.create()
, store: require('le-store-certbot').create({ , store: require('le-store-certbot').create({
configDir: '~/letsencrypt.test/etc/' configDir: '~/letsencrypt.test/etc/'
, webrootPath: '~/letsencrypt.test/tmp/:hostname'
}) })
, debug: true
}); });
var testId = Math.round(Date.now() / 1000).toString(); var testId = Math.round(Date.now() / 1000).toString();
var fakeEmail = 'coolaj86+le.' + testId + '@example.com'; var fakeEmail = 'coolaj86+le.' + testId + '@example.com';
var testEmail = 'coolaj86+le.' + testId + '@example.com'; var testEmail = 'coolaj86+le.' + testId + '@gmail.com';
var testAccount; var testAccount;
var tests = [ var tests = [
@ -66,9 +68,8 @@ var tests = [
}); });
} }
, function () { , function () {
throw new Error('NOT IMPLEMENTED');
return le.core.accounts.registerAsync({ return le.core.accounts.registerAsync({
email: 'coolaj86+le.' + testId + '@example.com' email: testEmail
, agreeTos: true , agreeTos: true
, rsaKeySize: 2048 , rsaKeySize: 2048
}).then(function (account) { }).then(function (account) {