-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstore.js
More file actions
364 lines (323 loc) · 12.3 KB
/
store.js
File metadata and controls
364 lines (323 loc) · 12.3 KB
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
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
const fs = require('fs');
const path = require('path');
const chalk = require('chalk');
const NodeGit = require('nodegit');
const Safe = require('./safe');
const log = require('./log');
const Store = class Store {
constructor (storePath, password) {
this.path = storePath; // the store path
this.password = password; // the store master password
this.storeBasePath = path.join(this.path, 'store.json'); // the store info json path
try {
// try to read the store info
this.store = require(this.storeBasePath);
} catch (error) {
log.error('Unable to read the password store info', error);
log.nl();
}
}
/**
* adds a new record to the password store
* @param {string} type
* @param {object} info
* @param {object} oldInfo
* @function
* @returns {Promise}
*/
add (type, info, oldInfo) {
return new Promise((resolve, reject) => {
delete info.confirm;
const data = {
label: info.label,
tags: info.tags.split(',').map(tag => tag.trim()),
type,
safe: Safe.encrypt(JSON.stringify(info), this.password)
};
const storeFileName = info.label.replace(/ +/g, '-').toLowerCase() + '.json';
const storePathType = path.join(this.path, type);
const storePath = path.join(storePathType, storeFileName);
if (!fs.existsSync(storePathType)) {
try {
fs.mkdirSync(storePathType);
} catch (error) {
reject(error);
return;
}
}
// if we're updating a store
const oldStoreFileName = oldInfo.label.replace(/ +/g, '-').toLowerCase() + '.json';
if (typeof oldInfo !== 'undefined' && oldStoreFileName !== storeFileName) {
try {
fs.unlinkSync(path.join(storePathType, oldStoreFileName));
this.removeIndex(`./${oldInfo.type}/${oldStoreFileName}`);
} catch (error) {
reject(error);
return;
}
}
try {
fs.writeFileSync(storePath, JSON.stringify(data, undefined, 4));
this.addIndex({
label: info.label,
type,
path: storePath.replace(this.path, '.')
});
resolve();
} catch (error) {
reject(error);
}
});
}
/**
* remove a password storage record
* @function
*/
remove () {
}
/**
* find a password storage record based on a search term
* @param {string} term
*/
find (term) {
return new Promise((resolve, reject) => {
let found = [];
if (!term) {
resolve(found);
return;
}
this.store.indexes.forEach(index => {
try {
const storeData = require(path.resolve(this.path, index.path));
const storeSafeString = Safe.decrypt(storeData.safe, this.password);
const storeSafeData = JSON.parse(storeSafeString.data);
if (storeSafeString.check) {
for (let key in storeSafeData) {
if (typeof storeSafeData[key] === 'string' && storeSafeData[key].toLowerCase().indexOf(term.toLowerCase()) > -1) {
found.push({...storeSafeData, type: index.type});
break;
}
}
}
} catch (error) {
// reject(error);
found.push({label: `Password store file missing (${index.path})! try running a reindex to fix this`, type: 'Unknown'});
}
});
resolve(found.map(pass => {
return {name: `${pass.label} (${pass.type})`, value: pass};
}));
});
}
/**
* list all the records inside the password store
* @function
* @returns {Promise}
*/
list () {
return new Promise((resolve, reject) => {
log.info(`Store contains ${this.store.indexes.length} records:`);
let list = {};
this.store.indexes.forEach(index => {
try {
const storeData = require(path.resolve(this.path, index.path));
const storeSafeString = Safe.decrypt(storeData.safe, this.password);
const storeSafeData = JSON.parse(storeSafeString.data);
if (storeSafeString.check) {
if (typeof list[index.type] === 'undefined') list[index.type] = [];
list[index.type].push(storeSafeData);
}
} catch (error) {
reject(error);
}
});
for (let keys = Object.keys(list), j = 0, end = keys.length; j < end; j++) {
const last = j >= end - 1;
log.simple(`${last ? '└──' : '├──'} ${chalk.yellowBright.bold(keys[j])} (${chalk.yellowBright.bold(list[keys[j]].length)}):`);
list[keys[j]].forEach((record, i) => {
log.simple(`${last ? ' ' : '│ '}${i >= list[keys[j]].length - 1 ? '└──' : '├──'} ${chalk.white(record.label)}`);
});
}
log.nl();
});
}
open (path) {
}
addIndex (index) {
if (this.store.indexes.indexOf(index) === -1) {
this.store.indexes.push(index);
this.save();
}
}
removeIndex (path) {
this.store.indexes = this.store.indexes.filter(index => {
return index.path !== path;
});
this.save();
}
reindex () {
}
/**
* save the stores info
* @function
* @returns {Store}
*/
save () {
try {
fs.writeFileSync(this.storeBasePath, JSON.stringify(this.store, undefined, 4));
} catch (error) {
log.error('There was a problem while saving the settings', error);
log.nl();
}
return this;
}
sync (options) {
return new Promise((resolve, reject) => {
const sync = Safe.decrypt(this.store.sync, this.password);
if (!sync.check) {
reject('Unable to decrypt repo credentials!');
return;
}
try {
const data = JSON.parse(sync.data);
let repository, index, remote, oid;
NodeGit.Repository.open(this.path).then(repo => {
repository = repo;
return repo.getRemote('origin');
}).then(rmt => {
remote = rmt;
return repository.fetch(rmt, {
callbacks: {
credentials: (url, username) => {
return NodeGit.Cred.userpassPlaintextNew(data.username, data.password);
}
}
});
}).then(() => {
return repository.checkoutBranch('master');
}).then(() => {
// refresh the current indexes
return repository.refreshIndex();
}).then(idx => {
index = idx;
// stage all files
return index.addAll();
}).then(() => {
return index.write();
}).then(() => {
return index.writeTree();
}).then(o => {
oid = o;
return NodeGit.Reference.nameToId(repository, 'HEAD');
}).then(head => {
return repository.getCommit(head);
}).then(parent => {
// initialize a git author
const author = NodeGit.Signature.create(data.username, data.email, Math.round(Date.now() / 1000), 60);
// commit the updates
return repository.createCommit('HEAD', author, author, 'Update', oid, [parent]);
}).then(() => {
// push the newly created commit
return remote.push(['refs/heads/master:refs/heads/master'], {
callbacks: {
credentials: (url, userName) => {
return NodeGit.Cred.userpassPlaintextNew(data.username, data.password);
},
transferProgress: progress => {
console.log('progress: ', progress);
}
}
});
}).then(() => {
resolve();
}).catch(error => {
reject('Unable to open password store repository!', error);
});
} catch (error) {
reject('Unable to parse repo credentials data!', error);
}
});
}
/**
* initialize the git repo for the current password storage
* @param {object} options
* @function
* @returns {Promise}
*/
syncInit (options) {
return new Promise((resolve, reject) => {
let repository, remote;
// initialize the git repo for this password storage
NodeGit.Repository.init(this.path, 0).then(repo => {
repository = repo;
// get all the remotes for this password storage
return repo.getRemotes();
}).then(remotes => {
// saving the credetials
this.store.sync = Safe.encrypt(JSON.stringify(options), this.password);
this.save();
// remove origin remote if available
if (remotes.length) {
return this.syncRemoveRemote();
}
return Promise.resolve();
}).then(() => {
return this.syncAddRemote(options.origin);
}).then(rmt => {
remote = rmt;
// initialize a git author
const author = NodeGit.Signature.create(options.username, options.email, Math.round(Date.now() / 1000), 60);
// create an initial commit
return repository.createCommitOnHead([], author, author, 'Initial commit');
}).then(() => {
// push the first commit
return remote.push(['refs/heads/master:refs/heads/master'], {
callbacks: {
credentials: (url, userName) => {
return NodeGit.Cred.userpassPlaintextNew(options.username, options.password);
}
}
});
}).then(() => {
resolve();
}).catch(error => {
reject('Unable to inialize password store git repo!', error);
});
});
}
/**
* adds a git remote to the current password storage repository
* @param {string} origin
* @function
* @returns {Promise}
*/
syncAddRemote (origin) {
return new Promise((resolve, reject) => {
NodeGit.Repository.open(this.path).then(repository => {
return NodeGit.Remote.create(repository, 'origin', origin);
}).then(remote => {
resolve(remote);
}).catch(error => {
reject(error);
});
});
}
/**
* removes the origin git remote from the current password storage repository
* @param {string} origin
* @function
* @returns {Promise}
*/
syncRemoveRemote () {
return new Promise((resolve, reject) => {
NodeGit.Repository.open(this.path).then(repository => {
return NodeGit.Remote.delete(repository, 'origin');
}).then(() => {
resolve();
}).catch(error => {
reject(error);
});
});
}
};
module.exports = Store;