π End-to-end encrypted cloud for everything.
Dart 2faandroidauthye2eeencryptionend-to-end-encryptionfluttergolanggoogle-photosgoogle-photos-alternativeiosopensourcephotosprivacyprivacy-toolstypescriptzero-knowledge
Extract Google Auth QR parser
Neeraj Gupta15a0002859db
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 016ac55ec3..7ce89438af 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@@ -1,10 +1,7 @@import 'dart:async';-import 'dart:convert';-import 'package:base32/base32.dart';import 'package:ente_auth/l10n/l10n.dart';import 'package:ente_auth/models/code.dart';-import 'package:ente_auth/models/protos/googleauth.pb.dart';import 'package:ente_auth/services/authenticator_service.dart';import 'package:ente_auth/store/code_store.dart';import 'package:ente_auth/ui/components/buttons/button_widget.dart';@@ -13,15 +10,9 @@ import 'package:ente_auth/ui/components/models/button_type.dart';import 'package:ente_auth/ui/scanner_gauth_page.dart';import 'package:ente_auth/ui/settings/data/import/import_success.dart';import 'package:ente_pure_utils/ente_pure_utils.dart';-import 'package:flutter/foundation.dart';import 'package:flutter/material.dart';-import 'package:logging/logging.dart';-const kGoogleAuthExportPrefix = 'otpauth-migration://offline?data=';--bool isGoogleAuthExportQr(String qrCodeData) {- return qrCodeData.startsWith(kGoogleAuthExportPrefix);-}+export 'package:ente_auth/ui/settings/data/import/google_auth_qr_parser.dart';Future<void> showGoogleAuthInstruction(BuildContext context) async {final l10n = context.l10n;@@ -109,75 +100,3 @@ Future<int> importGoogleAuthCodes(List<Code> codes) async {}return importedCount;}--List<Code> parseGoogleAuth(String qrCodeData) {- try {- List<Code> codes = <Code>[];- final String payload = qrCodeData.substring(kGoogleAuthExportPrefix.length);- final Uint8List base64Decoded = base64Decode(Uri.decodeComponent(payload));- final MigrationPayload mPayload = MigrationPayload.fromBuffer(- base64Decoded,- );- for (var otpParameter in mPayload.otpParameters) {- // Build the OTP URL- String otpUrl;- String issuer = otpParameter.issuer;- String account = otpParameter.name;- var counter = otpParameter.counter;- // Create a list of bytes from the list of integers.- Uint8List bytes = Uint8List.fromList(otpParameter.secret);-- // Encode the bytes to base 32.- String base32String = base32.encode(bytes);- String secret = base32String;- // identify digit count- int digits = 6;- int timer = 30; // default timer, no field in Google Auth- Algorithm algorithm = Algorithm.sha1;- switch (otpParameter.algorithm) {- case MigrationPayload_Algorithm.ALGORITHM_MD5:- throw Exception('GoogleAuthImport: MD5 is not supported');- case MigrationPayload_Algorithm.ALGORITHM_SHA1:- algorithm = Algorithm.sha1;- break;- case MigrationPayload_Algorithm.ALGORITHM_SHA256:- algorithm = Algorithm.sha256;- break;- case MigrationPayload_Algorithm.ALGORITHM_SHA512:- algorithm = Algorithm.sha512;- break;- case MigrationPayload_Algorithm.ALGORITHM_UNSPECIFIED:- algorithm = Algorithm.sha1;- break;- }- switch (otpParameter.digits) {- case MigrationPayload_DigitCount.DIGIT_COUNT_EIGHT:- digits = 8;- break;- case MigrationPayload_DigitCount.DIGIT_COUNT_SIX:- digits = 6;- break;- case MigrationPayload_DigitCount.DIGIT_COUNT_UNSPECIFIED:- digits = 6;- }-- if (otpParameter.type == MigrationPayload_OtpType.OTP_TYPE_TOTP ||- otpParameter.type == MigrationPayload_OtpType.OTP_TYPE_UNSPECIFIED) {- otpUrl =- 'otpauth://totp/$issuer:$account?secret=$secret&issuer=$issuer&algorithm=${algorithm.name}&digits=$digits&period=$timer';- } else if (otpParameter.type == MigrationPayload_OtpType.OTP_TYPE_HOTP) {- otpUrl =- 'otpauth://hotp/$issuer:$account?secret=$secret&issuer=$issuer&algorithm=${algorithm.name}&digits=$digits&counter=$counter';- } else {- throw Exception('Invalid OTP type');- }- codes.add(Code.fromOTPAuthUrl(otpUrl));- }- return codes;- } catch (e, s) {- Logger(- "GoogleAuthImport",- ).severe("Error while parsing Google Auth QR code", e, s);- throw Exception('Failed to parse Google Auth QR code \n ${e.toString()}');- }-}diff --git a/mobile/apps/auth/lib/ui/settings/data/import/google_auth_qr_parser.dart b/mobile/apps/auth/lib/ui/settings/data/import/google_auth_qr_parser.dartnew file mode 100644index 0000000000..657a859b6f--- /dev/null+++ b/mobile/apps/auth/lib/ui/settings/data/import/google_auth_qr_parser.dart@@ -0,0 +1,85 @@+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:logging/logging.dart';++const kGoogleAuthExportPrefix = 'otpauth-migration://offline?data=';++bool isGoogleAuthExportQr(String qrCodeData) {+ return qrCodeData.startsWith(kGoogleAuthExportPrefix);+}++List<Code> parseGoogleAuth(String qrCodeData) {+ try {+ List<Code> codes = <Code>[];+ final String payload = qrCodeData.substring(kGoogleAuthExportPrefix.length);+ final Uint8List base64Decoded = base64Decode(Uri.decodeComponent(payload));+ final MigrationPayload mPayload = MigrationPayload.fromBuffer(+ base64Decoded,+ );+ for (var otpParameter in mPayload.otpParameters) {+ // Build the OTP URL+ String otpUrl;+ String issuer = otpParameter.issuer;+ String account = otpParameter.name;+ var counter = otpParameter.counter;+ // Create a list of bytes from the list of integers.+ Uint8List bytes = Uint8List.fromList(otpParameter.secret);++ // Encode the bytes to base 32.+ String base32String = base32.encode(bytes);+ String secret = base32String;+ // identify digit count+ int digits = 6;+ int timer = 30; // default timer, no field in Google Auth+ Algorithm algorithm = Algorithm.sha1;+ switch (otpParameter.algorithm) {+ case MigrationPayload_Algorithm.ALGORITHM_MD5:+ throw Exception('GoogleAuthImport: MD5 is not supported');+ case MigrationPayload_Algorithm.ALGORITHM_SHA1:+ algorithm = Algorithm.sha1;+ break;+ case MigrationPayload_Algorithm.ALGORITHM_SHA256:+ algorithm = Algorithm.sha256;+ break;+ case MigrationPayload_Algorithm.ALGORITHM_SHA512:+ algorithm = Algorithm.sha512;+ break;+ case MigrationPayload_Algorithm.ALGORITHM_UNSPECIFIED:+ algorithm = Algorithm.sha1;+ break;+ }+ switch (otpParameter.digits) {+ case MigrationPayload_DigitCount.DIGIT_COUNT_EIGHT:+ digits = 8;+ break;+ case MigrationPayload_DigitCount.DIGIT_COUNT_SIX:+ digits = 6;+ break;+ case MigrationPayload_DigitCount.DIGIT_COUNT_UNSPECIFIED:+ digits = 6;+ }++ if (otpParameter.type == MigrationPayload_OtpType.OTP_TYPE_TOTP ||+ otpParameter.type == MigrationPayload_OtpType.OTP_TYPE_UNSPECIFIED) {+ otpUrl =+ 'otpauth://totp/$issuer:$account?secret=$secret&issuer=$issuer&algorithm=${algorithm.name}&digits=$digits&period=$timer';+ } else if (otpParameter.type == MigrationPayload_OtpType.OTP_TYPE_HOTP) {+ otpUrl =+ 'otpauth://hotp/$issuer:$account?secret=$secret&issuer=$issuer&algorithm=${algorithm.name}&digits=$digits&counter=$counter';+ } else {+ throw Exception('Invalid OTP type');+ }+ codes.add(Code.fromOTPAuthUrl(otpUrl));+ }+ return codes;+ } catch (e, s) {+ Logger(+ "GoogleAuthImport",+ ).severe("Error while parsing Google Auth QR code", e, s);+ throw Exception('Failed to parse Google Auth QR code \n ${e.toString()}');+ }+}diff --git a/mobile/apps/auth/lib/utils/gallery_import_util.dart b/mobile/apps/auth/lib/utils/gallery_import_util.dartindex 7f2acdc6b8..3a69266e69 100644--- a/mobile/apps/auth/lib/utils/gallery_import_util.dart+++ b/mobile/apps/auth/lib/utils/gallery_import_util.dart@@ -3,7 +3,7 @@ import 'dart:io';import 'package:backup_exclusion/backup_exclusion.dart';import 'package:ente_auth/l10n/l10n.dart';import 'package:ente_auth/models/code.dart';-import 'package:ente_auth/ui/settings/data/import/google_auth_import.dart';+import 'package:ente_auth/ui/settings/data/import/google_auth_qr_parser.dart';import 'package:ente_auth/utils/dialog_util.dart';import 'package:ente_qr/ente_qr.dart';import 'package:file_picker/file_picker.dart';