Fix parsing and add tests

This commit is contained in:
Neeraj Gupta 2023-04-03 12:28:27 +05:30
parent 1c3ebd026b
commit 0963ea7129
No known key found for this signature in database
GPG key ID: 3C5A1684DC1729E1
2 changed files with 16 additions and 3 deletions

View file

@ -67,7 +67,10 @@ class Code {
static String _getAccount(Uri uri) {
try {
final String path = Uri.decodeComponent(uri.path);
String path = Uri.decodeComponent(uri.path);
if (path.startsWith("/")) {
path = path.substring(1, path.length);
}
// Parse account name from documented auth URI
// otpauth://totp/ACCOUNT?secret=SUPERSECRET&issuer=SERVICE
if (uri.queryParameters.containsKey("issuer") && !path.contains(":")) {
@ -88,6 +91,7 @@ class Code {
if (issuerName.contains("period=")) {
return issuerName.substring(0, issuerName.indexOf("period="));
}
return issuerName;
}
final String path = Uri.decodeComponent(uri.path);
return path.split(':')[0].substring(1);

View file

@ -6,8 +6,17 @@ void main() {
final code1 = Code.fromRawData(
"otpauth://totp/example%20finance%3Aee%40ff.gg?secret=ASKZNWOU6SVYAMVS",
);
expect(code1.issuer, "example finance");
expect(code1.account, "ee@ff.gg");
expect(code1.issuer, "example finance", reason: "issuerMismatch");
expect(code1.account, "ee@ff.gg", reason: "accountMismatch");
expect(code1.secret, "ASKZNWOU6SVYAMVS");
});
test("parseDocumentedFormat", () {
final code = Code.fromRawData(
"otpauth://totp/testdata@ente.io?secret=ASKZNWOU6SVYAMVS&issuer=GitHub",
);
expect(code.issuer, "GitHub", reason: "issuerMismatch");
expect(code.account, "testdata@ente.io", reason: "accountMismatch");
expect(code.secret, "ASKZNWOU6SVYAMVS");
});
}