Skip to content

armgit / ente

Public Imported from GitHub
0

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

[auth] move backup password clearing to LocalBackupService

Prateek Sunal71bcc5549623
diff --git a/mobile/apps/auth/ios/Runner/Runner.entitlements b/mobile/apps/auth/ios/Runner/Runner.entitlements
index 4ea99d80fc..0a7198161c 100644
--- a/mobile/apps/auth/ios/Runner/Runner.entitlements
+++ b/mobile/apps/auth/ios/Runner/Runner.entitlements
@@ -9,9 +9,5 @@
<string>webcredentials:web.ente.io</string>
<string>webcredentials:auth.ente.io</string>
</array>
- <key>com.apple.developer.icloud-services</key>
- <array>
- <string>CloudDocuments</string>
- </array>
</dict>
</plist>
diff --git a/mobile/apps/auth/lib/core/configuration.dart b/mobile/apps/auth/lib/core/configuration.dart
index ed56c4521d..bb3d3cbf60 100644
--- a/mobile/apps/auth/lib/core/configuration.dart
+++ b/mobile/apps/auth/lib/core/configuration.dart
@@ -1,7 +1,6 @@
import 'dart:async';
import 'dart:typed_data';
-import 'package:ente_auth/services/secure_storage_service.dart';
import 'package:ente_base/models/database.dart';
import 'package:ente_configuration/base_configuration.dart';
import 'package:ente_crypto_dart/ente_crypto_dart.dart';
@@ -24,8 +23,8 @@ class Configuration extends BaseConfiguration {
@override
Future<void> init(List<EnteBaseDatabase> dbs) async {
- _preferences = await SharedPreferences.getInstance();
await super.init(dbs);
+ _preferences = await SharedPreferences.getInstance();
_secureStorage = const FlutterSecureStorage(
iOptions: IOSOptions(
accessibility: KeychainAccessibility.first_unlock_this_device,
@@ -52,19 +51,11 @@ class Configuration extends BaseConfiguration {
@override
// This includes both base keys (key, secretKey) and auth-specific keys.
- List<String> get secureStorageKeys {
- final keys = [
- BaseConfiguration.keyKey,
- BaseConfiguration.secretKeyKey,
- authSecretKeyKey,
- ];
- if (!_shouldPreserveLocalBackupPassword()) {
- keys.add(SecureStorageService.autoBackupPasswordKey);
- }
- // Note: offlineAuthSecretKey is intentionally not included here
- // as it persists across logouts
- return keys;
- }
+ List<String> get secureStorageKeys => [
+ BaseConfiguration.keyKey,
+ BaseConfiguration.secretKeyKey,
+ authSecretKeyKey,
+ ];
@override
Future<void> logout({bool autoLogout = false}) async {
@@ -112,8 +103,4 @@ class Configuration extends BaseConfiguration {
}
await _preferences.setBool(hasOptedForOfflineModeKey, true);
}
-
- bool _shouldPreserveLocalBackupPassword() {
- return hasOptedForOfflineMode() && !hasConfiguredAccount();
- }
}
diff --git a/mobile/apps/auth/lib/main.dart b/mobile/apps/auth/lib/main.dart
index 21ffffe271..a4a0ddbdbf 100644
--- a/mobile/apps/auth/lib/main.dart
+++ b/mobile/apps/auth/lib/main.dart
@@ -11,6 +11,7 @@ import 'package:ente_auth/l10n/l10n.dart';
import 'package:ente_auth/locale.dart';
import 'package:ente_auth/services/authenticator_service.dart';
import 'package:ente_auth/services/billing_service.dart';
+import 'package:ente_auth/services/local_backup_service.dart';
import 'package:ente_auth/services/notification_service.dart';
import 'package:ente_auth/services/preference_service.dart';
import 'package:ente_auth/services/update_service.dart';
@@ -185,4 +186,8 @@ Future<void> _init(bool bool, {String? via}) async {
Configuration.instance,
hasOptedForOfflineMode: Configuration.instance.hasOptedForOfflineMode(),
);
+ // await LocalBackupService.instance.init(
+ // Configuration.instance,
+ // hasOptedForOfflineMode: Configuration.instance.hasOptedForOfflineMode(),
+ // );
}
diff --git a/mobile/apps/auth/lib/services/local_backup_service.dart b/mobile/apps/auth/lib/services/local_backup_service.dart
index a2881b094e..b78632b5c4 100644
--- a/mobile/apps/auth/lib/services/local_backup_service.dart
+++ b/mobile/apps/auth/lib/services/local_backup_service.dart
@@ -6,7 +6,10 @@ import 'package:dir_utils/dir_utils.dart';
import 'package:ente_auth/models/export/ente.dart';
import 'package:ente_auth/services/secure_storage_service.dart';
import 'package:ente_auth/store/code_store.dart';
+import 'package:ente_configuration/base_configuration.dart';
import 'package:ente_crypto_dart/ente_crypto_dart.dart';
+import 'package:ente_events/event_bus.dart';
+import 'package:ente_events/models/signed_out_event.dart';
import 'package:intl/intl.dart';
import 'package:logging/logging.dart';
import 'package:shared_preferences/shared_preferences.dart';
@@ -21,6 +24,35 @@ class LocalBackupService {
static const _lastBackupDayKey = 'lastBackupDay';
static const _iosBookmarkKey = 'autoBackupIosBookmark';
+ Future<void> init(
+ BaseConfiguration config, {
+ bool hasOptedForOfflineMode = false,
+ }) async {
+ await _clearBackupPasswordIfFreshInstall(config, hasOptedForOfflineMode);
+
+ Bus.instance.on<SignedOutEvent>().listen((event) {
+ _clearBackupPassword();
+ });
+ }
+
+ /// Clear backup password on fresh install (like lock screen does).
+ /// Only clears if not logged in and not in offline mode.
+ Future<void> _clearBackupPasswordIfFreshInstall(
+ BaseConfiguration config,
+ bool hasOptedForOfflineMode,
+ ) async {
+ if ((Platform.isIOS || Platform.isMacOS) &&
+ !config.isLoggedIn() &&
+ !hasOptedForOfflineMode) {
+ await _clearBackupPassword();
+ }
+ }
+
+ Future<void> _clearBackupPassword() async {
+ await SecureStorageService.instance
+ .delete(SecureStorageService.autoBackupPasswordKey);
+ }
+
Future<bool> triggerAutomaticBackup({bool isManual = false}) async {
try {
final prefs = await SharedPreferences.getInstance();
@@ -128,13 +160,16 @@ class LocalBackupService {
bookmark: target.iosBookmark,
);
final result = await dirUtils.withAccess(dir, (path) async {
+ // Use EnteAuthBackups subdirectory
+ final backupPath = '$path/EnteAuthBackups';
+ await Directory(backupPath).create(recursive: true);
final success = await dirUtils.writeFile(
- PickedDirectory(path: path),
+ PickedDirectory(path: backupPath),
fileName,
contentBytes,
);
if (success) {
- await _manageOldBackups(path);
+ await _manageOldBackups(backupPath);
}
return success;
});
@@ -210,7 +245,8 @@ class LocalBackupService {
Future<void> deleteAllBackupsIn(String path, {String? iosBookmark}) async {
try {
final dirUtils = DirUtils.instance;
- final backupPath = path;
+ // On iOS, backups are in EnteAuthBackups subdirectory
+ final backupPath = Platform.isIOS ? '$path/EnteAuthBackups' : path;
Future<void> doDelete() async {
final backupDir = Directory(backupPath);
diff --git a/mobile/apps/auth/lib/ui/settings/data/local_backup/local_backup_experience.dart b/mobile/apps/auth/lib/ui/settings/data/local_backup/local_backup_experience.dart
index eb8b97249f..286e1dd6cb 100644
--- a/mobile/apps/auth/lib/ui/settings/data/local_backup/local_backup_experience.dart
+++ b/mobile/apps/auth/lib/ui/settings/data/local_backup/local_backup_experience.dart
@@ -692,13 +692,11 @@ class _LocalBackupExperienceState extends State<LocalBackupExperience> {
bool shouldTriggerBackup = true,
}) async {
if (Platform.isAndroid) {
- final saved = await _pickAndPersistAndroidLocation();
+ final saved = await _pickAndPersistAndroidLocation(
+ successMessage: successMessage,
+ shouldTriggerBackup: shouldTriggerBackup,
+ );
if (saved) {
- if (shouldTriggerBackup) {
- await LocalBackupService.instance.triggerAutomaticBackup(
- isManual: true,
- );
- }
} else if (requireSelection) {
_showSnackBar(context.l10n.selectFolderToContinue);
}
@@ -763,10 +761,15 @@ class _LocalBackupExperienceState extends State<LocalBackupExperience> {
}
try {
- await LocalBackupService.instance.writeBackupToDirectory(path);
+ // Create EnteAuthBackups subdirectory inside the selected path
+ final backupDir = Directory('$path/EnteAuthBackups');
+ await backupDir.create(recursive: true);
+ await LocalBackupService.instance
+ .writeBackupToDirectory(backupDir.path);
} finally {
await SecurityBookmarkService.instance.stopAccessingBookmark(bookmark);
}
+ // Store the parent path (with bookmark) - backups go to EnteAuthBackups subdir
await prefs.setString('autoBackupPath', path);
await prefs.setString(_iosBookmarkKey, bookmark);
await prefs.remove(_treeUriKey);
@@ -850,16 +853,27 @@ class _LocalBackupExperienceState extends State<LocalBackupExperience> {
return androidBasePath != null ? '$androidBasePath/EnteAuthBackups' : null;
}
- Future<bool> _pickAndPersistAndroidLocation() async {
+ Future<bool> _pickAndPersistAndroidLocation({
+ String? successMessage,
+ bool shouldTriggerBackup = true,
+ }) async {
final picked = await DirUtils.instance.pickDirectory();
final treeUri = picked?.treeUri;
if (treeUri == null || treeUri.isEmpty) {
return false;
}
- return _persistAndroidLocation(treeUri);
+ return _persistAndroidLocation(
+ treeUri,
+ successMessage: successMessage,
+ shouldTriggerBackup: shouldTriggerBackup,
+ );
}
- Future<bool> _persistAndroidLocation(String treeUri) async {
+ Future<bool> _persistAndroidLocation(
+ String treeUri, {
+ String? successMessage,
+ bool shouldTriggerBackup = true,
+ }) async {
final prefs = await SharedPreferences.getInstance();
try {
await prefs.setString(_treeUriKey, treeUri);
@@ -869,7 +883,19 @@ class _LocalBackupExperienceState extends State<LocalBackupExperience> {
_backupTreeUri = treeUri;
_backupPath = null;
});
- _showSnackBar(context.l10n.locationUpdatedAndBackupCreated);
+ if (shouldTriggerBackup) {
+ final backupSuccess = await LocalBackupService.instance
+ .triggerAutomaticBackup(isManual: true);
+ _showSnackBar(
+ backupSuccess
+ ? (successMessage ?? context.l10n.locationUpdatedAndBackupCreated)
+ : context.l10n.somethingWentWrongPleaseTryAgain,
+ );
+ return backupSuccess;
+ }
+ if (successMessage != null) {
+ _showSnackBar(successMessage);
+ }
return true;
} catch (_) {
_showSnackBar(context.l10n.noDefaultBackupFolder);