Skip to content

Commit

Permalink
Update Test Case for empty label
Browse files Browse the repository at this point in the history
Remove null-saftey, as test cases do not like it.

Match empty issuer and organization with URL parsing RegEx, add proper error message when manually adding token with ":" in label/organization name.

Allow empty issuer and label when manually adding token.
  • Loading branch information
DreamyLynn authored and justin-stephenson committed Aug 9, 2023
1 parent a17fc10 commit 7321545
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public static Collection<Object[]> data() {
{ "otpauth://totp",
Token.InvalidLabelException.class },
{ "otpauth://totp/",
Token.InvalidLabelException.class },
Token.InvalidSecretException.class },
{ "otpauth://totp/foo:bar:baz",
Token.InvalidLabelException.class },
{ "otpauth://totp/bar",
Expand Down
8 changes: 8 additions & 0 deletions mobile/src/main/java/org/fedorahosted/freeotp/ManualAdd.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import android.widget.TextView;
import android.widget.Toast;

import org.apache.commons.codec.binary.StringUtils;
import org.fedorahosted.freeotp.main.Activity;

public class ManualAdd extends AppCompatActivity {
Expand Down Expand Up @@ -105,6 +106,8 @@ private Uri makeUri() {

private boolean inputValid() {
String secret = mSecret.getText().toString();
String issuer = mIssuer.getText().toString();
String account = mAccount.getText().toString();
Boolean valid = true;
String msg = "";

Expand All @@ -113,6 +116,11 @@ private boolean inputValid() {
valid = false;
}

if(issuer.contains(":") || account.contains(":")) {
msg = "Issuer and account may not contain \":\"";
valid = false;
}

if (!valid) {
Toast.makeText(getApplicationContext(), msg,Toast.LENGTH_SHORT).show();
return false;
Expand Down
12 changes: 7 additions & 5 deletions mobile/src/main/java/org/fedorahosted/freeotp/Token.java
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public static class InvalidColorException extends InvalidUriException {}
public enum Type { HOTP, TOTP }

private static final String[] SAFE_ALGOS = { "SHA1", "SHA224", "SHA256", "SHA384", "SHA512" };
private static final Pattern PATTERN = Pattern.compile("^/(?:([^:]+):)?([^:]+)$");
private static final Pattern PATTERN = Pattern.compile("^/(?:([^:]*):)?([^:]*)$");

@SerializedName("algo")
private final String mAlgorithm;
Expand Down Expand Up @@ -227,11 +227,13 @@ private Token(Uri uri) throws InvalidUriException {

try {
Matcher matcher = PATTERN.matcher(uri.getPath());
if (!matcher.find())
if (!matcher.find()) {
throw new InvalidLabelException();

mIssuer = matcher.group(1);
mLabel = matcher.group(2);
}
else {
mIssuer = matcher.group(1);
mLabel = matcher.group(2);
}
} catch (NullPointerException e) {
throw new InvalidLabelException();
}
Expand Down

0 comments on commit 7321545

Please sign in to comment.