fix keystore fallback and tests

This commit is contained in:
AJ ONeal 2019-05-12 00:10:58 -06:00
parent 0489d20fd3
commit 6c6f9133e9
3 changed files with 17 additions and 4 deletions

View File

@ -17,6 +17,7 @@ module.exports.create = function (opts) {
} }
, setPassword: function (service, name, key) { , setPassword: function (service, name, key) {
var f = path.join(opts.configDir, name + keyext); var f = path.join(opts.configDir, name + keyext);
console.log('[DEBUG] set', f);
return fs.writeFile(f, key, 'utf8'); return fs.writeFile(f, key, 'utf8');
} }
, deletePassword: function (service, name) { , deletePassword: function (service, name) {
@ -28,7 +29,9 @@ module.exports.create = function (opts) {
return Promise.all(nodes.filter(function (node) { return Promise.all(nodes.filter(function (node) {
return keyext === node.slice(-4); return keyext === node.slice(-4);
}).map(function (node) { }).map(function (node) {
return fs.readFile(path.join(opts.configDir, node + keyext)); return fs.readFile(path.join(opts.configDir, node), 'utf8').then(function (data) {
return { password: data };
});
})); }));
}); });
} }

View File

@ -1,8 +1,11 @@
(function () {
'use strict'; 'use strict';
var keystore = require('./keystore.js').create({ var keystore = require('./keystore.js').create({
configDir: require('path').join(require('os').homedir(), '.local/telebit/') configDir: require('path').join(require('os').homedir(), '.config/telebit/')
, fallback: true
}); });
var name = "testy-mctestface-1"; var name = "testy-mctestface-1";
return keystore.get(name).then(function (jwk) { return keystore.get(name).then(function (jwk) {
console.log("get1", typeof jwk, jwk); console.log("get1", typeof jwk, jwk);
@ -10,8 +13,11 @@ return keystore.get(name).then(function (jwk) {
return require('keypairs').generate().then(function (jwk) { return require('keypairs').generate().then(function (jwk) {
var json = JSON.stringify(jwk.private); var json = JSON.stringify(jwk.private);
return keystore.set(name, json).then(function () { return keystore.set(name, json).then(function () {
return keystore.get(name).then(function (val2) { return keystore.all().then(function (vals) {
console.log("get2", val2); console.log("All", vals);
return keystore.get(name).then(function (val2) {
console.log("get2", val2);
});
}); });
}).catch(function (err) { }).catch(function (err) {
console.log('badness', err); console.log('badness', err);
@ -20,3 +26,4 @@ return keystore.get(name).then(function (jwk) {
} }
return jwk; return jwk;
}); });
}());

View File

@ -4,6 +4,9 @@ module.exports.create = function (opts) {
var service = opts.name || "Telebit"; var service = opts.name || "Telebit";
var keytar; var keytar;
try { try {
if (opts.fallback) {
throw new Error("forced fallback");
}
keytar = require('keytar'); keytar = require('keytar');
// TODO test that long "passwords" (JWTs and JWKs) can be stored in all OSes // TODO test that long "passwords" (JWTs and JWKs) can be stored in all OSes
} catch(e) { } catch(e) {