Skip to content

Commit

Permalink
Merge pull request #1 from jasnell/update
Browse files Browse the repository at this point in the history
Update code and dependencies
  • Loading branch information
jasnell authored Dec 25, 2016
2 parents 4ca38e6 + e2f452f commit 766beb9
Show file tree
Hide file tree
Showing 6 changed files with 95 additions and 175 deletions.
74 changes: 0 additions & 74 deletions SIGNED.md

This file was deleted.

54 changes: 27 additions & 27 deletions app.js
Original file line number Diff line number Diff line change
@@ -1,48 +1,48 @@
#!/usr/bin/env node

'use strict';
const app = require('commander');
const qrenroll = require('./qrenroll');
const enroll = require('./enroll');
const generate = require('./generate');

app.version('0.0.1');
app.version('2.0.0');

if (qrenroll.has_jsqrcode) {
app.command('enroll-qrcode <qrcode>')
.alias('enq')
.description('enroll using a qrcode file')
.option('-l, --label <label>', 'the label')
.action(qrenroll);
.alias('enq')
.description('enroll using a qrcode file')
.option('-l, --label <label>', 'the label')
.action(qrenroll);
}

app.command('enroll')
.alias('en')
.description('enroll')
.option('-s, --secret <secret>', 'the secret')
.option('-l, --label <label>', 'the label')
.option('-a, --algorithm <algorithm>', 'sha1, sha256, sha512')
.option('-e, --encoding <encoding>', 'ascii, hex, base32, base64')
.option('-d, --digits <digits>', 'number of digits', parseInt)
.option('-i, --issuer <issuer>', 'issuer')
.action(enroll);
.alias('en')
.description('enroll')
.option('-s, --secret <secret>', 'the secret')
.option('-l, --label <label>', 'the label')
.option('-a, --algorithm <algorithm>', 'sha1, sha256, sha512')
.option('-e, --encoding <encoding>', 'ascii, hex, base32, base64')
.option('-d, --digits <digits>', 'number of digits', parseInt)
.option('-i, --issuer <issuer>', 'issuer')
.action(enroll);

app.command('generate')
.arguments('[label]')
.alias('gen')
.option('-n, --nolabel', 'do not print the label')
.description('generate')
.action(generate);
.arguments('[label]')
.alias('gen')
.option('-n, --nolabel', 'do not print the label')
.description('generate')
.action(generate);

app.command('list')
.alias('l')
.description('list')
.action(generate.list);
.alias('l')
.description('list')
.action(generate.list);

app.command('delete')
.alias('d')
.arguments('<label>')
.description('delete')
.action(generate.delete);
.alias('d')
.arguments('<label>')
.description('delete')
.action(generate.delete);

app.parse(process.argv);

Expand Down
51 changes: 23 additions & 28 deletions enroll.js
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,47 +1,42 @@
'use strict';
const Configstore = require('configstore');
const pkg = require('./package.json');

const conf = new Configstore(pkg.name, {});

