Skip to content

armgit / ente

Public Imported from GitHub
0

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

Improve Google Authenticator batch imports

Neeraj Gupta1812fff3d974
diff --git a/mobile/apps/auth/lib/ui/home_page.dart b/mobile/apps/auth/lib/ui/home_page.dart
index 92c9d3663e..b5b2165d35 100644
--- a/mobile/apps/auth/lib/ui/home_page.dart
+++ b/mobile/apps/auth/lib/ui/home_page.dart
@@ -1268,19 +1268,22 @@ class _HomePageState extends State<HomePage> {
context,
logger: _logger,
);
- if (importResult == null) {
+ if (importResult == null || !mounted) {
return;
}
- final googleAuthCodes = importResult.googleAuthCodes;
- if (googleAuthCodes != null) {
- if (!mounted) return;
+ final firstMigration = importResult.googleAuthMigration;
+ if (firstMigration != null) {
+ final googleAuthCodes = await collectGoogleAuthImageBatches(
+ context,
+ firstMigration,
+ logger: _logger,
+ );
+ if (!mounted || googleAuthCodes == null) return;
final shouldImport = await confirmGoogleAuthImport(
context,
googleAuthCodes.length,
);
- if (!shouldImport) {
- return;
- }
+ if (!shouldImport) return;
await _completeGoogleAuthImport(googleAuthCodes);
return;
}
diff --git a/mobile/apps/auth/lib/ui/scanner_page.dart b/mobile/apps/auth/lib/ui/scanner_page.dart
index 6a805766e3..62fed42df7 100644
--- a/mobile/apps/auth/lib/ui/scanner_page.dart
+++ b/mobile/apps/auth/lib/ui/scanner_page.dart
@@ -6,6 +6,7 @@ import 'package:ente_auth/theme/ente_theme.dart';
import 'package:ente_auth/ui/components/buttons/icon_button_widget.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/utils/gallery_import_util.dart';
import 'package:ente_auth/utils/toast_util.dart';
import 'package:ente_pure_utils/ente_pure_utils.dart';
@@ -47,6 +48,7 @@ class ScannerPageState extends State<ScannerPage> {
bool _isHandlingGoogleAuthImport = false;
bool _isTogglingFlash = false;
bool? _isFlashOn;
+ final _migrationTracker = GoogleAuthMigrationTracker();
// Pause the camera on Android and resume it on iOS for hot reload.
@override
@@ -104,8 +106,18 @@ class ScannerPageState extends State<ScannerPage> {
horizontal: 16,
vertical: 8,
),
- child: showGalleryImport
- ? Row(
+ child: Column(
+ mainAxisSize: MainAxisSize.min,
+ children: [
+ if (_migrationTracker.batchSize > 0)
+ Text(
+ '${l10n.scanACode} '
+ '(${_migrationTracker.receivedBatchCount}/'
+ '${_migrationTracker.batchSize})',
+ style: Theme.of(context).textTheme.titleMedium,
+ ),
+ if (showGalleryImport)
+ Row(
children: [
const Spacer(),
if (showTorch)
@@ -140,11 +152,15 @@ class ScannerPageState extends State<ScannerPage> {
iconColor: actionIconColor,
),
],
- )
- : Text(
+ ),
+ if (!showGalleryImport &&
+ _migrationTracker.batchSize == 0)
+ Text(
l10n.scanACode,
style: Theme.of(context).textTheme.titleMedium,
),
+ ],
+ ),
),
),
),
@@ -275,16 +291,29 @@ class ScannerPageState extends State<ScannerPage> {
return;
}
_isHandlingGoogleAuthImport = true;
- bool shouldResumeCamera = true;
+ bool shouldResumeCamera = false;
try {
- await controller?.pause();
- final codes = parseGoogleAuth(qrCode);
- if (codes.isEmpty) {
+ final migration = parseGoogleAuthMigration(qrCode);
+ if (migration.codes.isEmpty) {
if (mounted) {
showToastAboveBottomControls(context, context.strings.invalidQRCode);
}
return;
}
+ final receivedBatchCount = _migrationTracker.receivedBatchCount;
+ final codes = _migrationTracker.add(migration);
+ if (codes == null) {
+ if (mounted &&
+ receivedBatchCount != _migrationTracker.receivedBatchCount) {
+ setState(() {});
+ }
+ return;
+ }
+ if (!mounted || _hasCompletedScan) {
+ return;
+ }
+ await controller?.pause();
+ shouldResumeCamera = true;
if (!mounted || _hasCompletedScan) {
return;
}
@@ -294,6 +323,13 @@ class ScannerPageState extends State<ScannerPage> {
}
shouldResumeCamera = false;
_completeWithResult(ScannerPageResult.googleAuthCodes(codes));
+ } on FormatException catch (error) {
+ if (mounted) {
+ showToastAboveBottomControls(
+ context,
+ '${context.strings.error} ${error.message}',
+ );
+ }
} catch (e, s) {
_logger.severe("Error importing Google Authenticator QR", e, s);
if (mounted) {
@@ -323,19 +359,23 @@ class ScannerPageState extends State<ScannerPage> {
context,
logger: _logger,
);
- if (importResult == null) {
+ if (importResult == null || !mounted) {
return;
}
- final googleAuthCodes = importResult.googleAuthCodes;
- if (googleAuthCodes != null) {
- if (!mounted) return;
+ final firstMigration = importResult.googleAuthMigration;
+ if (firstMigration != null) {
+ final googleAuthCodes = await collectGoogleAuthImageBatches(
+ context,
+ firstMigration,
+ logger: _logger,
+ tracker: _migrationTracker,
+ );
+ if (!mounted || _hasCompletedScan || googleAuthCodes == null) return;
final shouldImport = await confirmGoogleAuthImport(
context,
googleAuthCodes.length,
);
- if (!shouldImport || !mounted || _hasCompletedScan) {
- return;
- }
+ if (!shouldImport || !mounted || _hasCompletedScan) return;
shouldResumeCamera = false;
_completeWithResult(ScannerPageResult.googleAuthCodes(googleAuthCodes));
return;
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.dart
index bfb188a152..c7b84e1bf4 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,6 +5,7 @@ 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';
@@ -17,6 +18,8 @@ import 'package:logging/logging.dart';
export 'package:ente_auth/ui/settings/data/import/google_auth_qr_parser.dart';
+final _logger = Logger('GoogleAuthImport');
+
Future<bool> showGoogleAuthInstruction(BuildContext context) async {
final l10n = context.strings;
final isMobile = PlatformDetector.isMobile();
@@ -64,80 +67,121 @@ Future<bool> showGoogleAuthInstruction(BuildContext context) async {
}
Future<bool> _importGoogleAuthFromImage(BuildContext context) async {
- final migrationTracker = GoogleAuthMigrationTracker();
- List<Code>? codes;
- while (codes == null) {
- if (!context.mounted) return false;
- final importResult = await pickCodeFromImage(
- context,
- logger: Logger("GoogleAuthImport"),
- pickFromFiles: true,
- );
- if (importResult == null) return false;
+ if (!context.mounted) return false;
+ final importResult = await pickCodeFromImage(
+ context,
+ logger: _logger,
+ pickFromFiles: true,
+ );
+ if (importResult == null || !context.mounted) return false;
+ final codes = await collectGoogleAuthImageBatches(
+ context,
+ importResult.googleAuthMigration,
+ logger: _logger,
+ pickFromFiles: true,
+ );
- final migration = importResult.googleAuthMigration;
- if (migration == null || migration.codes.isEmpty) {
- if (!context.mounted) return false;
+ if (!context.mounted || codes == null) return false;
+ return _completeGoogleAuthImport(context, codes);
+}
+
+Future<List<Code>?> collectGoogleAuthImageBatches(
+ BuildContext context,
+ GoogleAuthMigration? migration, {
+ required Logger logger,
+ bool pickFromFiles = false,
+ GoogleAuthMigrationTracker? tracker,
+}) async {
+ final migrationTracker = tracker ?? GoogleAuthMigrationTracker();
+ while (true) {
+ final currentMigration = migration;
+ if (currentMigration == null || currentMigration.codes.isEmpty) {
+ if (!context.mounted) return null;
await showErrorDialog(
context,
context.strings.invalidQRCode,
context.strings.errorInvalidQRCodeBody,
);
- return false;
+ return null;
}
+
try {
- codes = migrationTracker.add(migration);
+ final codes = migrationTracker.add(currentMigration);
+ if (codes != null) return codes;
} on FormatException catch (error) {
- if (!context.mounted) return false;
+ if (!context.mounted) return null;
await showErrorDialog(
context,
context.strings.invalidQRCode,
error.message,
);
- return false;
+ return null;
}
- 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, codes.length);
- if (!shouldImport || !context.mounted) {
- return false;
+ if (!context.mounted) return null;
+ 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 || !context.mounted) {
+ return null;
+ }
+ final importResult = await pickCodeFromImage(
+ context,
+ logger: logger,
+ pickFromFiles: pickFromFiles,
+ );
+ if (importResult == null) return null;
+ migration = importResult.googleAuthMigration;
}
- return _completeGoogleAuthImport(context, codes);
}
Future<bool> _completeGoogleAuthImport(
BuildContext context,
List<Code> codes,
) async {
- final importedCount = await importGoogleAuthCodes(codes);
- if (!context.mounted) return false;
- await importSuccessDialog(context, importedCount);
+ int? importedCount;
+ final shouldImport = await confirmGoogleAuthImport(
+ context,
+ codes.length,
+ onImport: () async {
+ try {
+ importedCount = await importGoogleAuthCodes(codes);
+ } catch (error, stackTrace) {
+ _logger.severe(
+ 'Failed to import Google Authenticator codes',
+ error,
+ stackTrace,
+ );
+ if (context.mounted) {
+ await showGenericErrorDialog(context: context, error: error);
+ }
+ rethrow;
+ }
+ },
+ );
+ final count = importedCount;
+ if (!shouldImport || count == null || !context.mounted) return false;
+ await importSuccessDialog(context, count);
return true;
}
Future<bool> confirmGoogleAuthImport(
BuildContext context,
- int codeCount,
-) async {
+ int codeCount, {
+ FutureOr<void> Function()? onImport,
+}) async {
final l10n = context.strings;
final result = await showImportInstructionSheet(
context: context,
@@ -149,6 +193,7 @@ Future<bool> confirmGoogleAuthImport(
ImportInstructionAction(
label: l10n.importLabel,
result: ImportInstructionResult.primary,
+ onTap: onImport,
),
],
);
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.dart
index 3d5eec8559..72af7e3c26 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
@@ -20,8 +20,7 @@ class GoogleAuthMigrationTracker {
_reset();
return migration.codes;
}
- if (!migration.hasValidBatchMetadata ||
- (migration.batchSize > 1 && migration.batchId == 0)) {
+ if (!migration.hasValidBatchMetadata) {
throw const FormatException('Invalid Google Authenticator export batch');
}
if (migration.batchSize == 1) {
@@ -42,15 +41,6 @@ class GoogleAuthMigrationTracker {
return codes;
}
- bool record(GoogleAuthMigration migration) {
- if (!migration.hasValidBatchMetadata) return false;
- try {
- return add(migration) != null;
- } on FormatException {
- return false;
- }
- }
-
void _reset() {
_batchId = null;
_batchSize = null;
diff --git a/mobile/apps/auth/lib/ui/settings/data/import/import_instruction_sheet.dart b/mobile/apps/auth/lib/ui/settings/data/import/import_instruction_sheet.dart
index cf7e3fe783..bcd4137f30 100644
--- a/mobile/apps/auth/lib/ui/settings/data/import/import_instruction_sheet.dart
+++ b/mobile/apps/auth/lib/ui/settings/data/import/import_instruction_sheet.dart
@@ -1,3 +1,5 @@
+import 'dart:async';
+
import 'package:ente_components/ente_components.dart';
import 'package:flutter/material.dart';
@@ -8,11 +10,13 @@ class ImportInstructionAction {
required this.label,
required this.result,
this.variant = ButtonComponentVariant.primary,
+ this.onTap,
});
final String label;
final ImportInstructionResult result;
final ButtonComponentVariant variant;
+ final FutureOr<void> Function()? onTap;
}
Future<ImportInstructionResult?> showImportInstructionSheet({
@@ -25,38 +29,68 @@ Future<ImportInstructionResult?> showImportInstructionSheet({
String? semanticsIdentifier,
}) {
assert(body != null || content != null);
+ final hasAsyncAction = actions.any((action) => action.onTap != null);
+ ImportInstructionAction? executingAction;
return showBottomSheetComponent<ImportInstructionResult>(
context: context,
- builder: (sheetContext) {
- final colors = sheetContext.componentColors;
- final sheetContent =
- content ??
- Text(body!, style: TextStyles.body.copyWith(color: colors.textLight));
- final sheet = BottomSheetComponent(
- title: title,
- closeTooltip: cancelLabel,
- content: ConstrainedBox(
- constraints: BoxConstraints(
- maxHeight: MediaQuery.sizeOf(sheetContext).height * 0.45,
- ),
- child: SizedBox(
- width: double.infinity,
- child: SingleChildScrollView(child: sheetContent),
- ),
- ),
- actions: [
- for (final action in actions)
- ButtonComponent(
- label: action.label,
- variant: action.variant,
- shouldSurfaceExecutionStates: false,
- onTap: () => Navigator.of(sheetContext).pop(action.result),
+ isDismissible: !hasAsyncAction,
+ enableDrag: !hasAsyncAction,
+ builder: (_) {
+ return StatefulBuilder(
+ builder: (context, setState) {
+ final colors = context.componentColors;
+ final sheetContent =
+ content ??
+ Text(
+ body!,
+ style: TextStyles.body.copyWith(color: colors.textLight),
+ );
+ final sheet = BottomSheetComponent(
+ title: title,
+ closeTooltip: cancelLabel,
+ showCloseButton: executingAction == null,
+ content: ConstrainedBox(
+ constraints: BoxConstraints(
+ maxHeight: MediaQuery.sizeOf(context).height * 0.45,
+ ),
+ child: SizedBox(
+ width: double.infinity,
+ child: SingleChildScrollView(child: sheetContent),
+ ),
),
- ],
+ actions: [
+ for (final action in actions)
+ ButtonComponent(
+ label: action.label,
+ variant: action.variant,
+ isDisabled:
+ executingAction != null && executingAction != action,
+ shouldSurfaceExecutionStates: action.onTap != null,
+ onTap: action.onTap == null
+ ? () => Navigator.of(context).pop(action.result)
+ : () async {
+ setState(() => executingAction = action);
+ try {
+ await action.onTap!();
+ if (context.mounted &&
+ ModalRoute.of(context)?.isCurrent == true) {
+ Navigator.of(context).pop(action.result);
+ }
+ } finally {
+ if (context.mounted &&
+ ModalRoute.of(context)?.isCurrent == true) {
+ setState(() => executingAction = null);
+ }
+ }
+ },
+ ),
+ ],
+ );
+ return semanticsIdentifier == null
+ ? sheet
+ : Semantics(identifier: semanticsIdentifier, child: sheet);
+ },
);
- return semanticsIdentifier == null
- ? sheet
- : Semantics(identifier: semanticsIdentifier, child: sheet);
},
);
}
diff --git a/mobile/apps/auth/test/ui/settings/data/import/google_auth_import_test.dart b/mobile/apps/auth/test/ui/settings/data/import/google_auth_import_test.dart
index 8dc05d7f2b..44a5099c37 100644
--- a/mobile/apps/auth/test/ui/settings/data/import/google_auth_import_test.dart
+++ b/mobile/apps/auth/test/ui/settings/data/import/google_auth_import_test.dart
@@ -51,8 +51,17 @@ void main() {
final tracker = GoogleAuthMigrationTracker();
expect(
- tracker.record(_testMigration(batchId: 42, batchSize: 1, batchIndex: 0)),
- true,
+ tracker.add(_testMigration(batchId: 42, batchSize: 1, batchIndex: 0)),
+ isNotNull,
+ );
+ });
+
+ test('completes an export without batch metadata', () {
+ final tracker = GoogleAuthMigrationTracker();
+
+ expect(
+ tracker.add(_testMigration(batchId: 0, batchSize: 0, batchIndex: 0)),
+ isNotNull,
);
});
@@ -60,16 +69,29 @@ void main() {
final tracker = GoogleAuthMigrationTracker();
expect(
- tracker.record(_testMigration(batchId: 42, batchSize: 3, batchIndex: 2)),
- false,
+ tracker.add(_testMigration(batchId: 42, batchSize: 3, batchIndex: 2)),
+ isNull,
+ );
+ expect(
+ tracker.add(_testMigration(batchId: 42, batchSize: 3, batchIndex: 0)),
+ isNull,
+ );
+ expect(
+ tracker.add(_testMigration(batchId: 42, batchSize: 3, batchIndex: 1)),
+ isNotNull,
);
+ });
+
+ test('accepts multi-batch exports with a zero batch id', () {
+ final tracker = GoogleAuthMigrationTracker();
+
expect(
- tracker.record(_testMigration(batchId: 42, batchSize: 3, batchIndex: 0)),
- false,
+ tracker.add(_testMigration(batchId: 0, batchSize: 2, batchIndex: 0)),
+ isNull,
);
expect(
- tracker.record(_testMigration(batchId: 42, batchSize: 3, batchIndex: 1)),
- true,
+ tracker.add(_testMigration(batchId: 0, batchSize: 2, batchIndex: 1)),
+ isNotNull,
);
});
@@ -77,37 +99,31 @@ void main() {
final tracker = GoogleAuthMigrationTracker();
expect(
- tracker.record(_testMigration(batchId: 42, batchSize: 2, batchIndex: 0)),
- false,
+ tracker.add(_testMigration(batchId: 42, batchSize: 2, batchIndex: 0)),
+ isNull,
);
expect(
- tracker.record(_testMigration(batchId: 42, batchSize: 2, batchIndex: 0)),
- false,
+ tracker.add(_testMigration(batchId: 42, batchSize: 2, batchIndex: 0)),
+ isNull,
);
expect(
- tracker.record(_testMigration(batchId: 7, batchSize: 2, batchIndex: 1)),
- false,
+ () =>
+ tracker.add(_testMigration(batchId: 7, batchSize: 2, batchIndex: 1)),
+ throwsFormatException,
);
expect(
- tracker.record(_testMigration(batchId: 42, batchSize: 2, batchIndex: 1)),
- true,
+ tracker.add(_testMigration(batchId: 42, batchSize: 2, batchIndex: 1)),
+ isNotNull,
);
});
- test('does not complete without valid migration batch metadata', () {
+ test('rejects invalid migration batch metadata', () {
final tracker = GoogleAuthMigrationTracker();
expect(
- tracker.record(_testMigration(batchId: 0, batchSize: 2, batchIndex: 1)),
- false,
- );
- expect(
- tracker.record(_testMigration(batchId: 42, batchSize: 0, batchIndex: 0)),
- false,
- );
- expect(
- tracker.record(_testMigration(batchId: 42, batchSize: 2, batchIndex: 2)),
- false,
+ () =>
+ tracker.add(_testMigration(batchId: 42, batchSize: 2, batchIndex: 2)),
+ throwsFormatException,
);
});