[auth] Fix missing token (#6971)

## Description
When using Auth without backup, it was giving a error `Offline key is
missing`

**Reason**: During the `init` of `BaseConfiguration` if the `tokenKey`
is not set, we clear all the keys in the secure storage, and in this
process the `offlineAuthSecretKey` was also getting cleared

**Fix** Fixed by skipping the deletion of `offlineAuthSecretKey`  

## Tests
[Test Video](https://wormhole.app/qz3mol#Dlhr0NRpVQVQsrid2X-quA)
This commit is contained in:
Neeraj
2025-08-26 13:02:43 +05:30
committed by GitHub

View File

@@ -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<String> preservedKeys = [offlineAuthSecretKey];
Future<void> init(List<EnteBaseDatabase> 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<void> 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<void> 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<KeyGenResult> 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<void> _setupKeys() async {
try {
if (!_preferences.containsKey(tokenKey)) {
await _secureStorage.deleteAll();
await resetSecureStorage();
return;
}
_key = await _secureStorage.read(key: keyKey);