Skip to content

Commit

Permalink
Accept public ssh key' signature from command line, see issue theoapp#2
Browse files Browse the repository at this point in the history
  • Loading branch information
macno committed Apr 28, 2019
1 parent 2228c7d commit 2c66b68
Showing 1 changed file with 35 additions and 2 deletions.
37 changes: 35 additions & 2 deletions src/cmds/keys_cmds/add.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ exports.builder = yargs => {
.option('sign', {
alias: 's',
describe:
'sign Public ssh key with private key. (Needs THEO_PRIVATE_KEY and THEO_PRIVATE_KEY_PASSPHRASE env variable or -c and -p / -i)',
'sign Public ssh key with private key. (Needs THEO_PRIVATE_KEY env (or -c) and THEO_PRIVATE_KEY_PASSPHRASE env (or -p / -i))',
boolean: true
})
.option('certificate', {
Expand All @@ -59,6 +59,11 @@ exports.builder = yargs => {
describe: 'read passphrase for private key from stdin',
boolean: true
})
.option('signature', {
alias: 'g',
describe: "Public ssh key' signature",
type: 'string'
})
.demandOption(['key']);
};

Expand All @@ -68,6 +73,11 @@ exports.handler = async argv => {
let private_key;
let private_key_path;
let passphrase;
if (argv.sign && argv.signature) {
const e = new Error('--sign and --signature are mutually exclusive');
outputError(e);
process.exit(1);
}
if (argv.sign) {
if (argv.certificate) {
private_key_path = argv.certificate;
Expand All @@ -93,6 +103,21 @@ exports.handler = async argv => {
process.exit(11);
}
}
if (argv.signature) {
console.log('typeof argv.signature', typeof argv.signature);
if (typeof argv.key !== typeof argv.signature) {
const e = new Error('When using --signature number of keys and signatures must be the same');
outputError(e);
process.exit(1);
}
if (typeof argv.key !== 'string') {
if (argv.signature.length !== argv.key.length) {
const e = new Error('When using --signature number of keys and signatures must be the same');
outputError(e);
process.exit(1);
}
}
}
let public_keys;
if (argv.key) {
if (typeof argv.key === 'string') {
Expand All @@ -107,7 +132,6 @@ exports.handler = async argv => {
}
if (argv.sign) {
payload.keys = [];

let signer;
try {
signer = new Signer(private_key, passphrase);
Expand All @@ -127,9 +151,18 @@ exports.handler = async argv => {
process.exit(13);
}
});
} else if (argv.signature) {
payload.keys = [];
public_keys.forEach((public_key, i) => {
payload.keys.push({
key: public_key,
signature: argv.signature[i]
});
});
} else {
payload.keys = public_keys;
}
console.log(payload);
const account = await post('/accounts/' + argv.account + '/keys', payload);
outputJson(account);
} catch (err) {
Expand Down

0 comments on commit 2c66b68

Please sign in to comment.