From 87b463f4e88a424540e0d5ab003ba344537c14c8 Mon Sep 17 00:00:00 2001 From: Elias Bonnici Date: Wed, 10 Jan 2024 17:02:41 +0100 Subject: [PATCH] Fix parsing of `otpauth-migration` Uri. This solves the issue regarding duplicate accounts not being recognized. --- lib/oath/models.dart | 40 ++++++++++++++++++++++++---------------- 1 file changed, 24 insertions(+), 16 deletions(-) diff --git a/lib/oath/models.dart b/lib/oath/models.dart index d21dc85f..9ac3158d 100755 --- a/lib/oath/models.dart +++ b/lib/oath/models.dart @@ -188,22 +188,30 @@ class CredentialData with _$CredentialData { // Convert parsed credential values into CredentialData objects return splitCreds(base64.decode(uri.queryParameters['data']!)) - .map((values) => CredentialData( - secret: base32.encode(values[1]), - name: utf8.decode(values[2], allowMalformed: true), - issuer: values[3] != null - ? utf8.decode(values[3], allowMalformed: true) - : null, - hashAlgorithm: switch (values[4]) { - 2 => HashAlgorithm.sha256, - 3 => HashAlgorithm.sha512, - _ => HashAlgorithm.sha1, - }, - digits: values[5] == 2 ? 8 : defaultDigits, - oathType: values[6] == 1 ? OathType.hotp : OathType.totp, - counter: values[7] ?? defaultCounter, - )) - .toList(); + .map((values) { + String? issuer; + String name = utf8.decode(values[2], allowMalformed: true); + final nameIndex = name.indexOf(':'); + if (nameIndex >= 0) { + issuer = name.substring(0, nameIndex); + name = name.substring(nameIndex + 1); + } + return CredentialData( + secret: base32.encode(values[1]), + name: name, + issuer: values[3] != null + ? utf8.decode(values[3], allowMalformed: true) + : issuer, + hashAlgorithm: switch (values[4]) { + 2 => HashAlgorithm.sha256, + 3 => HashAlgorithm.sha512, + _ => HashAlgorithm.sha1, + }, + digits: values[5] == 2 ? 8 : defaultDigits, + oathType: values[6] == 1 ? OathType.hotp : OathType.totp, + counter: values[7] ?? defaultCounter, + ); + }).toList(); } factory CredentialData.fromOtpauth(Uri uri) {