diff --git a/lib/models/code.dart b/lib/models/code.dart index dd47c31be0..7f645ae667 100644 --- a/lib/models/code.dart +++ b/lib/models/code.dart @@ -13,6 +13,7 @@ class Code { final Algorithm algorithm; final Type type; final String rawData; + final int counter; bool? hasSynced; Code( @@ -23,6 +24,7 @@ class Code { this.secret, this.algorithm, this.type, + this.counter, this.rawData, { this.generatedID, }); @@ -35,6 +37,7 @@ class Code { String? secret, Algorithm? algorithm, Type? type, + int? counter, }) { final String updateAccount = account ?? this.account; final String updateIssuer = issuer ?? this.issuer; @@ -43,6 +46,7 @@ class Code { final String updatedSecret = secret ?? this.secret; final Algorithm updatedAlgo = algorithm ?? this.algorithm; final Type updatedType = type ?? this.type; + final int updatedCounter = counter ?? this.counter; return Code( updateAccount, @@ -52,6 +56,7 @@ class Code { updatedSecret, updatedAlgo, updatedType, + updatedCounter, "otpauth://${updatedType.name}/" + updateIssuer + ":" + @@ -77,6 +82,7 @@ class Code { secret, Algorithm.sha1, Type.totp, + 0, "otpauth://totp/" + issuer + ":" + @@ -99,6 +105,7 @@ class Code { getSanitizedSecret(uri.queryParameters['secret']!), _getAlgorithm(uri), _getType(uri), + _getCounter(uri), rawData, ); } catch(e) { @@ -163,6 +170,18 @@ class Code { } } + static int _getCounter(Uri uri) { + try { + final bool hasCounterKey = uri.queryParameters.containsKey('counter'); + if (!hasCounterKey) { + return 0; + } + return int.parse(uri.queryParameters['counter']!); + } catch (e) { + return defaultPeriod; + } + } + static Algorithm _getAlgorithm(Uri uri) { try { final algorithm = diff --git a/lib/ui/scanner_gauth_page.dart b/lib/ui/scanner_gauth_page.dart index ee84c89f6b..41909ed325 100644 --- a/lib/ui/scanner_gauth_page.dart +++ b/lib/ui/scanner_gauth_page.dart @@ -46,7 +46,7 @@ class ScannerGoogleAuthPageState extends State { child: QRView( key: qrKey, overlay: QrScannerOverlayShape( - borderColor: getEnteColorScheme(context).primary700), + borderColor: getEnteColorScheme(context).primary700,), onQRViewCreated: _onQRViewCreated, formatsAllowed: const [BarcodeFormat.qrcode], ), diff --git a/test/models/code_test.dart b/test/models/code_test.dart index 7eb91e9010..9a0d18e03e 100644 --- a/test/models/code_test.dart +++ b/test/models/code_test.dart @@ -19,6 +19,16 @@ void main() { expect(code.account, "testdata@ente.io", reason: "accountMismatch"); expect(code.secret, "ASKZNWOU6SVYAMVS"); }); + + test("validateCount", () { + final code = Code.fromRawData( + "otpauth://hotp/testdata@ente.io?secret=ASKZNWOU6SVYAMVS&issuer=GitHub&counter=15", + ); + expect(code.issuer, "GitHub", reason: "issuerMismatch"); + expect(code.account, "testdata@ente.io", reason: "accountMismatch"); + expect(code.secret, "ASKZNWOU6SVYAMVS"); + expect(code.counter, 15); + }); // test("parseWithFunnyAccountName", () {