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