-
Notifications
You must be signed in to change notification settings - Fork 25
/
index.js
65 lines (55 loc) · 1.44 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
const async = require('async');
const decrypter = require('./lib/decrypter');
const encoder = require('./lib/encoder');
const hmac = require('./lib/hmac');
const keys = require('./lib/keys');
const secrets = require('./lib/secrets');
const xtend = require('xtend');
const defaults = {
limit: 1
};
function Credstash(config) {
this.table = config ? config.table : undefined;
}
Credstash.prototype.list = function(options, done) {
if (typeof options === 'function') {
done = options;
options = defaults;
} else {
options = xtend(defaults, options);
}
return async.waterfall([
async.apply(secrets.list, this.table, options),
async.apply(keys.decrypt),
async.apply(hmac.check),
async.apply(decrypter.decryptedObject)
], function (err, secrets) {
if (err) {
return done(err);
}
done(null, secrets);
});
};
Credstash.prototype.get = function(name, options, done) {
if (typeof options === 'function') {
done = options;
options = defaults;
} else {
options = xtend(defaults, options);
}
return async.waterfall([
async.apply(secrets.get, this.table, name, options),
async.apply(keys.decrypt),
async.apply(hmac.check),
async.apply(decrypter.decryptedList)
], function (err, secrets) {
if (err) {
return done(err);
}
if (options.limit === 1) {
return done(null, secrets && secrets[0]);
}
done(null, secrets);
});
};
module.exports = Credstash;