function doEnroll(label, secret, options, callback) {
setImmediate(function() {
options = options || {};
try {
var value = {secret:secret};
Object.keys(options).forEach(function(key) {
value[key] = options[key];
});
conf.set(label, value);
callback();
} catch (err) {
callback(err);
}
});
function doEnroll(label, secret, options) {
options = options || {};
if (typeof options !== 'object')
throw new TypeError('options must be an object');

var value = {secret:secret};
var keys = Object.keys(options);
for (var n = 0; n < keys.length; n++) {
value[keys[n]] = options[key];
}
conf.set(label, value);
}

function enroll(env, options) {
if (!env.secret)
console.log('cannot enroll without a secret');
if (!env.label)
console.log('cannot enroll without a label');
if (!env.secret) {
console.error('cannot enroll without a secret');
process.exit(-1);
}
if (!env.label) {
console.error('cannot enroll without a label');
process.exit(-1);
}
var options = {};
if (env.algorithm)
options.algorithm = env.algorithm;
if (env.encoding)
options.encoding = env.encoding;
var digits = parseInt(env.digits);
var digits = env.digits | 0;
if (digits)
options.digits = parseInt(digits);
options.digits = digits;
if (env.issuer)
options.issuer = env.issuer;
doEnroll(env.label, env.secret, options, function(err) {
if (err) {
console.log('could not enroll');
process.exit(1);
}
console.log('enrolled!');
process.exit(0);
});
doEnroll(env.label, env.secret, options);
}
module.exports = enroll;
module.exports.enroll = doEnroll;
22 changes: 11 additions & 11 deletions generate.js
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
'use strict';
const Configstore = require('configstore');
const pkg = require('./package.json');
const passcode = require('passcode');
Expand Down Expand Up @@ -30,28 +31,27 @@ function generate(arg, options) {
var nolabel = options.nolabel;
if (arg) doGenerate(arg, nolabel);
else {
Object.keys(conf.all).forEach(function(item) {
doGenerate(item,nolabel);
});
var keys = Object.keys(conf.all);
for (var n = 0; n < keys.length; n++) {
doGenerate(keys[n],nolabel);
};
}
process.exit(0);
}

generate.list = function(arg, options) {
Object.keys(conf.all).forEach(function(item) {
console.log(item);
});
process.exit(0);
var keys = Object.keys(conf.all);
for (var n = 0; n < keys.length; n++) {
console.log(keys[n]);
};
};

generate.delete = function(arg, options) {
if (!arg) {
console.log('no label provided');
process.exit(1);
console.error('no label provided');
process.exit(-1);
}
conf.del(arg);
console.log('deleted!');
process.exit(0);
};

module.exports = generate;
4 changes: 2 additions & 2 deletions package.json
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "tfa",
"version": "1.0.1",
"version": "2.0.0",
"description": "a two-factor auth client",
"main": "app.js",
"author": "James M Snell <jasnell@gmail.com>",
Expand All @@ -23,7 +23,7 @@
"dependencies": {
"chalk": "^1.1.1",
"commander": "^2.8.1",
"configstore": "^1.2.1",
"configstore": "^2.1.0",
"passcode": "^1.0.2"
},
"optionalDependencies": {
Expand Down
65 changes: 32 additions & 33 deletions qrenroll.js
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
'use strict';

function has_jsqrcode() {
try {
require('jsqrcode');
Expand All @@ -20,46 +22,43 @@ if (has) {
const fs = require('fs');

function enroll(arg, options) {
setImmediate(function() {
var qr_path = path.resolve(process.cwd(), arg);
if (!fs.existsSync(qr_path)) {
console.log('cannot read QRCode image');
process.exit(1);
var qr_path = path.resolve(process.cwd(), arg);
if (!fs.existsSync(qr_path)) {
console.error('cannot read QRCode image');
process.exit(-1);
}
var image = new Image();
image.onload = function() {
var result;
try{
result = qrcode.decode(image);
var parsed = url.parse(result);
var label = options.label || (unescape(parsed.pathname.substr(1)));
var query = qs.parse(parsed.query);
var secret = query.secret;
var opts = {};
if (query.issuer) opts.issuer = query.issuer;
en.enroll(label, secret, opts, function(err) {
if (err) {
console.error('unable to enroll');
process.exit(-1);
}
console.log('enrolled!');
});
} catch(e) {
console.error('unable to read qr code');
process.exit(-1);
}
var image = new Image();
image.onload = function() {
var result;
try{
result = qrcode.decode(image);
var parsed = url.parse(result);
var label = options.label || (unescape(parsed.pathname.substr(1)));
var query = qs.parse(parsed.query);
var secret = query.secret;
var opts = {};
if (query.issuer) opts.issuer = query.issuer;
en.enroll(label, secret, opts, function(err) {
if (err) {
console.log('unable to enroll');
process.exit(1);
}
console.log('enrolled!');
process.exit(0);
});
}catch(e){
console.log('unable to read qr code');
process.exit(1);
}
};
image.src = qr_path;
});
};
image.src = qr_path;
}

module.exports = enroll;
} else {
module.exports = function() {
console.log(
console.error(
'qrcode scanning not supported. please install optional dependencies');
process.exit(1);
process.exit(-1);
};
}
module.exports.has_jsqrcode = has;

0 comments on commit 766beb9

Please sign in to comment.