Update after MSC change

This commit is contained in:
Benoit Marty 2020-01-22 18:22:01 +01:00
parent 41c691f26c
commit cbf418c401
3 changed files with 34 additions and 43 deletions

View File

@ -26,8 +26,7 @@ import im.vector.matrix.android.api.permalinks.PermalinkFactory
* request=<event-id>
* &action=verify
* &key_<keyid>=<key-in-base64>...
* &verification_algorithms=<algorithm>
* &verification_key=<random-key-in-base64>
* &secret=<shared_secret>
* &other_user_key=<master-key-in-base64>
* </pre>
*/
@ -35,18 +34,17 @@ fun QrCodeData.toUrl(): String {
return buildString {
append(PermalinkFactory.createPermalink(userId))
append("?request=")
append(PermalinkFactory.escape(requestId))
append("&action=verify")
append(PermalinkFactory.escape(requestEventId))
append("&action=")
append(action)
for ((keyId, key) in keys) {
append("&key_$keyId=")
append(PermalinkFactory.escape(key))
}
append("&verification_algorithms=")
append(PermalinkFactory.escape(verificationAlgorithms))
append("&verification_key=")
append(PermalinkFactory.escape(verificationKey))
append("&secret=")
append(PermalinkFactory.escape(sharedSecret))
append("&other_user_key=")
append(PermalinkFactory.escape(otherUserKey))
}
@ -85,15 +83,12 @@ fun String.toQrCodeData(): QrCodeData? {
(it.substringBefore("=") to it.substringAfter("="))
}.toMap()
if (keyValues["action"] != "verify") {
return null
}
val action = keyValues["action"] ?: return null
val requestId = keyValues["request"]
?.let { PermalinkFactory.unescape(it) }
?.takeIf { MatrixPatterns.isEventId(it) } ?: return null
val verificationAlgorithms = keyValues["verification_algorithms"] ?: return null
val verificationKey = keyValues["verification_key"] ?: return null
val sharedSecret = keyValues["secret"] ?: return null
val otherUserKey = keyValues["other_user_key"] ?: return null
val keys = keyValues.keys
@ -106,9 +101,9 @@ fun String.toQrCodeData(): QrCodeData? {
return QrCodeData(
userId,
requestId,
action,
keys,
verificationAlgorithms,
verificationKey,
sharedSecret,
otherUserKey
)
}

View File

@ -22,15 +22,19 @@ package im.vector.matrix.android.internal.crypto.verification.qrcode
data class QrCodeData(
val userId: String,
// the event ID of the associated verification request event.
val requestId: String,
val requestEventId: String,
// The action
val action: String,
// key_<key_id>: each key that the user wants verified will have an entry of this form, where the value is the key in unpadded base64.
// The QR code should contain at least the user's master cross-signing key.
val keys: Map<String, String>,
// algorithm
val verificationAlgorithms: String,
// random single-use shared secret in unpadded base64. It must be at least 256-bits long (43 characters when base64-encoded).
val verificationKey: String,
val sharedSecret: String,
// the other user's master cross-signing key, in unpadded base64. In other words, if Alice is displaying the QR code,
// this would be the copy of Bob's master cross-signing key that Alice has.
val otherUserKey: String
)
) {
companion object {
const val ACTION_VERIFY = "verify"
}
}

View File

@ -28,17 +28,17 @@ class QrCodeTest {
private val basicQrCodeData = QrCodeData(
userId = "@benoit:matrix.org",
requestId = "\$azertyazerty",
requestEventId = "\$azertyazerty",
action = QrCodeData.ACTION_VERIFY,
keys = mapOf(
"1" to "abcdef",
"2" to "ghijql"
),
verificationAlgorithms = "verificationAlgorithm",
verificationKey = "verificationKey",
sharedSecret = "sharedSecret",
otherUserKey = "otherUserKey"
)
private val basicUrl = "https://matrix.to/#/@benoit:matrix.org?request=\$azertyazerty&action=verify&key_1=abcdef&key_2=ghijql&verification_algorithms=verificationAlgorithm&verification_key=verificationKey&other_user_key=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"
@Test
fun testNominalCase() {
@ -51,11 +51,10 @@ class QrCodeTest {
decodedData.shouldNotBeNull()
decodedData.userId shouldBeEqualTo "@benoit:matrix.org"
decodedData.requestId shouldBeEqualTo "\$azertyazerty"
decodedData.requestEventId shouldBeEqualTo "\$azertyazerty"
decodedData.keys["1"]?.shouldBeEqualTo("abcdef")
decodedData.keys["2"]?.shouldBeEqualTo("ghijql")
decodedData.verificationAlgorithms shouldBeEqualTo "verificationAlgorithm"
decodedData.verificationKey shouldBeEqualTo "verificationKey"
decodedData.sharedSecret shouldBeEqualTo "sharedSecret"
decodedData.otherUserKey shouldBeEqualTo "otherUserKey"
}
@ -64,7 +63,7 @@ class QrCodeTest {
val url = basicQrCodeData
.copy(
userId = "@benoit/foo:matrix.org",
requestId = "\$azertyazerty/bar"
requestEventId = "\$azertyazerty/bar"
)
.toUrl()
@ -77,11 +76,10 @@ class QrCodeTest {
decodedData.shouldNotBeNull()
decodedData.userId shouldBeEqualTo "@benoit/foo:matrix.org"
decodedData.requestId shouldBeEqualTo "\$azertyazerty/bar"
decodedData.requestEventId shouldBeEqualTo "\$azertyazerty/bar"
decodedData.keys["1"]?.shouldBeEqualTo("abcdef")
decodedData.keys["2"]?.shouldBeEqualTo("ghijql")
decodedData.verificationAlgorithms shouldBeEqualTo "verificationAlgorithm"
decodedData.verificationKey shouldBeEqualTo "verificationKey"
decodedData.sharedSecret shouldBeEqualTo "sharedSecret"
decodedData.otherUserKey shouldBeEqualTo "otherUserKey"
}
@ -93,14 +91,15 @@ class QrCodeTest {
}
@Test
fun testBadActionCase() {
fun testOtherActionCase() {
basicUrl.replace("&action=verify", "&action=confirm")
.toQrCodeData()
.shouldBeNull()
?.action
?.shouldBeEqualTo("confirm")
}
@Test
fun testBadRequestId() {
fun testBadRequestEventId() {
basicUrl.replace("\$azertyazerty", "@azertyazerty")
.toQrCodeData()
.shouldBeNull()
@ -121,15 +120,8 @@ class QrCodeTest {
}
@Test
fun testMissingVerificationAlgorithm() {
basicUrl.replace("&verification_algorithms=verificationAlgorithm", "")
.toQrCodeData()
.shouldBeNull()
}
@Test
fun testMissingVerificationKey() {
basicUrl.replace("&verification_key=verificationKey", "")
fun testMissingSecret() {
basicUrl.replace("&secret=sharedSecret", "")
.toQrCodeData()
.shouldBeNull()
}