This repository has been archived by the owner on Apr 25, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.js
265 lines (228 loc) · 6.88 KB
/
main.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
/*
* An Electron Desktop app compatible with Google Authenticator.
* Copyright (C) 2018 Nikolas Eller
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
//Windows Squirrel
//handle setup events as quickly as possible
const setupEvents = require('./installers/windows/setupEvents');
if (setupEvents.handleSquirrelEvent()) {
// squirrel event handled and app will exit in 1000ms, so don't do anything else
return;
}
const {app, BrowserWindow} = require('electron');
const path = require('path');
const url = require('url');
const {ipcMain} = require('electron');
const {dialog} = require('electron');
const windowStateKeeper = require('electron-window-state');
require('electron-context-menu')({
showInspectElement: false
});
//Storage
let StorageEngine = require('./lib/storage');
let storage;
// Keep a global reference of the window object, if you don't, the window will
// be closed automatically when the JavaScript object is garbage collected.
let win;
function createWindow() {
// Get stored window dimension from last time
let mainWindowState = windowStateKeeper({
defaultWidth: 800,
defaultHeight: 600
});
// Create the browser window.
win = new BrowserWindow({
width: mainWindowState.width,
height: mainWindowState.height,
minHeight: 300,
minWidth: 450,
icon: path.join(__dirname, 'img/icon64x64.png')
});
// Remove menu
// TODO Bug in electron 4.1.4
win.setMenu(null);
// and load the index.html of the app.
win.loadURL(url.format({
pathname: path.join(__dirname, 'pages/index/index.html'),
protocol: 'file:',
slashes: true
}));
// Open the DevTools.
//win.webContents.openDevTools();
// Emitted when the window is closed.
win.on('closed', () => {
// Dereference the window object, usually you would store windows
// in an array if your app supports multi windows, this is the time
// when you should delete the corresponding element.
win = null;
let promise = storage.lockFile();
promise.catch((e) => {
catchError(e);
});
});
// Let us register listeners on the window, so we can update the state
// automatically (the listeners will be removed when the window is closed)
// and restore the maximized or full screen state
mainWindowState.manage(win);
}
// This method will be called when Electron has finished
// initialization and is ready to create browser windows.
// Some APIs can only be used after this event occurs.
app.on('ready', function () {
storageInit();
createWindow();
});
// Quit when all windows are closed.
app.on('window-all-closed', () => {
// On macOS it is common for applications and their menu bar
// to stay active until the user quits explicitly with Cmd + Q
if (process.platform !== 'darwin') {
app.quit();
}
});
app.on('activate', () => {
// On macOS it's common to re-create a window in the app when the
// dock icon is clicked and there are no other windows open.
if (win === null) {
storageInit();
createWindow()
}
});
/**
* Show dialog if an error was thrown
* @param error
*/
function catchError(error) {
dialog.showErrorBox('EAuthenticator Error', error.message);
}
/********************************************************
* Storage functions
*******************************************************/
/**
* Init the storage
*/
function storageInit() {
try {
storage = new StorageEngine(app.getPath("userData") + '/eauth.data');
}
catch (e) {
catchError(e);
}
}
// ipc message = {status: Number, error: String}
// status like HTTP status codes https://en.wikipedia.org/wiki/List_of_HTTP_status_codes
ipcMain.on('getAllAccounts', (event) => {
event.returnValue = storage.getAllAccounts();
});
/**
* arg has the attribute name and newName
*/
ipcMain.on('renameAccount', (event, arg) => {
try {
storage.renameAccount(arg.name, arg.newName);
event.returnValue = {status: 200, error: ""};
}
catch (e) {
event.returnValue = {status: 400, error: e.message};
}
event.returnValue = 'pong'
});
/**
* arg has the attribute name
*/
ipcMain.on('deleteAccount', (event, arg) => {
try {
storage.deleteAccount(arg.name);
event.returnValue = {status: 200, error: ""};
}
catch (e) {
event.returnValue = {status: 400, error: e.message};
}
});
/**
* arg has attribute account ({name: nameOfAccount, secret: preShared
*/
ipcMain.on('addAccount', (event, arg) => {
try {
storage.addAccount(arg.account);
event.returnValue = {status: 200, error: ""};
}
catch (e) {
event.returnValue = {status: 400, error: e.message};
}
});
/**
* arg has attribute oldPassword and newPassword
*/
ipcMain.on('changePassword', (event, arg) => {
try {
storage.changePassword(arg.oldPassword, arg.newPassword);
event.returnValue = {status: 200, error: ""};
}
catch (e) {
event.returnValue = {status: 400, error: e.message};
}
});
/**
* arg has attribute oldPassword
*/
ipcMain.on('resetPassword', (event, arg) => {
try {
storage.resetPassword(arg.oldPassword);
event.returnValue = {status: 200, error: ""};
}
catch (e) {
event.returnValue = {status: 400, error: e.message};
}
});
ipcMain.on('lockFile', (event) => {
let promise = storage.lockFile();
promise.then(() => {
event.returnValue = {status: 200, error: ""};
}, (e) => {
event.returnValue = {status: 400, error: e.message};
});
});
/**
* arg has attribute password
*/
ipcMain.on('unlockFile', (event, arg) => {
let promise = storage.unlockFile(arg.password);
promise.then(() => {
event.returnValue = {status: 200, error: ""};
}, (e) => {
event.returnValue = {status: 400, error: e.message};
});
});
ipcMain.on('needPassword', (event) => {
try {
event.returnValue = storage.needPassword();
}
catch (e) {
catchError(e);
}
});
ipcMain.on('noFileFound', (event) => {
try {
event.returnValue = storage.noFileFound();
} catch (e) {
catchError(e);
}
});
ipcMain.on('useDefaultPassword', (event) => {
try {
event.returnValue = storage.useDefPass();
} catch (e) {
catchError(e);
}
});