diff --git a/mobile/packages/configuration/lib/base_configuration.dart b/mobile/packages/configuration/lib/base_configuration.dart index d413f1fdf1..d934faea08 100644 --- a/mobile/packages/configuration/lib/base_configuration.dart +++ b/mobile/packages/configuration/lib/base_configuration.dart @@ -35,6 +35,7 @@ class BaseConfiguration { static const userIDKey = "user_id"; static const endPointKey = "endpoint"; static const lastTempFolderClearTimeKey = "last_temp_folder_clear_time"; + static const offlineAuthSecretKey = "offline_auth_secret_key"; final kTempFolderDeletionTimeBuffer = const Duration(days: 1).inMicroseconds; @@ -52,6 +53,11 @@ class BaseConfiguration { String? _volatilePassword; + // Keys that should not be deleted during logout + // These keys are necessary for functionality that needs to work even when users + // aren't signed in, such as using Auth without backup + List preservedKeys = [offlineAuthSecretKey]; + Future init(List dbs) async { _databases = dbs; _documentsDirectory = (await getApplicationDocumentsDirectory()).path; @@ -62,13 +68,13 @@ class BaseConfiguration { accessibility: KeychainAccessibility.first_unlock_this_device, ), ); - _setupKeys(); - _setupFolders(); + await _setupKeys(); + await _setupFolders(); } Future logout({bool autoLogout = false}) async { await _preferences.clear(); - _secureStorage.deleteAll(); + await resetSecureStorage(); for (final db in _databases) { await db.clearTable(); } @@ -78,6 +84,16 @@ class BaseConfiguration { Bus.instance.fire(SignedOutEvent()); } + Future resetSecureStorage() async { + // Delete all keys except preserved ones + final allKeys = await _secureStorage.readAll(); + for (final key in allKeys.keys) { + if (!preservedKeys.contains(key)) { + await _secureStorage.delete(key: key); + } + } + } + Future generateKey(String password) async { // Create a master key final masterKey = CryptoUtil.generateKey(); @@ -217,7 +233,7 @@ class BaseConfiguration { if (split.length != mnemonicKeyWordCount) { String wordThatIsFollowedByEmptySpaceInSplit = ''; for (int i = 0; i < split.length; i++) { - String word = split[i]; + final String word = split[i]; if (word.isEmpty) { wordThatIsFollowedByEmptySpaceInSplit = '\n\nExtra space after word at position $i'; @@ -382,7 +398,7 @@ class BaseConfiguration { Future _setupKeys() async { try { if (!_preferences.containsKey(tokenKey)) { - await _secureStorage.deleteAll(); + await resetSecureStorage(); return; } _key = await _secureStorage.read(key: keyKey);