feat: implement Hive service for encrypted local data storage, user management, and playlist configuration.

This commit is contained in:
Michael committed 2025-12-05 11:29:58 +01:00
1 parent 35667e1d24
commit 154e2b2246
1 file changed
+8 -24
+8 -24
View File
@@ -1,6 +1,5 @@
import 'dart:convert'; import 'dart:convert';
import 'package:crypto/crypto.dart'; import 'package:crypto/crypto.dart';
import 'package:flutter_secure_storage/flutter_secure_storage.dart';
import 'package:hive_flutter/hive_flutter.dart'; import 'package:hive_flutter/hive_flutter.dart';
import 'package:uuid/uuid.dart'; import 'package:uuid/uuid.dart';
import '../models/app_user.dart'; import '../models/app_user.dart';
@@ -9,10 +8,9 @@ import '../models/playlist_config.dart';
class HiveService { class HiveService {
static const String _usersBoxName = 'users'; static const String _usersBoxName = 'users';
static const String _playlistsBoxName = 'playlists'; static const String _playlistsBoxName = 'playlists';
static const String _encryptionKeyName = 'hive_encryption_key';
static const _storage = FlutterSecureStorage();
static bool _initialized = false; static bool _initialized = false;
static List<int>? _encryptionKey;
/// Initialize Hive for Web with encryption /// Initialize Hive for Web with encryption
static Future<void> init() async { static Future<void> init() async {
@@ -45,32 +43,18 @@ class HiveService {
_initialized = true; _initialized = true;
} }
/// Get or create 256-bit encryption key /// Get or create 256-bit encryption key (web-compatible)
static Future<List<int>> _getOrCreateEncryptionKey() async { static Future<List<int>> _getOrCreateEncryptionKey() async {
try { // For web: use session-based key (regenerated each session)
final existingKey = await _storage.read(key: _encryptionKeyName); // Note: Data persists in IndexedDB but encryption key is not stored
if (_encryptionKey != null) {
if (existingKey != null) { return _encryptionKey!;
return base64Decode(existingKey);
}
} catch (e) {
// Storage might not be available in some web contexts
// Fall back to session-based key (not persisted)
} }
// Generate new 256-bit key // Generate new 256-bit key
final key = Hive.generateSecureKey(); _encryptionKey = Hive.generateSecureKey();
try { return _encryptionKey!;
await _storage.write(
key: _encryptionKeyName,
value: base64Encode(key),
);
} catch (e) {
// Ignore if storage fails - key will be regenerated next session
}
return key;
} }
/// Seed default admin user (admin/admin) /// Seed default admin user (admin/admin)