added encryption

This commit is contained in:
2026-03-09 10:54:07 +05:30
parent 6e184dc590
commit 6720e28d08
27 changed files with 2093 additions and 709 deletions

View File

@@ -107,28 +107,66 @@ _Last updated: 2026-03-04_
- Entry filtering by date
- Pagination support
### Frontend-Backend Integration (Completed)
### Zero-Knowledge Encryption Implementation (Completed)
**API Service Layer** — Created `src/lib/api.ts` with all backend calls
**AuthContext Updated** — Now syncs users with MongoDB on login
**Crypto Module** — Created `src/lib/crypto.ts` with complete zero-knowledge privacy
- Auto-registers new users in MongoDB
- Fetches existing user profiles
- Provides `userId` (MongoDB ID) to all pages
**HomePage** Entry creation via POST `/api/entries/{userId}`
- Save with success/error feedback
- Clears form after save
**HistoryPage** — Fetches entries via GET `/api/entries/{userId}`
- Calendar shows days with entries
- Lists recent entries with timestamps
- Filters by current month
**SettingsPage** — Updates user settings via PUT `/api/users/update/{userId}`
- Theme selector (light/dark) with MongoDB persistence
- Profile info from Firebase
- Libsodium.js integrated for cryptography (XSalsa20-Poly1305)
- Key derivation from Firebase credentials using Argon2i KDF
- Device key generation and localStorage persistence
- Encrypted secret key storage in IndexedDB
- Entry encryption/decryption utilities
**Key Management Flow**
- **Login:** KDF derives master key from `firebaseUID + firebaseIDToken + salt`
- **Device Setup:** Random device key generated, stored in localStorage
- **Key Cache:** Master key encrypted with device key → IndexedDB
- **Memory:** Master key kept in memory during session only
- **Subsequent Login:** Cached encrypted key recovered via device key
- **New Device:** Full KDF derivation, new device key generated
- **Logout:** Master key cleared from memory; device key persists for next session
**AuthContext Enhanced**
- Added `secretKey` state (in-memory only)
- Integrated encryption initialization on login
- Device key and IndexedDB cache management
- Automatic recovery of cached keys on same device
**Backend Models Updated** — Zero-knowledge storage
- `JournalEntryCreate`: title/content optional (null if encrypted)
- `EncryptionMetadata`: stores ciphertext, nonce, algorithm
- Server stores **encryption metadata only**, never plaintext
- All entries encrypted with XSalsa20-Poly1305 (libsodium)
**API Routes** — Encrypted entry flow
- POST `/api/entries/{userId}` accepts encrypted entries
- Validation ensures ciphertext and nonce present
- Entry retrieval returns full encryption metadata
- Update routes support re-encryption
- Server processes only encrypted data
**HomePage** — Encrypted entry creation
- Entry and title combined: `title\n\n{entry}`
- Encrypted with master key before transmission
- Sends ciphertext, nonce, algorithm metadata to backend
- Success feedback confirms secure storage
**HistoryPage** — Entry decryption & display
- Fetches encrypted entries from server
- Client-side decryption with master key
- Splits decrypted content: first line = title
- Graceful handling of decryption failures
- Displays original title or `[Encrypted]` on error
### Next Steps (Implementation)
🔄 Add entry detail view / edit functionality
🔄 Firebase token verification in backend middleware
🔄 Search/filter entries by date range
🔄 Client-side encryption for entries
🔄 Entry detail view with full decryption
🔄 Edit encrypted entries (re-encrypt on changes)
🔄 Search/filter encrypted entries (client-side only)
🔄 Export/backup encrypted entries with device key