@secrez/core 1.0.3 → 1.0.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +0 -0
- package/README.md +16 -17
- package/_sullof/mom.js +23 -0
- package/coverage.report +95 -99
- package/package.json +40 -43
- package/src/Entry.js +44 -42
- package/src/Secrez.js +234 -198
- package/src/_Secrez.js +135 -130
- package/src/config/ConfigUtils.js +98 -75
- package/src/config/booleans.js +3 -6
- package/src/config/index.js +9 -12
- package/src/index.js +6 -6
- package/.eslintignore +0 -0
- package/.eslintrc +0 -33
- package/.jshintrc +0 -3
- package/pnpm-lock.yaml +0 -2460
package/src/_Secrez.js
CHANGED
|
@@ -1,264 +1,269 @@
|
|
|
1
|
-
const fs = require(
|
|
2
|
-
const utils = require(
|
|
1
|
+
const fs = require("fs-extra");
|
|
2
|
+
const utils = require("@secrez/utils");
|
|
3
3
|
|
|
4
|
-
const {
|
|
5
|
-
RETURN_UINT8_ARRAY
|
|
6
|
-
} = require('./config/booleans')
|
|
4
|
+
const { RETURN_UINT8_ARRAY } = require("./config/booleans");
|
|
7
5
|
|
|
8
|
-
const Crypto = require(
|
|
9
|
-
const bs64 = Crypto.bs64
|
|
6
|
+
const Crypto = require("@secrez/crypto");
|
|
7
|
+
const bs64 = Crypto.bs64;
|
|
10
8
|
|
|
11
9
|
module.exports = function () {
|
|
12
|
-
|
|
13
10
|
const __ = {
|
|
14
|
-
|
|
15
11
|
sharedKeys: {},
|
|
16
12
|
|
|
17
13
|
getSharedKey(publicKey) {
|
|
18
14
|
if (!__.sharedKeys[publicKey]) {
|
|
19
|
-
let publicKeyArr = Crypto.bs64.decode(publicKey.split(
|
|
20
|
-
__.sharedKeys[publicKey] = Crypto.getSharedSecret(
|
|
15
|
+
let publicKeyArr = Crypto.bs64.decode(publicKey.split("$")[0]);
|
|
16
|
+
__.sharedKeys[publicKey] = Crypto.getSharedSecret(
|
|
17
|
+
publicKeyArr,
|
|
18
|
+
__.boxPrivateKey
|
|
19
|
+
);
|
|
21
20
|
}
|
|
22
|
-
return __.sharedKeys[publicKey]
|
|
21
|
+
return __.sharedKeys[publicKey];
|
|
23
22
|
},
|
|
24
23
|
|
|
25
24
|
decrypt(encryptedData, returnUint8Array) {
|
|
26
|
-
return Crypto.decrypt(encryptedData, __.masterKeyArray, returnUint8Array)
|
|
25
|
+
return Crypto.decrypt(encryptedData, __.masterKeyArray, returnUint8Array);
|
|
27
26
|
},
|
|
28
27
|
|
|
29
28
|
preDecrypt(encryptedData, returnUint8Array) {
|
|
30
|
-
return Crypto.decrypt(
|
|
31
|
-
|
|
32
|
-
|
|
29
|
+
return Crypto.decrypt(
|
|
30
|
+
encryptedData,
|
|
31
|
+
__.derivedPassword,
|
|
32
|
+
returnUint8Array
|
|
33
|
+
);
|
|
34
|
+
},
|
|
35
|
+
};
|
|
33
36
|
|
|
34
37
|
class _Secrez {
|
|
35
|
-
|
|
36
38
|
constructor(secrez) {
|
|
37
|
-
this.secrez = secrez
|
|
39
|
+
this.secrez = secrez;
|
|
38
40
|
}
|
|
39
41
|
|
|
40
42
|
async init(password, iterations) {
|
|
41
|
-
__.password = password
|
|
42
|
-
__.iterations = iterations
|
|
43
|
-
__.derivedPassword = await _Secrez.derivePassword(password, iterations)
|
|
43
|
+
__.password = password;
|
|
44
|
+
__.iterations = iterations;
|
|
45
|
+
__.derivedPassword = await _Secrez.derivePassword(password, iterations);
|
|
44
46
|
}
|
|
45
47
|
|
|
46
48
|
preEncrypt(data) {
|
|
47
|
-
return Crypto.encrypt(data, __.derivedPassword)
|
|
49
|
+
return Crypto.encrypt(data, __.derivedPassword);
|
|
48
50
|
}
|
|
49
51
|
|
|
50
52
|
preDecrypt(encryptedData) {
|
|
51
|
-
let conf = this.conf
|
|
53
|
+
let conf = this.conf;
|
|
52
54
|
/* istanbul ignore if */
|
|
53
55
|
if (!conf) {
|
|
54
|
-
conf = this.readConf()
|
|
56
|
+
conf = this.readConf();
|
|
55
57
|
if (!this.secrez) {
|
|
56
|
-
throw new Error(
|
|
58
|
+
throw new Error("Secrez not initiated");
|
|
57
59
|
}
|
|
58
60
|
if (!fs.existsSync(this.secrez.config.keysPath)) {
|
|
59
|
-
throw new Error(
|
|
61
|
+
throw new Error("Account not set yet");
|
|
60
62
|
}
|
|
61
|
-
conf = JSON.parse(fs.readFileSync(this.secrez.config.keysPath,
|
|
63
|
+
conf = JSON.parse(fs.readFileSync(this.secrez.config.keysPath, "utf8"));
|
|
62
64
|
}
|
|
63
65
|
if (encryptedData === __.encryptedMasterKey) {
|
|
64
|
-
throw new Error(
|
|
66
|
+
throw new Error("Forbidden");
|
|
65
67
|
}
|
|
66
|
-
return __.preDecrypt(encryptedData)
|
|
68
|
+
return __.preDecrypt(encryptedData);
|
|
67
69
|
}
|
|
68
70
|
|
|
69
71
|
encrypt(data, urlSafe) {
|
|
70
|
-
let encrypted = Crypto.encrypt(data, __.masterKeyArray)
|
|
72
|
+
let encrypted = Crypto.encrypt(data, __.masterKeyArray);
|
|
71
73
|
if (urlSafe) {
|
|
72
|
-
encrypted = Crypto.fromBase64ToFsSafeBase64(encrypted)
|
|
74
|
+
encrypted = Crypto.fromBase64ToFsSafeBase64(encrypted);
|
|
73
75
|
}
|
|
74
|
-
return encrypted
|
|
76
|
+
return encrypted;
|
|
75
77
|
}
|
|
76
78
|
|
|
77
79
|
decrypt(encryptedData, urlSafe, returnUint8Array) {
|
|
78
80
|
if (urlSafe) {
|
|
79
|
-
encryptedData = Crypto.fromFsSafeBase64ToBase64(encryptedData)
|
|
81
|
+
encryptedData = Crypto.fromFsSafeBase64ToBase64(encryptedData);
|
|
80
82
|
}
|
|
81
|
-
if (
|
|
82
|
-
|
|
83
|
+
if (
|
|
84
|
+
encryptedData === __.encryptedBoxPrivateKey ||
|
|
85
|
+
encryptedData === __.encryptedSignPrivateKey
|
|
83
86
|
) {
|
|
84
|
-
throw new Error(
|
|
87
|
+
throw new Error("Forbidden");
|
|
85
88
|
}
|
|
86
|
-
return __.decrypt(encryptedData, returnUint8Array)
|
|
89
|
+
return __.decrypt(encryptedData, returnUint8Array);
|
|
87
90
|
}
|
|
88
91
|
|
|
89
92
|
encryptShared(data, publicKey) {
|
|
90
|
-
return Crypto.boxEncrypt(__.getSharedKey(publicKey), data)
|
|
93
|
+
return Crypto.boxEncrypt(__.getSharedKey(publicKey), data);
|
|
91
94
|
}
|
|
92
95
|
|
|
93
96
|
decryptShared(encryptedData, publicKey) {
|
|
94
|
-
return Crypto.boxDecrypt(__.getSharedKey(publicKey), encryptedData)
|
|
97
|
+
return Crypto.boxDecrypt(__.getSharedKey(publicKey), encryptedData);
|
|
95
98
|
}
|
|
96
99
|
|
|
97
100
|
async isInitiated() {
|
|
98
|
-
return !!__.derivedPassword
|
|
101
|
+
return !!__.derivedPassword;
|
|
99
102
|
}
|
|
100
103
|
|
|
101
104
|
signMessage(message) {
|
|
102
|
-
return Crypto.getSignature(message, __.signPrivateKey)
|
|
105
|
+
return Crypto.getSignature(message, __.signPrivateKey);
|
|
103
106
|
}
|
|
104
107
|
|
|
105
108
|
async verifyPassword(password) {
|
|
106
|
-
return
|
|
109
|
+
return (
|
|
110
|
+
__.derivedPassword ===
|
|
111
|
+
(await _Secrez.derivePassword(password, __.iterations))
|
|
112
|
+
);
|
|
107
113
|
}
|
|
108
114
|
|
|
109
115
|
async changePassword(password = __.password, iterations = __.iterations) {
|
|
110
|
-
let data = this.conf.data
|
|
111
|
-
__.password = password
|
|
112
|
-
__.iterations = iterations
|
|
113
|
-
__.derivedPassword = await _Secrez.derivePassword(password, iterations)
|
|
114
|
-
delete data.keys
|
|
115
|
-
data.key = this.preEncrypt(__.masterKey)
|
|
116
|
-
return data
|
|
116
|
+
let data = this.conf.data;
|
|
117
|
+
__.password = password;
|
|
118
|
+
__.iterations = iterations;
|
|
119
|
+
__.derivedPassword = await _Secrez.derivePassword(password, iterations);
|
|
120
|
+
delete data.keys;
|
|
121
|
+
data.key = this.preEncrypt(__.masterKey);
|
|
122
|
+
return data;
|
|
117
123
|
}
|
|
118
124
|
|
|
119
125
|
async restoreKey() {
|
|
120
|
-
delete this.conf.data.keys
|
|
121
|
-
this.conf.data.key = this.preEncrypt(__.masterKey)
|
|
126
|
+
delete this.conf.data.keys;
|
|
127
|
+
this.conf.data.key = this.preEncrypt(__.masterKey);
|
|
122
128
|
}
|
|
123
129
|
|
|
124
130
|
setConf(conf, doNotVerify) {
|
|
125
131
|
/* istanbul ignore if */
|
|
126
132
|
if (!doNotVerify && !this.verifySavedData(conf)) {
|
|
127
|
-
throw new Error(
|
|
133
|
+
throw new Error("The configuration file is corrupted");
|
|
128
134
|
} else {
|
|
129
|
-
this.conf = conf
|
|
130
|
-
return true
|
|
135
|
+
this.conf = conf;
|
|
136
|
+
return true;
|
|
131
137
|
}
|
|
132
138
|
}
|
|
133
139
|
|
|
134
140
|
verifySavedData(conf) {
|
|
135
|
-
let publicKey = Crypto.bs64.decode(conf.data.sign.publicKey)
|
|
136
|
-
return Crypto.verifySignature(
|
|
141
|
+
let publicKey = Crypto.bs64.decode(conf.data.sign.publicKey);
|
|
142
|
+
return Crypto.verifySignature(
|
|
143
|
+
JSON.stringify(this.sortObj(conf.data)),
|
|
144
|
+
conf.signature,
|
|
145
|
+
publicKey
|
|
146
|
+
);
|
|
137
147
|
}
|
|
138
148
|
|
|
139
149
|
async signup() {
|
|
140
|
-
__.masterKey = Crypto.generateKey()
|
|
141
|
-
__.masterKeyArray = Crypto.bs64.decode(__.masterKey)
|
|
142
|
-
let key = this.preEncrypt(__.masterKey)
|
|
143
|
-
__.encryptedMasterKey = key
|
|
144
|
-
let hash = Crypto.b64Hash(__.masterKey)
|
|
145
|
-
__.masterKeyHash = hash
|
|
146
|
-
const boxPair = Crypto.generateBoxKeyPair()
|
|
150
|
+
__.masterKey = Crypto.generateKey();
|
|
151
|
+
__.masterKeyArray = Crypto.bs64.decode(__.masterKey);
|
|
152
|
+
let key = this.preEncrypt(__.masterKey);
|
|
153
|
+
__.encryptedMasterKey = key;
|
|
154
|
+
let hash = Crypto.b64Hash(__.masterKey);
|
|
155
|
+
__.masterKeyHash = hash;
|
|
156
|
+
const boxPair = Crypto.generateBoxKeyPair();
|
|
147
157
|
const box = {
|
|
148
158
|
secretKey: this.encrypt(boxPair.secretKey),
|
|
149
|
-
publicKey: Crypto.bs64.encode(boxPair.publicKey)
|
|
150
|
-
}
|
|
151
|
-
__.boxPublicKey = boxPair.secretKey
|
|
152
|
-
__.boxPrivateKey = boxPair.secretKey
|
|
153
|
-
__.encryptedBoxPrivateKey = box.secretKey
|
|
159
|
+
publicKey: Crypto.bs64.encode(boxPair.publicKey),
|
|
160
|
+
};
|
|
161
|
+
__.boxPublicKey = boxPair.secretKey;
|
|
162
|
+
__.boxPrivateKey = boxPair.secretKey;
|
|
163
|
+
__.encryptedBoxPrivateKey = box.secretKey;
|
|
154
164
|
|
|
155
|
-
const signPair = Crypto.generateSignatureKeyPair()
|
|
165
|
+
const signPair = Crypto.generateSignatureKeyPair();
|
|
156
166
|
const sign = {
|
|
157
167
|
secretKey: this.encrypt(signPair.secretKey),
|
|
158
|
-
publicKey: Crypto.bs64.encode(signPair.publicKey)
|
|
159
|
-
}
|
|
160
|
-
__.signPublicKey = signPair.secretKey
|
|
161
|
-
__.signPrivateKey = signPair.secretKey
|
|
162
|
-
__.encryptedSignPrivateKey = sign.secretKey
|
|
168
|
+
publicKey: Crypto.bs64.encode(signPair.publicKey),
|
|
169
|
+
};
|
|
170
|
+
__.signPublicKey = signPair.secretKey;
|
|
171
|
+
__.signPrivateKey = signPair.secretKey;
|
|
172
|
+
__.encryptedSignPrivateKey = sign.secretKey;
|
|
163
173
|
return {
|
|
164
174
|
sign,
|
|
165
175
|
box,
|
|
166
176
|
key,
|
|
167
|
-
hash
|
|
168
|
-
}
|
|
177
|
+
hash,
|
|
178
|
+
};
|
|
169
179
|
}
|
|
170
180
|
|
|
171
181
|
async signin(data) {
|
|
172
182
|
try {
|
|
173
|
-
__.masterKey = await __.preDecrypt(data.key)
|
|
174
|
-
__.encryptedMasterKey = data.key
|
|
175
|
-
__.masterKeyArray = Crypto.bs64.decode(__.masterKey)
|
|
176
|
-
__.masterKeyHash = data.hash
|
|
177
|
-
__.boxPublicKey = Crypto.bs64.decode(data.box.secretKey)
|
|
178
|
-
__.boxPrivateKey = __.decrypt(data.box.secretKey, RETURN_UINT8_ARRAY)
|
|
179
|
-
__.signPublicKey = Crypto.bs64.decode(data.sign.secretKey)
|
|
180
|
-
__.signPrivateKey = __.decrypt(data.sign.secretKey, RETURN_UINT8_ARRAY)
|
|
181
|
-
__.encryptedBoxPrivateKey = data.box.secretKey
|
|
182
|
-
__.encryptedSignPrivateKey = data.sign.secretKey
|
|
183
|
-
|
|
183
|
+
__.masterKey = await __.preDecrypt(data.key);
|
|
184
|
+
__.encryptedMasterKey = data.key;
|
|
185
|
+
__.masterKeyArray = Crypto.bs64.decode(__.masterKey);
|
|
186
|
+
__.masterKeyHash = data.hash;
|
|
187
|
+
__.boxPublicKey = Crypto.bs64.decode(data.box.secretKey);
|
|
188
|
+
__.boxPrivateKey = __.decrypt(data.box.secretKey, RETURN_UINT8_ARRAY);
|
|
189
|
+
__.signPublicKey = Crypto.bs64.decode(data.sign.secretKey);
|
|
190
|
+
__.signPrivateKey = __.decrypt(data.sign.secretKey, RETURN_UINT8_ARRAY);
|
|
191
|
+
__.encryptedBoxPrivateKey = data.box.secretKey;
|
|
192
|
+
__.encryptedSignPrivateKey = data.sign.secretKey;
|
|
184
193
|
} catch (e) {
|
|
185
|
-
throw new Error(
|
|
194
|
+
throw new Error("Wrong password or wrong number of iterations");
|
|
186
195
|
}
|
|
187
196
|
if (utils.secureCompare(Crypto.b64Hash(__.masterKey), data.hash)) {
|
|
188
|
-
return data.hash
|
|
197
|
+
return data.hash;
|
|
189
198
|
} else {
|
|
190
|
-
throw new Error(
|
|
199
|
+
throw new Error("Hash on file does not match the master key");
|
|
191
200
|
}
|
|
192
201
|
}
|
|
193
202
|
|
|
194
203
|
async sharedSignin(data, authenticator, secret) {
|
|
195
|
-
let key = data.keys[authenticator]
|
|
204
|
+
let key = data.keys[authenticator];
|
|
196
205
|
try {
|
|
197
|
-
let masterKey = this.recoverSharedSecrets(key.parts, secret)
|
|
206
|
+
let masterKey = this.recoverSharedSecrets(key.parts, secret);
|
|
198
207
|
/* istanbul ignore if */
|
|
199
208
|
if (!utils.secureCompare(Crypto.b64Hash(masterKey), data.hash)) {
|
|
200
|
-
throw new Error(
|
|
209
|
+
throw new Error("Hash on file does not match the master key");
|
|
201
210
|
}
|
|
202
|
-
__.masterKey = masterKey
|
|
203
|
-
__.masterKeyArray = Crypto.bs64.decode(__.masterKey)
|
|
204
|
-
__.masterKeyHash = data.hash
|
|
205
|
-
__.boxPublicKey = Crypto.bs64.decode(data.box.secretKey)
|
|
206
|
-
__.boxPrivateKey = __.decrypt(data.box.secretKey, RETURN_UINT8_ARRAY)
|
|
207
|
-
__.signPublicKey = Crypto.bs64.decode(data.sign.secretKey)
|
|
208
|
-
__.signPrivateKey = __.decrypt(data.sign.secretKey, RETURN_UINT8_ARRAY)
|
|
209
|
-
__.encryptedBoxPrivateKey = data.box.secretKey
|
|
210
|
-
__.encryptedSignPrivateKey = data.sign.secretKey
|
|
211
|
-
return data.hash
|
|
211
|
+
__.masterKey = masterKey;
|
|
212
|
+
__.masterKeyArray = Crypto.bs64.decode(__.masterKey);
|
|
213
|
+
__.masterKeyHash = data.hash;
|
|
214
|
+
__.boxPublicKey = Crypto.bs64.decode(data.box.secretKey);
|
|
215
|
+
__.boxPrivateKey = __.decrypt(data.box.secretKey, RETURN_UINT8_ARRAY);
|
|
216
|
+
__.signPublicKey = Crypto.bs64.decode(data.sign.secretKey);
|
|
217
|
+
__.signPrivateKey = __.decrypt(data.sign.secretKey, RETURN_UINT8_ARRAY);
|
|
218
|
+
__.encryptedBoxPrivateKey = data.box.secretKey;
|
|
219
|
+
__.encryptedSignPrivateKey = data.sign.secretKey;
|
|
220
|
+
return data.hash;
|
|
212
221
|
} catch (e) {
|
|
213
|
-
throw new Error(
|
|
222
|
+
throw new Error("Wrong data/secret");
|
|
214
223
|
}
|
|
215
224
|
}
|
|
216
225
|
|
|
217
|
-
static async derivePassword(
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
password = Crypto.SHA3(password)
|
|
222
|
-
let salt = Crypto.SHA3(password + iterations.toString())
|
|
223
|
-
return bs64.encode(Crypto.deriveKey(password, salt, iterations, 32))
|
|
226
|
+
static async derivePassword(password = __.password, iterations) {
|
|
227
|
+
password = Crypto.SHA3(password);
|
|
228
|
+
let salt = Crypto.SHA3(password + iterations.toString());
|
|
229
|
+
return bs64.encode(Crypto.deriveKey(password, salt, iterations, 32));
|
|
224
230
|
}
|
|
225
231
|
|
|
226
232
|
generateSharedSecrets(secret) {
|
|
227
|
-
let parts = Crypto.splitSecret(__.masterKey, 2, 2)
|
|
228
|
-
parts[1] = this.preEncrypt(parts[
|
|
229
|
-
parts[2] = Crypto.encrypt(parts[
|
|
230
|
-
return parts
|
|
233
|
+
let parts = Crypto.splitSecret(__.masterKey, 2, 2);
|
|
234
|
+
parts[1] = this.preEncrypt(parts["1"]);
|
|
235
|
+
parts[2] = Crypto.encrypt(parts["2"], Crypto.SHA3(secret));
|
|
236
|
+
return parts;
|
|
231
237
|
}
|
|
232
238
|
|
|
233
239
|
recoverSharedSecrets(parts, secret) {
|
|
234
240
|
parts = {
|
|
235
241
|
1: __.preDecrypt(parts[1], RETURN_UINT8_ARRAY),
|
|
236
|
-
2: Crypto.decrypt(parts[2], Crypto.SHA3(secret), RETURN_UINT8_ARRAY)
|
|
237
|
-
}
|
|
238
|
-
return Crypto.joinSecret(parts)
|
|
242
|
+
2: Crypto.decrypt(parts[2], Crypto.SHA3(secret), RETURN_UINT8_ARRAY),
|
|
243
|
+
};
|
|
244
|
+
return Crypto.joinSecret(parts);
|
|
239
245
|
}
|
|
240
246
|
|
|
241
247
|
signData(data) {
|
|
242
|
-
const signature = this.signMessage(JSON.stringify(this.sortObj(data)))
|
|
248
|
+
const signature = this.signMessage(JSON.stringify(this.sortObj(data)));
|
|
243
249
|
const conf = {
|
|
244
250
|
data,
|
|
245
|
-
signature
|
|
246
|
-
}
|
|
247
|
-
return conf
|
|
251
|
+
signature,
|
|
252
|
+
};
|
|
253
|
+
return conf;
|
|
248
254
|
}
|
|
249
255
|
|
|
250
256
|
sortObj(obj) {
|
|
251
|
-
const sortedData = {}
|
|
257
|
+
const sortedData = {};
|
|
252
258
|
for (let prop of Object.keys(obj).sort()) {
|
|
253
|
-
if (typeof obj[prop] ===
|
|
254
|
-
obj[prop] = this.sortObj(obj[prop])
|
|
259
|
+
if (typeof obj[prop] === "object") {
|
|
260
|
+
obj[prop] = this.sortObj(obj[prop]);
|
|
255
261
|
}
|
|
256
|
-
sortedData[prop] = obj[prop]
|
|
262
|
+
sortedData[prop] = obj[prop];
|
|
257
263
|
}
|
|
258
|
-
return sortedData
|
|
264
|
+
return sortedData;
|
|
259
265
|
}
|
|
260
|
-
|
|
261
266
|
}
|
|
262
267
|
|
|
263
|
-
return _Secrez
|
|
264
|
-
}
|
|
268
|
+
return _Secrez;
|
|
269
|
+
};
|
|
@@ -1,141 +1,164 @@
|
|
|
1
|
-
const path = require(
|
|
2
|
-
const _ = require(
|
|
3
|
-
const utils = require(
|
|
4
|
-
const fs = require(
|
|
5
|
-
const pkg = require(
|
|
6
|
-
const config = require(
|
|
1
|
+
const path = require("path");
|
|
2
|
+
const _ = require("lodash");
|
|
3
|
+
const utils = require("@secrez/utils");
|
|
4
|
+
const fs = require("fs-extra");
|
|
5
|
+
const pkg = require("../../package");
|
|
6
|
+
const config = require(".");
|
|
7
7
|
|
|
8
8
|
class ConfigUtils {
|
|
9
|
-
|
|
10
9
|
static isValidType(type) {
|
|
11
|
-
type = parseInt(type)
|
|
12
|
-
return Object.values(config.types).indexOf(type) !== -1
|
|
10
|
+
type = parseInt(type);
|
|
11
|
+
return Object.values(config.types).indexOf(type) !== -1;
|
|
13
12
|
}
|
|
14
13
|
|
|
15
|
-
static async setSecrez(
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
config =
|
|
21
|
-
config.
|
|
22
|
-
config.
|
|
23
|
-
config.
|
|
24
|
-
config.
|
|
25
|
-
config.
|
|
26
|
-
config.
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
await fs.
|
|
33
|
-
await fs.ensureDir(config.keysDataPath)
|
|
34
|
-
await fs.ensureDir(config.localDataPath)
|
|
35
|
-
|
|
36
|
-
await fs.emptyDir(config.tmpPath)
|
|
14
|
+
static async setSecrez(config, container, localWorkingDir) {
|
|
15
|
+
config = _.clone(config);
|
|
16
|
+
config.container = container;
|
|
17
|
+
config.root = path.basename(container);
|
|
18
|
+
config.dataPath = path.join(container, "data");
|
|
19
|
+
config.workingDir = "/";
|
|
20
|
+
config.localWorkingDir = localWorkingDir;
|
|
21
|
+
config.keysDataPath = path.join(container, "keys");
|
|
22
|
+
config.keysPath = path.join(config.keysDataPath, "default.json");
|
|
23
|
+
config.oldKeysPath = path.join(container, "keys.json");
|
|
24
|
+
config.localDataPath = path.join(container, "local");
|
|
25
|
+
config.tmpPath = path.join(container, "tmp");
|
|
26
|
+
|
|
27
|
+
await fs.ensureDir(config.dataPath);
|
|
28
|
+
await fs.ensureDir(config.keysDataPath);
|
|
29
|
+
await fs.ensureDir(config.localDataPath);
|
|
30
|
+
|
|
31
|
+
await fs.emptyDir(config.tmpPath);
|
|
37
32
|
|
|
38
33
|
if (!(await fs.pathExists(config.oldKeysPath))) {
|
|
39
|
-
await fs.writeFile(
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
34
|
+
await fs.writeFile(
|
|
35
|
+
path.join(container, "keys.json"),
|
|
36
|
+
JSON.stringify(
|
|
37
|
+
{
|
|
38
|
+
data: {
|
|
39
|
+
why: "This file is here to cause an error in old versions of Secrez during login, and avoid that Secrez thinks the db is empty and reset it.",
|
|
40
|
+
},
|
|
41
|
+
},
|
|
42
|
+
null,
|
|
43
|
+
2
|
|
44
|
+
)
|
|
45
|
+
);
|
|
44
46
|
}
|
|
45
47
|
|
|
46
|
-
let oldEnvPath = path.join(container,
|
|
47
|
-
let oldHistoryPath = path.join(container,
|
|
48
|
+
let oldEnvPath = path.join(container, "env.json");
|
|
49
|
+
let oldHistoryPath = path.join(container, "history");
|
|
48
50
|
|
|
49
|
-
config.envPath = path.join(config.localDataPath,
|
|
50
|
-
config.historyPath = path.join(config.localDataPath,
|
|
51
|
+
config.envPath = path.join(config.localDataPath, "env.json");
|
|
52
|
+
config.historyPath = path.join(config.localDataPath, "mainHistory");
|
|
51
53
|
|
|
52
54
|
/* istanbul ignore if */
|
|
53
55
|
if (await fs.pathExists(oldEnvPath)) {
|
|
54
|
-
await fs.move(oldEnvPath, config.envPath)
|
|
56
|
+
await fs.move(oldEnvPath, config.envPath);
|
|
55
57
|
}
|
|
56
58
|
/* istanbul ignore if */
|
|
57
59
|
if (await fs.pathExists(oldHistoryPath)) {
|
|
58
|
-
await fs.move(oldHistoryPath, config.historyPath)
|
|
60
|
+
await fs.move(oldHistoryPath, config.historyPath);
|
|
59
61
|
}
|
|
60
|
-
let readmePath = path.join(container,
|
|
62
|
+
let readmePath = path.join(container, "README");
|
|
61
63
|
if (!(await fs.pathExists(readmePath))) {
|
|
62
|
-
await fs.writeFile(
|
|
64
|
+
await fs.writeFile(
|
|
65
|
+
readmePath,
|
|
66
|
+
`
|
|
63
67
|
This folder has been generated by ${utils.capitalize(pkg.name)} v${pkg.version}.
|
|
64
68
|
It contains your secret database.
|
|
65
69
|
Be very careful, and don't touch anything :o)
|
|
66
|
-
`,
|
|
67
|
-
|
|
70
|
+
`,
|
|
71
|
+
"utf-8"
|
|
72
|
+
);
|
|
73
|
+
await fs.writeFile(config.envPath, "{}");
|
|
68
74
|
}
|
|
69
|
-
let gitignorePath = path.join(container,
|
|
75
|
+
let gitignorePath = path.join(container, ".gitignore");
|
|
70
76
|
if (!(await fs.pathExists(gitignorePath))) {
|
|
71
|
-
await fs.writeFile(
|
|
77
|
+
await fs.writeFile(
|
|
78
|
+
gitignorePath,
|
|
79
|
+
`tmp
|
|
72
80
|
env.json
|
|
73
81
|
history
|
|
74
82
|
local
|
|
75
|
-
`,
|
|
83
|
+
`,
|
|
84
|
+
"utf-8"
|
|
85
|
+
);
|
|
76
86
|
} else {
|
|
77
|
-
let gitignore = await fs.readFile(gitignorePath,
|
|
87
|
+
let gitignore = await fs.readFile(gitignorePath, "utf8");
|
|
78
88
|
/* istanbul ignore if */
|
|
79
89
|
if (!/local/.test(gitignore)) {
|
|
80
|
-
gitignore =
|
|
81
|
-
fs.writeFile(gitignorePath, gitignore)
|
|
90
|
+
gitignore = _.trim(gitignore) + "\nlocal\n";
|
|
91
|
+
fs.writeFile(gitignorePath, gitignore);
|
|
82
92
|
}
|
|
83
93
|
}
|
|
84
|
-
return config
|
|
94
|
+
return config;
|
|
85
95
|
}
|
|
86
96
|
|
|
87
97
|
static getDatasetPath(config, index) {
|
|
88
|
-
return config.dataPath + (index > 0 ? `.${index}` :
|
|
98
|
+
return config.dataPath + (index > 0 ? `.${index}` : "");
|
|
89
99
|
}
|
|
90
100
|
|
|
91
101
|
static setAndGetDataset(config, index) {
|
|
92
|
-
let dataPath = config.dataPath
|
|
93
|
-
if (
|
|
94
|
-
|
|
102
|
+
let dataPath = config.dataPath;
|
|
103
|
+
if (
|
|
104
|
+
typeof index === "number" &&
|
|
105
|
+
parseInt(index.toString()) === index &&
|
|
106
|
+
index > 0
|
|
107
|
+
) {
|
|
108
|
+
dataPath += "." + index;
|
|
95
109
|
if (!fs.existsSync(dataPath)) {
|
|
96
|
-
let lastData = ConfigUtils.getLastDataset(config)
|
|
97
|
-
if (
|
|
98
|
-
|
|
110
|
+
let lastData = ConfigUtils.getLastDataset(config);
|
|
111
|
+
if (
|
|
112
|
+
lastData ===
|
|
113
|
+
path.basename(config.dataPath) + (index > 1 ? "." + (index - 1) : "")
|
|
114
|
+
) {
|
|
115
|
+
fs.mkdirSync(dataPath);
|
|
99
116
|
} else {
|
|
100
|
-
throw new Error(
|
|
117
|
+
throw new Error("Wrong data index");
|
|
101
118
|
}
|
|
102
119
|
}
|
|
103
120
|
}
|
|
104
|
-
return dataPath
|
|
121
|
+
return dataPath;
|
|
105
122
|
}
|
|
106
123
|
|
|
107
124
|
static deleteDataset(config, index) {
|
|
108
|
-
let dataPath = config.dataPath
|
|
109
|
-
if (
|
|
110
|
-
|
|
125
|
+
let dataPath = config.dataPath;
|
|
126
|
+
if (
|
|
127
|
+
typeof index === "number" &&
|
|
128
|
+
parseInt(index.toString()) === index &&
|
|
129
|
+
index > 0
|
|
130
|
+
) {
|
|
131
|
+
dataPath += "." + index;
|
|
111
132
|
if (fs.existsSync(dataPath)) {
|
|
112
|
-
fs.removeSync(dataPath)
|
|
113
|
-
return true
|
|
133
|
+
fs.removeSync(dataPath);
|
|
134
|
+
return true;
|
|
114
135
|
}
|
|
115
136
|
}
|
|
116
|
-
return false
|
|
137
|
+
return false;
|
|
117
138
|
}
|
|
118
139
|
|
|
119
140
|
static getLastDataset(config) {
|
|
120
|
-
return ConfigUtils.listDatasets(config).pop()
|
|
141
|
+
return ConfigUtils.listDatasets(config).pop();
|
|
121
142
|
}
|
|
122
143
|
|
|
123
144
|
static listDatasets(config) {
|
|
124
|
-
return fs
|
|
145
|
+
return fs
|
|
146
|
+
.readdirSync(config.container)
|
|
147
|
+
.sort()
|
|
148
|
+
.filter((e) => /^data/.test(e));
|
|
125
149
|
}
|
|
126
150
|
|
|
127
151
|
static async getEnv(config) {
|
|
128
152
|
if (await fs.pathExists(config.envPath)) {
|
|
129
|
-
return JSON.parse(await fs.readFile(config.envPath,
|
|
153
|
+
return JSON.parse((await fs.readFile(config.envPath, "utf8")) || "{}");
|
|
130
154
|
} else {
|
|
131
|
-
return {}
|
|
155
|
+
return {};
|
|
132
156
|
}
|
|
133
157
|
}
|
|
134
158
|
|
|
135
159
|
static async putEnv(config, env) {
|
|
136
|
-
return await fs.writeFile(config.envPath, JSON.stringify(env))
|
|
160
|
+
return await fs.writeFile(config.envPath, JSON.stringify(env));
|
|
137
161
|
}
|
|
138
|
-
|
|
139
162
|
}
|
|
140
163
|
|
|
141
|
-
module.exports = ConfigUtils
|
|
164
|
+
module.exports = ConfigUtils;
|