QRcode: Url encode the keys

This commit is contained in:
Benoit Marty 2020-01-30 10:17:04 +01:00
parent 2111daea52
commit 2bccd19f84
2 changed files with 25 additions and 2 deletions

View File

@ -52,7 +52,7 @@ fun QrCodeData.toUrl(): String {
append(URLEncoder.encode(action, ENCODING)) append(URLEncoder.encode(action, ENCODING))
for ((keyId, key) in keys) { for ((keyId, key) in keys) {
append("&key_$keyId=") append("&key_${URLEncoder.encode(keyId, ENCODING)}=")
append(URLEncoder.encode(key, ENCODING)) append(URLEncoder.encode(key, ENCODING))
} }
@ -105,7 +105,7 @@ fun String.toQrCodeData(): QrCodeData? {
val keys = keyValues.keys val keys = keyValues.keys
.filter { it.startsWith("key_") } .filter { it.startsWith("key_") }
.map { .map {
it.substringAfter("key_") to (keyValues[it] ?: return null) URLDecoder.decode(it.substringAfter("key_"), ENCODING) to (keyValues[it] ?: return null)
} }
.toMap() .toMap()

View File

@ -84,6 +84,29 @@ class QrCodeTest {
decodedData.otherUserKey shouldBeEqualTo "otherUserKey" decodedData.otherUserKey shouldBeEqualTo "otherUserKey"
} }
@Test
fun testUrlCharInKeys() {
val url = basicQrCodeData
.copy(
keys = mapOf(
"/=" to "abcdef",
"&?" to "ghijql"
)
)
.toUrl()
url shouldBeEqualTo basicUrl
.replace("key_1=abcdef", "key_%2F%3D=abcdef")
.replace("key_2=ghijql", "key_%26%3F=ghijql")
val decodedData = url.toQrCodeData()
decodedData.shouldNotBeNull()
decodedData.keys["/="]?.shouldBeEqualTo("abcdef")
decodedData.keys["&&"]?.shouldBeEqualTo("ghijql")
}
@Test @Test
fun testMissingActionCase() { fun testMissingActionCase() {
basicUrl.replace("&action=verify", "") basicUrl.replace("&action=verify", "")