fixed logic to get stripped label

This commit is contained in:
Yadav 2020-06-08 21:47:04 +05:30
parent 00110d2f02
commit 364f7402b8
2 changed files with 21 additions and 4 deletions

View file

@ -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())));

View file

@ -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();
}
}
}