Skip to content

armgit / ente

Public Imported from GitHub
0

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

Import Google Authenticator QR exports from scanner

Neeraj Guptaf8e1a9c12f27
diff --git a/mobile/apps/auth/changes/google-authenticator-qr-import.md b/mobile/apps/auth/changes/google-authenticator-qr-import.md
new file mode 100644
index 0000000000..0b8f6fcda7
--- /dev/null
+++ b/mobile/apps/auth/changes/google-authenticator-qr-import.md
@@ -0,0 +1 @@
+- Improved support for importing codes from Google Authenticator QR exports.
diff --git a/mobile/apps/auth/lib/ui/home_page.dart b/mobile/apps/auth/lib/ui/home_page.dart
index 2fe625894a..77ac562793 100644
--- a/mobile/apps/auth/lib/ui/home_page.dart
+++ b/mobile/apps/auth/lib/ui/home_page.dart
@@ -39,6 +39,8 @@ import 'package:ente_auth/ui/home/speed_dial_label_widget.dart';
import 'package:ente_auth/ui/home/widgets/auth_logo_widget.dart';
import 'package:ente_auth/ui/reorder_codes_page.dart';
import 'package:ente_auth/ui/scanner_page.dart';
+import 'package:ente_auth/ui/settings/data/import/google_auth_import.dart';
+import 'package:ente_auth/ui/settings/data/import/import_success.dart';
import 'package:ente_auth/ui/settings_page.dart';
import 'package:ente_auth/ui/share/code_share.dart';
import 'package:ente_auth/ui/sort_option_menu.dart';
@@ -1353,13 +1355,26 @@ class _HomePageState extends State<HomePage> {
),
);
if (result != null) {
+ final googleAuthCodes = result.googleAuthCodes;
+ if (googleAuthCodes != null) {
+ final importedCodeCount = await importGoogleAuthCodes(googleAuthCodes);
+ if (importedCodeCount > 0) {
+ LocalBackupService.instance.triggerDailyBackupIfNeeded().ignore();
+ }
+ await importSuccessDialog(context, importedCodeCount);
+ return;
+ }
+ final code = result.code;
+ if (code == null) {
+ return;
+ }
await CodeStore.instance.addCode(
- result.code,
+ code,
shouldSync: result.fromGallery ? false : true,
);
// Focus the new code by searching
if (_shouldFocusAddedCode) {
- _focusNewCode(result.code);
+ _focusNewCode(code);
}
LocalBackupService.instance.triggerDailyBackupIfNeeded().ignore();
}
diff --git a/mobile/apps/auth/lib/ui/scanner_page.dart b/mobile/apps/auth/lib/ui/scanner_page.dart
index cbd96908e2..7375142986 100644
--- a/mobile/apps/auth/lib/ui/scanner_page.dart
+++ b/mobile/apps/auth/lib/ui/scanner_page.dart
@@ -5,6 +5,7 @@ import 'package:ente_auth/l10n/l10n.dart';
import 'package:ente_auth/models/code.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/utils/gallery_import_util.dart';
import 'package:ente_auth/utils/toast_util.dart';
import 'package:ente_pure_utils/ente_pure_utils.dart';
@@ -13,10 +14,20 @@ import 'package:logging/logging.dart';
import 'package:qr_code_scanner_plus/qr_code_scanner_plus.dart';
class ScannerPageResult {
- final Code code;
+ final Code? code;
final bool fromGallery;
+ final List<Code>? googleAuthCodes;
- const ScannerPageResult({required this.code, required this.fromGallery});
+ const ScannerPageResult.single({
+ required Code this.code,
+ required this.fromGallery,
+ }) : googleAuthCodes = null;
+
+ int get importedCodeCount => googleAuthCodes?.length ?? 0;
+
+ const ScannerPageResult.googleAuthCodes(this.googleAuthCodes)
+ : code = null,
+ fromGallery = false;
}
class ScannerPage extends StatefulWidget {
@@ -34,6 +45,7 @@ class ScannerPageState extends State<ScannerPage> {
String? totp;
bool _isImportingFromGallery = false;
bool _hasCompletedScan = false;
+ bool _isHandlingGoogleAuthImport = false;
// In order to get hot reload to work we need to pause the camera if the platform
// is android, or resume the camera if the platform is iOS.
@@ -156,9 +168,16 @@ class ScannerPageState extends State<ScannerPage> {
return;
}
+ if (isGoogleAuthExportQr(qrCode)) {
+ unawaited(_handleGoogleAuthImport(qrCode));
+ return;
+ }
+
try {
final code = Code.fromOTPAuthUrl(qrCode);
- _completeWithResult(ScannerPageResult(code: code, fromGallery: false));
+ _completeWithResult(
+ ScannerPageResult.single(code: code, fromGallery: false),
+ );
} catch (e) {
if (mounted) {
showToast(context, context.l10n.invalidQRCode);
@@ -166,6 +185,43 @@ class ScannerPageState extends State<ScannerPage> {
}
}
+ Future<void> _handleGoogleAuthImport(String qrCode) async {
+ if (_hasCompletedScan || _isHandlingGoogleAuthImport) {
+ return;
+ }
+ _isHandlingGoogleAuthImport = true;
+ bool shouldResumeCamera = true;
+ try {
+ await controller?.pauseCamera();
+ final codes = parseGoogleAuth(qrCode);
+ if (codes.isEmpty) {
+ if (mounted) {
+ showToast(context, context.l10n.invalidQRCode);
+ }
+ return;
+ }
+ if (!mounted || _hasCompletedScan) {
+ return;
+ }
+ final shouldImport = await confirmGoogleAuthImport(context, codes.length);
+ if (!shouldImport || !mounted || _hasCompletedScan) {
+ return;
+ }
+ shouldResumeCamera = false;
+ _completeWithResult(ScannerPageResult.googleAuthCodes(codes));
+ } catch (e, s) {
+ _logger.severe("Error importing Google Authenticator QR", e, s);
+ if (mounted) {
+ showToast(context, context.l10n.invalidQRCode);
+ }
+ } finally {
+ if (shouldResumeCamera && mounted && !_hasCompletedScan) {
+ await controller?.resumeCamera();
+ }
+ _isHandlingGoogleAuthImport = false;
+ }
+ }
+
Future<void> _handleImportFromGallery() async {
if (_isImportingFromGallery || _hasCompletedScan) {
return;
@@ -181,7 +237,9 @@ class ScannerPageState extends State<ScannerPage> {
return;
}
shouldResumeCamera = false;
- _completeWithResult(ScannerPageResult(code: code, fromGallery: true));
+ _completeWithResult(
+ ScannerPageResult.single(code: code, fromGallery: true),
+ );
} finally {
if (shouldResumeCamera && mounted && !_hasCompletedScan) {
await controller?.resumeCamera();
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 a32cdf9d0e..016ac55ec3 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
@@ -19,6 +19,10 @@ import 'package:logging/logging.dart';
const kGoogleAuthExportPrefix = 'otpauth-migration://offline?data=';
+bool isGoogleAuthExportQr(String qrCodeData) {
+ return qrCodeData.startsWith(kGoogleAuthExportPrefix);
+}
+
Future<void> showGoogleAuthInstruction(BuildContext context) async {
final l10n = context.l10n;
final result = await showDialogWidget(
@@ -55,14 +59,55 @@ Future<void> showGoogleAuthInstruction(BuildContext context) async {
if (codes == null || codes.isEmpty) {
return;
}
- for (final code in codes) {
- await CodeStore.instance.addCode(code, shouldSync: false);
- }
- unawaited(AuthenticatorService.instance.onlineSync());
+ final importedCount = await importGoogleAuthCodes(codes);
// ignore: unawaited_futures
- importSuccessDialog(context, codes.length);
+ importSuccessDialog(context, importedCount);
+ }
+ }
+}
+
+Future<bool> confirmGoogleAuthImport(
+ BuildContext context,
+ int codeCount,
+) async {
+ final l10n = context.l10n;
+ final result = await showDialogWidget(
+ context: context,
+ title: l10n.importFromApp("Google Authenticator"),
+ body:
+ "Import $codeCount ${codeCount == 1 ? "code" : "codes"} from Google Authenticator?",
+ buttons: [
+ ButtonWidget(
+ buttonType: ButtonType.primary,
+ labelText: l10n.importLabel,
+ isInAlert: true,
+ buttonSize: ButtonSize.large,
+ buttonAction: ButtonAction.first,
+ ),
+ ButtonWidget(
+ buttonType: ButtonType.secondary,
+ labelText: l10n.cancel,
+ buttonSize: ButtonSize.large,
+ isInAlert: true,
+ buttonAction: ButtonAction.cancel,
+ ),
+ ],
+ );
+ return result?.action == ButtonAction.first;
+}
+
+Future<int> importGoogleAuthCodes(List<Code> codes) async {
+ int importedCount = 0;
+ for (final code in codes) {
+ final result = await CodeStore.instance.addCode(code, shouldSync: false);
+ if (result != AddResult.duplicate) {
+ importedCount++;
}
}
+ if (importedCount > 0) {
+ unawaited(AuthenticatorService.instance.onlineSync());
+ }
+ return importedCount;
}
List<Code> parseGoogleAuth(String qrCodeData) {
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
new file mode 100644
index 0000000000..05969e016f
--- /dev/null
+++ b/mobile/apps/auth/test/ui/settings/data/import/google_auth_import_test.dart
@@ -0,0 +1,52 @@
+import 'dart:convert';
+import 'dart:typed_data';
+
+import 'package:base32/base32.dart';
+import 'package:ente_auth/models/code.dart';
+import 'package:ente_auth/models/protos/googleauth.pb.dart';
+import 'package:ente_auth/ui/settings/data/import/google_auth_import.dart';
+import 'package:fixnum/fixnum.dart';
+import 'package:flutter_test/flutter_test.dart';
+
+void main() {
+ test('parses Google Authenticator migration QR codes', () {
+ final secret = Uint8List.fromList([1, 2, 3, 4, 5, 6, 7, 8]);
+ final payload = MigrationPayload()
+ ..otpParameters.add(
+ MigrationPayload_OtpParameters()
+ ..issuer = 'GitHub'
+ ..name = '[email protected]'
+ ..secret = secret
+ ..algorithm = MigrationPayload_Algorithm.ALGORITHM_SHA256
+ ..digits = MigrationPayload_DigitCount.DIGIT_COUNT_EIGHT
+ ..type = MigrationPayload_OtpType.OTP_TYPE_TOTP,
+ )
+ ..otpParameters.add(
+ MigrationPayload_OtpParameters()
+ ..issuer = 'Example'
+ ..name = '[email protected]'
+ ..secret = secret
+ ..algorithm = MigrationPayload_Algorithm.ALGORITHM_SHA1
+ ..digits = MigrationPayload_DigitCount.DIGIT_COUNT_SIX
+ ..type = MigrationPayload_OtpType.OTP_TYPE_HOTP
+ ..counter = Int64(42),
+ );
+ final qrCode =
+ '$kGoogleAuthExportPrefix${Uri.encodeComponent(base64Encode(payload.writeToBuffer()))}';
+
+ expect(isGoogleAuthExportQr(qrCode), true);
+
+ final codes = parseGoogleAuth(qrCode);
+ expect(codes, hasLength(2));
+ expect(codes[0].issuer, 'GitHub');
+ expect(codes[0].account, '[email protected]');
+ expect(codes[0].secret, base32.encode(secret));
+ expect(codes[0].algorithm, Algorithm.sha256);
+ expect(codes[0].digits, 8);
+ expect(codes[0].type, Type.totp);
+ expect(codes[1].issuer, 'Example');
+ expect(codes[1].account, '[email protected]');
+ expect(codes[1].counter, 42);
+ expect(codes[1].type, Type.hotp);
+ });
+}