From 364f7402b83fe773a4e25f8c77325f770c220cb8 Mon Sep 17 00:00:00 2001 From: Yadav Date: Mon, 8 Jun 2020 21:47:04 +0530 Subject: [PATCH] fixed logic to get stripped label --- .../flocke/andotp/ApplicationTest.java | 6 ++++++ .../flocke/andotp/Database/Entry.java | 19 +++++++++++++++---- 2 files changed, 21 insertions(+), 4 deletions(-) diff --git a/app/src/androidTest/java/org/shadowice/flocke/andotp/ApplicationTest.java b/app/src/androidTest/java/org/shadowice/flocke/andotp/ApplicationTest.java index fa9b49e4..b2d1e8b9 100644 --- a/app/src/androidTest/java/org/shadowice/flocke/andotp/ApplicationTest.java +++ b/app/src/androidTest/java/org/shadowice/flocke/andotp/ApplicationTest.java @@ -176,6 +176,12 @@ public class ApplicationTest { Entry entry = new Entry("otpauth://totp/ACME%20Co:john.doe@email.com?secret=HXDMVJECJJWSRB3HWIZR4IFUGFTMXBOZ&issuer=ACME%20Co&ALGORITHM=SHA1&digits=6&period=30"); assertEquals("john.doe@email.com", entry.getLabel()); + Entry entry2 = new Entry("otpauth://totp/ :john.doe@email.com?secret=HXDMVJECJJWSRB3HWIZR4IFUGFTMXBOZ&ALGORITHM=SHA1&digits=6&period=30"); + assertEquals(":john.doe@email.com", entry2.getLabel()); + + Entry entry3 = new Entry("otpauth://totp/ :john.doe@email.com?secret=HXDMVJECJJWSRB3HWIZR4IFUGFTMXBOZ&issuer=%20&ALGORITHM=SHA1&digits=6&period=30"); + assertEquals("john.doe@email.com", entry3.getLabel()); + assertEquals("HXDMVJECJJWSRB3HWIZR4IFUGFTMXBOZ", new String(new Base32().encode(entry.getSecret()))); diff --git a/app/src/main/java/org/shadowice/flocke/andotp/Database/Entry.java b/app/src/main/java/org/shadowice/flocke/andotp/Database/Entry.java index 8eb9c1ed..835abb2f 100644 --- a/app/src/main/java/org/shadowice/flocke/andotp/Database/Entry.java +++ b/app/src/main/java/org/shadowice/flocke/andotp/Database/Entry.java @@ -130,13 +130,10 @@ public class Entry { } String secret = uri.getQueryParameter("secret"); - String label = uri.getPath().substring(1); - if(label.contains(":")) { - label = label.split(":")[1]; - } String counter = uri.getQueryParameter("counter"); String issuer = uri.getQueryParameter("issuer"); + String label = getStrippedLabel(issuer, uri.getPath().substring(1)); String period = uri.getQueryParameter("period"); String digits = uri.getQueryParameter("digits"); String algorithm = uri.getQueryParameter("algorithm"); @@ -536,4 +533,18 @@ public class Entry { return true; } + + /** + * Returns the label with issuer prefix removed (if present) + * @param issuer + * @param label + * @return + */ + private String getStrippedLabel(String issuer, String label) { + if (issuer == null || issuer.isEmpty() || !label.startsWith(issuer + ":")) { + return label.trim(); + } else { + return label.substring(issuer.length() + 1).trim(); + } + } }