-
Notifications
You must be signed in to change notification settings - Fork 0
/
background.js
394 lines (380 loc) · 20.3 KB
/
background.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
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
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
var connected = false;
var user = ""; // use to show message for popup
var password = ""; // use to encrypt and decrypt info
var resp;
// next fields are fetched from page and saved to file if needed
var save_user = undefined;
var save_password = undefined;
var save_url = undefined;
var save_form = undefined;
var fileContent = {}; // will be updated whenever needed
// var socket = io('http://localhost:1337'); // when running locally
var socket = io('http://184.72.209.38:1337'); // when running on AWS
function save_details(){
if (connected) {
chrome.storage.local.set({'fileContent': fileContent});
// console.log("this is fileContent: " + JSON.stringify(fileContent));
var content = "";
for (var url in fileContent) {
for (var form in fileContent[url]) {
for (var usr in fileContent[url][form]) {
content += CryptoJS.AES.encrypt(url, password) + " " +
CryptoJS.AES.encrypt(form, password) + " " +
CryptoJS.AES.encrypt(usr, password) + " " +
CryptoJS.AES.encrypt(fileContent[url][form][usr], password) + "\n";
}
}
}
socket.emit('update_server', {
user: user,
password: password,
content: content
});
}
}
socket.on('server_ready', function (data) {
console.log("server is ready! you can now sign in.");
chrome.runtime.onMessage.addListener(function(msg){
var req = msg.request;
switch (req){
case "login":
case "register":
user = msg.user;
password = msg.password;
socket.emit(req, {user: user, password: password});
break;
case "fetch_details":
if (connected) {
// should check no input password exists!
// console.log(msg.details); // details fetched from site
save_user = msg.details.uname;
save_password = msg.details.password;
save_url = msg.details.url;
save_form = msg.details.parent_id;
}
break;
case "save_details":
// console.log("save request! this is file content: " + JSON.stringify(fileContent));
if (connected && save_user && save_password) {
// should check save_url fits current page
chrome.tabs.query({active: true, lastFocusedWindow: true}, function (tabs) {
if (tabs && tabs[0]) {
var temp_url = tabs[0].url.split('/');
var current_url = temp_url[0] + "//" + temp_url[2] + "/";
if (msg.url === current_url) {
if (fileContent) {
if (fileContent[save_url]) { // site exists in database
// check if details are up to date
if (fileContent[save_url][save_form]) { // form exists in database
if (fileContent[save_url][save_form][save_user]) { // user exists in database
if (fileContent[save_url][save_form][save_user] !== save_password) {
// user exists, password is different!
// should alert and ask if user wants to change password!
if (confirm("There is a different password saved for this user in database. Update password?")) {
console.log("replacing password for user " + save_user + " in " + save_url);
fileContent[save_url][save_form][save_user] = save_password;
save_details();
}
}
else {
console.log("User exists in database! Details were placed...");
}
}
else {
// user is not yet saved!!
console.log("adding another user for site " + save_url);
fileContent[save_url][save_form][save_user] = save_password;
save_details();
}
}
else {
// form is not yet saved!! (Only good to remember maybe if already signed up?)
console.log("adding another form for site " + save_url);
fileContent[save_url][save_form] = {};
fileContent[save_url][save_form][save_user] = save_password;
save_details();
}
}
else {
// no details are saved for this url or user!
console.log("This site was never saved for this user!");
fileContent[save_url] = {};
fileContent[save_url][save_form] = {};
fileContent[save_url][save_form][save_user] = save_password;
save_details();
}
}
save_user = undefined;
save_password = undefined;
save_url = undefined;
save_form = undefined;
}
}
});
}
break;
}
});
});
socket.on('login_response', function (data) {
resp = data.response;
// get popup window
var views = chrome.extension.getViews({
type: "popup"
});
for (var i = 0; i < views.length; i++) {
views[i].document.getElementById('status').innerHTML = resp;
}
if (resp === "signed in") {
connected = true;
var content = {};
if (data.content !== "problem") {
// console.log(data.content); // prints file content so client could see it is encrypted!!
var details = data.content.split('\n');
// decrypt all details sent from server!
for (var i = 0; i < details.length; i++) {
var line = details[i].split(' ');
if (line.length === 4) {
if (!content[CryptoJS.AES.decrypt(line[0], password).toString(CryptoJS.enc.Utf8)])
content[CryptoJS.AES.decrypt(line[0], password).toString(CryptoJS.enc.Utf8)] = {};
if (!content[CryptoJS.AES.decrypt(line[0], password).toString(CryptoJS.enc.Utf8)][CryptoJS.AES.decrypt(line[1], password).toString(CryptoJS.enc.Utf8)])
content[CryptoJS.AES.decrypt(line[0], password).toString(CryptoJS.enc.Utf8)][CryptoJS.AES.decrypt(line[1], password).toString(CryptoJS.enc.Utf8)] = {};
content[CryptoJS.AES.decrypt(line[0], password).toString(CryptoJS.enc.Utf8)]
[CryptoJS.AES.decrypt(line[1], password).toString(CryptoJS.enc.Utf8)]
[CryptoJS.AES.decrypt(line[2], password).toString(CryptoJS.enc.Utf8)] =
CryptoJS.AES.decrypt(line[3], password).toString(CryptoJS.enc.Utf8);
}
}
chrome.storage.local.set({'fileContent': content, 'user': user}, function(){
fileContent = content;
// fileContent is now the decrypted details for the client for every website he saved
// console.log('set fileContent (' + JSON.stringify(fileContent) + ') in local storage');
});
}
else{
connected = false;
if (confirm("Password Keeper:\nPassword file was altered! Deleting your account.\nWould you like to save your last backup of passwords (non decrypted!!) locally?")){
// user wants a copy from chrome local storage
chrome.storage.local.get(['user'], function(user_items){
if (user_items && user_items.user){
if (user_items.user === user){
var finished = false;
var p = prompt("Please Enter your password for your account.\nPressing cancel will result with loss of all data!");
while (!finished){
if (p !== null) {
if (p === password) {
chrome.storage.local.get(['fileContent'], function (items) {
if (items && items.fileContent) {
fileContent = items.fileContent; // should be the last backuped locally
// console.log("restored from last backup: " + JSON.stringify(fileContent));
content = "";
for (var url in fileContent) {
for (var form in fileContent[url]) {
for (var usr in fileContent[url][form]) {
content += url + " " + form + " " + usr + " " + fileContent[url][form][usr] + "\n";
}
}
}
// console.log("this is content: " + content);
var file = new Blob([content]);
var a = document.createElement("a");
url = URL.createObjectURL(file);
a.href = url;
a.download = user + "_passwords.txt";
document.body.appendChild(a);
a.click();
setTimeout(function () {
document.body.removeChild(a);
window.URL.revokeObjectURL(url);
}, 0);
}
// the reason for thie code duplication is Asynchronous of JavaScript. That solved my problem.
socket.emit('delete_user', {user: user, password: password});
resp = "ERROR!";
chrome.tabs.getAllInWindow(null, function (tabs) {
for (var i = 0; i < tabs.length; i++) {
var tab_url = tabs[i].url.split('/');
tab_url = tab_url[0] + '//' + tab_url[2] + '/';
if (fileContent[tab_url])
chrome.tabs.update(tabs[i].id, {url: tabs[i].url});
}
console.log("Problem in server! Deleting account!");
chrome.runtime.reload();
});
});
finished = true;
}
else {
p = prompt("Wrong Password given!\nPlease Enter your password for your account.\nPressing cancel will resuly with loss of all data!");
}
}
else{
finished = true;
}
}
}
else{
alert("Unfortunatley you have no backup currently in local storage. Nothing was saved...");
}
}
});
}
else{
socket.emit('delete_user', {user: user, password: password});
resp = "ERROR!";
chrome.tabs.getAllInWindow(null, function (tabs) {
for (var i = 0; i < tabs.length; i++) {
var tab_url = tabs[i].url.split('/');
tab_url = tab_url[0] + '//' + tab_url[2] + '/';
if (fileContent[tab_url])
chrome.tabs.update(tabs[i].id, {url: tabs[i].url});
}
console.log("Problem in server! Deleting account!");
chrome.runtime.reload();
});
return;
}
}
for (var i = 0; i < views.length; i++) {
views[i].close(); // closes the popup window on successful sign-in
}
// refresh all saved pages on sign in
chrome.tabs.getAllInWindow(null, function (tabs) {
for (var i = 0; i < tabs.length; i++) {
if (tabs[i] && !tabs[i].url.startsWith("chrome://")){
var tab_url = tabs[i].url.split('/');
tab_url = tab_url[0] + '//' + tab_url[2] + '/';
if (fileContent[tab_url]) {
chrome.tabs.update(tabs[i].id, {url: tabs[i].url});
}
chrome.tabs.executeScript(tabs[i].id, {
file: 'content.js'
});
}
}
});
}
console.log(resp);
});
socket.on('register_response', function (data) {
resp = data.response;
var views = chrome.extension.getViews({
type: "popup"
});
for (var i = 0; i < views.length; i++) {
views[i].document.getElementById('status').innerHTML = resp;
}
console.log(resp);
});
socket.on('account_deleted', function (data) {
// should send something?
if (connected){
connected = false;
alert("Your account has been deleted from the server.\n" +
"Either someone with your details deleted it from another computer, or our server been hacked.\n");
chrome.storage.local.get(['user'], function(user_items){
var finished = false;
var p = prompt("You can save your passwords to your computer.\n" +
"Please enter your password if you wish to do so.\n" +
"Pressing cancel will result with lose of this data");
while (!finished){
if (p !== null){
if (p === password){
var content = "";
if (fileContent && JSON.stringify(fileContent) !== "{}") {
for (var url in fileContent) {
for (var form in fileContent[url]) {
for (var usr in fileContent[url][form]) {
content += url + " " + form + " " + usr + " " + fileContent[url][form][usr] + "\n";
}
}
}
}
var file = new Blob([content]);
var a = document.createElement("a");
url = URL.createObjectURL(file);
a.href = url;
a.download = user + "_passwords.txt";
document.body.appendChild(a);
a.click();
setTimeout(function () {
document.body.removeChild(a);
window.URL.revokeObjectURL(url);
}, 0);
finished = true;
}
else{
prompt("Wrong password given!\n" +
"Please enter your password if you wish to save your passwords.\n" +
"Pressing cancel will result with lose of this data");
}
}
else{
console.log("User canceled his request.");
finished = true;
}
}
resp = "user deleted";
chrome.tabs.getAllInWindow(null, function (tabs) {
for (var i = 0; i < tabs.length; i++) {
var tab_url = tabs[i].url.split('/');
tab_url = tab_url[0] + '//' + tab_url[2] + '/';
if (fileContent[tab_url])
chrome.tabs.update(tabs[i].id, {url: tabs[i].url});
}
chrome.runtime.reload();
});
});
}
});
socket.on('update_file', function (data) {
if (connected) {
var content = {};
// console.log(data.content); // prints file content so client could see it is encrypted!!
var details = data.content.split('\n');
// decrypt all details sent from server!
for (var i = 0; i < details.length; i++) {
var line = details[i].split(' ');
if (line.length === 4) {
if (!content[CryptoJS.AES.decrypt(line[0], password).toString(CryptoJS.enc.Utf8)])
content[CryptoJS.AES.decrypt(line[0], password).toString(CryptoJS.enc.Utf8)] = {};
if (!content[CryptoJS.AES.decrypt(line[0], password).toString(CryptoJS.enc.Utf8)][CryptoJS.AES.decrypt(line[1], password).toString(CryptoJS.enc.Utf8)])
content[CryptoJS.AES.decrypt(line[0], password).toString(CryptoJS.enc.Utf8)][CryptoJS.AES.decrypt(line[1], password).toString(CryptoJS.enc.Utf8)] = {};
content[CryptoJS.AES.decrypt(line[0], password).toString(CryptoJS.enc.Utf8)]
[CryptoJS.AES.decrypt(line[1], password).toString(CryptoJS.enc.Utf8)]
[CryptoJS.AES.decrypt(line[2], password).toString(CryptoJS.enc.Utf8)] =
CryptoJS.AES.decrypt(line[3], password).toString(CryptoJS.enc.Utf8);
}
}
chrome.storage.local.set({'fileContent': content}, function () {
fileContent = content;
// fileContent is now the decrypted details for the client for every website he saved
// console.log('set fileContent (' + JSON.stringify(fileContent) + ') in local storage');
});
}
});
chrome.tabs.onActivated.addListener(function (activeInfo) { // launch whenever we switch tab
var tabId = activeInfo.tabId;
if (connected) {
chrome.tabs.get(tabId, function (tab) {
if (tab && !tab.url.startsWith("chrome://")){
chrome.tabs.executeScript(tabId, {
file: 'content.js'
});
}
});
}
});
chrome.tabs.onUpdated.addListener(function (tabId, changeInfo) {
if (connected) {
chrome.tabs.get(tabId, function (tab) {
if (tab && !tab.url.startsWith("chrome://")){
if (changeInfo.status === "complete") {
chrome.tabs.executeScript(tabId, {
file: 'content.js'
});
}
}
});
}
});