Handle # in label (#149)

This commit is contained in:
Neeraj Gupta 2023-07-13 20:58:47 +05:30 committed by GitHub
commit 902b30d9bc
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 20 additions and 0 deletions

View file

@ -90,6 +90,7 @@ class Code {
static Code fromRawData(String rawData) {
Uri uri = Uri.parse(rawData);
try {
return Code(
_getAccount(uri),
_getIssuer(uri),
@ -100,6 +101,15 @@ class Code {
_getType(uri),
rawData,
);
} catch(e) {
// if account name contains # without encoding,
// rest of the url are treated as url fragment
if(rawData.contains("#")) {
return Code.fromRawData(rawData.replaceAll("#", '%23'));
} else {
rethrow;
}
}
}
static String _getAccount(Uri uri) {

View file

@ -19,4 +19,14 @@ void main() {
expect(code.account, "testdata@ente.io", reason: "accountMismatch");
expect(code.secret, "ASKZNWOU6SVYAMVS");
});
//
test("parseWithFunnyAccountName", () {
final code = Code.fromRawData(
"otpauth://totp/Mongo Atlas:Acc !@#444?algorithm=sha1&digits=6&issuer=Mongo Atlas&period=30&secret=NI4CTTFEV4G2JFE6",
);
expect(code.issuer, "Mongo Atlas", reason: "issuerMismatch");
expect(code.account, "Acc !@#444", reason: "accountMismatch");
expect(code.secret, "NI4CTTFEV4G2JFE6");
});
}