forked from atom/node-keytar
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkeytar.js
291 lines (227 loc) · 6.41 KB
/
keytar.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
const fs = require("fs");
const os = require("os");
const crypto = require("crypto");
function checkRequired(val, name) {
if (!val || val.length <= 0) {
throw new Error(name + ' is required.');
}
}
// Serves as a drop-in replacement for keytar C++ bindings, written in pure node.js.
class credentialStore {
constructor(filePath, key) {
this.algorithm = 'aes-256-ctr';
this.iv = null;
this.secretKey = null;
if (key) {
this.secretKey = crypto.scryptSync(key, 'salt', 32);
}
this.filePath = filePath;
this.load();
}
async setPassword(service, account, password) {
this.throwIfObject(service, account, password);
let serviceMap = this.services.get(service) || new Map();
serviceMap.set(account, password);
this.services.set(service, serviceMap);
this.save();
}
async getPassword(service, account) {
this.throwIfObject(service, account);
let serviceMap = this.services.get(service);
if (serviceMap && serviceMap.has(account)) {
return serviceMap.get(account) || null;
}
return null;
}
async deletePassword(service, account) {
this.throwIfObject(service, account);
let serviceMap = this.services.get(service);
if (serviceMap && serviceMap.has(account)) {
serviceMap.delete(account);
this.services.set(service, serviceMap);
this.save();
return true;
}
return false;
}
async findPassword(service) {
this.throwIfObject(service);
let serviceMap = this.services.get(service);
if (serviceMap) {
return serviceMap.values().next().value;
}
return null;
}
async findCredentials(service) {
this.throwIfObject(service);
let serviceMap = this.services.get(service);
let retVal = [];
if (serviceMap) {
for (const [k, v] of serviceMap.entries()) {
retVal.push({
account: k,
password: v
});
}
}
return retVal;
}
throwIfObject(service, account, password) {
if (service instanceof Object) {
throw new Error("Parameter 'service' must be a string");
}
if (account instanceof Object) {
throw new Error("Parameter 'username' must be a string");
}
if (password instanceof Object) {
throw new Error("Parameter 'password' must be a string");
}
}
saveData() {
let data = {services: []}
for (const [serviceName, serviceMap] of this.services.entries()) {
let services = {};
services.key = serviceName;
services.value = [];
for (const [accountName, accountPassword] of serviceMap.entries()) {
let account = {};
account.key = accountName;
account.value = accountPassword;
services.value.push(account);
}
data.services.push(services);
}
return data;
}
loadData(data) {
if (data.services) {
let serviceMap = new Map();
for (const service of data.services) {
let accountMap = new Map();
for (const account of service.value) {
accountMap.set(account.key, account.value);
}
serviceMap.set(service.key, accountMap);
}
return serviceMap;
}
}
save() {
if (this.secretKey) {
this.saveEncrypted();
}
else {
this.saveUnencrypted();
}
}
load() {
if (this.secretKey) {
this.loadEncrypted();
}
else {
this.loadUnencrypted();
}
}
saveUnencrypted() {
let data = this.saveData();
try {
fs.writeFileSync(this.filePath, JSON.stringify(data), "utf8");
}
catch (e) {
console.error(e);
}
}
loadUnencrypted() {
try {
let contents = JSON.parse(fs.readFileSync(this.filePath, "utf8"));
if (contents.iv) {
// We can't decrypt. Erase and start over.
this.services = new Map();
return;
}
let data = this.loadData(contents);
this.services = data;
return;
}
catch(e) {
console.error(e);
}
this.services = new Map();
}
saveEncrypted() {
let data = this.saveData();
let contents = JSON.stringify(data);
let secrets = this.encrypt(contents);
try {
fs.writeFileSync(this.filePath, JSON.stringify(secrets), "utf8");
}
catch (e) {
console.error(e);
}
}
loadEncrypted() {
try {
let secrets = fs.readFileSync(this.filePath, "utf8");
let hash = JSON.parse(secrets);
if (hash.iv) {
this.iv = Buffer.from(hash.iv.data)
let contents = this.decrypt(hash);
let data = this.loadData(JSON.parse(contents));
this.services = data;
}
else if (hash.services) {
// Attempt to recover data.
loadUnencrypted();
this.iv = crypto.randomBytes(16);
}
return;
}
catch(e) {
console.error(e);
}
this.services = new Map();
this.iv = crypto.randomBytes(16);
}
encrypt(text) {
const cipher = crypto.createCipheriv(this.algorithm, Buffer.from(this.secretKey), this.iv);
const encrypted = Buffer.concat([cipher.update(text), cipher.final()]);
return {
iv: this.iv,
contents: encrypted.toString('hex')
}
};
decrypt(hash) {
const decipher = crypto.createDecipheriv(this.algorithm, Buffer.from(this.secretKey), this.iv);
const decrypted = Buffer.concat([decipher.update(Buffer.from(hash.contents, 'hex')), decipher.final()]);
return decrypted.toString();
};
}
let credsDir = os.homedir() + '/.local/creds';
fs.mkdirSync(credsDir, { recursive: true });
keytar = new credentialStore(credsDir + '/keytar.json', process.env.ENCRYPTION_KEY || null);
module.exports = {
getPassword: function (service, account) {
checkRequired(service, 'Service')
checkRequired(account, 'Account')
return keytar.getPassword(service, account)
},
setPassword: function (service, account, password) {
checkRequired(service, 'Service')
checkRequired(account, 'Account')
checkRequired(password, 'Password')
return keytar.setPassword(service, account, password)
},
deletePassword: function (service, account) {
checkRequired(service, 'Service')
checkRequired(account, 'Account')
return keytar.deletePassword(service, account)
},
findPassword: function (service) {
checkRequired(service, 'Service')
return keytar.findPassword(service)
},
findCredentials: function (service) {
checkRequired(service, 'Service')
return keytar.findCredentials(service)
}
}