generate account on CLI first use

This commit is contained in:
AJ ONeal 2019-05-14 19:50:41 -06:00
parent 5db75f1aa4
commit 202a733354
1 changed files with 33 additions and 27 deletions

View File

@ -126,6 +126,7 @@ Console.setup = function (state) {
Console.rl = rl; Console.rl = rl;
}; };
Console.teardown = function () { Console.teardown = function () {
if (!Console.rl) { return; }
// https://github.com/nodejs/node/issues/21319 // https://github.com/nodejs/node/issues/21319
if (useTty) { try { Console.stdin.push(null); } catch(e) { /*ignore*/ } } if (useTty) { try { Console.stdin.push(null); } catch(e) { /*ignore*/ } }
Console.rl.close(); Console.rl.close();
@ -134,7 +135,7 @@ Console.teardown = function () {
}; };
function askEmail(cb) { function askEmail(cb) {
Console.setup(); Console.setup(state);
if (state.config.email) { cb(); return; } if (state.config.email) { cb(); return; }
console.info(TPLS.remote.setup.email); console.info(TPLS.remote.setup.email);
// TODO attempt to read email from npmrc or the like? // TODO attempt to read email from npmrc or the like?
@ -361,6 +362,21 @@ function bootstrap(opts) {
method: 'HEAD' method: 'HEAD'
, url: RC.resolve('/acme/new-nonce') , url: RC.resolve('/acme/new-nonce')
, headers: { "User-Agent": 'Telebit/' + pkg.version } , headers: { "User-Agent": 'Telebit/' + pkg.version }
}).catch(RC.createRelauncher(bootstrap._replay(opts), bootstrap._state)).catch(function (err) {
if ('ENOENT' === err.code || 'ECONNREFUSED' === err.code) {
console.error("Either the telebit service was not already (and could not be started) or its socket could not be written to.");
console.error(err);
} else if ('ENOTSOCK' === err.code) {
console.error("Strange socket error:");
console.error(err);
// Is this ignorable?
//return;
} else {
console.error("Unknown error:");
console.error(err);
}
console.error(err);
process.exit(17);
}).then(function (resp) { }).then(function (resp) {
var nonce = resp.headers['replay-nonce']; var nonce = resp.headers['replay-nonce'];
var newAccountUrl = RC.resolve('/acme/new-acct'); var newAccountUrl = RC.resolve('/acme/new-acct');
@ -396,33 +412,20 @@ function bootstrap(opts) {
}).then(function (resp) { }).then(function (resp) {
//nonce = resp.headers['replay-nonce']; //nonce = resp.headers['replay-nonce'];
if (!resp.body || 'valid' !== resp.body.status) { if (!resp.body || 'valid' !== resp.body.status) {
console.error('request jws:', jws); throw new Error("Did not successfully create or restore account:\n"
console.error('response:'); + "Email: " + opts.email + "\n"
console.error(resp.headers); + "Request JWS:\n" + JSON.stringify(jws, null, 2) + "\n"
console.error(resp.body); + "Response:\n"
throw new Error("did not successfully create or restore account"); + "Headers:\n" + JSON.stringify(resp.headers, null, 2) + "\n"
+ "Body:\n" + resp.body + "\n"
);
} }
return resp.body; return resp.body;
}); });
}); });
}).catch(RC.createRelauncher(bootstrap._replay(opts), bootstrap._bootstate)).catch(function (err) {
if ('ENOENT' === err.code || 'ECONNREFUSED' === err.code) {
console.error("Either the telebit service was not already (and could not be started) or its socket could not be written to.");
console.error(err);
} else if ('ENOTSOCK' === err.code) {
console.error("Strange socket error:");
console.error(err);
// Is this ignorable?
//return;
} else {
console.error("Unknown error:");
console.error(err);
}
console.error(err);
process.exit(17);
}); });
} }
bootstrap._bootstate = {}; bootstrap._state = {};
bootstrap._replay = function (_opts) { bootstrap._replay = function (_opts) {
return function (opts) { return function (opts) {
// supply opts to match reverse signature (.length checking) // supply opts to match reverse signature (.length checking)
@ -465,6 +468,7 @@ function handleConfig(config) {
parseCli(state); parseCli(state);
}); });
}); });
// TODO XXXXXXX
return; return;
} }
@ -505,6 +509,8 @@ function handleConfig(config) {
} }
}); });
return; return;
} else {
Console.teardown();
} }
//console.log("no questioning:"); //console.log("no questioning:");
@ -895,12 +901,12 @@ util.promisify(fs.readFile)(confpath, 'utf8').catch(function (err) {
state.pub = keypairs.neuter({ jwk: key }); state.pub = keypairs.neuter({ jwk: key });
// we don't have config yet // we don't have config yet
state.config = {}; state.config = {};
return bootstrap({ key: state.key, onlyReturnExisting: true }).catch(function (err) { return bootstrap({ key: state.key, onlyReturnExisting: true }).catch(function (/*#err*/) {
console.error("[DEBUG] local account not created?"); //#console.warn("[DEBUG] local account not created?");
console.error(err); //#console.warn(err);
// Ask for email address. The prior email may have been bad // Ask for email address. The prior email may have been bad
return require('util').promisify(askEmail).then(function (email) { return require('util').promisify(askEmail)().then(function () {
return bootstrap({ key: state.key, email: email }); return bootstrap({ key: state.key, email: state.config.email });
}); });
}).catch(function (err) { }).catch(function (err) {
console.error(err); console.error(err);