QR code: update code which build URL

This commit is contained in:
Benoit Marty 2020-01-24 18:12:47 +01:00
parent efc8cfb9a1
commit df49ab8362
2 changed files with 13 additions and 11 deletions

View File

@ -18,6 +18,10 @@ package im.vector.matrix.android.internal.crypto.verification.qrcode
import im.vector.matrix.android.api.MatrixPatterns
import im.vector.matrix.android.api.permalinks.PermalinkFactory
import java.net.URLDecoder
import java.net.URLEncoder
private const val ENCODING = "utf-8"
/**
* Generate an URL to generate a QR code of the form:
@ -34,21 +38,19 @@ fun QrCodeData.toUrl(): String {
return buildString {
append(PermalinkFactory.createPermalink(userId))
append("?request=")
append(PermalinkFactory.escape(requestEventId))
append(URLEncoder.encode(requestEventId, ENCODING))
append("&action=")
append(PermalinkFactory.escape(action))
append(URLEncoder.encode(action, ENCODING))
for ((keyId, key) in keys) {
append("&key_")
append(PermalinkFactory.escape(keyId))
append("=")
append(PermalinkFactory.escape(key))
append("&key_$keyId=")
append(URLEncoder.encode(key, ENCODING))
}
append("&secret=")
append(PermalinkFactory.escape(sharedSecret))
append(URLEncoder.encode(sharedSecret, ENCODING))
append("&other_user_key=")
append(PermalinkFactory.escape(otherUserKey))
append(URLEncoder.encode(otherUserKey, ENCODING))
}
}
@ -82,7 +84,7 @@ fun String.toQrCodeData(): QrCodeData? {
.filter { it.isNotEmpty() }
val keyValues = urlParams.map {
(it.substringBefore("=") to it.substringAfter("=").let { value -> PermalinkFactory.unescape(value) })
(it.substringBefore("=") to it.substringAfter("=").let { value -> URLDecoder.decode(value, ENCODING) })
}.toMap()
val action = keyValues["action"] ?: return null

View File

@ -39,7 +39,7 @@ class QrCodeTest {
otherUserKey = "otherUserKey"
)
private val basicUrl = "https://matrix.to/#/@benoit:matrix.org?request=\$azertyazerty&action=verify&key_1=abcdef&key_2=ghijql&secret=sharedSecret&other_user_key=otherUserKey"
private val basicUrl = "https://matrix.to/#/@benoit:matrix.org?request=%24azertyazerty&action=verify&key_1=abcdef&key_2=ghijql&secret=sharedSecret&other_user_key=otherUserKey"
@Test
fun testNominalCase() {
@ -101,7 +101,7 @@ class QrCodeTest {
@Test
fun testBadRequestEventId() {
basicUrl.replace("\$azertyazerty", "@azertyazerty")
basicUrl.replace("%24azertyazerty", "%32azertyazerty")
.toQrCodeData()
.shouldBeNull()
}