telebit.js/lib/keystore-fallback.js

40 lines
1.1 KiB
JavaScript
Raw Normal View History

'use strict';
2019-03-09 12:05:37 +00:00
/*global Promise*/
var fs = require('fs').promises;
var path = require('path');
module.exports.create = function (opts) {
2019-03-09 12:05:37 +00:00
var keyext = '.key';
return {
getPassword: function (service, name) {
2019-03-09 12:05:37 +00:00
var f = path.join(opts.configDir, name + keyext);
return fs.readFile(f, 'utf8').catch(function (err) {
if ('ENOEXIST' === err.code) {
return;
}
});
}
, setPassword: function (service, name, key) {
2019-03-09 12:05:37 +00:00
var f = path.join(opts.configDir, name + keyext);
return fs.writeFile(f, key, 'utf8');
}
, deletePassword: function (service, name) {
2019-03-09 12:05:37 +00:00
var f = path.join(opts.configDir, name + keyext);
return fs.unlink(f);
}
2019-03-09 12:05:37 +00:00
, findCredentials: function (/*service*/) {
2019-05-12 05:40:22 +00:00
return fs.readdir(opts.configDir).then(function (nodes) {
2019-03-09 12:05:37 +00:00
return Promise.all(nodes.filter(function (node) {
return keyext === node.slice(-4);
}).map(function (node) {
2019-05-12 06:10:58 +00:00
return fs.readFile(path.join(opts.configDir, node), 'utf8').then(function (data) {
return { password: data };
});
2019-03-09 12:05:37 +00:00
}));
});
}
, insecure: true
};
};