-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
update import, export logic and add capability to change the algorith…
…m used to generate passwords
- Loading branch information
1 parent
422c34f
commit 2a5d289
Showing
14 changed files
with
387 additions
and
69 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package kryptonbutterfly.totp.misc.otp; | ||
|
||
enum Digits | ||
{ | ||
_6(6), | ||
_7(7), | ||
_8(8); | ||
|
||
public final int digits; | ||
|
||
Digits(int digits) | ||
{ | ||
this.digits = digits; | ||
} | ||
|
||
public static Digits of(String value) | ||
{ | ||
return switch (value) | ||
{ | ||
case "6" -> _6; | ||
case "7" -> _7; | ||
case "8" -> _8; | ||
default -> null; | ||
}; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
package kryptonbutterfly.totp.misc.otp; | ||
|
||
import java.net.MalformedURLException; | ||
import java.net.URISyntaxException; | ||
import java.util.HashMap; | ||
|
||
import kryptonbutterfly.totp.prefs.OtpAlgo; | ||
|
||
public sealed interface OtpUri permits TotpUri | ||
{ | ||
static final String PROTOCOL_TERMINATOR = "://"; | ||
static final String PROTOCOL = "otpauth://"; | ||
static final String TYPE_TERMINATOR = "/"; | ||
static final String PATH_TERMINATOR = "?"; | ||
static final String PARAM_DELIM = "="; | ||
static final String QUERY_DELIM = "&"; | ||
static final String ISSUER_TERMINATOR = ":"; | ||
|
||
static final String SECRET = "secret"; | ||
static final String ALGORITHM = "algorithm"; | ||
static final String DIGITS = "digits"; | ||
static final String COUNTER = "counter"; | ||
static final String PERIOD = "period"; | ||
static final String ISSUER = "issuer"; | ||
|
||
public static final int DEFAULT_PERIOD = 30; | ||
public static final Digits DEFAULT_DIGITS = Digits._6; | ||
public static final OtpAlgo DEFAULT_ALGO = OtpAlgo.SHA1; | ||
|
||
public String toStringUrl(); | ||
|
||
public String account(); | ||
|
||
public String secret(); | ||
|
||
public OtpAlgo algorithm(); | ||
|
||
public int digits(); | ||
|
||
public int counter(); | ||
|
||
public int period(); | ||
|
||
public String type(); | ||
|
||
public String issuer(); | ||
|
||
public static OtpUri parseOtpUrl(String url) throws MalformedURLException, URISyntaxException | ||
{ | ||
if (!url.startsWith(PROTOCOL)) | ||
{ | ||
final int term = url.indexOf(PROTOCOL_TERMINATOR); | ||
if (term < 0) | ||
throw new MalformedURLException("No protocol specified."); | ||
final var protocol = url.substring(term); | ||
throw new MalformedURLException( | ||
"Invalid protocol '%s' for OTP URL. Expected %s.".formatted(protocol, PROTOCOL)); | ||
} | ||
final var urlPart = url.substring(PROTOCOL.length()); | ||
|
||
final int typeTerm = urlPart.indexOf(TYPE_TERMINATOR); | ||
if (typeTerm < 0) | ||
throw new MalformedURLException("OTP URL is missing type declaration!"); | ||
final var type = urlPart.substring(0, typeTerm); | ||
return switch (type) | ||
{ | ||
case "totp" -> TotpUri.parse(url); | ||
case "htop" -> throw new IllegalStateException( | ||
"HTOP's currently are not supported."); | ||
default -> throw new MalformedURLException( | ||
"'%s' is not a valid OTP type!".formatted(type)); | ||
}; | ||
} | ||
|
||
public static String buildOtpUriString(OtpUri uri, HashMap<String, String> query) | ||
{ | ||
final var sb = new StringBuilder(PROTOCOL) | ||
.append(uri.type()) | ||
.append(TYPE_TERMINATOR) | ||
.append(uri.account()) | ||
.append(PATH_TERMINATOR); | ||
|
||
boolean isFirst = true; | ||
for (final var p : query.entrySet()) | ||
{ | ||
if (isFirst) | ||
isFirst = false; | ||
else | ||
sb.append(QUERY_DELIM); | ||
sb.append(p.getKey()) | ||
.append(PARAM_DELIM) | ||
.append(p.getValue()); | ||
} | ||
|
||
return sb.toString(); | ||
} | ||
} |
Oops, something went wrong.