-
Notifications
You must be signed in to change notification settings - Fork 0
/
gpg.js
74 lines (66 loc) · 1.65 KB
/
gpg.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
66
67
68
69
70
71
72
73
74
const gpg = require("gpg");
function extractFromKey(key, testRE, extractionIndex) {
const match = key.match(testRE);
if (match) {
return match[extractionIndex];
}
return null;
}
function getKeyStatus(expirationDateString) {
if (expirationDateString === null) {
return "valid";
}
const now = Date.now();
const weekFromNow = now + 1000 * 60 * 60 * 24 * 7;
const expirationDate = new Date(expirationDateString).getTime();
if (expirationDate < now) {
return "expired";
}
if (expirationDate < weekFromNow) {
return "expiring";
}
return "valid";
}
function parseKey(key) {
const email = extractFromKey(key, /<([^>]+)>/, 1);
const expirationDateString = extractFromKey(
key,
/\[expire[sd]: (\d\d\d\d-\d\d-\d\d)\]/,
1
);
return {
email,
status: getKeyStatus(expirationDateString),
};
}
function parseKeys(listKeysOutput) {
const keyLines = listKeysOutput.trim().split("\n");
const pubs = keyLines
.filter((line) => line.match(/^pub/))
.map((l) => l.trim());
const uids = keyLines
.filter((line) => line.match(/^uid/))
.map((l) => l.trim());
const keys = pubs.reduce(
(memo, curr, index) => memo.concat(curr + uids[index]),
[]
);
return keys.map(parseKey);
}
function listKeys(homedir) {
return new Promise((resolve, reject) => {
gpg.call("", [`--homedir=${homedir}`, "--list-keys"], (err, result) => {
if (err) {
return reject(err);
}
return resolve(result.toString());
});
});
}
async function getKeys(keyringDir) {
const listKeysOutput = await listKeys(keyringDir);
return parseKeys(listKeysOutput);
}
module.exports = {
getKeys,
};