Skip to content

armgit / ente

Public Imported from GitHub
0

πŸ’š End-to-end encrypted cloud for everything.

Refactor BaseConfiguration to preserve offlineAuthSecretKey during logout

AmanRajSinghMourya5b5f563d476f
diff --git a/mobile/packages/configuration/lib/base_configuration.dart b/mobile/packages/configuration/lib/base_configuration.dart
index 5dfc722834..59b346c6f9 100644
--- a/mobile/packages/configuration/lib/base_configuration.dart
+++ b/mobile/packages/configuration/lib/base_configuration.dart
@@ -4,11 +4,11 @@ import 'dart:convert';
import 'dart:io' as io;
import 'package:bip39/bip39.dart' as bip39;
-import 'package:ente_configuration/constants.dart';
import 'package:ente_base/models/database.dart';
import 'package:ente_base/models/key_attributes.dart';
import 'package:ente_base/models/key_gen_result.dart';
import 'package:ente_base/models/private_key_attributes.dart';
+import 'package:ente_configuration/constants.dart';
import 'package:ente_crypto_dart/ente_crypto_dart.dart';
import 'package:ente_events/event_bus.dart';
import 'package:ente_events/models/endpoint_updated_event.dart';
@@ -35,11 +35,19 @@ 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;
static final _logger = Logger("Configuration");
+ // 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. E.g offline auth
+ List<String> preservedKeys = [
+ offlineAuthSecretKey,
+ ];
+
String? _cachedToken;
late SharedPreferences _preferences;
String? _key;
@@ -62,13 +70,19 @@ class BaseConfiguration {
accessibility: KeychainAccessibility.first_unlock_this_device,
),
);
- _setupKeys();
- _setupFolders();
+ await _setupKeys();
+ await _setupFolders();
}
Future<void> logout({bool autoLogout = false}) async {
await _preferences.clear();
- _secureStorage.deleteAll();
+ // 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);
+ }
+ }
for (final db in _databases) {
await db.clearTable();
}
@@ -217,7 +231,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';
@@ -379,10 +393,17 @@ class BaseConfiguration {
return _volatilePassword;
}
- Future<void> _setupKeys() async {
+ Future<void> _setupKeys() async {
try {
if (!_preferences.containsKey(tokenKey)) {
- await _secureStorage.deleteAll();
+ _logger.info("No token found, clearing secure storage");
+ // 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);
+ }
+ }
return;
}
_key = await _secureStorage.read(key: keyKey);