diff options
Diffstat (limited to 'src/routes/(app)/settings')
| -rw-r--r-- | src/routes/(app)/settings/+layout.svelte | 20 | ||||
| -rw-r--r-- | src/routes/(app)/settings/security/+page.svelte | 19 |
2 files changed, 34 insertions, 5 deletions
diff --git a/src/routes/(app)/settings/+layout.svelte b/src/routes/(app)/settings/+layout.svelte index ce45f915..99bdc4ce 100644 --- a/src/routes/(app)/settings/+layout.svelte +++ b/src/routes/(app)/settings/+layout.svelte @@ -13,6 +13,8 @@ import { sign, send, getPrivateKey } from "$lib/nostr"; import { invalidateAll } from "$app/navigation"; import { bytesToHex } from "@noble/hashes/utils.js"; + import { getCachedPrfKey } from "$lib/passwordCache"; + import { wrapCanonicalKey, saveEncryptedKeys, passwordMethodId } from "$lib/keyWrapping"; import { SimplePool } from "nostr-tools/pool"; import { finalizeEvent } from "nostr-tools/pure"; @@ -201,14 +203,21 @@ } } - // If password is changing, derive new authPubkey + // If password is changing, derive new authPubkey and re-wrap canonical key const pw = body.get("password") as string; const un = (body.get("username") as string) || user.username; + let pendingEncryptedKeys: Record<string, string> | null = null; if (pw && pw === body.get("confirm")) { try { const { deriveAuthKeypair } = await import("$lib/deriveAuthKey"); - const { pubkey: authPubkey } = await deriveAuthKeypair(un, pw); + const { sk, pubkey: authPubkey } = await deriveAuthKeypair(un, pw); body.set("authPubkey", authPubkey); + + const canonicalKey = getCachedPrfKey(); + if (canonicalKey) { + const wrapped = await wrapCanonicalKey(sk.buffer as ArrayBuffer, canonicalKey); + pendingEncryptedKeys = { ...(user.encryptedKeys || {}), [passwordMethodId()]: wrapped }; + } } catch (e) { console.log("Failed to derive authPubkey for password change", e); } @@ -222,6 +231,13 @@ const result = deserialize(await response.text()); if (result.type === "success") { + if (pendingEncryptedKeys) { + try { + await saveEncryptedKeys(pendingEncryptedKeys); + } catch (e) { + console.log("Failed to save re-wrapped encryptedKeys", e); + } + } await invalidateAll(); if (pw) $password = pw; } diff --git a/src/routes/(app)/settings/security/+page.svelte b/src/routes/(app)/settings/security/+page.svelte index 0991c679..9ab16ded 100644 --- a/src/routes/(app)/settings/security/+page.svelte +++ b/src/routes/(app)/settings/security/+page.svelte @@ -9,7 +9,12 @@ import { save, pin as current } from "$lib/store"; import { getNsec } from "$lib/nostr"; import { registerPasskey } from "$lib/passkey"; - import { rememberPrfKey, defaultRememberForMs } from "$lib/passwordCache"; + import { getCachedPrfKey, rememberPrfKey, defaultRememberForMs } from "$lib/passwordCache"; + import { + wrapCanonicalKey, + saveEncryptedKeys, + passkeyMethodId, + } from "$lib/keyWrapping"; import { invalidate } from "$app/navigation"; import { page } from "$app/stores"; @@ -155,8 +160,16 @@ let addPasskey = async () => { passkeyLoading = true; try { - const prfKey = await registerPasskey(); - rememberPrfKey(prfKey, defaultRememberForMs); + const { prfKey, credentialId } = await registerPasskey(); + const canonicalKey = getCachedPrfKey(); + if (canonicalKey) { + const wrapped = await wrapCanonicalKey(prfKey, canonicalKey); + const updated = { ...(user.encryptedKeys || {}), [passkeyMethodId(credentialId)]: wrapped }; + await saveEncryptedKeys(updated); + user.encryptedKeys = updated; + } else { + rememberPrfKey(prfKey, defaultRememberForMs); + } success("Passkey added"); } catch (e: any) { if (e.name !== "NotAllowedError") { |
