-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathworker_wrapper.js
More file actions
93 lines (83 loc) · 4.59 KB
/
worker_wrapper.js
File metadata and controls
93 lines (83 loc) · 4.59 KB
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
import { setEncryptionKey, generateSHA256Hash, arrayBufferToBase64, decryptString } from './src/lib/encryptionUtils.js';
import { triggerFindMsg, getQueueFromPageAndProcessMessages, sendErrorToPage } from './src/findMessages.js';
// Open options page when the extension icon is clicked
chrome.action.onClicked.addListener(() => {
chrome.runtime.openOptionsPage();
});
// Listener for messages from content scripts (or options page)
chrome.runtime.onMessage.addListener((request, sender, sendResponse) => {
const handleMessage = async () => {
switch (request.action) {
case "triggerFindMsg":
console.log("Received triggerFindMsg");
await triggerFindMsg();
break;
case "encryptionKeyReceived":
const senderTabId = sender.tab?.id;
const encryptionKey = request.encryptionKey;
if (!senderTabId || !encryptionKey) {
console.error("Worker: Missing sender tab ID or encryption key.");
return;
}
try {
// Generate a SHA-256 hash of the encryption key
const potentialKeyHash = await generateSHA256Hash(encryptionKey);
// Attempt to validate the key against existing data
let keyIsValid = false;
const connections = await chrome.storage.local.get();
let firstEncryptedConnection = null;
// Find the first encrypted connection in local storage
for (const id in connections) {
if (connections[id] && typeof connections[id] === 'object' && connections[id].encrypted) {
firstEncryptedConnection = connections[id];
break;
}
}
// Attempt to validate the key against the existing connection data
if (firstEncryptedConnection) {
console.log("Worker: Attempting validation against connection:", firstEncryptedConnection.connectionName);
try {
await decryptString(
firstEncryptedConnection.password,
firstEncryptedConnection.iv,
potentialKeyHash
);
console.log("Worker: Validation successful via connection data.");
keyIsValid = true;
} catch (decryptionError) {
console.warn("Worker: Validation via connection data failed (decryption error). Key invalid.");
}
// If the key is valid, save it to session storage
if (keyIsValid) {
const base64HashedKey = arrayBufferToBase64(potentialKeyHash);
await setEncryptionKey(base64HashedKey);
// Continue with the process
await getQueueFromPageAndProcessMessages();
} else {
// Key is definitively invalid
const errorMessage = "Incorrect encryption key. Please try again.";
console.log(`Worker: Sending 'invalidKeyRetry' to tab ${senderTabId}`);
chrome.tabs.sendMessage(senderTabId, {
action: "invalidKeyRetry",
message: errorMessage
}).catch(err => console.warn(`Worker: Failed to send invalidKeyRetry message: ${err.message}`));
}
} else {
// No encrypted connections found.
sendErrorToPage("No Connections Found", "No connections found. Please add a connection first.");
}
} catch (error) {
console.error("Worker: Error processing encryptionKeyReceived message:", error);
if (senderTabId) {
console.error("Need to implement/import sendErrorToPage if called here");
sendErrorToPage("Key Processing Error", `An error occurred: ${error.message}`);
}
}
break; // End case "encryptionKeyReceived"
default:
console.warn("Unknown message action:", request.action);
}
};
handleMessage();
return false;
});