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

75 lines
2.9 KiB
Dart
Raw Normal View History

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';
2021-12-06 18:31:19 +00:00
import 'package:selfprivacy/logic/models/backblaze_bucket.dart';
2021-02-03 20:26:38 +00:00
import 'package:selfprivacy/logic/models/backblaze_credential.dart';
2021-01-06 17:35:57 +00:00
import 'package:selfprivacy/logic/models/cloudflare_domain.dart';
import 'package:selfprivacy/logic/models/server_details.dart';
import 'package:selfprivacy/logic/models/user.dart';
class HiveConfig {
static Future<void> init() async {
await Hive.initFlutter();
Hive.registerAdapter(UserAdapter());
Hive.registerAdapter(HetznerServerDetailsAdapter());
Hive.registerAdapter(CloudFlareDomainAdapter());
2021-02-03 20:26:38 +00:00
Hive.registerAdapter(BackblazeCredentialAdapter());
2021-12-06 18:31:19 +00:00
Hive.registerAdapter(BackblazeBucketAdapter());
2021-03-30 17:38:40 +00:00
Hive.registerAdapter(HetznerDataBaseAdapter());
2021-01-06 17:35:57 +00:00
await Hive.openBox(BNames.appSettings);
2021-07-29 05:24:42 +00:00
await Hive.openBox<User>(BNames.users);
2021-08-29 13:54:28 +00:00
await Hive.openBox(BNames.servicesState);
2021-07-29 05:24:42 +00:00
var cipher = HiveAesCipher(await getEncryptedKey(BNames.key));
2021-01-06 17:35:57 +00:00
await Hive.openBox(BNames.appConfig, encryptionCipher: cipher);
2021-09-15 13:15:54 +00:00
var sshCipher = HiveAesCipher(await getEncryptedKey(BNames.sshEnckey));
2021-09-02 19:32:07 +00:00
await Hive.openBox(BNames.sshConfig, encryptionCipher: sshCipher);
2021-01-06 17:35:57 +00:00
}
static Future<Uint8List> getEncryptedKey(String encKey) async {
2021-08-29 09:50:24 +00:00
final secureStorage = FlutterSecureStorage();
2021-09-02 19:32:07 +00:00
var hasEncryptionKey = await secureStorage.containsKey(key: encKey);
2021-08-29 09:50:24 +00:00
if (!hasEncryptionKey) {
2021-01-06 17:35:57 +00:00
var 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
}
2021-09-02 19:32:07 +00:00
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
}
}
class BNames {
static String appConfig = 'appConfig';
static String isDarkModeOn = 'isDarkModeOn';
static String isOnbordingShowing = 'isOnbordingShowing';
2021-07-29 05:24:42 +00:00
static String users = 'users';
static String rootKeys = 'rootKeys';
2021-01-06 17:35:57 +00:00
static String appSettings = 'appSettings';
2021-08-29 13:54:28 +00:00
static String servicesState = 'servicesState';
2021-01-06 17:35:57 +00:00
static String key = 'key';
2021-09-02 19:32:07 +00:00
static String sshEnckey = 'sshEngkey';
2021-01-06 17:35:57 +00:00
2021-01-21 07:35:38 +00:00
static String cloudFlareDomain = 'cloudFlareDomain';
2021-01-06 17:35:57 +00:00
static String hetznerKey = 'hetznerKey';
static String cloudFlareKey = 'cloudFlareKey';
static String rootUser = 'rootUser';
2021-01-21 21:01:42 +00:00
static String hetznerServer = 'hetznerServer';
2021-02-16 18:48:15 +00:00
static String hasFinalChecked = 'hasFinalChecked';
2021-01-21 21:01:42 +00:00
static String isServerStarted = 'isServerStarted';
2021-02-03 19:51:07 +00:00
static String backblazeKey = 'backblazeKey';
2021-12-06 18:31:19 +00:00
static String backblazeBucket = 'backblazeBucket';
2021-02-16 18:48:15 +00:00
static String isLoading = 'isLoading';
2021-03-31 11:37:39 +00:00
static String isServerResetedFirstTime = 'isServerResetedFirstTime';
static String isServerResetedSecondTime = 'isServerResetedSecondTime';
2021-09-02 19:32:07 +00:00
static String sshConfig = 'sshConfig';
static String sshPrivateKey = "sshPrivateKey";
static String sshPublicKey = "sshPublicKey";
2021-01-06 17:35:57 +00:00
}