This repository has been archived by the owner on Oct 28, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
background.js
71 lines (65 loc) · 2.64 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
const authenticateURL = "https://kinto.dev.mozaws.net/v1/fxa-oauth/login?redirect=https://natim.github.io/get_fxa_token/%23";
const redirectIntermediate = "https://natim.github.io/get_fxa_token/#";
function tabCallback(tabId, changeInfo, updatedTab) {
if (changeInfo.status === 'complete' && updatedTab.url.indexOf(redirectIntermediate) === 0) {
chrome.tabs.remove(tabId);
const bearer = updatedTab.url.split(redirectIntermediate)[1];
chrome.storage.local.set({bearer: bearer}, function() {
if (chrome.runtime.lastError) {
console.error(chrome.runtime.lastError);
chrome.runtime.sendMessage({ action: 'error', error: chrome.runtime.lastError });
} else {
chrome.runtime.sendMessage({ action: 'authenticated', bearer: bearer });
}
});
}
}
function handleMaybeInt(maybeString) {
const maybeInt = parseInt(maybeString, 10);
if (Number.isNaN(maybeInt)) {
return null;
}
return maybeInt;
}
function handlePassphraseCleaning() {
chrome.storage.local.get(["lastModified", "lockAfterSeconds", "temporaryPassphrase"], function(data) {
const currentTime = Date.now();
const lastModified = handleMaybeInt(data.lastModified);
const lockAfterSeconds = handleMaybeInt(data.lockAfterSeconds);
if (data.temporaryPassphrase && lastModified) {
console.log("lastModified", lastModified,
"lockAfterSeconds", lockAfterSeconds,
"time spent", (currentTime - lastModified) / 1000);
if (!lockAfterSeconds || currentTime - lastModified > lockAfterSeconds * 1000) {
console.log("cleaning the passphrase in the background.");
chrome.storage.local.set({
lastModified: null,
temporaryPassphrase: null
});
} else {
console.log("Looking for passphrase cleaning");
/* Try again when the passphrase is supposed to expire */
const waitForMilliseconds = (lockAfterSeconds * 1000) - (currentTime - lastModified) + 1;
setTimeout(handlePassphraseCleaning, waitForMilliseconds);
}
} else if (data.temporaryPassphrase && lockAfterSeconds) {
/* If we don't have a lastModified yet, it's because it just started */
setTimeout(handlePassphraseCleaning, lockAfterSeconds * 1000);
}
});
}
function handleAuthentication() {
chrome.tabs.create({ 'url': authenticateURL }, function () {
chrome.tabs.onUpdated.addListener(tabCallback);
});
}
chrome.runtime.onMessage.addListener(function (eventData) {
switch (eventData.action) {
case 'passphraseCleaner':
handlePassphraseCleaning();
break;
case 'authenticate':
handleAuthentication();
break;
}
});