selfprivacy.org.app/lib/config/hive_config.dart

122 lines
4.4 KiB
Dart
Raw Normal View History

2021-01-06 19:35:57 +02:00
import 'dart:convert';
import 'dart:typed_data';
import 'package:flutter_secure_storage/flutter_secure_storage.dart';
import 'package:hive_flutter/hive_flutter.dart';
import 'package:selfprivacy/logic/models/hive/backblaze_bucket.dart';
import 'package:selfprivacy/logic/models/hive/backblaze_credential.dart';
import 'package:selfprivacy/logic/models/hive/server_details.dart';
import 'package:selfprivacy/logic/models/hive/server_domain.dart';
import 'package:selfprivacy/logic/models/hive/user.dart';
2021-01-06 19:35:57 +02:00
class HiveConfig {
static Future<void> init() async {
await Hive.initFlutter();
Hive.registerAdapter(UserAdapter());
Hive.registerAdapter(ServerHostingDetailsAdapter());
Hive.registerAdapter(ServerDomainAdapter());
2021-02-03 22:26:38 +02:00
Hive.registerAdapter(BackblazeCredentialAdapter());
2021-12-06 20:31:19 +02:00
Hive.registerAdapter(BackblazeBucketAdapter());
Hive.registerAdapter(ServerVolumeAdapter());
2021-01-06 19:35:57 +02:00
Hive.registerAdapter(DnsProviderAdapter());
Hive.registerAdapter(ServerProviderAdapter());
await Hive.openBox(BNames.appSettingsBox);
2021-07-29 08:24:42 +03:00
2022-06-05 22:36:32 +03:00
final HiveAesCipher cipher = HiveAesCipher(
2022-06-10 00:13:06 +03:00
await getEncryptedKey(BNames.serverInstallationEncryptionKey),
);
2021-09-15 16:15:54 +03:00
await Hive.openBox<User>(BNames.usersDeprecated);
await Hive.openBox<User>(BNames.usersBox, encryptionCipher: cipher);
2022-06-05 22:36:32 +03:00
final Box<User> deprecatedUsers = Hive.box<User>(BNames.usersDeprecated);
if (deprecatedUsers.isNotEmpty) {
2022-06-05 22:36:32 +03:00
final Box<User> users = Hive.box<User>(BNames.usersBox);
users.addAll(deprecatedUsers.values.toList());
deprecatedUsers.clear();
}
await Hive.openBox(BNames.serverInstallationBox, encryptionCipher: cipher);
2021-01-06 19:35:57 +02:00
}
2022-06-05 22:36:32 +03:00
static Future<Uint8List> getEncryptedKey(final String encKey) async {
const FlutterSecureStorage secureStorage = FlutterSecureStorage();
final bool hasEncryptionKey = await secureStorage.containsKey(key: encKey);
2021-08-29 12:50:24 +03:00
if (!hasEncryptionKey) {
2022-06-05 22:36:32 +03:00
final List<int> key = Hive.generateSecureKey();
2021-09-02 22:32:07 +03:00
await secureStorage.write(key: encKey, value: base64UrlEncode(key));
2021-01-06 19:35:57 +02:00
}
2022-06-05 22:36:32 +03:00
final String? string = await secureStorage.read(key: encKey);
2021-03-15 17:39:44 +02:00
return base64Url.decode(string!);
2021-01-06 19:35:57 +02:00
}
}
/// Mappings for the different boxes and their keys
2021-01-06 19:35:57 +02:00
class BNames {
/// App settings box. Contains app settings like [isDarkModeOn], [isOnboardingShowing]
static String appSettingsBox = 'appSettings';
/// A boolean field of [appSettingsBox] box.
2021-01-06 19:35:57 +02:00
static String isDarkModeOn = 'isDarkModeOn';
/// A boolean field of [appSettingsBox] box.
static String isOnboardingShowing = 'isOnboardingShowing';
2021-01-06 19:35:57 +02:00
/// Encryption key to decrypt [serverInstallationBox] and [usersBox] box.
static String serverInstallationEncryptionKey = 'key';
2021-01-06 19:35:57 +02:00
/// Server installation box. Contains server details and provider tokens.
static String serverInstallationBox = 'appConfig';
/// A List<String> field of [serverInstallationBox] box.
static String rootKeys = 'rootKeys';
/// A boolean field of [serverInstallationBox] box.
static String hasFinalChecked = 'hasFinalChecked';
/// A boolean field of [serverInstallationBox] box.
static String isServerStarted = 'isServerStarted';
/// A [ServerDomain] field of [serverInstallationBox] box.
static String serverDomain = 'cloudFlareDomain';
/// A String field of [serverInstallationBox] box.
2021-01-06 19:35:57 +02:00
static String hetznerKey = 'hetznerKey';
/// A String field of [serverInstallationBox] box.
2021-01-06 19:35:57 +02:00
static String cloudFlareKey = 'cloudFlareKey';
/// A [User] field of [serverInstallationBox] box.
2021-01-06 19:35:57 +02:00
static String rootUser = 'rootUser';
/// A [ServerHostingDetails] field of [serverInstallationBox] box.
static String serverDetails = 'hetznerServer';
/// A [BackblazeCredential] field of [serverInstallationBox] box.
static String backblazeCredential = 'backblazeKey';
/// A [BackblazeBucket] field of [serverInstallationBox] box.
2021-12-06 20:31:19 +02:00
static String backblazeBucket = 'backblazeBucket';
/// A boolean field of [serverInstallationBox] box.
2021-02-16 20:48:15 +02:00
static String isLoading = 'isLoading';
/// A boolean field of [serverInstallationBox] box.
2021-03-31 14:37:39 +03:00
static String isServerResetedFirstTime = 'isServerResetedFirstTime';
/// A boolean field of [serverInstallationBox] box.
2021-03-31 14:37:39 +03:00
static String isServerResetedSecondTime = 'isServerResetedSecondTime';
2022-05-31 02:06:08 +03:00
/// A boolean field of [serverInstallationBox] box.
static String isRecoveringServer = 'isRecoveringServer';
/// Deprecated users box as it is unencrypted
static String usersDeprecated = 'users';
/// Box with users
static String usersBox = 'usersEncrypted';
2021-01-06 19:35:57 +02:00
}