mirror of
https://github.com/fosrl/pangolin.git
synced 2026-08-10 22:48:14 +02:00
Rename and add browser target update
This commit is contained in:
@@ -10,16 +10,22 @@ import {
|
|||||||
clientSiteResources
|
clientSiteResources
|
||||||
} from "@server/db";
|
} from "@server/db";
|
||||||
import { Config, ConfigSchema } from "./types";
|
import { Config, ConfigSchema } from "./types";
|
||||||
import { ProxyResourcesResults, updateProxyResources } from "./proxyResources";
|
import {
|
||||||
|
PublicResourcesResults,
|
||||||
|
updatePublicResources
|
||||||
|
} from "./publicResources";
|
||||||
import { fromError } from "zod-validation-error";
|
import { fromError } from "zod-validation-error";
|
||||||
import logger from "@server/logger";
|
import logger from "@server/logger";
|
||||||
import { sites } from "@server/db";
|
import { sites } from "@server/db";
|
||||||
import { eq, and, isNotNull } from "drizzle-orm";
|
import { eq, and, isNotNull } from "drizzle-orm";
|
||||||
import { addTargets as addProxyTargets } from "@server/routers/newt/targets";
|
import {
|
||||||
|
addTargets as addProxyTargets,
|
||||||
|
sendBrowserGatewayTargets
|
||||||
|
} from "@server/routers/newt/targets";
|
||||||
import {
|
import {
|
||||||
ClientResourcesResults,
|
ClientResourcesResults,
|
||||||
updateClientResources
|
updatePrivateResources
|
||||||
} from "./clientResources";
|
} from "./privateResources";
|
||||||
import { updateResourcePolicies } from "./resourcePolicies";
|
import { updateResourcePolicies } from "./resourcePolicies";
|
||||||
import { BlueprintSource } from "@server/routers/blueprints/types";
|
import { BlueprintSource } from "@server/routers/blueprints/types";
|
||||||
import { stringify as stringifyYaml } from "yaml";
|
import { stringify as stringifyYaml } from "yaml";
|
||||||
@@ -54,18 +60,18 @@ export async function applyBlueprint({
|
|||||||
let error: any | null = null;
|
let error: any | null = null;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
let proxyResourcesResults: ProxyResourcesResults = [];
|
let proxyResourcesResults: PublicResourcesResults = [];
|
||||||
let clientResourcesResults: ClientResourcesResults = [];
|
let clientResourcesResults: ClientResourcesResults = [];
|
||||||
await db.transaction(async (trx) => {
|
await db.transaction(async (trx) => {
|
||||||
await updateResourcePolicies(orgId, config, trx);
|
await updateResourcePolicies(orgId, config, trx);
|
||||||
|
|
||||||
proxyResourcesResults = await updateProxyResources(
|
proxyResourcesResults = await updatePublicResources(
|
||||||
orgId,
|
orgId,
|
||||||
config,
|
config,
|
||||||
trx,
|
trx,
|
||||||
siteId
|
siteId
|
||||||
);
|
);
|
||||||
clientResourcesResults = await updateClientResources(
|
clientResourcesResults = await updatePrivateResources(
|
||||||
orgId,
|
orgId,
|
||||||
config,
|
config,
|
||||||
trx,
|
trx,
|
||||||
@@ -104,13 +110,27 @@ export async function applyBlueprint({
|
|||||||
(hc) => hc.targetId === target.targetId
|
(hc) => hc.targetId === target.targetId
|
||||||
);
|
);
|
||||||
|
|
||||||
await addProxyTargets(
|
if (["http", "tcp", "udp"].includes(target.mode)) {
|
||||||
site.newt.newtId,
|
await addProxyTargets(
|
||||||
[target],
|
site.newt.newtId,
|
||||||
matchingHealthcheck ? [matchingHealthcheck] : [],
|
[target],
|
||||||
result.proxyResource.mode === "udp" ? "udp" : "tcp",
|
matchingHealthcheck
|
||||||
site.newt.version
|
? [matchingHealthcheck]
|
||||||
);
|
: [],
|
||||||
|
result.proxyResource.mode === "udp"
|
||||||
|
? "udp"
|
||||||
|
: "tcp",
|
||||||
|
site.newt.version
|
||||||
|
);
|
||||||
|
} else if (
|
||||||
|
["ssh", "rdp", "vnc"].includes(target.mode)
|
||||||
|
) {
|
||||||
|
await sendBrowserGatewayTargets(
|
||||||
|
site.newt.newtId,
|
||||||
|
[target],
|
||||||
|
site.newt.version
|
||||||
|
);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+1
-1
@@ -105,7 +105,7 @@ export type ClientResourcesResults = {
|
|||||||
oldSites: { siteId: number }[];
|
oldSites: { siteId: number }[];
|
||||||
}[];
|
}[];
|
||||||
|
|
||||||
export async function updateClientResources(
|
export async function updatePrivateResources(
|
||||||
orgId: string,
|
orgId: string,
|
||||||
config: Config,
|
config: Config,
|
||||||
trx: Transaction,
|
trx: Transaction,
|
||||||
@@ -52,19 +52,19 @@ import { encrypt } from "@server/lib/crypto";
|
|||||||
import { generateId } from "@server/auth/sessions/app";
|
import { generateId } from "@server/auth/sessions/app";
|
||||||
import serverConfig from "@server/lib/config";
|
import serverConfig from "@server/lib/config";
|
||||||
|
|
||||||
export type ProxyResourcesResults = {
|
export type PublicResourcesResults = {
|
||||||
proxyResource: Resource;
|
proxyResource: Resource;
|
||||||
targetsToUpdate: Target[];
|
targetsToUpdate: Target[];
|
||||||
healthchecksToUpdate: TargetHealthCheck[];
|
healthchecksToUpdate: TargetHealthCheck[];
|
||||||
}[];
|
}[];
|
||||||
|
|
||||||
export async function updateProxyResources(
|
export async function updatePublicResources(
|
||||||
orgId: string,
|
orgId: string,
|
||||||
config: Config,
|
config: Config,
|
||||||
trx: Transaction,
|
trx: Transaction,
|
||||||
siteId?: number
|
siteId?: number
|
||||||
): Promise<ProxyResourcesResults> {
|
): Promise<PublicResourcesResults> {
|
||||||
const results: ProxyResourcesResults = [];
|
const results: PublicResourcesResults = [];
|
||||||
|
|
||||||
for (const [resourceNiceId, resourceData] of Object.entries(
|
for (const [resourceNiceId, resourceData] of Object.entries(
|
||||||
config["proxy-resources"]
|
config["proxy-resources"]
|
||||||
+34
-22
@@ -37,6 +37,10 @@ import BrandedAuthSurface from "@app/components/BrandedAuthSurface";
|
|||||||
import PoweredByPangolin from "@app/components/PoweredByPangolin";
|
import PoweredByPangolin from "@app/components/PoweredByPangolin";
|
||||||
import AuthPageFooterNotices from "@app/components/AuthPageFooterNotices";
|
import AuthPageFooterNotices from "@app/components/AuthPageFooterNotices";
|
||||||
import { useTranslations } from "next-intl";
|
import { useTranslations } from "next-intl";
|
||||||
|
import {
|
||||||
|
loadEncryptedLocalStorage,
|
||||||
|
saveEncryptedLocalStorage
|
||||||
|
} from "@app/lib/secureLocalStorage";
|
||||||
|
|
||||||
declare module "react" {
|
declare module "react" {
|
||||||
namespace JSX {
|
namespace JSX {
|
||||||
@@ -63,22 +67,14 @@ type RdpCredentialsForm = {
|
|||||||
enableClipboard: boolean;
|
enableClipboard: boolean;
|
||||||
};
|
};
|
||||||
|
|
||||||
function loadStoredCredentials(key: string): RdpCredentialsForm {
|
const DEFAULT_RDP_CREDENTIALS: RdpCredentialsForm = {
|
||||||
try {
|
username: "",
|
||||||
const saved = localStorage.getItem(key);
|
password: "",
|
||||||
if (saved) return JSON.parse(saved) as RdpCredentialsForm;
|
domain: "",
|
||||||
} catch {
|
kdcProxyUrl: "",
|
||||||
// ignore
|
pcb: "",
|
||||||
}
|
enableClipboard: true
|
||||||
return {
|
};
|
||||||
username: "",
|
|
||||||
password: "",
|
|
||||||
domain: "",
|
|
||||||
kdcProxyUrl: "",
|
|
||||||
pcb: "",
|
|
||||||
enableClipboard: true
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
const isIronError = (error: unknown): error is IronError => {
|
const isIronError = (error: unknown): error is IronError => {
|
||||||
return (
|
return (
|
||||||
@@ -113,9 +109,25 @@ export default function RdpClient({
|
|||||||
|
|
||||||
const form = useForm<RdpCredentialsForm>({
|
const form = useForm<RdpCredentialsForm>({
|
||||||
resolver: zodResolver(formSchema),
|
resolver: zodResolver(formSchema),
|
||||||
defaultValues: loadStoredCredentials(STORAGE_KEY)
|
defaultValues: DEFAULT_RDP_CREDENTIALS
|
||||||
});
|
});
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
let cancelled = false;
|
||||||
|
|
||||||
|
void loadEncryptedLocalStorage<RdpCredentialsForm>(
|
||||||
|
STORAGE_KEY,
|
||||||
|
target?.authToken
|
||||||
|
).then((saved) => {
|
||||||
|
if (cancelled || !saved) return;
|
||||||
|
form.reset({ ...DEFAULT_RDP_CREDENTIALS, ...saved });
|
||||||
|
});
|
||||||
|
|
||||||
|
return () => {
|
||||||
|
cancelled = true;
|
||||||
|
};
|
||||||
|
}, [form, target?.authToken]);
|
||||||
|
|
||||||
const [showLogin, setShowLogin] = useState(true);
|
const [showLogin, setShowLogin] = useState(true);
|
||||||
const [moduleReady, setModuleReady] = useState(false);
|
const [moduleReady, setModuleReady] = useState(false);
|
||||||
const [connecting, setConnecting] = useState(false);
|
const [connecting, setConnecting] = useState(false);
|
||||||
@@ -293,11 +305,11 @@ export default function RdpClient({
|
|||||||
try {
|
try {
|
||||||
const sessionInfo = await userInteraction.connect(builder.build());
|
const sessionInfo = await userInteraction.connect(builder.build());
|
||||||
|
|
||||||
try {
|
void saveEncryptedLocalStorage(
|
||||||
localStorage.setItem(STORAGE_KEY, JSON.stringify(values));
|
STORAGE_KEY,
|
||||||
} catch {
|
values,
|
||||||
// ignore
|
target.authToken
|
||||||
}
|
);
|
||||||
setConnecting(false);
|
setConnecting(false);
|
||||||
setShowLogin(false);
|
setShowLogin(false);
|
||||||
userInteraction.setVisibility(true);
|
userInteraction.setVisibility(true);
|
||||||
|
|||||||
+33
-20
@@ -32,6 +32,10 @@ import { useTranslations } from "next-intl";
|
|||||||
import BrandedAuthSurface from "@app/components/BrandedAuthSurface";
|
import BrandedAuthSurface from "@app/components/BrandedAuthSurface";
|
||||||
import PoweredByPangolin from "@app/components/PoweredByPangolin";
|
import PoweredByPangolin from "@app/components/PoweredByPangolin";
|
||||||
import AuthPageFooterNotices from "@app/components/AuthPageFooterNotices";
|
import AuthPageFooterNotices from "@app/components/AuthPageFooterNotices";
|
||||||
|
import {
|
||||||
|
loadEncryptedLocalStorage,
|
||||||
|
saveEncryptedLocalStorage
|
||||||
|
} from "@app/lib/secureLocalStorage";
|
||||||
|
|
||||||
type AuthTab = "password" | "privateKey";
|
type AuthTab = "password" | "privateKey";
|
||||||
|
|
||||||
@@ -48,15 +52,11 @@ type ConnectCredentials = {
|
|||||||
certificate?: string;
|
certificate?: string;
|
||||||
};
|
};
|
||||||
|
|
||||||
function loadStoredCredentials(key: string): SshCredentialsForm {
|
const DEFAULT_SSH_CREDENTIALS: SshCredentialsForm = {
|
||||||
try {
|
username: "",
|
||||||
const saved = localStorage.getItem(key);
|
password: "",
|
||||||
if (saved) return JSON.parse(saved) as SshCredentialsForm;
|
privateKey: ""
|
||||||
} catch {
|
};
|
||||||
// ignore
|
|
||||||
}
|
|
||||||
return { username: "", password: "", privateKey: "" };
|
|
||||||
}
|
|
||||||
|
|
||||||
export default function SshClient({
|
export default function SshClient({
|
||||||
target,
|
target,
|
||||||
@@ -86,9 +86,25 @@ export default function SshClient({
|
|||||||
});
|
});
|
||||||
|
|
||||||
const form = useForm<SshCredentialsForm>({
|
const form = useForm<SshCredentialsForm>({
|
||||||
defaultValues: loadStoredCredentials(STORAGE_KEY)
|
defaultValues: DEFAULT_SSH_CREDENTIALS
|
||||||
});
|
});
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
let cancelled = false;
|
||||||
|
|
||||||
|
void loadEncryptedLocalStorage<SshCredentialsForm>(
|
||||||
|
STORAGE_KEY,
|
||||||
|
target?.authToken
|
||||||
|
).then((saved) => {
|
||||||
|
if (cancelled || !saved) return;
|
||||||
|
form.reset({ ...DEFAULT_SSH_CREDENTIALS, ...saved });
|
||||||
|
});
|
||||||
|
|
||||||
|
return () => {
|
||||||
|
cancelled = true;
|
||||||
|
};
|
||||||
|
}, [form, target?.authToken]);
|
||||||
|
|
||||||
function handleKeyFile(e: React.ChangeEvent<HTMLInputElement>) {
|
function handleKeyFile(e: React.ChangeEvent<HTMLInputElement>) {
|
||||||
const file = e.target.files?.[0];
|
const file = e.target.files?.[0];
|
||||||
if (!file) return;
|
if (!file) return;
|
||||||
@@ -252,14 +268,11 @@ export default function SshClient({
|
|||||||
})
|
})
|
||||||
);
|
);
|
||||||
if (!override) {
|
if (!override) {
|
||||||
try {
|
void saveEncryptedLocalStorage(
|
||||||
localStorage.setItem(
|
STORAGE_KEY,
|
||||||
STORAGE_KEY,
|
form.getValues(),
|
||||||
JSON.stringify(form.getValues())
|
target.authToken
|
||||||
);
|
);
|
||||||
} catch {
|
|
||||||
// ignore
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -625,7 +638,7 @@ export default function SshClient({
|
|||||||
|
|
||||||
{connected && (
|
{connected && (
|
||||||
<div className="fixed inset-0 z-50 flex flex-col bg-neutral-900">
|
<div className="fixed inset-0 z-50 flex flex-col bg-neutral-900">
|
||||||
<div className="flex flex-wrap items-center gap-2 bg-black p-2 text-white">
|
{/* <div className="flex flex-wrap items-center gap-2 bg-black p-2 text-white">
|
||||||
<Button
|
<Button
|
||||||
size="sm"
|
size="sm"
|
||||||
variant="destructive"
|
variant="destructive"
|
||||||
@@ -633,7 +646,7 @@ export default function SshClient({
|
|||||||
>
|
>
|
||||||
{t("sshTerminate")}
|
{t("sshTerminate")}
|
||||||
</Button>
|
</Button>
|
||||||
</div>
|
</div> */}
|
||||||
<div
|
<div
|
||||||
ref={terminalRef}
|
ref={terminalRef}
|
||||||
className="flex-1 overflow-hidden"
|
className="flex-1 overflow-hidden"
|
||||||
|
|||||||
+29
-15
@@ -28,20 +28,18 @@ import BrandedAuthSurface from "@app/components/BrandedAuthSurface";
|
|||||||
import PoweredByPangolin from "@app/components/PoweredByPangolin";
|
import PoweredByPangolin from "@app/components/PoweredByPangolin";
|
||||||
import AuthPageFooterNotices from "@app/components/AuthPageFooterNotices";
|
import AuthPageFooterNotices from "@app/components/AuthPageFooterNotices";
|
||||||
import { useTranslations } from "next-intl";
|
import { useTranslations } from "next-intl";
|
||||||
|
import {
|
||||||
|
loadEncryptedLocalStorage,
|
||||||
|
saveEncryptedLocalStorage
|
||||||
|
} from "@app/lib/secureLocalStorage";
|
||||||
|
|
||||||
type VncCredentialsForm = {
|
type VncCredentialsForm = {
|
||||||
password: string;
|
password: string;
|
||||||
};
|
};
|
||||||
|
|
||||||
function loadStoredCredentials(key: string): VncCredentialsForm {
|
const DEFAULT_VNC_CREDENTIALS: VncCredentialsForm = {
|
||||||
try {
|
password: ""
|
||||||
const saved = localStorage.getItem(key);
|
};
|
||||||
if (saved) return JSON.parse(saved) as VncCredentialsForm;
|
|
||||||
} catch {
|
|
||||||
// ignore
|
|
||||||
}
|
|
||||||
return { password: "" };
|
|
||||||
}
|
|
||||||
|
|
||||||
export default function VncClient({
|
export default function VncClient({
|
||||||
target,
|
target,
|
||||||
@@ -62,9 +60,25 @@ export default function VncClient({
|
|||||||
|
|
||||||
const form = useForm<VncCredentialsForm>({
|
const form = useForm<VncCredentialsForm>({
|
||||||
resolver: zodResolver(formSchema),
|
resolver: zodResolver(formSchema),
|
||||||
defaultValues: loadStoredCredentials(STORAGE_KEY)
|
defaultValues: DEFAULT_VNC_CREDENTIALS
|
||||||
});
|
});
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
let cancelled = false;
|
||||||
|
|
||||||
|
void loadEncryptedLocalStorage<VncCredentialsForm>(
|
||||||
|
STORAGE_KEY,
|
||||||
|
target?.authToken
|
||||||
|
).then((saved) => {
|
||||||
|
if (cancelled || !saved) return;
|
||||||
|
form.reset({ ...DEFAULT_VNC_CREDENTIALS, ...saved });
|
||||||
|
});
|
||||||
|
|
||||||
|
return () => {
|
||||||
|
cancelled = true;
|
||||||
|
};
|
||||||
|
}, [form, target?.authToken]);
|
||||||
|
|
||||||
const [connected, setConnected] = useState(false);
|
const [connected, setConnected] = useState(false);
|
||||||
const [connectError, setConnectError] = useState<string | null>(null);
|
const [connectError, setConnectError] = useState<string | null>(null);
|
||||||
const rfbRef = useRef<any>(null);
|
const rfbRef = useRef<any>(null);
|
||||||
@@ -132,11 +146,11 @@ export default function VncClient({
|
|||||||
rfb.resizeSession = true;
|
rfb.resizeSession = true;
|
||||||
|
|
||||||
rfb.addEventListener("connect", () => {
|
rfb.addEventListener("connect", () => {
|
||||||
try {
|
void saveEncryptedLocalStorage(
|
||||||
localStorage.setItem(STORAGE_KEY, JSON.stringify(values));
|
STORAGE_KEY,
|
||||||
} catch {
|
values,
|
||||||
// ignore
|
target.authToken
|
||||||
}
|
);
|
||||||
setConnected(true);
|
setConnected(true);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,124 @@
|
|||||||
|
type EncryptedStorageEnvelope = {
|
||||||
|
v: 1;
|
||||||
|
s: string;
|
||||||
|
i: string;
|
||||||
|
d: string;
|
||||||
|
};
|
||||||
|
|
||||||
|
const PBKDF2_ITERATIONS = 120000;
|
||||||
|
|
||||||
|
function toArrayBuffer(bytes: Uint8Array): ArrayBuffer {
|
||||||
|
return bytes.buffer.slice(
|
||||||
|
bytes.byteOffset,
|
||||||
|
bytes.byteOffset + bytes.byteLength
|
||||||
|
) as ArrayBuffer;
|
||||||
|
}
|
||||||
|
|
||||||
|
function bytesToBase64(bytes: Uint8Array): string {
|
||||||
|
let binary = "";
|
||||||
|
for (const byte of bytes) {
|
||||||
|
binary += String.fromCharCode(byte);
|
||||||
|
}
|
||||||
|
return btoa(binary);
|
||||||
|
}
|
||||||
|
|
||||||
|
function base64ToBytes(value: string): Uint8Array {
|
||||||
|
const binary = atob(value);
|
||||||
|
const bytes = new Uint8Array(binary.length);
|
||||||
|
for (let i = 0; i < binary.length; i++) {
|
||||||
|
bytes[i] = binary.charCodeAt(i);
|
||||||
|
}
|
||||||
|
return bytes;
|
||||||
|
}
|
||||||
|
|
||||||
|
async function deriveKey(authToken: string, salt: ArrayBuffer) {
|
||||||
|
const subtle = window.crypto?.subtle;
|
||||||
|
if (!subtle) {
|
||||||
|
throw new Error("Web Crypto is unavailable");
|
||||||
|
}
|
||||||
|
|
||||||
|
const tokenKey = await subtle.importKey(
|
||||||
|
"raw",
|
||||||
|
toArrayBuffer(new TextEncoder().encode(authToken)),
|
||||||
|
"PBKDF2",
|
||||||
|
false,
|
||||||
|
["deriveKey"]
|
||||||
|
);
|
||||||
|
|
||||||
|
return subtle.deriveKey(
|
||||||
|
{
|
||||||
|
name: "PBKDF2",
|
||||||
|
salt,
|
||||||
|
iterations: PBKDF2_ITERATIONS,
|
||||||
|
hash: "SHA-256"
|
||||||
|
},
|
||||||
|
tokenKey,
|
||||||
|
{ name: "AES-GCM", length: 256 },
|
||||||
|
false,
|
||||||
|
["encrypt", "decrypt"]
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function saveEncryptedLocalStorage<T>(
|
||||||
|
storageKey: string,
|
||||||
|
value: T,
|
||||||
|
authToken: string | null | undefined
|
||||||
|
) {
|
||||||
|
if (typeof window === "undefined") return;
|
||||||
|
if (!authToken) {
|
||||||
|
window.localStorage.removeItem(storageKey);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
const salt = window.crypto.getRandomValues(new Uint8Array(16));
|
||||||
|
const iv = window.crypto.getRandomValues(new Uint8Array(12));
|
||||||
|
const key = await deriveKey(authToken, toArrayBuffer(salt));
|
||||||
|
const plaintext = new TextEncoder().encode(JSON.stringify(value));
|
||||||
|
const encrypted = await window.crypto.subtle.encrypt(
|
||||||
|
{ name: "AES-GCM", iv: toArrayBuffer(iv) },
|
||||||
|
key,
|
||||||
|
toArrayBuffer(plaintext)
|
||||||
|
);
|
||||||
|
|
||||||
|
const payload: EncryptedStorageEnvelope = {
|
||||||
|
v: 1,
|
||||||
|
s: bytesToBase64(salt),
|
||||||
|
i: bytesToBase64(iv),
|
||||||
|
d: bytesToBase64(new Uint8Array(encrypted))
|
||||||
|
};
|
||||||
|
|
||||||
|
window.localStorage.setItem(storageKey, JSON.stringify(payload));
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function loadEncryptedLocalStorage<T>(
|
||||||
|
storageKey: string,
|
||||||
|
authToken: string | null | undefined
|
||||||
|
): Promise<T | null> {
|
||||||
|
if (typeof window === "undefined") return null;
|
||||||
|
if (!authToken) return null;
|
||||||
|
|
||||||
|
const raw = window.localStorage.getItem(storageKey);
|
||||||
|
if (!raw) return null;
|
||||||
|
|
||||||
|
try {
|
||||||
|
const payload = JSON.parse(raw) as EncryptedStorageEnvelope;
|
||||||
|
if (payload.v !== 1 || !payload.s || !payload.i || !payload.d) {
|
||||||
|
throw new Error("Invalid encrypted payload");
|
||||||
|
}
|
||||||
|
|
||||||
|
const salt = base64ToBytes(payload.s);
|
||||||
|
const iv = base64ToBytes(payload.i);
|
||||||
|
const data = base64ToBytes(payload.d);
|
||||||
|
const key = await deriveKey(authToken, toArrayBuffer(salt));
|
||||||
|
const decrypted = await window.crypto.subtle.decrypt(
|
||||||
|
{ name: "AES-GCM", iv: toArrayBuffer(iv) },
|
||||||
|
key,
|
||||||
|
toArrayBuffer(data)
|
||||||
|
);
|
||||||
|
const json = new TextDecoder().decode(decrypted);
|
||||||
|
return JSON.parse(json) as T;
|
||||||
|
} catch {
|
||||||
|
window.localStorage.removeItem(storageKey);
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user