π End-to-end encrypted cloud for everything.
Dart 2faandroidauthye2eeencryptionend-to-end-encryptionfluttergolanggoogle-photosgoogle-photos-alternativeiosopensourcephotosprivacyprivacy-toolstypescriptzero-knowledge
Restore local changes from stash
a5xwina7300b7ac720
diff --git a/mobile/apps/auth/android/app/build.gradle b/mobile/apps/auth/android/app/build.gradleindex 3da027f155..d8ffec49e8 100644--- a/mobile/apps/auth/android/app/build.gradle+++ b/mobile/apps/auth/android/app/build.gradle@@ -30,7 +30,7 @@ if (keystorePropertiesFile.exists()) {android {namespace "io.ente.auth"- compileSdk 36+ compileSdk 34ndkVersion flutter.ndkVersioncompileOptions {@@ -57,7 +57,7 @@ android {// You can update the following values to match your application needs.// For more information, see: https://docs.flutter.dev/deployment/android#reviewing-the-gradle-build-configuration.minSdkVersion 21- targetSdkVersion 35+ targetSdkVersion 34versionCode flutterVersionCode.toInteger()versionName flutterVersionNametestInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"diff --git a/mobile/apps/auth/lib/l10n/arb/app_en.arb b/mobile/apps/auth/lib/l10n/arb/app_en.arbindex 6fc8f8ad55..f0ae7db124 100644--- a/mobile/apps/auth/lib/l10n/arb/app_en.arb+++ b/mobile/apps/auth/lib/l10n/arb/app_en.arb@@ -519,5 +519,17 @@"algorithm": "Algorithm","type": "Type","period": "Period",- "digits": "Digits"+ "digits": "Digits",+ "localBackupSettingsTitle": "Local Backup Settings",+ "enableAutomaticBackups": "Enable Automatic Backups",+ "backupLocation": "Backup Location",+ "backupLocationDescription": "Select a folder to save backups. Backups run automatically when entries are added, deleted, or edited.",+ "currentLocation": "Current Location:",+ "notSetPleaseChooseLocation": "Not set. Please choose a location.",+ "chooseLocation": "Choose Location",+ "securityNotice": "Security Notice",+ "backupSecurityNotice": "This encrypted backup holds your 2FA keys. If lost, you may not be able to recover your accounts. Keep it safe!",+ "locationUpdatedAndBackupCreated": "Location updated and initial backup created!",+ "initialBackupCreated": "Initial backup created!",+ "pleaseChooseBackupLocation": "Please choose a backup location."}\ No newline at end of filediff --git a/mobile/apps/auth/lib/main.dart b/mobile/apps/auth/lib/main.dartindex 7187d5f3f0..bb2c652ffd 100644--- a/mobile/apps/auth/lib/main.dart+++ b/mobile/apps/auth/lib/main.dart@@ -11,7 +11,6 @@ import 'package:ente_auth/ente_theme_data.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';@@ -154,7 +153,6 @@ Future<void> _init(bool bool, {String? via}) async {await PreferenceService.instance.init();await CodeStore.instance.init();- await LocalBackupService.instance.init();await CodeDisplayStore.instance.init();await Configuration.instance.init();await Network.instance.init();diff --git a/mobile/apps/auth/lib/services/local_backup_service.dart b/mobile/apps/auth/lib/services/local_backup_service.dartindex 95f3b77d92..ef3b15bd1e 100644--- a/mobile/apps/auth/lib/services/local_backup_service.dart+++ b/mobile/apps/auth/lib/services/local_backup_service.dart@@ -1,45 +1,83 @@-import 'dart:io';-+import 'dart:convert';+import 'dart:io';+import 'package:ente_auth/models/export/ente.dart';import 'package:ente_auth/store/code_store.dart';-import 'package:flutter/foundation.dart';+import 'package:ente_crypto_dart/ente_crypto_dart.dart';+import 'package:flutter_secure_storage/flutter_secure_storage.dart';+import 'package:intl/intl.dart'; //for time based file naming+import 'package:logging/logging.dart';import 'package:shared_preferences/shared_preferences.dart';class LocalBackupService {+ final _logger = Logger('LocalBackupService');static final LocalBackupService instance =LocalBackupService._privateConstructor();LocalBackupService._privateConstructor();- Future<void> init() async {}-+ // to create an encrypted backup file if the toggle is onFuture<void> triggerAutomaticBackup() async {try {final prefs = await SharedPreferences.getInstance();- final isEnabled = prefs.getBool('isAutoBackupEnabled') ?? false; //checks if toggle is on+ final isEnabled = prefs.getBool('isAutoBackupEnabled') ?? false;if (!isEnabled) return;final backupPath = prefs.getString('autoBackupPath');if (backupPath == null) return;++ const storage = FlutterSecureStorage();+ final password = await storage.read(key: 'autoBackupPassword');+ if (password == null || password.isEmpty) {+ _logger.warning("Automatic backup skipped: password not set.");+ return;+ }- debugPrint("--- Change detected, triggering automatic backup... ---");+ _logger.info("Change detected, triggering automatic encrypted backup...");- final allCodes = await CodeStore.instance.getAllCodes(sortCodes: false);- final validCodes = allCodes.where((code) => !code.hasError);- String backupContent = "";- for (final code in validCodes) {- backupContent += "${code.toOTPAuthUrlFormat()}\n";- }+ final plainTextContent = await CodeStore.instance.getCodesForExport();++ if (plainTextContent.trim().isEmpty) return;++ final kekSalt = CryptoUtil.getSaltToDeriveKey();+ final derivedKeyResult = await CryptoUtil.deriveSensitiveKey(+ utf8.encode(password),+ kekSalt,+ );++ final encResult = await CryptoUtil.encryptData(+ utf8.encode(plainTextContent),+ derivedKeyResult.key,+ );++ final encContent = CryptoUtil.bin2base64(encResult.encryptedData!);+ final encNonce = CryptoUtil.bin2base64(encResult.header!);++ final EnteAuthExport data = EnteAuthExport(+ version: 1,+ encryptedData: encContent,+ encryptionNonce: encNonce,+ kdfParams: KDFParams(+ memLimit: derivedKeyResult.memLimit,+ opsLimit: derivedKeyResult.opsLimit,+ salt: CryptoUtil.bin2base64(kekSalt),+ ),+ );++ final encryptedJson = jsonEncode(data.toJson());- if (backupContent.trim().isEmpty) return;+ final now = DateTime.now();+ final formatter = DateFormat('yyyy-MM-dd_HH-mm-ss');+ final formattedDate = formatter.format(now);+ final fileName = 'ente-auth-auto-backup-$formattedDate.txt';- const fileName = 'ente-auth-auto-backup.txt';final filePath = '$backupPath/$fileName';final backupFile = File(filePath);- await backupFile.writeAsString(backupContent);++ await backupFile.writeAsString(encryptedJson);- debugPrint('Automatic backup successful! Saved to: $filePath');+ _logger.info('Automatic encrypted backup successful! Saved to: $filePath');} catch (e, s) {- debugPrint('Silent error during automatic backup: $e\n$s');+ _logger.severe('Silent error during automatic backup', e, s);}}}\ No newline at end of filediff --git a/mobile/apps/auth/lib/store/code_store.dart b/mobile/apps/auth/lib/store/code_store.dartindex 7939f91f4b..7da780c53a 100644--- a/mobile/apps/auth/lib/store/code_store.dart+++ b/mobile/apps/auth/lib/store/code_store.dart@@ -239,6 +239,27 @@ class CodeStore {_isOfflineImportRunning = false;}}++ Future<String> getCodesForExport() async {+ final allCodes = await getAllCodes(sortCodes: false);+ String data = "";+ for (final code in allCodes) {+ if (code.hasError) continue;+ data += "${code.toOTPAuthUrlFormat()}\n";+ }+ return data;+}+++++++++++}enum AddResult {diff --git a/mobile/apps/auth/lib/ui/settings/data/local_backup_settings_page.dart b/mobile/apps/auth/lib/ui/settings/data/local_backup_settings_page.dartindex c77349cf52..449e3ce9b0 100644--- a/mobile/apps/auth/lib/ui/settings/data/local_backup_settings_page.dart+++ b/mobile/apps/auth/lib/ui/settings/data/local_backup_settings_page.dart@@ -1,7 +1,10 @@+import 'package:ente_auth/l10n/l10n.dart';import 'package:ente_auth/services/local_backup_service.dart';import 'package:ente_auth/theme/ente_theme.dart';+import 'package:ente_auth/utils/dialog_util.dart';import 'package:file_picker/file_picker.dart';import 'package:flutter/material.dart';+import 'package:flutter_secure_storage/flutter_secure_storage.dart';import 'package:shared_preferences/shared_preferences.dart';class LocalBackupSettingsPage extends StatefulWidget {@@ -43,18 +46,47 @@ class _LocalBackupSettingsPageState extends State<LocalBackupSettingsPage> {});await LocalBackupService.instance.triggerAutomaticBackup(); //whenever backup path is set, we trigger+ final l10n = context.l10n;ScaffoldMessenger.of(context).showSnackBar(- const SnackBar(- content: Text('Location updated and initial backup created!'),),+ SnackBar(+ content: Text(l10n.locationUpdatedAndBackupCreated),),);}}+Future<void> _showSetPasswordDialog() async {+ final l10n = context.l10n;+ await showTextInputDialog(+ context,+ title: l10n.setPasswordTitle,+ submitButtonLabel: l10n.saveAction,+ hintText: l10n.enterPassword,+ isPasswordInput: true,+ onSubmit: (String password) async {+ if (password.length < 8) {+ ScaffoldMessenger.of(context).showSnackBar(+ const SnackBar(content: Text('Password must be at least 8 characters long.')),+ );+ return;+ }++ // to store the backup password securely+ const storage = FlutterSecureStorage();+ await storage.write(key: 'autoBackupPassword', value: password);++ ScaffoldMessenger.of(context).showSnackBar(+ const SnackBar(content: Text('Backup password saved successfully!')),+ );+ },+ );+}+@overrideWidget build(BuildContext context) {+ final l10n = context.l10n;return Scaffold(appBar: AppBar(- title: const Text("Local Backup Settings"),+ title: Text(l10n.localBackupSettingsTitle),),body: Padding(padding: const EdgeInsets.all(16),@@ -65,13 +97,14 @@ class _LocalBackupSettingsPageState extends State<LocalBackupSettingsPage> {mainAxisAlignment: MainAxisAlignment.spaceBetween,children: [Text(- "Enable Automatic Backups",+ l10n.enableAutomaticBackups,style: getEnteTextTheme(context).largeBold,),Switch(value: _isBackupEnabled,activeColor: Colors.white,onChanged: (value) async {+ final l10n = context.l10n;final prefs = await SharedPreferences.getInstance();await prefs.setBool('isAutoBackupEnabled', value);setState(() {@@ -82,16 +115,16 @@ class _LocalBackupSettingsPageState extends State<LocalBackupSettingsPage> {if (_backupPath != null) { //ensuring path was setawait LocalBackupService.instance.triggerAutomaticBackup();ScaffoldMessenger.of(context).showSnackBar(- const SnackBar(- content: Text('Initial backup created!'),),+ SnackBar(+ content: Text(l10n.initialBackupCreated),),);}else {// we ask user to set a backup locationScaffoldMessenger.of(context).showSnackBar(- const SnackBar(- content:- Text('Please choose a backup location.'),),+ SnackBar(+ content:+ Text(l10n.pleaseChooseBackupLocation),),);}}@@ -108,12 +141,12 @@ class _LocalBackupSettingsPageState extends State<LocalBackupSettingsPage> {crossAxisAlignment: CrossAxisAlignment.start,children: [Text(- "Backup Location",+ l10n.backupLocation,style: getEnteTextTheme(context).largeBold,),const SizedBox(height: 8),Text(- "Select a folder to save backups. Backups run automatically when entries are added, deleted, or edited.",+ l10n.backupLocationDescription,style: getEnteTextTheme(context).small,),const SizedBox(height: 16),@@ -121,12 +154,12 @@ class _LocalBackupSettingsPageState extends State<LocalBackupSettingsPage> {crossAxisAlignment: CrossAxisAlignment.start,children: [Text(- 'Current Location:',+ l10n.currentLocation,style: getEnteTextTheme(context).body,),const SizedBox(height: 4),Text(- _backupPath ?? 'Not set. Please choose a location.',+ _backupPath ?? l10n.notSetPleaseChooseLocation,style: getEnteTextTheme(context).small.copyWith(color: _backupPath != null? null@@ -138,9 +171,23 @@ class _LocalBackupSettingsPageState extends State<LocalBackupSettingsPage> {width: double.infinity,child: ElevatedButton(onPressed: _pickAndSaveBackupLocation,- child: const Text('Choose Location'),+ child: Text(l10n.chooseLocation),),),++ const SizedBox(height: 10),+ SizedBox(+ width: double.infinity,+ child: ElevatedButton(+ style: ElevatedButton.styleFrom(+ backgroundColor: Theme.of(context).colorScheme.secondary,+ foregroundColor: Theme.of(context).colorScheme.onSecondary,+ ),+ onPressed: _showSetPasswordDialog,+ child: const Text('Set Backup Password'),+ ),+ ),+],),const SizedBox(height: 20),@@ -166,7 +213,7 @@ class _LocalBackupSettingsPageState extends State<LocalBackupSettingsPage> {),const SizedBox(width: 8),Text(- "Security Notice",+ l10n.securityNotice,style: getEnteTextTheme(context).smallBold.copyWith(@@ -177,7 +224,7 @@ class _LocalBackupSettingsPageState extends State<LocalBackupSettingsPage> {),const SizedBox(height: 8),Text(- "we can give any security warnings here- like say: make sure you choose a safe location etc??",+ l10n.backupSecurityNotice,style: getEnteTextTheme(context).mini.copyWith(color: Colors.orange.shade700,),