TF-886 Store encryptionKey in Hive database
This commit is contained in:
@@ -1,6 +1,5 @@
|
||||
import 'package:hive/hive.dart';
|
||||
import 'package:tmail_ui_user/features/caching/config/hive_cache_client.dart';
|
||||
import 'package:tmail_ui_user/features/caching/config/hive_cache_config.dart';
|
||||
import 'package:tmail_ui_user/features/login/data/model/authentication_info_cache.dart';
|
||||
|
||||
class AuthenticationInfoCacheClient extends HiveCacheClient<AuthenticationInfoCache> {
|
||||
@@ -11,7 +10,6 @@ class AuthenticationInfoCacheClient extends HiveCacheClient<AuthenticationInfoCa
|
||||
@override
|
||||
Future<void> clearAllData() {
|
||||
return Future.sync(() async {
|
||||
await HiveCacheConfig.removeEncryptionKey();
|
||||
final boxAuthenticationInfo = await openBox();
|
||||
return boxAuthenticationInfo.clear();
|
||||
}).catchError((error) {
|
||||
@@ -101,7 +99,7 @@ class AuthenticationInfoCacheClient extends HiveCacheClient<AuthenticationInfoCa
|
||||
@override
|
||||
Future<Box<AuthenticationInfoCache>> openBox() async {
|
||||
return Future.sync(() async {
|
||||
final encryptionKey = await HiveCacheConfig.getEncryptionKey();
|
||||
final encryptionKey = await getEncryptionKey();
|
||||
return Hive.openBox<AuthenticationInfoCache>(
|
||||
tableName,
|
||||
encryptionCipher: HiveAesCipher(encryptionKey!));
|
||||
|
||||
@@ -3,6 +3,7 @@ import 'package:flutter/foundation.dart';
|
||||
import 'package:tmail_ui_user/features/caching/account_cache_client.dart';
|
||||
import 'package:tmail_ui_user/features/caching/authentication_info_cache_client.dart';
|
||||
import 'package:tmail_ui_user/features/caching/email_cache_client.dart';
|
||||
import 'package:tmail_ui_user/features/caching/encryption_key_cache_client.dart';
|
||||
import 'package:tmail_ui_user/features/caching/mailbox_cache_client.dart';
|
||||
import 'package:tmail_ui_user/features/caching/recent_search_cache_client.dart';
|
||||
import 'package:tmail_ui_user/features/caching/state_cache_client.dart';
|
||||
@@ -16,6 +17,7 @@ class CachingManager {
|
||||
final RecentSearchCacheClient _recentSearchCacheClient;
|
||||
final TokenOidcCacheClient _tokenOidcCacheClient;
|
||||
final AccountCacheClient _accountCacheClient;
|
||||
final EncryptionKeyCacheClient _encryptionKeyCacheClient;
|
||||
final AuthenticationInfoCacheClient _authenticationInfoCacheClient;
|
||||
|
||||
CachingManager(
|
||||
@@ -25,6 +27,7 @@ class CachingManager {
|
||||
this._recentSearchCacheClient,
|
||||
this._tokenOidcCacheClient,
|
||||
this._accountCacheClient,
|
||||
this._encryptionKeyCacheClient,
|
||||
this._authenticationInfoCacheClient,
|
||||
);
|
||||
|
||||
@@ -37,6 +40,7 @@ class CachingManager {
|
||||
_recentSearchCacheClient.clearAllData(),
|
||||
_tokenOidcCacheClient.clearAllData(),
|
||||
_accountCacheClient.clearAllData(),
|
||||
_encryptionKeyCacheClient.clearAllData(),
|
||||
_authenticationInfoCacheClient.clearAllData(),
|
||||
]);
|
||||
} else {
|
||||
@@ -47,6 +51,7 @@ class CachingManager {
|
||||
_recentSearchCacheClient.deleteBox(),
|
||||
_tokenOidcCacheClient.deleteBox(),
|
||||
_accountCacheClient.deleteBox(),
|
||||
_encryptionKeyCacheClient.deleteBox(),
|
||||
_authenticationInfoCacheClient.deleteBox(),
|
||||
]);
|
||||
}
|
||||
|
||||
@@ -1,5 +1,8 @@
|
||||
|
||||
import 'dart:typed_data';
|
||||
|
||||
import 'package:hive/hive.dart';
|
||||
import 'package:tmail_ui_user/features/caching/config/hive_cache_config.dart';
|
||||
|
||||
abstract class HiveCacheClient<T> {
|
||||
|
||||
@@ -32,4 +35,8 @@ abstract class HiveCacheClient<T> {
|
||||
}
|
||||
|
||||
Future<void> clearAllData();
|
||||
|
||||
Future<Uint8List?> getEncryptionKey() {
|
||||
return HiveCacheConfig.getEncryptionKey();
|
||||
}
|
||||
}
|
||||
@@ -6,11 +6,11 @@ import 'package:core/utils/app_logger.dart';
|
||||
import 'package:get/get.dart';
|
||||
import 'package:hive/hive.dart';
|
||||
import 'package:path_provider/path_provider.dart' as path_provider;
|
||||
import 'package:shared_preferences/shared_preferences.dart';
|
||||
import 'package:tmail_ui_user/features/login/data/local/encryption_key_cache_manager.dart';
|
||||
import 'package:tmail_ui_user/features/login/data/model/account_cache.dart';
|
||||
import 'package:tmail_ui_user/features/login/data/model/authentication_info_cache.dart';
|
||||
import 'package:tmail_ui_user/features/login/data/model/encryption_key_cache.dart';
|
||||
import 'package:tmail_ui_user/features/login/data/model/token_oidc_cache.dart';
|
||||
import 'package:tmail_ui_user/features/login/data/utils/login_constant.dart';
|
||||
import 'package:tmail_ui_user/features/mailbox/data/model/mailbox_cache.dart';
|
||||
import 'package:tmail_ui_user/features/mailbox/data/model/mailbox_rights_cache.dart';
|
||||
import 'package:tmail_ui_user/features/mailbox/data/model/state_cache.dart';
|
||||
@@ -37,22 +37,32 @@ class HiveCacheConfig {
|
||||
}
|
||||
}
|
||||
|
||||
static Future<Uint8List?> getEncryptionKey() async {
|
||||
final sharedPreference = Get.find<SharedPreferences>();
|
||||
final containsEncryptionKey = sharedPreference.containsKey(LoginConstant.keyHiveEncrypt);
|
||||
if (!containsEncryptionKey) {
|
||||
final key = Hive.generateSecureKey();
|
||||
await sharedPreference.setString(LoginConstant.keyHiveEncrypt, base64UrlEncode(key));
|
||||
static Future<void> initializeEncryptionKey() async {
|
||||
final encryptionKeyCacheManager = Get.find<EncryptionKeyCacheManager>();
|
||||
final encryptionKeyCache = await encryptionKeyCacheManager.getEncryptionKeyStored();
|
||||
if (encryptionKeyCache == null) {
|
||||
final secureKey = Hive.generateSecureKey();
|
||||
final secureKeyEncode = base64UrlEncode(secureKey);
|
||||
log('HiveCacheConfig::_initializeEncryptionKey(): secureKeyEncode: $secureKeyEncode');
|
||||
await encryptionKeyCacheManager.storeEncryptionKey(EncryptionKeyCache(secureKeyEncode));
|
||||
}
|
||||
final keyStored = sharedPreference.getString(LoginConstant.keyHiveEncrypt) ?? '';
|
||||
final encryptionKey = base64Url.decode(keyStored);
|
||||
log('HiveCacheConfig::getEncryptionKey(): Encryption key: $encryptionKey');
|
||||
return encryptionKey;
|
||||
}
|
||||
|
||||
static Future<bool> removeEncryptionKey() {
|
||||
final sharedPreference = Get.find<SharedPreferences>();
|
||||
return sharedPreference.remove(LoginConstant.keyHiveEncrypt);
|
||||
static Future<Uint8List?> getEncryptionKey() async {
|
||||
final encryptionKeyCacheManager = Get.find<EncryptionKeyCacheManager>();
|
||||
var encryptionKeyCache = await encryptionKeyCacheManager.getEncryptionKeyStored();
|
||||
if (encryptionKeyCache == null) {
|
||||
await initializeEncryptionKey();
|
||||
encryptionKeyCache = await encryptionKeyCacheManager.getEncryptionKeyStored();
|
||||
}
|
||||
|
||||
if (encryptionKeyCache != null) {
|
||||
log('HiveCacheConfig::getEncryptionKey(): encryptionKey: ${encryptionKeyCache.value}');
|
||||
final encryptionKey = base64Url.decode(encryptionKeyCache.value);
|
||||
return encryptionKey;
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
void registerAdapter() {
|
||||
@@ -65,6 +75,7 @@ class HiveCacheConfig {
|
||||
Hive.registerAdapter(RecentSearchCacheAdapter());
|
||||
Hive.registerAdapter(TokenOidcCacheAdapter());
|
||||
Hive.registerAdapter(AccountCacheAdapter());
|
||||
Hive.registerAdapter(EncryptionKeyCacheAdapter());
|
||||
Hive.registerAdapter(AuthenticationInfoCacheAdapter());
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,124 @@
|
||||
import 'package:hive/hive.dart';
|
||||
import 'package:tmail_ui_user/features/caching/config/hive_cache_client.dart';
|
||||
import 'package:tmail_ui_user/features/login/data/model/encryption_key_cache.dart';
|
||||
|
||||
class EncryptionKeyCacheClient extends HiveCacheClient<EncryptionKeyCache> {
|
||||
|
||||
@override
|
||||
String get tableName => "EncryptionKeyCache";
|
||||
|
||||
@override
|
||||
Future<void> clearAllData() {
|
||||
return Future.sync(() async {
|
||||
final boxEncryptionKey = await openBox();
|
||||
return boxEncryptionKey.clear();
|
||||
}).catchError((error) {
|
||||
throw error;
|
||||
});
|
||||
}
|
||||
|
||||
@override
|
||||
Future<void> deleteItem(String key) {
|
||||
return Future.sync(() async {
|
||||
final boxEncryptionKey = await openBox();
|
||||
return boxEncryptionKey.delete(key);
|
||||
}).catchError((error) {
|
||||
throw error;
|
||||
});
|
||||
}
|
||||
|
||||
@override
|
||||
Future<void> deleteMultipleItem(List<String> listKey) {
|
||||
return Future.sync(() async {
|
||||
final boxEncryptionKey = await openBox();
|
||||
return boxEncryptionKey.deleteAll(listKey);
|
||||
}).catchError((error) {
|
||||
throw error;
|
||||
});
|
||||
}
|
||||
|
||||
@override
|
||||
Future<List<EncryptionKeyCache>> getAll() {
|
||||
return Future.sync(() async {
|
||||
final boxEncryptionKey = await openBox();
|
||||
return boxEncryptionKey.values.toList();
|
||||
}).catchError((error) {
|
||||
throw error;
|
||||
});
|
||||
}
|
||||
|
||||
@override
|
||||
Future<EncryptionKeyCache?> getItem(String key) {
|
||||
return Future.sync(() async {
|
||||
final boxEncryptionKey = await openBox();
|
||||
return boxEncryptionKey.get(key);
|
||||
}).catchError((error) {
|
||||
throw error;
|
||||
});
|
||||
}
|
||||
|
||||
@override
|
||||
Future<void> insertItem(String key, EncryptionKeyCache newObject) {
|
||||
return Future.sync(() async {
|
||||
final boxEncryptionKey = await openBox();
|
||||
return boxEncryptionKey.put(key, newObject);
|
||||
}).catchError((error) {
|
||||
throw error;
|
||||
});
|
||||
}
|
||||
|
||||
@override
|
||||
Future<void> insertMultipleItem(Map<String, EncryptionKeyCache> mapObject) {
|
||||
return Future.sync(() async {
|
||||
final boxEncryptionKey = await openBox();
|
||||
return boxEncryptionKey.putAll(mapObject);
|
||||
}).catchError((error) {
|
||||
throw error;
|
||||
});
|
||||
}
|
||||
|
||||
@override
|
||||
Future<bool> isExistItem(String key) {
|
||||
return Future.sync(() async {
|
||||
final boxEncryptionKey = await openBox();
|
||||
return boxEncryptionKey.containsKey(key);
|
||||
}).catchError((error) {
|
||||
throw error;
|
||||
});
|
||||
}
|
||||
|
||||
@override
|
||||
Future<bool> isExistTable() {
|
||||
return Future.sync(() async {
|
||||
return Hive.boxExists(tableName);
|
||||
}).catchError((error) {
|
||||
throw error;
|
||||
});
|
||||
}
|
||||
|
||||
@override
|
||||
Future<Box<EncryptionKeyCache>> openBox() async {
|
||||
return Hive.openBox<EncryptionKeyCache>(tableName);
|
||||
}
|
||||
|
||||
@override
|
||||
Future<void> updateItem(String key, EncryptionKeyCache newObject) {
|
||||
return Future.sync(() async {
|
||||
final boxEncryptionKey = await openBox();
|
||||
return boxEncryptionKey.put(key, newObject);
|
||||
}).catchError((error) {
|
||||
throw error;
|
||||
});
|
||||
}
|
||||
|
||||
@override
|
||||
Future<void> updateMultipleItem(Map<String, EncryptionKeyCache> mapObject) {
|
||||
return Future.sync(() async {
|
||||
final boxEncryptionKey = await openBox();
|
||||
return boxEncryptionKey.putAll(mapObject);
|
||||
}).catchError((error) {
|
||||
throw error;
|
||||
});
|
||||
}
|
||||
|
||||
}
|
||||
@@ -9,5 +9,6 @@ class CachingConstants {
|
||||
static const int RECENT_SEARCH_HIVE_CACHE_IDENTIFY = 7;
|
||||
static const int TOKEN_OIDC_HIVE_CACHE_IDENTIFY = 8;
|
||||
static const int ACCOUNT_HIVE_CACHE_IDENTIFY = 9;
|
||||
static const int AUTHENTICATION_INFO_HIVE_CACHE_IDENTIFY = 10;
|
||||
static const int ENCRYPTION_KEY_HIVE_CACHE_IDENTIFY = 10;
|
||||
static const int AUTHENTICATION_INFO_HIVE_CACHE_IDENTIFY = 11;
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
import 'package:tmail_ui_user/features/caching/encryption_key_cache_client.dart';
|
||||
import 'package:tmail_ui_user/features/login/data/model/encryption_key_cache.dart';
|
||||
|
||||
class EncryptionKeyCacheManager {
|
||||
final EncryptionKeyCacheClient _encryptionKeyCacheClient;
|
||||
|
||||
EncryptionKeyCacheManager(this._encryptionKeyCacheClient);
|
||||
|
||||
Future<void> storeEncryptionKey(EncryptionKeyCache encryptionKeyCache) {
|
||||
return _encryptionKeyCacheClient.insertItem(
|
||||
EncryptionKeyCache.keyCacheValue,
|
||||
encryptionKeyCache);
|
||||
}
|
||||
|
||||
Future<EncryptionKeyCache?> getEncryptionKeyStored() {
|
||||
return _encryptionKeyCacheClient.getItem(EncryptionKeyCache.keyCacheValue);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
import 'package:equatable/equatable.dart';
|
||||
import 'package:hive/hive.dart';
|
||||
import 'package:tmail_ui_user/features/caching/utils/caching_constants.dart';
|
||||
|
||||
part 'encryption_key_cache.g.dart';
|
||||
|
||||
@HiveType(typeId: CachingConstants.ENCRYPTION_KEY_HIVE_CACHE_IDENTIFY)
|
||||
class EncryptionKeyCache extends HiveObject with EquatableMixin {
|
||||
|
||||
static const String keyCacheValue = 'hiveEncryptionKey';
|
||||
|
||||
@HiveField(0)
|
||||
final String value;
|
||||
|
||||
EncryptionKeyCache(this.value);
|
||||
|
||||
@override
|
||||
List<Object?> get props => [value];
|
||||
}
|
||||
@@ -1,5 +1,4 @@
|
||||
|
||||
class LoginConstant {
|
||||
static const String keyBaseUrl = 'KEY_BASE_URL';
|
||||
static const String keyHiveEncrypt = 'KEY_HIVE_ENCRYPT';
|
||||
}
|
||||
@@ -24,6 +24,7 @@ void main() async {
|
||||
));
|
||||
await MainBindings().dependencies();
|
||||
await HiveCacheConfig().setUp();
|
||||
await HiveCacheConfig.initializeEncryptionKey();
|
||||
await Executor().warmUp();
|
||||
await dotenv.load(fileName: 'env.file');
|
||||
runApp(const TMailApp());
|
||||
|
||||
@@ -6,6 +6,7 @@ import 'package:tmail_ui_user/features/caching/account_cache_client.dart';
|
||||
import 'package:tmail_ui_user/features/caching/authentication_info_cache_client.dart';
|
||||
import 'package:tmail_ui_user/features/caching/caching_manager.dart';
|
||||
import 'package:tmail_ui_user/features/caching/email_cache_client.dart';
|
||||
import 'package:tmail_ui_user/features/caching/encryption_key_cache_client.dart';
|
||||
import 'package:tmail_ui_user/features/caching/mailbox_cache_client.dart';
|
||||
import 'package:tmail_ui_user/features/caching/recent_search_cache_client.dart';
|
||||
import 'package:tmail_ui_user/features/caching/state_cache_client.dart';
|
||||
@@ -13,6 +14,7 @@ import 'package:tmail_ui_user/features/caching/token_oidc_cache_client.dart';
|
||||
import 'package:tmail_ui_user/features/cleanup/data/local/recent_search_cache_manager.dart';
|
||||
import 'package:tmail_ui_user/features/login/data/local/account_cache_manager.dart';
|
||||
import 'package:tmail_ui_user/features/login/data/local/authentication_info_cache_manager.dart';
|
||||
import 'package:tmail_ui_user/features/login/data/local/encryption_key_cache_manager.dart';
|
||||
import 'package:tmail_ui_user/features/login/data/local/oidc_configuration_cache_manager.dart';
|
||||
import 'package:tmail_ui_user/features/login/data/local/token_oidc_cache_manager.dart';
|
||||
import 'package:tmail_ui_user/features/mailbox/data/local/mailbox_cache_manager.dart';
|
||||
@@ -42,8 +44,10 @@ class LocalBindings extends Bindings {
|
||||
Get.put(TokenOidcCacheClient());
|
||||
Get.put(TokenOidcCacheManager(Get.find<TokenOidcCacheClient>()));
|
||||
Get.put(AccountCacheClient());
|
||||
Get.put(AuthenticationInfoCacheClient());
|
||||
Get.put(AccountCacheManager(Get.find<AccountCacheClient>()));
|
||||
Get.put(EncryptionKeyCacheClient());
|
||||
Get.put(EncryptionKeyCacheManager(Get.find<EncryptionKeyCacheClient>()));
|
||||
Get.put(AuthenticationInfoCacheClient());
|
||||
Get.put(AuthenticationInfoCacheManager(Get.find<AuthenticationInfoCacheClient>()));
|
||||
Get.put(OidcConfigurationCacheManager(Get.find<SharedPreferences>()));
|
||||
Get.put(LanguageCacheManager(Get.find<SharedPreferences>()));
|
||||
@@ -54,6 +58,7 @@ class LocalBindings extends Bindings {
|
||||
Get.find<RecentSearchCacheClient>(),
|
||||
Get.find<TokenOidcCacheClient>(),
|
||||
Get.find<AccountCacheClient>(),
|
||||
Get.find<EncryptionKeyCacheClient>(),
|
||||
Get.find<AuthenticationInfoCacheClient>(),
|
||||
));
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user