@secrez/core 1.0.2 → 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 +24 -28
- package/package.json +40 -43
- package/src/Entry.js +44 -42
- package/src/Secrez.js +235 -199
- 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,342 +1,378 @@
|
|
|
1
|
-
const homedir = require(
|
|
2
|
-
const fs = require(
|
|
3
|
-
const _ = require(
|
|
4
|
-
const Crypto = require(
|
|
5
|
-
const ConfigUtils = require(
|
|
6
|
-
const Entry = require(
|
|
7
|
-
|
|
8
|
-
const {
|
|
9
|
-
DO_NOT_VERIFY,
|
|
10
|
-
URL_SAFE
|
|
11
|
-
} = require('./config/booleans')
|
|
1
|
+
const homedir = require("homedir");
|
|
2
|
+
const fs = require("fs-extra");
|
|
3
|
+
const _ = require("lodash");
|
|
4
|
+
const Crypto = require("@secrez/crypto");
|
|
5
|
+
const ConfigUtils = require("./config/ConfigUtils");
|
|
6
|
+
const Entry = require("./Entry");
|
|
12
7
|
|
|
13
|
-
|
|
8
|
+
const { DO_NOT_VERIFY, URL_SAFE } = require("./config/booleans");
|
|
14
9
|
|
|
15
|
-
|
|
16
|
-
|
|
10
|
+
module.exports = function () {
|
|
11
|
+
let _secrez;
|
|
12
|
+
const _Secrez = require("./_Secrez")();
|
|
17
13
|
|
|
18
14
|
class Secrez {
|
|
19
|
-
|
|
20
15
|
constructor() {
|
|
21
|
-
this.config = _.clone(require(
|
|
22
|
-
this.types = this.config.types
|
|
16
|
+
this.config = _.clone(require("./config"));
|
|
17
|
+
this.types = this.config.types;
|
|
23
18
|
}
|
|
24
19
|
|
|
25
20
|
async init(
|
|
26
|
-
|
|
27
|
-
|
|
21
|
+
container = `${homedir()}/.secrez`,
|
|
22
|
+
localWorkingDir = homedir()
|
|
28
23
|
) {
|
|
29
|
-
if (
|
|
30
|
-
|
|
24
|
+
if (
|
|
25
|
+
process.env.NODE_ENV === "test" &&
|
|
26
|
+
container === `${homedir()}/.secrez`
|
|
27
|
+
) {
|
|
28
|
+
throw new Error(
|
|
29
|
+
"You are not supposed to test Secrez in the default folder. This can lead to mistakes and loss of data."
|
|
30
|
+
);
|
|
31
31
|
}
|
|
32
|
-
this.config = await ConfigUtils.setSecrez(
|
|
32
|
+
this.config = await ConfigUtils.setSecrez(
|
|
33
|
+
this.config,
|
|
34
|
+
container,
|
|
35
|
+
localWorkingDir
|
|
36
|
+
);
|
|
33
37
|
}
|
|
34
38
|
|
|
35
39
|
async signup(password, iterations) {
|
|
36
40
|
if (!this.config || !this.config.keysPath) {
|
|
37
|
-
throw new Error(
|
|
41
|
+
throw new Error("Secrez not initiated");
|
|
38
42
|
}
|
|
39
43
|
if (!(await fs.pathExists(this.config.keysPath))) {
|
|
44
|
+
let id = Crypto.b64Hash(Crypto.generateKey());
|
|
45
|
+
_secrez = new _Secrez(this);
|
|
40
46
|
|
|
41
|
-
|
|
42
|
-
_secrez = new _Secrez(this)
|
|
47
|
+
await _secrez.init(password, iterations);
|
|
43
48
|
|
|
44
|
-
await _secrez.
|
|
45
|
-
|
|
46
|
-
let {sign, box, key, hash} = await _secrez.signup()
|
|
47
|
-
this.masterKeyHash = hash
|
|
49
|
+
let { sign, box, key, hash } = await _secrez.signup();
|
|
50
|
+
this.masterKeyHash = hash;
|
|
48
51
|
|
|
49
52
|
const data = {
|
|
50
|
-
id,
|
|
53
|
+
id,
|
|
54
|
+
sign,
|
|
55
|
+
box,
|
|
56
|
+
key,
|
|
57
|
+
hash,
|
|
51
58
|
when: _secrez.encrypt(Date.now().toString()),
|
|
52
|
-
version: this.config.VERSION
|
|
53
|
-
}
|
|
54
|
-
_secrez.setConf(await this.signAndSave(data), DO_NOT_VERIFY)
|
|
59
|
+
version: this.config.VERSION,
|
|
60
|
+
};
|
|
61
|
+
_secrez.setConf(await this.signAndSave(data), DO_NOT_VERIFY);
|
|
55
62
|
} else {
|
|
56
|
-
throw new Error(
|
|
63
|
+
throw new Error(
|
|
64
|
+
"An account already exists. Please, sign in or chose a different container directory"
|
|
65
|
+
);
|
|
57
66
|
}
|
|
58
67
|
}
|
|
59
68
|
|
|
60
69
|
async signin(password, iterations) {
|
|
61
70
|
if (!this.config || !this.config.keysPath) {
|
|
62
|
-
throw new Error(
|
|
71
|
+
throw new Error("Secrez not initiated");
|
|
63
72
|
}
|
|
64
73
|
if (!iterations) {
|
|
65
|
-
const env = await ConfigUtils.getEnv(this.config)
|
|
66
|
-
iterations = env.iterations
|
|
74
|
+
const env = await ConfigUtils.getEnv(this.config);
|
|
75
|
+
iterations = env.iterations;
|
|
67
76
|
}
|
|
68
77
|
if (!iterations || iterations !== parseInt(iterations.toString())) {
|
|
69
|
-
throw new Error(
|
|
78
|
+
throw new Error("Iterations is missed");
|
|
70
79
|
}
|
|
71
|
-
iterations = parseInt(iterations)
|
|
72
|
-
const conf = await this.readConf()
|
|
73
|
-
const data = conf.data
|
|
74
|
-
_secrez = new _Secrez(this)
|
|
75
|
-
await _secrez.init(password, iterations)
|
|
80
|
+
iterations = parseInt(iterations);
|
|
81
|
+
const conf = await this.readConf();
|
|
82
|
+
const data = conf.data;
|
|
83
|
+
_secrez = new _Secrez(this);
|
|
84
|
+
await _secrez.init(password, iterations);
|
|
76
85
|
/* istanbul ignore if */
|
|
77
86
|
if (!data.key && !data.keys) {
|
|
78
|
-
throw new Error(
|
|
87
|
+
throw new Error("No valid data found");
|
|
79
88
|
}
|
|
80
89
|
if (data.key) {
|
|
81
|
-
let masterKeyHash = await _secrez.signin(data)
|
|
82
|
-
this.setMasterKeyHash(conf, masterKeyHash)
|
|
90
|
+
let masterKeyHash = await _secrez.signin(data);
|
|
91
|
+
this.setMasterKeyHash(conf, masterKeyHash);
|
|
83
92
|
} else {
|
|
84
|
-
throw new Error(
|
|
93
|
+
throw new Error("A second factor is required");
|
|
85
94
|
}
|
|
86
|
-
return 0
|
|
95
|
+
return 0;
|
|
87
96
|
}
|
|
88
97
|
|
|
89
98
|
async sharedSignin(authenticator, secret) {
|
|
90
|
-
if (
|
|
91
|
-
|
|
99
|
+
if (
|
|
100
|
+
!this.config ||
|
|
101
|
+
!this.config.keysPath ||
|
|
102
|
+
!_secrez ||
|
|
103
|
+
!_secrez.isInitiated()
|
|
104
|
+
) {
|
|
105
|
+
throw new Error(
|
|
106
|
+
"A standard sign in must be run before to initiate Secrez"
|
|
107
|
+
);
|
|
92
108
|
}
|
|
93
|
-
const conf = await this.readConf()
|
|
94
|
-
const data = conf.data
|
|
109
|
+
const conf = await this.readConf();
|
|
110
|
+
const data = conf.data;
|
|
95
111
|
/* istanbul ignore if */
|
|
96
112
|
if (!data.keys) {
|
|
97
|
-
throw new Error(
|
|
113
|
+
throw new Error("No second factor registered");
|
|
98
114
|
}
|
|
99
115
|
if (!data.keys[authenticator]) {
|
|
100
|
-
throw new Error(
|
|
116
|
+
throw new Error(
|
|
117
|
+
`No second factor registered with the authenticator ${authenticator}`
|
|
118
|
+
);
|
|
101
119
|
}
|
|
102
|
-
let masterKeyHash = await _secrez.sharedSignin(
|
|
103
|
-
|
|
104
|
-
|
|
120
|
+
let masterKeyHash = await _secrez.sharedSignin(
|
|
121
|
+
data,
|
|
122
|
+
authenticator,
|
|
123
|
+
secret
|
|
124
|
+
);
|
|
125
|
+
this.setMasterKeyHash(conf, masterKeyHash);
|
|
126
|
+
return 0;
|
|
105
127
|
}
|
|
106
128
|
|
|
107
129
|
async derivePassword(password, iterations) {
|
|
108
|
-
return await _Secrez.derivePassword(password, iterations)
|
|
130
|
+
return await _Secrez.derivePassword(password, iterations);
|
|
109
131
|
}
|
|
110
132
|
|
|
111
133
|
async signAndSave(data) {
|
|
112
|
-
const conf = _secrez.signData(data)
|
|
113
|
-
await fs.writeFile(this.config.keysPath, JSON.stringify(conf))
|
|
114
|
-
return conf
|
|
134
|
+
const conf = _secrez.signData(data);
|
|
135
|
+
await fs.writeFile(this.config.keysPath, JSON.stringify(conf));
|
|
136
|
+
return conf;
|
|
115
137
|
}
|
|
116
138
|
|
|
117
139
|
async saveIterations(iterations) {
|
|
118
|
-
const env = await ConfigUtils.getEnv(this.config)
|
|
119
|
-
env.iterations = iterations
|
|
120
|
-
await ConfigUtils.putEnv(this.config, env)
|
|
140
|
+
const env = await ConfigUtils.getEnv(this.config);
|
|
141
|
+
env.iterations = iterations;
|
|
142
|
+
await ConfigUtils.putEnv(this.config, env);
|
|
121
143
|
}
|
|
122
144
|
|
|
123
145
|
generateSharedSecrets(secret) {
|
|
124
|
-
return _secrez.generateSharedSecrets(secret)
|
|
146
|
+
return _secrez.generateSharedSecrets(secret);
|
|
125
147
|
}
|
|
126
148
|
|
|
127
149
|
async removeSharedSecret(authenticator, all) {
|
|
128
|
-
let data = _secrez.conf.data
|
|
129
|
-
let code = 1
|
|
130
|
-
let removeAll = true
|
|
150
|
+
let data = _secrez.conf.data;
|
|
151
|
+
let code = 1;
|
|
152
|
+
let removeAll = true;
|
|
131
153
|
if (!all) {
|
|
132
154
|
if (data.keys && data.keys[authenticator]) {
|
|
133
|
-
delete data.keys[authenticator]
|
|
155
|
+
delete data.keys[authenticator];
|
|
134
156
|
}
|
|
135
157
|
for (let authenticator in data.keys) {
|
|
136
|
-
if (
|
|
137
|
-
|
|
138
|
-
|
|
158
|
+
if (
|
|
159
|
+
data.keys[authenticator].type === this.config.sharedKeys.FIDO2_KEY
|
|
160
|
+
) {
|
|
161
|
+
removeAll = false;
|
|
162
|
+
break;
|
|
139
163
|
}
|
|
140
164
|
}
|
|
141
165
|
}
|
|
142
166
|
if (removeAll) {
|
|
143
|
-
code = 2
|
|
144
|
-
_secrez.restoreKey()
|
|
167
|
+
code = 2;
|
|
168
|
+
_secrez.restoreKey();
|
|
145
169
|
}
|
|
146
|
-
let conf = await this.signAndSave(data)
|
|
147
|
-
_secrez.setConf(conf, DO_NOT_VERIFY)
|
|
148
|
-
return code
|
|
170
|
+
let conf = await this.signAndSave(data);
|
|
171
|
+
_secrez.setConf(conf, DO_NOT_VERIFY);
|
|
172
|
+
return code;
|
|
149
173
|
}
|
|
150
174
|
|
|
151
175
|
async saveSharedSecrets(sharedData) {
|
|
152
|
-
let conf = await this.readConf()
|
|
176
|
+
let conf = await this.readConf();
|
|
153
177
|
if (!conf.data.keys) {
|
|
154
|
-
conf.data.keys = {}
|
|
178
|
+
conf.data.keys = {};
|
|
155
179
|
}
|
|
156
|
-
let authenticator = sharedData.authenticator
|
|
157
|
-
delete sharedData.authenticator
|
|
180
|
+
let authenticator = sharedData.authenticator;
|
|
181
|
+
delete sharedData.authenticator;
|
|
158
182
|
if (sharedData.id) {
|
|
159
|
-
sharedData.id = this.preEncryptData(sharedData.id)
|
|
183
|
+
sharedData.id = this.preEncryptData(sharedData.id);
|
|
160
184
|
}
|
|
161
185
|
if (sharedData.salt) {
|
|
162
|
-
sharedData.salt = this.preEncryptData(sharedData.salt)
|
|
186
|
+
sharedData.salt = this.preEncryptData(sharedData.salt);
|
|
163
187
|
}
|
|
164
188
|
if (sharedData.credential) {
|
|
165
|
-
sharedData.credential = this.preEncryptData(sharedData.credential)
|
|
189
|
+
sharedData.credential = this.preEncryptData(sharedData.credential);
|
|
166
190
|
}
|
|
167
|
-
conf.data.keys[authenticator] = sharedData
|
|
191
|
+
conf.data.keys[authenticator] = sharedData;
|
|
168
192
|
if (conf.data.key) {
|
|
169
|
-
delete conf.data.key
|
|
193
|
+
delete conf.data.key;
|
|
170
194
|
}
|
|
171
|
-
conf = await this.signAndSave(conf.data)
|
|
172
|
-
_secrez.setConf(conf, DO_NOT_VERIFY)
|
|
173
|
-
return conf
|
|
195
|
+
conf = await this.signAndSave(conf.data);
|
|
196
|
+
_secrez.setConf(conf, DO_NOT_VERIFY);
|
|
197
|
+
return conf;
|
|
174
198
|
}
|
|
175
199
|
|
|
176
200
|
getConf() {
|
|
177
|
-
return _secrez.conf
|
|
201
|
+
return _secrez.conf;
|
|
178
202
|
}
|
|
179
203
|
|
|
180
204
|
getPublicKey() {
|
|
181
|
-
return
|
|
205
|
+
return (
|
|
206
|
+
_secrez.conf.data.box.publicKey + "$" + _secrez.conf.data.sign.publicKey
|
|
207
|
+
);
|
|
182
208
|
}
|
|
183
209
|
|
|
184
210
|
async readConf() {
|
|
185
211
|
if (!this.config || !this.config.keysPath) {
|
|
186
|
-
throw new Error(
|
|
212
|
+
throw new Error("Secrez not initiated");
|
|
187
213
|
}
|
|
188
214
|
if (await fs.pathExists(this.config.keysPath)) {
|
|
189
|
-
return JSON.parse(await fs.readFile(this.config.keysPath,
|
|
215
|
+
return JSON.parse(await fs.readFile(this.config.keysPath, "utf8"));
|
|
190
216
|
} else {
|
|
191
|
-
throw new Error(
|
|
217
|
+
throw new Error("Account not set yet");
|
|
192
218
|
}
|
|
193
219
|
}
|
|
194
220
|
|
|
195
221
|
async upgradeAccount(password, iterations) {
|
|
196
|
-
let data = await _secrez.changePassword(password, iterations)
|
|
197
|
-
_secrez.setConf(await this.signAndSave(data), DO_NOT_VERIFY)
|
|
222
|
+
let data = await _secrez.changePassword(password, iterations);
|
|
223
|
+
_secrez.setConf(await this.signAndSave(data), DO_NOT_VERIFY);
|
|
198
224
|
}
|
|
199
225
|
|
|
200
226
|
async verifyPassword(password) {
|
|
201
|
-
return await _secrez.verifyPassword(password)
|
|
227
|
+
return await _secrez.verifyPassword(password);
|
|
202
228
|
}
|
|
203
229
|
|
|
204
230
|
signMessage(message) {
|
|
205
|
-
return _secrez.signMessage(message)
|
|
231
|
+
return _secrez.signMessage(message);
|
|
206
232
|
}
|
|
207
233
|
|
|
208
234
|
verifySignedMessage(message, signature, publicKey) {
|
|
209
|
-
return Crypto.verifySignature(
|
|
235
|
+
return Crypto.verifySignature(
|
|
236
|
+
message,
|
|
237
|
+
signature,
|
|
238
|
+
Crypto.bs64.decode(publicKey || this.getConf().data.sign.publicKey)
|
|
239
|
+
);
|
|
210
240
|
}
|
|
211
241
|
|
|
212
242
|
async getSecondFactorData(authenticator) {
|
|
213
|
-
if (
|
|
214
|
-
|
|
243
|
+
if (
|
|
244
|
+
!this.config ||
|
|
245
|
+
!this.config.keysPath ||
|
|
246
|
+
!_secrez ||
|
|
247
|
+
!_secrez.isInitiated()
|
|
248
|
+
) {
|
|
249
|
+
throw new Error(
|
|
250
|
+
"A standard sign in must be run before to initiate Secrez"
|
|
251
|
+
);
|
|
215
252
|
}
|
|
216
|
-
const conf = await this.readConf()
|
|
217
|
-
let data = conf.data.keys[authenticator]
|
|
253
|
+
const conf = await this.readConf();
|
|
254
|
+
let data = conf.data.keys[authenticator];
|
|
218
255
|
if (data) {
|
|
219
|
-
return data
|
|
256
|
+
return data;
|
|
220
257
|
} else {
|
|
221
|
-
throw new Error(
|
|
258
|
+
throw new Error(
|
|
259
|
+
`No registered data with the authenticator ${authenticator}`
|
|
260
|
+
);
|
|
222
261
|
}
|
|
223
262
|
}
|
|
224
263
|
|
|
225
264
|
setMasterKeyHash(conf, masterKeyHash) {
|
|
226
265
|
/* istanbul ignore if */
|
|
227
266
|
if (!_secrez.setConf(conf)) {
|
|
228
|
-
throw new Error(
|
|
267
|
+
throw new Error("keys.json looks corrupted");
|
|
229
268
|
}
|
|
230
|
-
this.masterKeyHash = masterKeyHash
|
|
269
|
+
this.masterKeyHash = masterKeyHash;
|
|
231
270
|
}
|
|
232
271
|
|
|
233
272
|
preserveEntry(prev, next) {
|
|
234
|
-
let options = prev.get()
|
|
273
|
+
let options = prev.get();
|
|
235
274
|
for (let o in options) {
|
|
236
275
|
if (!next[o]) {
|
|
237
|
-
next.set(o, options[o])
|
|
276
|
+
next.set(o, options[o]);
|
|
238
277
|
}
|
|
239
278
|
}
|
|
240
|
-
return next
|
|
279
|
+
return next;
|
|
241
280
|
}
|
|
242
281
|
|
|
243
282
|
encryptData(data, urlSafe) {
|
|
244
|
-
return _secrez.encrypt(data, urlSafe)
|
|
283
|
+
return _secrez.encrypt(data, urlSafe);
|
|
245
284
|
}
|
|
246
285
|
|
|
247
|
-
decryptData(encryptedData, urlSafe) {
|
|
248
|
-
return _secrez.decrypt(encryptedData, urlSafe)
|
|
286
|
+
decryptData(encryptedData, urlSafe, returnUint8Array) {
|
|
287
|
+
return _secrez.decrypt(encryptedData, urlSafe, returnUint8Array);
|
|
249
288
|
}
|
|
250
289
|
|
|
251
290
|
preEncryptData(data) {
|
|
252
|
-
return _secrez.preEncrypt(data)
|
|
291
|
+
return _secrez.preEncrypt(data);
|
|
253
292
|
}
|
|
254
293
|
|
|
255
294
|
preDecryptData(encryptedData) {
|
|
256
|
-
return _secrez.preDecrypt(encryptedData)
|
|
295
|
+
return _secrez.preDecrypt(encryptedData);
|
|
257
296
|
}
|
|
258
297
|
|
|
259
298
|
encryptSharedData(data, publicKey) {
|
|
260
|
-
return _secrez.encryptShared(data, publicKey)
|
|
299
|
+
return _secrez.encryptShared(data, publicKey);
|
|
261
300
|
}
|
|
262
301
|
|
|
263
302
|
decryptSharedData(encryptedData, publicKey) {
|
|
264
|
-
return _secrez.decryptShared(encryptedData, publicKey)
|
|
303
|
+
return _secrez.decryptShared(encryptedData, publicKey);
|
|
265
304
|
}
|
|
266
305
|
|
|
267
306
|
encryptEntry(entry, useTs) {
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
throw new Error('An Entry instance is expected as parameter')
|
|
307
|
+
if (!entry || entry.constructor.name !== "Entry") {
|
|
308
|
+
throw new Error("An Entry instance is expected as parameter");
|
|
271
309
|
}
|
|
272
310
|
|
|
273
|
-
const {
|
|
274
|
-
type,
|
|
275
|
-
name,
|
|
276
|
-
content,
|
|
277
|
-
preserveContent,
|
|
278
|
-
id
|
|
279
|
-
} = entry.get()
|
|
311
|
+
const { type, name, content, preserveContent, id } = entry.get();
|
|
280
312
|
|
|
281
313
|
if (this.masterKeyHash) {
|
|
282
|
-
|
|
283
314
|
if (!ConfigUtils.isValidType(type)) {
|
|
284
|
-
throw new Error(
|
|
315
|
+
throw new Error("Unsupported type");
|
|
285
316
|
}
|
|
286
317
|
|
|
287
|
-
let ts =
|
|
318
|
+
let ts =
|
|
319
|
+
useTs && entry.ts
|
|
288
320
|
? entry.ts
|
|
289
|
-
: Crypto.getTimestampWithMicroseconds().join(
|
|
321
|
+
: Crypto.getTimestampWithMicroseconds().join(".");
|
|
290
322
|
let encryptedEntry = new Entry({
|
|
291
323
|
id,
|
|
292
324
|
type,
|
|
293
|
-
ts
|
|
294
|
-
})
|
|
325
|
+
ts,
|
|
326
|
+
});
|
|
295
327
|
if (name) {
|
|
296
|
-
let encryptedName =
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
328
|
+
let encryptedName =
|
|
329
|
+
type +
|
|
330
|
+
_secrez.encrypt(
|
|
331
|
+
JSON.stringify({
|
|
332
|
+
i: id,
|
|
333
|
+
t: ts,
|
|
334
|
+
n: name,
|
|
335
|
+
}),
|
|
336
|
+
URL_SAFE
|
|
337
|
+
);
|
|
338
|
+
let extraName;
|
|
302
339
|
if (encryptedName.length > 255) {
|
|
303
|
-
extraName = encryptedName.substring(254)
|
|
304
|
-
encryptedName = encryptedName.substring(0, 254) +
|
|
340
|
+
extraName = encryptedName.substring(254);
|
|
341
|
+
encryptedName = encryptedName.substring(0, 254) + "$";
|
|
305
342
|
}
|
|
306
343
|
|
|
307
344
|
encryptedEntry.set({
|
|
308
345
|
encryptedName,
|
|
309
|
-
extraName
|
|
310
|
-
})
|
|
346
|
+
extraName,
|
|
347
|
+
});
|
|
311
348
|
|
|
312
349
|
if (preserveContent) {
|
|
313
|
-
encryptedEntry = this.preserveEntry(entry, encryptedEntry)
|
|
350
|
+
encryptedEntry = this.preserveEntry(entry, encryptedEntry);
|
|
314
351
|
}
|
|
315
|
-
|
|
316
352
|
}
|
|
317
353
|
if (content) {
|
|
318
354
|
encryptedEntry.set({
|
|
319
|
-
encryptedContent: _secrez.encrypt(
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
355
|
+
encryptedContent: _secrez.encrypt(
|
|
356
|
+
JSON.stringify({
|
|
357
|
+
i: id,
|
|
358
|
+
t: ts,
|
|
359
|
+
c: content,
|
|
360
|
+
})
|
|
361
|
+
),
|
|
362
|
+
});
|
|
325
363
|
if (preserveContent) {
|
|
326
|
-
encryptedEntry = this.preserveEntry(entry, encryptedEntry)
|
|
364
|
+
encryptedEntry = this.preserveEntry(entry, encryptedEntry);
|
|
327
365
|
}
|
|
328
366
|
}
|
|
329
|
-
return encryptedEntry
|
|
330
|
-
|
|
367
|
+
return encryptedEntry;
|
|
331
368
|
} else {
|
|
332
|
-
throw new Error(
|
|
369
|
+
throw new Error("User not logged");
|
|
333
370
|
}
|
|
334
371
|
}
|
|
335
372
|
|
|
336
373
|
decryptEntry(encryptedEntry, urlSafe) {
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
throw new Error('An Entry instance is expected as parameter')
|
|
374
|
+
if (!encryptedEntry || encryptedEntry.constructor.name !== "Entry") {
|
|
375
|
+
throw new Error("An Entry instance is expected as parameter");
|
|
340
376
|
}
|
|
341
377
|
|
|
342
378
|
const {
|
|
@@ -345,32 +381,30 @@ module.exports = function () {
|
|
|
345
381
|
encryptedName,
|
|
346
382
|
preserveContent,
|
|
347
383
|
nameId,
|
|
348
|
-
nameTs
|
|
349
|
-
} = encryptedEntry.get()
|
|
384
|
+
nameTs,
|
|
385
|
+
} = encryptedEntry.get();
|
|
350
386
|
|
|
351
387
|
if (this.masterKeyHash) {
|
|
352
|
-
|
|
353
388
|
try {
|
|
354
|
-
|
|
355
389
|
if (encryptedName) {
|
|
356
|
-
let data = encryptedName
|
|
390
|
+
let data = encryptedName;
|
|
357
391
|
if (extraName) {
|
|
358
|
-
data = encryptedName.substring(0, 254) + extraName
|
|
392
|
+
data = encryptedName.substring(0, 254) + extraName;
|
|
359
393
|
}
|
|
360
|
-
let type = parseInt(data[0])
|
|
361
|
-
let e = JSON.parse(_secrez.decrypt(data.substring(1), URL_SAFE))
|
|
362
|
-
let id = e.i
|
|
363
|
-
let ts = e.t
|
|
364
|
-
let name = e.n
|
|
365
|
-
let content =
|
|
394
|
+
let type = parseInt(data[0]);
|
|
395
|
+
let e = JSON.parse(_secrez.decrypt(data.substring(1), URL_SAFE));
|
|
396
|
+
let id = e.i;
|
|
397
|
+
let ts = e.t;
|
|
398
|
+
let name = e.n;
|
|
399
|
+
let content = "";
|
|
366
400
|
|
|
367
401
|
// during the indexing internalFS reads only the names of the files
|
|
368
402
|
if (encryptedContent) {
|
|
369
|
-
let e = JSON.parse(_secrez.decrypt(encryptedContent))
|
|
403
|
+
let e = JSON.parse(_secrez.decrypt(encryptedContent));
|
|
370
404
|
if (id !== e.i || ts !== e.t) {
|
|
371
|
-
throw new Error(
|
|
405
|
+
throw new Error("Data is corrupted");
|
|
372
406
|
}
|
|
373
|
-
content = e.c
|
|
407
|
+
content = e.c;
|
|
374
408
|
}
|
|
375
409
|
|
|
376
410
|
let decryptedEntry = new Entry({
|
|
@@ -378,64 +412,66 @@ module.exports = function () {
|
|
|
378
412
|
type,
|
|
379
413
|
ts,
|
|
380
414
|
name,
|
|
381
|
-
content
|
|
382
|
-
})
|
|
415
|
+
content,
|
|
416
|
+
});
|
|
383
417
|
|
|
384
418
|
if (preserveContent) {
|
|
385
|
-
decryptedEntry = this.preserveEntry(
|
|
419
|
+
decryptedEntry = this.preserveEntry(
|
|
420
|
+
encryptedEntry,
|
|
421
|
+
decryptedEntry
|
|
422
|
+
);
|
|
386
423
|
}
|
|
387
424
|
|
|
388
|
-
return decryptedEntry
|
|
425
|
+
return decryptedEntry;
|
|
389
426
|
}
|
|
390
427
|
|
|
391
428
|
// when the encryptedName has been already decrypted and we need only the content
|
|
392
429
|
if (encryptedContent) {
|
|
393
|
-
let e = JSON.parse(_secrez.decrypt(encryptedContent))
|
|
430
|
+
let e = JSON.parse(_secrez.decrypt(encryptedContent));
|
|
394
431
|
|
|
395
432
|
if ((nameId && e.i !== nameId) || (nameTs && e.t !== nameTs)) {
|
|
396
|
-
throw new Error(
|
|
433
|
+
throw new Error("Content is corrupted");
|
|
397
434
|
}
|
|
398
435
|
|
|
399
436
|
let decryptedEntry = new Entry({
|
|
400
437
|
id: e.i,
|
|
401
438
|
ts: e.i,
|
|
402
|
-
content: e.c
|
|
403
|
-
})
|
|
439
|
+
content: e.c,
|
|
440
|
+
});
|
|
404
441
|
|
|
405
442
|
if (preserveContent) {
|
|
406
|
-
decryptedEntry = this.preserveEntry(
|
|
443
|
+
decryptedEntry = this.preserveEntry(
|
|
444
|
+
encryptedEntry,
|
|
445
|
+
decryptedEntry
|
|
446
|
+
);
|
|
407
447
|
}
|
|
408
448
|
|
|
409
|
-
return decryptedEntry
|
|
449
|
+
return decryptedEntry;
|
|
410
450
|
}
|
|
411
|
-
|
|
412
451
|
} catch (e) {
|
|
413
|
-
if (e.message ===
|
|
414
|
-
throw e
|
|
415
|
-
} else if (e.message ===
|
|
416
|
-
throw e
|
|
452
|
+
if (e.message === "Data is corrupted") {
|
|
453
|
+
throw e;
|
|
454
|
+
} else if (e.message === "Content is corrupted") {
|
|
455
|
+
throw e;
|
|
417
456
|
}
|
|
418
|
-
throw new Error(
|
|
457
|
+
throw new Error("Fatal error during decryption");
|
|
419
458
|
}
|
|
420
459
|
|
|
421
|
-
throw new Error(
|
|
460
|
+
throw new Error("Missing parameters");
|
|
422
461
|
} else {
|
|
423
|
-
throw new Error(
|
|
462
|
+
throw new Error("User not logged");
|
|
424
463
|
}
|
|
425
464
|
}
|
|
426
465
|
|
|
427
466
|
signout() {
|
|
428
467
|
if (this.masterKeyHash) {
|
|
429
|
-
delete this.masterKeyHash
|
|
430
|
-
_secrez = undefined
|
|
468
|
+
delete this.masterKeyHash;
|
|
469
|
+
_secrez = undefined;
|
|
431
470
|
} else {
|
|
432
|
-
throw new Error(
|
|
471
|
+
throw new Error("User not logged");
|
|
433
472
|
}
|
|
434
473
|
}
|
|
435
|
-
|
|
436
474
|
}
|
|
437
475
|
|
|
438
|
-
return Secrez
|
|
439
|
-
|
|
440
|
-
}
|
|
441
|
-
|
|
476
|
+
return Secrez;
|
|
477
|
+
};
|