feat(crypto): stronghold persistence + passphrase device-key backup/restore
Release desktop app / build (, ubuntu-22.04) (push) Has been cancelled
Release desktop app / build (, windows-latest) (push) Has been cancelled
Release desktop app / build (--target aarch64-apple-darwin --bundles app,updater, macos-14) (push) Has been cancelled
Release desktop app / build (--target x86_64-apple-darwin --bundles app,updater, macos-13) (push) Has been cancelled

This commit is contained in:
2026-04-19 18:41:35 +02:00
parent 3c2579b3ed
commit 0d94b684bf
7 changed files with 423 additions and 5 deletions
+52 -4
View File
@@ -1,12 +1,21 @@
import { base64FromBytes, bytesFromBase64, type SecretStore } from '@chat-app/shared/auth';
// M1 dev-only impl: persists secrets as base64 in localStorage.
// Swap this out for a tauri-plugin-stronghold implementation before release.
// The SecretStore interface stays identical so callers won't notice.
import { isTauriRuntime } from './globalShortcut';
import { makeStrongholdStore, migrateLocalStorageToStronghold } from './strongholdStore';
// Secret store with two backends:
// - Tauri: Stronghold-encrypted vault file in appLocalDataDir. Survives app
// reinstalls and is encrypted at rest with a password derived from the
// authenticated user-id.
// - Web / pre-auth: localStorage (legacy dev fallback).
//
// Callers don't need to care which one is active — they import a singleton
// and call setSecretStoreUser(userId) once the session is known. Until that
// happens, calls fall through to localStorage.
const PREFIX = 'chatapp.secret:';
export const devLocalSecretStore: SecretStore = {
const localStore: SecretStore = {
async getSecret(key: string): Promise<Uint8Array | null> {
const raw = window.localStorage.getItem(PREFIX + key);
if (!raw) return null;
@@ -20,3 +29,42 @@ export const devLocalSecretStore: SecretStore = {
window.localStorage.removeItem(PREFIX + key);
},
};
let activeBackend: SecretStore = localStore;
let activeUserId: string | null = null;
export async function setSecretStoreUser(userId: string | null): Promise<void> {
if (userId === activeUserId) return;
activeUserId = userId;
if (userId && isTauriRuntime()) {
const stronghold = makeStrongholdStore(userId);
activeBackend = stronghold;
try {
await migrateLocalStorageToStronghold(userId, PREFIX);
} catch (err: unknown) {
console.warn('stronghold migration failed', err);
}
} else {
activeBackend = localStore;
}
}
// Singleton with stable identity — internals delegate to whichever backend is
// currently active. Existing call-sites that imported `devLocalSecretStore`
// keep working without changes.
export const devLocalSecretStore: SecretStore = {
async getSecret(key) {
return activeBackend.getSecret(key);
},
async setSecret(key, value) {
return activeBackend.setSecret(key, value);
},
async removeSecret(key) {
return activeBackend.removeSecret(key);
},
};
export function isStrongholdActive(): boolean {
return activeBackend !== localStore;
}