Skip to content

Commit

Permalink
Add getURL to generate otpauth URL
Browse files Browse the repository at this point in the history
  • Loading branch information
amdelamar committed Jun 17, 2017
1 parent 60706b5 commit 296ddd7
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 4 deletions.
45 changes: 45 additions & 0 deletions src/main/java/com/amdelamar/jotp/OTP.java
Original file line number Diff line number Diff line change
Expand Up @@ -211,4 +211,49 @@ private static boolean validateParameters(String secret, String base, int digits
}
return true;
}

/**
* Gets the "otpauth://" URL for adding to 2FA compatible devices/apps.
*
* @param secret
* Shhhhh. (Base32)
* @param digits
* Length of code (Commonly '6')
* @param type
* Type.TOTP or Type.HOTP
* @param issuer
* Company or Domain name
* @param email
* Username or Email address
* @return otpauth://...
* @throws OTPException
* @throws BadOperationException
*/
public static String getURL(String secret, int digits, Type type, String issuer, String email)
throws OTPException, BadOperationException {

validateParameters(secret, secret, digits, type);

StringBuilder sb = new StringBuilder();
sb.append("otpauth://");

if (type == Type.HOTP) {
sb.append("hotp/");
} else if (type == Type.TOTP) {
sb.append("totp/");
}

sb.append(issuer + ":");
sb.append(email + "?");
sb.append("secret=" + secret);
sb.append("&issuer=" + issuer);
sb.append("&algorithm=SHA1");
sb.append("&digits=" + digits);

if (type == Type.TOTP) {
sb.append("&period=30");
}

return sb.toString();
}
}
27 changes: 23 additions & 4 deletions src/test/java/com/amdelamar/jotp/Tests.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

import com.amdelamar.jotp.OTP;
import com.amdelamar.jotp.exception.BadOperationException;
import com.amdelamar.jotp.exception.OTPException;
import com.amdelamar.jotp.type.Type;

/**
Expand Down Expand Up @@ -59,15 +60,35 @@ public void encodeTests() {

// run 5 tests
for (int i = 0; i < 5; i++) {
System.out.println(OTP.random("ABCDEFGHIJKLMNOPQRSTUVWXYZ234567", 12));
assertNotNull(OTP.random("ABCDEFGHIJKLMNOPQRSTUVWXYZ234567", 12));
}

// run 5 tests
for (int i = 0; i < 5; i++) {
System.out.println(OTP.randomBase32(OTP.BYTES));
assertNotNull(OTP.randomBase32(OTP.BYTES));
}
}

@Test
public void urlTests() throws OTPException, BadOperationException {

String url = OTP.getURL(OTP.randomBase32(OTP.BYTES), 6, Type.HOTP, "Example1",
"test1@example.com");
assertNotNull(url);

url = OTP.getURL(OTP.randomBase32(OTP.BYTES), 4, Type.HOTP, "Example2",
"test2@example.com");
assertNotNull(url);

url = OTP.getURL(OTP.randomBase32(OTP.BYTES), 6, Type.TOTP, "Example3",
"test3@example.com");
assertNotNull(url);

url = OTP.getURL(OTP.randomBase32(OTP.BYTES), 4, Type.TOTP, "Example4",
"test4@example.com");
assertNotNull(url);
}

@Test
public void totpTests() throws BadOperationException, IOException, InterruptedException {

Expand All @@ -76,8 +97,6 @@ public void totpTests() throws BadOperationException, IOException, InterruptedEx
String secret = OTP.randomBase32(OTP.BYTES);
String code1 = OTP.create(secret, OTP.timeInHex(), 6, Type.TOTP);

System.out.println(OTP.timeInHex());

// 30 sec window, so wait just a second
// If its beyond 30sec since the first OTP,
// then we will get a different base value.
Expand Down

0 comments on commit 296ddd7

Please sign in to comment.