refactor: encrypt the sensitive data

This commit is contained in:
Kevin Yue
2023-07-22 07:33:53 +08:00
parent bf96a88e21
commit 601f422863
40 changed files with 1274 additions and 275 deletions

View File

@@ -14,7 +14,11 @@ export const portalGatewaysAtom = atom<GatewayData[]>((get) => {
});
export const selectedGatewayAtom = atom(
(get) => get(currentPortalDataAtom).selectedGateway,
(get) => {
const { selectedGateway } = get(currentPortalDataAtom);
const gateways = get(portalGatewaysAtom);
return gateways.find(({ name }) => name === selectedGateway);
},
async (get, set, update: string) => {
const portalData = get(currentPortalDataAtom);
await set(updatePortalDataAtom, { ...portalData, selectedGateway: update });

View File

@@ -62,7 +62,7 @@ export const loginPortalAtom = atom(
}
// Here, we have got the portal config successfully, refresh the cached portal data
const previousSelectedGateway = get(selectedGatewayAtom);
const previousSelectedGateway = get(selectedGatewayAtom)?.name;
const selectedGateway = gateways.find(
({ name }) => name === previousSelectedGateway
);

View File

@@ -1,7 +1,7 @@
import { atom } from "jotai";
import { atomWithDefault } from "jotai/utils";
import { PortalCredential } from "../services/portalService";
import { atomWithTauriStorage } from "../services/storeService";
import { atomWithTauriStorage } from "../services/storageService";
import { unwrap } from "./unwrap";
export type GatewayData = {
@@ -31,7 +31,11 @@ const DEFAULT_APP_DATA: AppData = {
clearCookies: true,
};
export const appDataAtom = atomWithTauriStorage("APP_DATA", DEFAULT_APP_DATA);
const keyHint = {
key: "APP_DATA",
encrypted: true,
};
export const appDataAtom = atomWithTauriStorage(keyHint, DEFAULT_APP_DATA);
const unwrappedAppDataAtom = atom(
(get) => get(unwrap(appDataAtom)) || DEFAULT_APP_DATA
);
@@ -51,6 +55,11 @@ export const currentPortalDataAtom = atom<PortalData>((get) => {
return portalData || { address: portalAddress, gateways: [] };
});
export const allPortalsAtom = atom((get) => {
const { portals } = get(unwrappedAppDataAtom);
return portals.map(({ address }) => address);
});
export const updatePortalDataAtom = atom(
null,
async (get, set, update: PortalData) => {

View File

@@ -5,7 +5,7 @@ import settingsService, {
DEFAULT_SETTINGS_DATA,
SETTINGS_DATA,
} from "../services/settingsService";
import { atomWithTauriStorage } from "../services/storeService";
import { atomWithTauriStorage } from "../services/storageService";
import { unwrap } from "./unwrap";
const settingsDataAtom = atomWithTauriStorage(

View File

@@ -4,6 +4,7 @@ import vpnService from "../services/vpnService";
import { selectedGatewayAtom, switchGatewayAtom } from "./gateway";
import { notifyErrorAtom, notifySuccessAtom } from "./notification";
import { unwrap } from "./unwrap";
import { portalAddressAtom } from "./portal";
export type Status =
| "disconnected"
@@ -75,14 +76,16 @@ export const statusTextAtom = atom<string>((get) => {
if (status === "connected") {
const selectedGateway = get(selectedGatewayAtom);
return selectedGateway
? `Gateway: ${selectedGateway}`
: statusTextMap[status];
const portalAddress = get(portalAddressAtom);
return selectedGateway?.address === portalAddress
? statusTextMap[status]
: selectedGateway?.address!;
}
if (switchingGateway) {
const selectedGateway = get(selectedGatewayAtom);
return `Switching to ${selectedGateway}`;
return `Switching to ${selectedGateway?.name}`;
}
return statusTextMap[status];