π End-to-end encrypted cloud for everything.
Dart 2faandroidauthye2eeencryptionend-to-end-encryptionfluttergolanggoogle-photosgoogle-photos-alternativeiosopensourcephotosprivacyprivacy-toolstypescriptzero-knowledge
Import Google Authenticator QR batches together
Neeraj Gupta54b37826c622
diff --git a/mobile/apps/auth/changes/google-auth-multi-qr-import.md b/mobile/apps/auth/changes/google-auth-multi-qr-import.mdnew file mode 100644index 0000000000..7b3fa0ef38--- /dev/null+++ b/mobile/apps/auth/changes/google-auth-multi-qr-import.md@@ -0,0 +1 @@+- Import all Google Authenticator QR batches in one session.diff --git a/mobile/apps/auth/lib/ui/scanner_gauth_page.dart b/mobile/apps/auth/lib/ui/scanner_gauth_page.dartindex b578b8d14c..72b0c6ab75 100644--- a/mobile/apps/auth/lib/ui/scanner_gauth_page.dart+++ b/mobile/apps/auth/lib/ui/scanner_gauth_page.dart@@ -1,9 +1,11 @@import 'dart:async';import 'dart:io';+import 'package:ente_auth/models/code.dart';import 'package:ente_auth/theme/ente_theme.dart';import 'package:ente_auth/ui/components/scanner_camera_view.dart';-import 'package:ente_auth/ui/settings/data/import/google_auth_import.dart';+import 'package:ente_auth/ui/settings/data/import/google_auth_migration_tracker.dart';+import 'package:ente_auth/ui/settings/data/import/google_auth_qr_parser.dart';import 'package:ente_auth/utils/toast_util.dart';import 'package:ente_qr_scanner/ente_qr_scanner.dart';import 'package:ente_strings/ente_strings.dart';@@ -19,6 +21,7 @@ class ScannerGoogleAuthPage extends StatefulWidget {class ScannerGoogleAuthPageState extends State<ScannerGoogleAuthPage> {EnteQrScannerController? controller;StreamSubscription<String>? _scanSubscription;+ final _migrationTracker = GoogleAuthMigrationTracker();bool _hasCompletedScan = false;// Pause the camera on Android and resume it on iOS for hot reload.@@ -35,6 +38,11 @@ class ScannerGoogleAuthPageState extends State<ScannerGoogleAuthPage> {@overrideWidget build(BuildContext context) {final l10n = context.strings;+ final progress = _migrationTracker.batchSize == 0+ ? l10n.scanACode+ : '${l10n.scanACode} '+ '(${_migrationTracker.receivedBatchCount}/'+ '${_migrationTracker.batchSize})';return Scaffold(appBar: AppBar(title: Text(l10n.scan)),body: Column(@@ -50,7 +58,7 @@ class ScannerGoogleAuthPageState extends State<ScannerGoogleAuthPage> {onScannerCreated: _onScannerCreated,),),- Expanded(flex: 1, child: Center(child: Text(l10n.scanACode))),+ Expanded(flex: 1, child: Center(child: Text(progress))),],),);@@ -76,13 +84,20 @@ class ScannerGoogleAuthPageState extends State<ScannerGoogleAuthPage> {try {final migration = parseGoogleAuthMigration(qrCode);- _completeWithMigration(migration);+ final receivedBatchCount = _migrationTracker.receivedBatchCount;+ final codes = _migrationTracker.add(migration);+ if (codes != null) {+ _completeWithCodes(codes);+ } else if (receivedBatchCount != _migrationTracker.receivedBatchCount &&+ mounted) {+ setState(() {});+ }} catch (e) {- _completeWithError(e);+ _showError(e);}}- void _completeWithMigration(GoogleAuthMigration migration) {+ void _completeWithCodes(List<Code> codes) {if (_hasCompletedScan) {return;}@@ -91,19 +106,11 @@ class ScannerGoogleAuthPageState extends State<ScannerGoogleAuthPage> {if (!mounted) {return;}- Navigator.of(context).pop(migration);+ Navigator.of(context).pop(codes);}- void _completeWithError(Object error) {- if (_hasCompletedScan) {- return;- }- _hasCompletedScan = true;- _cancelScanSubscription();- if (!mounted) {- return;- }- Navigator.of(context).pop();+ void _showError(Object error) {+ if (!mounted) return;showToastAboveBottomControls(context, "${context.strings.error} $error");}diff --git a/mobile/apps/auth/lib/ui/settings/data/import/google_auth_import.dart b/mobile/apps/auth/lib/ui/settings/data/import/google_auth_import.dartindex 2ebffa6c09..bfb188a152 100644--- a/mobile/apps/auth/lib/ui/settings/data/import/google_auth_import.dart+++ b/mobile/apps/auth/lib/ui/settings/data/import/google_auth_import.dart@@ -5,7 +5,6 @@ import 'package:ente_auth/services/authenticator_service.dart';import 'package:ente_auth/store/code_store.dart';import 'package:ente_auth/ui/scanner_gauth_page.dart';import 'package:ente_auth/ui/settings/data/import/google_auth_migration_tracker.dart';-import 'package:ente_auth/ui/settings/data/import/google_auth_qr_parser.dart';import 'package:ente_auth/ui/settings/data/import/import_instruction_sheet.dart';import 'package:ente_auth/ui/settings/data/import/import_success.dart';import 'package:ente_auth/utils/dialog_util.dart';@@ -18,8 +17,6 @@ import 'package:logging/logging.dart';export 'package:ente_auth/ui/settings/data/import/google_auth_qr_parser.dart';-final _migrationTracker = GoogleAuthMigrationTracker();-Future<bool> showGoogleAuthInstruction(BuildContext context) async {final l10n = context.strings;final isMobile = PlatformDetector.isMobile();@@ -50,60 +47,91 @@ Future<bool> showGoogleAuthInstruction(BuildContext context) async {if (!context.mounted) return false;switch (result) {case ImportInstructionResult.primary:- final GoogleAuthMigration? migration = await Navigator.of(context).push(+ final List<Code>? codes = await Navigator.of(context).push(MaterialPageRoute(builder: (BuildContext context) {return const ScannerGoogleAuthPage();},),);- if (!context.mounted || migration == null || migration.codes.isEmpty) {+ if (!context.mounted || codes == null || codes.isEmpty) {return false;}- return _completeGoogleAuthImport(context, migration);+ return _completeGoogleAuthImport(context, codes);case ImportInstructionResult.secondary:return _importGoogleAuthFromImage(context);}}Future<bool> _importGoogleAuthFromImage(BuildContext context) async {- final importResult = await pickCodeFromImage(- context,- logger: Logger("GoogleAuthImport"),- pickFromFiles: true,- );- if (importResult == null) {- return false;- }- final migration = importResult.googleAuthMigration;- if (migration == null || migration.codes.isEmpty) {+ final migrationTracker = GoogleAuthMigrationTracker();+ List<Code>? codes;+ while (codes == null) {if (!context.mounted) return false;- await showErrorDialog(+ final importResult = await pickCodeFromImage(context,- context.strings.invalidQRCode,- context.strings.errorInvalidQRCodeBody,+ logger: Logger("GoogleAuthImport"),+ pickFromFiles: true,);- return false;+ if (importResult == null) return false;++ final migration = importResult.googleAuthMigration;+ if (migration == null || migration.codes.isEmpty) {+ if (!context.mounted) return false;+ await showErrorDialog(+ context,+ context.strings.invalidQRCode,+ context.strings.errorInvalidQRCodeBody,+ );+ return false;+ }+ try {+ codes = migrationTracker.add(migration);+ } on FormatException catch (error) {+ if (!context.mounted) return false;+ await showErrorDialog(+ context,+ context.strings.invalidQRCode,+ error.message,+ );+ return false;+ }+ if (codes == null && context.mounted) {+ final result = await showImportInstructionSheet(+ context: context,+ title: 'Google Authenticator',+ body:+ '${context.strings.selectFile} '+ '(${migrationTracker.receivedBatchCount}/${migrationTracker.batchSize})',+ cancelLabel: context.strings.cancel,+ semanticsIdentifier: 'auth_import_google_authenticator_next_file',+ actions: [+ ImportInstructionAction(+ label: context.strings.selectFile,+ result: ImportInstructionResult.primary,+ ),+ ],+ );+ if (result != ImportInstructionResult.primary) return false;+ }}+if (!context.mounted) return false;- final shouldImport = await confirmGoogleAuthImport(- context,- migration.codes.length,- );+ final shouldImport = await confirmGoogleAuthImport(context, codes.length);if (!shouldImport || !context.mounted) {return false;}- return _completeGoogleAuthImport(context, migration);+ return _completeGoogleAuthImport(context, codes);}Future<bool> _completeGoogleAuthImport(BuildContext context,- GoogleAuthMigration migration,+ List<Code> codes,) async {- final importedCount = await importGoogleAuthCodes(migration.codes);+ final importedCount = await importGoogleAuthCodes(codes);if (!context.mounted) return false;await importSuccessDialog(context, importedCount);- return _migrationTracker.record(migration);+ return true;}Future<bool> confirmGoogleAuthImport(diff --git a/mobile/apps/auth/lib/ui/settings/data/import/google_auth_migration_tracker.dart b/mobile/apps/auth/lib/ui/settings/data/import/google_auth_migration_tracker.dartindex 7dd83465e5..3d5eec8559 100644--- a/mobile/apps/auth/lib/ui/settings/data/import/google_auth_migration_tracker.dart+++ b/mobile/apps/auth/lib/ui/settings/data/import/google_auth_migration_tracker.dart@@ -1,22 +1,59 @@+import 'package:ente_auth/models/code.dart';import 'package:ente_auth/ui/settings/data/import/google_auth_qr_parser.dart';class GoogleAuthMigrationTracker {- final Map<(int, int), Set<int>> _importedBatchIndices = {};+ int? _batchId;+ int? _batchSize;+ final Map<int, List<Code>> _batches = {};++ int get receivedBatchCount => _batches.length;+ int get batchSize => _batchSize ?? 0;++ List<Code>? add(GoogleAuthMigration migration) {+ if (_batchId != null &&+ (_batchId != migration.batchId || _batchSize != migration.batchSize)) {+ throw const FormatException(+ 'QR code belongs to a different Google Authenticator export',+ );+ }+ if (migration.batchSize == 0) {+ _reset();+ return migration.codes;+ }+ if (!migration.hasValidBatchMetadata ||+ (migration.batchSize > 1 && migration.batchId == 0)) {+ throw const FormatException('Invalid Google Authenticator export batch');+ }+ if (migration.batchSize == 1) {+ _reset();+ return migration.codes;+ }++ _batchId = migration.batchId;+ _batchSize = migration.batchSize;+ _batches.putIfAbsent(migration.batchIndex, () => migration.codes);+ if (_batches.length != migration.batchSize) return null;++ final codes = [+ for (var index = 0; index < migration.batchSize; index++)+ ..._batches[index]!,+ ];+ _reset();+ return codes;+ }bool record(GoogleAuthMigration migration) {if (!migration.hasValidBatchMetadata) return false;- if (migration.batchSize == 1) return true;- if (migration.batchId == 0) return false;-- final key = (migration.batchId, migration.batchSize);- final importedIndices = _importedBatchIndices.putIfAbsent(- key,- () => <int>{},- );- importedIndices.add(migration.batchIndex);- if (importedIndices.length != migration.batchSize) return false;+ try {+ return add(migration) != null;+ } on FormatException {+ return false;+ }+ }- _importedBatchIndices.remove(key);- return true;+ void _reset() {+ _batchId = null;+ _batchSize = null;+ _batches.clear();}}