Skip to content

Commit 5ec3aae

Browse files
committed
fix: fixes #194 - adds support for encryption events
1 parent ebeb1e9 commit 5ec3aae

File tree

6 files changed

+276
-176
lines changed

6 files changed

+276
-176
lines changed

package-lock.json

Lines changed: 193 additions & 172 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@
8080
"codemirror": "^5.49.0",
8181
"crypto-js": "^3.1.9-1",
8282
"electron-log": "^3.0.8",
83+
"electron-prompt": "^1.3.1",
8384
"electron-updater": "4.1.2",
8485
"form-data": "^2.5.1",
8586
"fs-extra": "^8.1.0",

scripts/packages/encryption/renderer/encryption.js

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
const AES = require('crypto-js/aes.js');
22
const CryptoJS = require('crypto-js/crypto-js.js');
3+
const prompt = require('electron-prompt');
34
/**
45
* A class that handles `encryption-*` web events in the renderer process
56
* and performs data encryption/decryption.
@@ -26,11 +27,13 @@ class EncryptionService {
2627

2728
_decodeHandler(e) {
2829
const { method } = e.detail;
30+
e.preventDefault();
2931
e.detail.result = this.decode(method, e.detail);
3032
}
3133

3234
_encodeHandler(e) {
3335
const { method } = e.detail;
36+
e.preventDefault();
3437
e.detail.result = this.encode(method, e.detail);
3538
}
3639

@@ -56,11 +59,22 @@ class EncryptionService {
5659
}
5760

5861
async decodeAes(data, passphrase) {
59-
if (!passphrase === undefined) {
60-
passphrase = prompt('Enter password to open the file.');
62+
if (passphrase === undefined) {
63+
const win = require('electron').remote.getCurrentWindow();
64+
passphrase = await prompt({
65+
title: 'File password',
66+
label: 'Enter password to open the file',
67+
}, win);
68+
if (passphrase === null) {
69+
throw new Error('Password is required to open the file.');
70+
}
71+
}
72+
try {
73+
const bytes = AES.decrypt(data, passphrase);
74+
return bytes.toString(CryptoJS.enc.Utf8);
75+
} catch (_) {
76+
throw new Error('Invalid password.');
6177
}
62-
const bytes = AES.decrypt(data, passphrase);
63-
return bytes.toString(CryptoJS.enc.Utf8);
6478
}
6579
}
6680
exports.EncryptionService = EncryptionService;
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
exports.EncryptionService = require('./encryption').EncryptionService;
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
const { assert } = require('chai');
2+
const { EncryptionService } = require('../renderer');
3+
4+
describe('EncryptionService', function() {
5+
describe('Encrypting content', () => {
6+
let instance;
7+
before(() => {
8+
instance = new EncryptionService();
9+
instance.listen();
10+
});
11+
12+
after(() => {
13+
instance.unlisten();
14+
});
15+
16+
it('encodes a content using AES.encrypt', async () => {
17+
const e = new CustomEvent('encryption-encode', {
18+
bubbles: true,
19+
cancelable: true,
20+
detail: {
21+
method: 'aes',
22+
data: 'test-data',
23+
passphrase: 'test'
24+
}
25+
});
26+
document.body.dispatchEvent(e);
27+
const result = await e.detail.result;
28+
assert.typeOf(result, 'string');
29+
assert.notEqual(result, 'test-data');
30+
});
31+
});
32+
33+
34+
describe('Decrypting content', () => {
35+
const encoded = 'U2FsdGVkX19MRBu8liXK/sbNBOQWG1mewbjEBb8cTAw=';
36+
let instance;
37+
before(() => {
38+
instance = new EncryptionService();
39+
instance.listen();
40+
});
41+
42+
after(() => {
43+
instance.unlisten();
44+
});
45+
46+
it('decodes a content using AES.decrypt and provided password', async () => {
47+
const e = new CustomEvent('encryption-decode', {
48+
bubbles: true,
49+
cancelable: true,
50+
detail: {
51+
method: 'aes',
52+
data: encoded,
53+
passphrase: 'test'
54+
}
55+
});
56+
document.body.dispatchEvent(e);
57+
const result = await e.detail.result;
58+
assert.typeOf(result, 'string');
59+
assert.equal(result, 'test-data');
60+
});
61+
});
62+
});

src/arc-electron.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ class ArcElectron extends ArcAppMixin(LitElement) {
5757
/* global log */
5858
this.log = log;
5959
this.sysVars = process.env;
60+
this.withEncrypt = true;
6061
}
6162

6263
connectedCallback() {

0 commit comments

Comments
 (0)