Skip to content

Commit

Permalink
refactor user settings (#1191)
Browse files Browse the repository at this point in the history
  • Loading branch information
Sneezry authored May 13, 2024
1 parent 053d5c4 commit e8083ae
Show file tree
Hide file tree
Showing 19 changed files with 515 additions and 448 deletions.
66 changes: 17 additions & 49 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
"devDependencies": {
"@types/argon2-browser": "^1.18.1",
"@types/chai": "^4.2.14",
"@types/chrome": "^0.0.210",
"@types/chrome": "^0.0.266",
"@types/crypto-js": "^4.1.1",
"@types/mocha": "^10.0.6",
"@types/sinon": "^17.0.2",
Expand Down
44 changes: 16 additions & 28 deletions src/background.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,12 @@ import { CodeState } from "./models/otp";

import { getOTPAuthPerLineFromOPTAuthMigration } from "./models/migration";
import { isChrome, isFirefox } from "./browser";
import { UserSettings } from "./models/settings";

let contentTab: chrome.tabs.Tab | undefined;
let LocalStorage: {
[key: string]: any;
};

chrome.runtime.onMessage.addListener(async (message, sender) => {
LocalStorage =
(await chrome.storage.local.get("LocalStorage")).LocalStorage || {};
await UserSettings.updateItems();

if (message.action === "getCapture") {
if (!sender.tab) {
Expand Down Expand Up @@ -273,8 +270,8 @@ function getBackupToken(service: string) {
if (!value) {
return false;
}
LocalStorage.driveToken = value;
chrome.storage.local.set({ LocalStorage });
UserSettings.items.driveToken = value;
UserSettings.commitItems();
chrome.runtime.sendMessage({ action: "drivetoken", value });
return true;
}
Expand Down Expand Up @@ -308,10 +305,7 @@ function getBackupToken(service: string) {
authUrl = `https://login.microsoftonline.com/common/oauth2/v2.0/authorize?client_id=${
getCredentials().onedrive.client_id
}&response_type=code&redirect_uri=${redirUrl}&scope=https%3A%2F%2Fgraph.microsoft.com%2FFiles.ReadWrite${
LocalStorage.oneDriveBusiness !== "true" &&
LocalStorage.oneDriveBusiness !== true
? ".AppFolder"
: ""
UserSettings.items.oneDriveBusiness !== true ? ".AppFolder" : ""
}%20https%3A%2F%2Fgraph.microsoft.com%2FUser.Read%20offline_access&response_mode=query&prompt=consent`;
}
chrome.identity.launchWebAuthFlow(
Expand Down Expand Up @@ -345,8 +339,8 @@ function getBackupToken(service: string) {
const value = kvMatches[2];
if (key === "access_token") {
if (service === "dropbox") {
LocalStorage.dropboxToken = value;
chrome.storage.local.set({ LocalStorage });
UserSettings.items.dropboxToken = value;
UserSettings.commitItems();
uploadBackup("dropbox");
return;
}
Expand Down Expand Up @@ -379,9 +373,9 @@ function getBackupToken(service: string) {
if (res.error) {
console.error(res.error_description);
} else {
LocalStorage.driveToken = res.access_token;
LocalStorage.driveRefreshToken = res.refresh_token;
chrome.storage.local.set({ LocalStorage });
UserSettings.items.driveToken = res.access_token;
UserSettings.items.driveRefreshToken = res.refresh_token;
UserSettings.commitItems();
success = true;
}
} catch (error) {
Expand Down Expand Up @@ -412,9 +406,9 @@ function getBackupToken(service: string) {
if (res.error) {
console.error(res.error_description);
} else {
LocalStorage.oneDriveToken = res.access_token;
LocalStorage.oneDriveRefreshToken = res.refresh_token;
chrome.storage.local.set({ LocalStorage });
UserSettings.items.oneDriveToken = res.access_token;
UserSettings.items.oneDriveRefreshToken = res.refresh_token;
UserSettings.commitItems();
success = true;
}
} catch (error) {
Expand Down Expand Up @@ -558,27 +552,21 @@ async function setAutolock() {
return;
}

if (Number(LocalStorage.autolock) > 0) {
if (Number(UserSettings.items.autolock) > 0) {
chrome.alarms.create("autolock", {
delayInMinutes: Number(LocalStorage.autolock),
delayInMinutes: Number(UserSettings.items.autolock),
});
}
}

async function updateContextMenu() {
LocalStorage =
(await chrome.storage.local.get("LocalStorage")).LocalStorage || {};

chrome.permissions.contains(
{
permissions: ["contextMenus"],
},
(result) => {
if (result) {
if (
LocalStorage.enableContextMenu === "true" ||
LocalStorage.enableContextMenu === true
) {
if (UserSettings.items.enableContextMenu === true) {
chrome.contextMenus.removeAll();
chrome.contextMenus.create({
id: "otpContextMenu",
Expand Down
31 changes: 12 additions & 19 deletions src/components/Popup/DrivePage.vue
Original file line number Diff line number Diff line change
Expand Up @@ -36,38 +36,36 @@
import Vue from "vue";
import { isChrome } from "../../browser";
import { Drive } from "../../models/backup";
import { UserSettings } from "../../models/settings";
const service = "drive";
export default Vue.extend({
data: function () {
return {
email: this.i18n.loading,
LocalStorage: {} as { [key: string]: any },
};
},
created() {
chrome.storage.local.get("LocalStorage").then((res) => {
this.LocalStorage = res.LocalStorage || {};
});
UserSettings.updateItems();
},
computed: {
encryption: function () {
return this.$store.state.accounts.encryption;
},
isEncrypted: {
get(): boolean {
if (this.LocalStorage[`${service}Encrypted`] === null) {
if (UserSettings.items[`${service}Encrypted`] === null) {
this.$store.commit("backup/setEnc", { service, value: true });
this.LocalStorage[`${service}Encrypted`] = true;
chrome.storage.local.set({ LocalStorage: this.LocalStorage });
UserSettings.items[`${service}Encrypted`] = true;
UserSettings.commitItems();
return true;
}
return this.$store.state.backup.driveEncrypted;
},
set(newValue: string) {
this.LocalStorage.driveEncrypted = newValue;
chrome.storage.local.set({ LocalStorage: this.LocalStorage });
UserSettings.items.driveEncrypted = newValue === "true";
UserSettings.commitItems();
this.$store.commit("backup/setEnc", { service, value: newValue });
},
},
Expand All @@ -85,13 +83,13 @@ export default Vue.extend({
xhr.open(
"POST",
"https://accounts.google.com/o/oauth2/revoke?token=" +
this.LocalStorage.driveToken
UserSettings.items.driveToken
);
xhr.onreadystatechange = () => {
if (xhr.readyState === 4) {
if (isChrome) {
chrome.identity.removeCachedAuthToken(
{ token: this.LocalStorage.driveToken },
{ token: UserSettings.items.driveToken as string },
() => {
resolve(true);
}
Expand All @@ -104,8 +102,7 @@ export default Vue.extend({
};
xhr.send();
});
this.LocalStorage.driveToken = undefined;
chrome.storage.local.set({ LocalStorage: this.LocalStorage });
UserSettings.removeItem("driveToken");
this.$store.commit("backup/setToken", { service, value: false });
this.$store.commit("style/hideInfo");
},
Expand All @@ -116,16 +113,12 @@ export default Vue.extend({
);
if (response === true) {
this.$store.commit("notification/alert", this.i18n.updateSuccess);
} else if (
this.LocalStorage.driveRevoked === "true" ||
this.LocalStorage.driveRevoked === true
) {
} else if (UserSettings.items.driveRevoked === true) {
this.$store.commit(
"notification/alert",
chrome.i18n.getMessage("token_revoked", ["Google Drive"])
);
this.LocalStorage.driveRevoked = undefined;
chrome.storage.local.set({ LocalStorage: this.LocalStorage });
UserSettings.removeItem("driveRevoked");
this.$store.commit("backup/setToken", { service, value: false });
} else {
this.$store.commit("notification/alert", this.i18n.updateFailure);
Expand Down
Loading

0 comments on commit e8083ae

Please sign in to comment.