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

253 lines
9.6 KiB
Dart
Raw Normal View History

2021-01-06 17:35:57 +00:00
import 'dart:convert';
import 'package:flutter/services.dart';
2021-01-06 17:35:57 +00:00
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/backups_credential.dart';
import 'package:selfprivacy/logic/models/hive/dns_provider_credential.dart';
import 'package:selfprivacy/logic/models/hive/server.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/server_provider_credential.dart';
import 'package:selfprivacy/logic/models/hive/user.dart';
import 'package:selfprivacy/logic/models/hive/wizards_data/server_installation_wizard_data.dart';
import 'package:selfprivacy/utils/platform_adapter.dart';
2021-01-06 17:35:57 +00:00
class HiveConfig {
static Future<void> init() async {
final String? storagePath = PlatformAdapter.storagePath;
print('HiveConfig: Custom storage path: $storagePath');
await Hive.initFlutter(storagePath);
2021-01-06 17:35:57 +00:00
Hive.registerAdapter(UserAdapter());
Hive.registerAdapter(ServerHostingDetailsAdapter());
Hive.registerAdapter(ServerDomainAdapter());
Hive.registerAdapter(BackupsCredentialAdapter());
Hive.registerAdapter(ServerProviderVolumeAdapter());
Hive.registerAdapter(BackblazeBucketAdapter());
Hive.registerAdapter(ServerProviderCredentialAdapter());
Hive.registerAdapter(DnsProviderCredentialAdapter());
Hive.registerAdapter(ServerAdapter());
Hive.registerAdapter(DnsProviderTypeAdapter());
Hive.registerAdapter(ServerProviderTypeAdapter());
Hive.registerAdapter(UserTypeAdapter());
2023-06-29 09:52:09 +00:00
Hive.registerAdapter(BackupsProviderTypeAdapter());
Hive.registerAdapter(ServerInstallationWizardDataAdapter());
await Hive.openBox(BNames.appSettingsBox);
2021-07-29 05:24:42 +00:00
try {
final HiveAesCipher cipher = HiveAesCipher(
await getEncryptedKey(BNames.serverInstallationEncryptionKey),
);
2024-06-25 15:06:50 +00:00
await Hive.openBox(BNames.serverInstallationBox,
encryptionCipher: cipher);
await Hive.openBox(BNames.resourcesBox, encryptionCipher: cipher);
await Hive.openBox(BNames.wizardDataBox, encryptionCipher: cipher);
final Box resourcesBox = Hive.box(BNames.resourcesBox);
if (resourcesBox.isEmpty) {
2024-06-25 15:06:50 +00:00
final Box serverInstallationBox =
Hive.box(BNames.serverInstallationBox);
final String? serverProviderKey =
2024-06-25 15:06:50 +00:00
serverInstallationBox.get(BNames.hetznerKey);
final String? serverLocation =
2024-06-25 15:06:50 +00:00
serverInstallationBox.get(BNames.serverLocation);
final String? dnsProviderKey =
2024-06-25 15:06:50 +00:00
serverInstallationBox.get(BNames.cloudFlareKey);
final BackupsCredential? backblazeCredential =
2024-06-25 15:06:50 +00:00
serverInstallationBox.get(BNames.backblazeCredential);
final ServerDomain? serverDomain =
2024-06-25 15:06:50 +00:00
serverInstallationBox.get(BNames.serverDomain);
final ServerHostingDetails? serverDetails =
2024-06-25 15:06:50 +00:00
serverInstallationBox.get(BNames.serverDetails);
final BackblazeBucket? backblazeBucket =
2024-06-25 15:06:50 +00:00
serverInstallationBox.get(BNames.backblazeBucket);
final String? serverType =
2024-06-25 15:06:50 +00:00
serverInstallationBox.get(BNames.serverTypeIdentifier);
final ServerProviderType? serverProvider =
2024-06-25 15:06:50 +00:00
serverInstallationBox.get(BNames.serverProvider);
final DnsProviderType? dnsProvider =
2024-06-25 15:06:50 +00:00
serverInstallationBox.get(BNames.dnsProvider);
if (serverProviderKey != null &&
(serverProvider != null ||
(serverDetails != null &&
serverDetails.provider != ServerProviderType.unknown))) {
final ServerProviderCredential serverProviderCredential =
2024-06-25 15:06:50 +00:00
ServerProviderCredential(
tokenId: null,
token: serverProviderKey,
provider: serverProvider ?? serverDetails!.provider,
2024-06-25 15:06:50 +00:00
associatedServerIds:
serverDetails != null ? [serverDetails.id] : [],
);
await resourcesBox
.put(BNames.serverProviderTokens, [serverProviderCredential]);
}
if (dnsProviderKey != null &&
(dnsProvider != null ||
(serverDomain != null &&
serverDomain.provider != DnsProviderType.unknown))) {
final DnsProviderCredential dnsProviderCredential =
2024-06-25 15:06:50 +00:00
DnsProviderCredential(
tokenId: null,
token: dnsProviderKey,
provider: dnsProvider ?? serverDomain!.provider,
associatedDomainNames:
2024-06-25 15:06:50 +00:00
serverDomain != null ? [serverDomain.domainName] : [],
);
await resourcesBox
.put(BNames.dnsProviderTokens, [dnsProviderCredential]);
}
if (backblazeCredential != null) {
await resourcesBox
.put(BNames.backupsProviderTokens, [backblazeCredential]);
}
if (backblazeBucket != null) {
await resourcesBox.put(BNames.backblazeBucket, backblazeBucket);
}
if (serverDetails != null && serverDomain != null) {
await resourcesBox.put(BNames.servers, [
Server(
domain: serverDomain,
hostingDetails: serverDetails.copyWith(
serverLocation: serverLocation,
serverType: serverType,
),
),
]);
}
}
} on PlatformException catch (e) {
print('HiveConfig: Error while opening boxes: $e');
rethrow;
}
2021-01-06 17:35:57 +00:00
}
2022-06-05 19:36:32 +00:00
static Future<Uint8List> getEncryptedKey(final String encKey) async {
const FlutterSecureStorage secureStorage = FlutterSecureStorage();
try {
final bool hasEncryptionKey =
await secureStorage.containsKey(key: encKey);
if (!hasEncryptionKey) {
final List<int> key = Hive.generateSecureKey();
await secureStorage.write(key: encKey, value: base64UrlEncode(key));
}
2021-01-06 17:35:57 +00:00
final String? string = await secureStorage.read(key: encKey);
return base64Url.decode(string!);
} on PlatformException catch (e) {
print('HiveConfig: Error while getting encryption key: $e');
rethrow;
2021-01-06 17:35:57 +00:00
}
}
}
/// Mappings for the different boxes and their keys
2021-01-06 17:35:57 +00:00
class BNames {
/// App settings box. Contains app settings like [darkThemeModeOn], [shouldShowOnboarding]
static String appSettingsBox = 'appSettings';
/// A boolean field of [appSettingsBox] box.
static String darkThemeModeOn = 'isDarkModeOn';
2021-01-06 17:35:57 +00:00
2023-02-23 14:41:45 +00:00
/// A boolean field of [appSettingsBox] box.
static String systemThemeModeOn = 'isAutoDarkModeOn';
2023-02-23 14:41:45 +00:00
/// A boolean field of [appSettingsBox] box.
static String shouldShowOnboarding = 'isOnboardingShowing';
/// A string field
static String appLocale = 'appLocale';
2021-01-06 17:35:57 +00:00
2024-04-12 12:41:32 +00:00
/// Encryption key to decrypt [serverInstallationBox] box.
static String serverInstallationEncryptionKey = 'key';
2021-01-06 17:35:57 +00: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 17:35:57 +00:00
static String hetznerKey = 'hetznerKey';
/// A String field of [serverInstallationBox] box.
static String serverProvider = 'serverProvider';
/// A String field of [serverInstallationBox] box.
static String dnsProvider = 'dnsProvider';
/// A String field of [serverLocation] box.
static String serverLocation = 'serverLocation';
/// A String field of [serverInstallationBox] box.
2021-01-06 17:35:57 +00:00
static String cloudFlareKey = 'cloudFlareKey';
/// A String field of [serverInstallationBox] box.
static String serverTypeIdentifier = 'serverTypeIdentifier';
/// A [User] field of [serverInstallationBox] box.
2021-01-06 17:35:57 +00:00
static String rootUser = 'rootUser';
/// A [ServerHostingDetails] field of [serverInstallationBox] box.
static String serverDetails = 'hetznerServer';
/// A [BackupsCredential] field of [serverInstallationBox] box.
static String backblazeCredential = 'backblazeKey';
/// A [BackblazeBucket] field of [serverInstallationBox] box.
2021-12-06 18:31:19 +00:00
static String backblazeBucket = 'backblazeBucket';
/// A boolean field of [serverInstallationBox] box.
2021-02-16 18:48:15 +00:00
static String isLoading = 'isLoading';
/// A boolean field of [serverInstallationBox] box.
2021-03-31 11:37:39 +00:00
static String isServerResetedFirstTime = 'isServerResetedFirstTime';
/// A boolean field of [serverInstallationBox] box.
2021-03-31 11:37:39 +00:00
static String isServerResetedSecondTime = 'isServerResetedSecondTime';
2022-05-30 23:06:08 +00:00
/// A boolean field of [serverInstallationBox] box.
static String isRecoveringServer = 'isRecoveringServer';
/// Resources and provider tokens box,
static String resourcesBox = 'resourcesBox';
/// Server Provider Tokens of [resourcesBox] box.
static String serverProviderTokens = 'serverProviderTokens';
/// DNS Provider Tokens of [resourcesBox] box.
static String dnsProviderTokens = 'dnsProviderTokens';
/// Backups Provider Tokens of [resourcesBox] box.
static String backupsProviderTokens = 'backupsProviderTokens';
/// Servers of [resourcesBox] box.
static String servers = 'servers';
/// Wizard data box
static String wizardDataBox = 'wizardDataBox';
/// Server installation wizard data of [wizardDataBox] box.
static String serverInstallationWizardData = 'serverInstallationWizardData';
2021-01-06 17:35:57 +00:00
}