Skip to content

Commit

Permalink
Create asValidObject method - not compiling
Browse files Browse the repository at this point in the history
  • Loading branch information
bmarty committed Mar 5, 2020
1 parent 36c52d2 commit 7b5a50e
Show file tree
Hide file tree
Showing 23 changed files with 188 additions and 148 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,19 +37,6 @@ internal data class MessageVerificationAcceptContent(
override val transactionID: String?
get() = relatesTo?.eventId

override fun isValid(): Boolean {
if (transactionID.isNullOrBlank()
|| keyAgreementProtocol.isNullOrBlank()
|| hash.isNullOrBlank()
|| commitment.isNullOrBlank()
|| messageAuthenticationCode.isNullOrBlank()
|| shortAuthenticationStrings.isNullOrEmpty()) {
Timber.e("## received invalid verification request")
return false
}
return true
}

override fun toEventContent() = toContent()

companion object : VerificationInfoAcceptFactory {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,21 +28,13 @@ data class MessageVerificationCancelContent(
@Json(name = "code") override val code: String? = null,
@Json(name = "reason") override val reason: String? = null,
@Json(name = "m.relates_to") val relatesTo: RelationDefaultContent?

) : VerificationInfoCancel {

override val transactionID: String?
get() = relatesTo?.eventId

override fun toEventContent() = toContent()

override fun isValid(): Boolean {
if (transactionID.isNullOrBlank() || code.isNullOrBlank()) {
return false
}
return true
}

companion object {
fun create(transactionId: String, reason: CancelCode): MessageVerificationCancelContent {
return MessageVerificationCancelContent(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,20 @@ import im.vector.matrix.android.internal.crypto.verification.VerificationInfo
@JsonClass(generateAdapter = true)
internal data class MessageVerificationDoneContent(
@Json(name = "m.relates_to") val relatesTo: RelationDefaultContent?
) : VerificationInfo {
) : VerificationInfo<ValidVerificationDone> {

override val transactionID: String?
get() = relatesTo?.eventId

override fun isValid() = transactionID?.isNotEmpty() == true

override fun toEventContent(): Content? = toContent()

override fun asValidObject(): ValidVerificationDone? {
if (transactionID.isNullOrEmpty()) {
return null
}

return ValidVerificationDone
}
}

internal object ValidVerificationDone
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ import im.vector.matrix.android.api.session.events.model.toContent
import im.vector.matrix.android.api.session.room.model.relation.RelationDefaultContent
import im.vector.matrix.android.internal.crypto.verification.VerificationInfoKey
import im.vector.matrix.android.internal.crypto.verification.VerificationInfoKeyFactory
import timber.log.Timber

@JsonClass(generateAdapter = true)
internal data class MessageVerificationKeyContent(
Expand All @@ -36,14 +35,6 @@ internal data class MessageVerificationKeyContent(
override val transactionID: String?
get() = relatesTo?.eventId

override fun isValid(): Boolean {
if (transactionID.isNullOrBlank() || key.isNullOrBlank()) {
Timber.e("## received invalid verification request")
return false
}
return true
}

override fun toEventContent() = toContent()

companion object : VerificationInfoKeyFactory {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,6 @@ internal data class MessageVerificationMacContent(

override fun toEventContent() = toContent()

override fun isValid(): Boolean {
if (transactionID.isNullOrBlank() || keys.isNullOrBlank() || mac.isNullOrEmpty()) {
return false
}
return true
}

companion object : VerificationInfoMacFactory {
override fun create(tid: String, mac: Map<String, String>, keys: String): VerificationInfoMac {
return MessageVerificationMacContent(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,6 @@ internal data class MessageVerificationReadyContent(

override fun toEventContent() = toContent()

override fun isValid(): Boolean {
if (transactionID.isNullOrBlank() || methods.isNullOrEmpty() || fromDevice.isNullOrEmpty()) {
return false
}
return true
}

companion object : MessageVerificationReadyFactory {
override fun create(tid: String, methods: List<String>, fromDevice: String): VerificationInfoReady {
return MessageVerificationReadyContent(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,6 @@ data class MessageVerificationRequestContent(
@Json(name = "m.new_content") override val newContent: Content? = null
) : MessageContent, VerificationInfoRequest {

override fun isValid(): Boolean {
if (transactionID.isNullOrBlank() || methods.isNullOrEmpty() || fromDevice.isNullOrEmpty()) {
return false
}
return true
}

override val transactionID: String?
get() = relatesTo?.eventId

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,19 +67,6 @@ internal data class KeyVerificationAccept(
override var commitment: String? = null
) : SendToDeviceObject, VerificationInfoAccept {

override fun isValid(): Boolean {
if (transactionID.isNullOrBlank()
|| keyAgreementProtocol.isNullOrBlank()
|| hash.isNullOrBlank()
|| commitment.isNullOrBlank()
|| messageAuthenticationCode.isNullOrBlank()
|| shortAuthenticationStrings.isNullOrEmpty()) {
Timber.e("## received invalid verification request")
return false
}
return true
}

override fun toSendToDeviceObject() = this

companion object : VerificationInfoAcceptFactory {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,4 @@ internal data class KeyVerificationCancel(
}

override fun toSendToDeviceObject() = this

override fun isValid(): Boolean {
if (transactionID.isNullOrBlank() || code.isNullOrBlank()) {
return false
}
return true
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,4 @@ internal data class KeyVerificationDone(
) : SendToDeviceObject, VerificationInfoDone {

override fun toSendToDeviceObject() = this

override fun isValid(): Boolean {
if (transactionID.isNullOrBlank()) {
return false
}
return true
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,4 @@ internal data class KeyVerificationKey(
}

override fun toSendToDeviceObject() = this

override fun isValid(): Boolean {
if (transactionID.isNullOrBlank() || key.isNullOrBlank()) {
return false
}
return true
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,6 @@ internal data class KeyVerificationMac(

) : SendToDeviceObject, VerificationInfoMac {

override fun isValid(): Boolean {
if (transactionID.isNullOrBlank() || keys.isNullOrBlank() || mac.isNullOrEmpty()) {
return false
}
return true
}

override fun toSendToDeviceObject(): SendToDeviceObject? = this

companion object : VerificationInfoMacFactory {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,4 @@ internal data class KeyVerificationReady(
) : SendToDeviceObject, VerificationInfoReady {

override fun toSendToDeviceObject() = this

override fun isValid(): Boolean {
return !transactionID.isNullOrBlank() && !fromDevice.isNullOrBlank() && !methods.isNullOrEmpty()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,4 @@ internal data class KeyVerificationRequest(
) : SendToDeviceObject, VerificationInfoRequest {

override fun toSendToDeviceObject() = this

override fun isValid(): Boolean {
if (transactionID.isNullOrBlank() || methods.isNullOrEmpty() || fromDevice.isNullOrEmpty()) {
return false
}
return true
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,11 @@ package im.vector.matrix.android.internal.crypto.verification
import im.vector.matrix.android.api.session.events.model.Content
import im.vector.matrix.android.internal.crypto.model.rest.SendToDeviceObject

interface VerificationInfo {
interface VerificationInfo<ValidObject> {
fun toEventContent(): Content? = null
fun toSendToDeviceObject(): SendToDeviceObject? = null
fun isValid(): Boolean

fun asValidObject(): ValidObject?

/**
* String to identify the transaction.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@
*/
package im.vector.matrix.android.internal.crypto.verification

internal interface VerificationInfoAccept : VerificationInfo {
import timber.log.Timber

internal interface VerificationInfoAccept : VerificationInfo<ValidVerificationInfoAccept> {
/**
* The key agreement protocol that Bob’s device has selected to use, out of the list proposed by Alice’s device
*/
Expand All @@ -41,6 +43,27 @@ internal interface VerificationInfoAccept : VerificationInfo {
* and the canonical JSON representation of the m.key.verification.start message.
*/
var commitment: String?

override fun asValidObject(): ValidVerificationInfoAccept? {
if (transactionID.isNullOrBlank()
|| keyAgreementProtocol.isNullOrBlank()
|| hash.isNullOrBlank()
|| commitment.isNullOrBlank()
|| messageAuthenticationCode.isNullOrBlank()
|| shortAuthenticationStrings.isNullOrEmpty()) {
Timber.e("## received invalid verification request")
return null
}

return ValidVerificationInfoAccept(
transactionID!!,
keyAgreementProtocol!!,
hash!!,
messageAuthenticationCode!!,
shortAuthenticationStrings!!,
commitment
)
}
}

internal interface VerificationInfoAcceptFactory {
Expand All @@ -52,3 +75,12 @@ internal interface VerificationInfoAcceptFactory {
messageAuthenticationCode: String,
shortAuthenticationStrings: List<String>): VerificationInfoAccept
}

internal data class ValidVerificationInfoAccept(
val transactionID: String,
val keyAgreementProtocol: String,
val hash: String,
val messageAuthenticationCode: String,
val shortAuthenticationStrings: List<String>,
var commitment: String?
)
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
*/
package im.vector.matrix.android.internal.crypto.verification

internal interface VerificationInfoCancel : VerificationInfo {
internal interface VerificationInfoCancel : VerificationInfo<ValidVerificationInfoCancel> {
/**
* machine-readable reason for cancelling, see [CancelCode]
*/
Expand All @@ -25,4 +25,19 @@ internal interface VerificationInfoCancel : VerificationInfo {
* human-readable reason for cancelling. This should only be used if the receiving client does not understand the code given.
*/
val reason: String?

override fun asValidObject(): ValidVerificationInfoCancel? {
if (transactionID.isNullOrBlank() || code.isNullOrBlank()) {
return null
}
return ValidVerificationInfoCancel(
code!!,
reason
)
}
}

internal data class ValidVerificationInfoCancel(
val code: String,
val reason: String?
)
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,14 @@
*/
package im.vector.matrix.android.internal.crypto.verification

interface VerificationInfoDone : VerificationInfo
internal interface VerificationInfoDone : VerificationInfo<ValidVerificationInfoDone> {

override fun asValidObject(): ValidVerificationInfoDone? {
if (transactionID.isNullOrBlank()) {
return null
}
return ValidVerificationInfoDone
}
}

internal object ValidVerificationInfoDone
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,26 @@ package im.vector.matrix.android.internal.crypto.verification
/**
* Sent by both devices to send their ephemeral Curve25519 public key to the other device.
*/
internal interface VerificationInfoKey : VerificationInfo {
internal interface VerificationInfoKey : VerificationInfo<ValidVerificationInfoKey> {
/**
* The device’s ephemeral public key, as an unpadded base64 string
*/
val key: String?

override fun asValidObject(): ValidVerificationInfoKey? {
if (transactionID.isNullOrBlank() || key.isNullOrBlank()) {
return null
}
return ValidVerificationInfoKey(
key!!
)
}
}

internal interface VerificationInfoKeyFactory {
fun create(tid: String, pubKey: String): VerificationInfoKey
}

internal data class ValidVerificationInfoKey(
val key: String
)
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
*/
package im.vector.matrix.android.internal.crypto.verification

internal interface VerificationInfoMac : VerificationInfo {
internal interface VerificationInfoMac : VerificationInfo<ValidVerificationInfoMac> {
/**
* A map of key ID to the MAC of the key, as an unpadded base64 string, calculated using the MAC key
*/
Expand All @@ -28,8 +28,23 @@ internal interface VerificationInfoMac : VerificationInfo {
* give the MAC of the string “ed25519:ABCDEFG,ed25519:HIJKLMN”.
*/
val keys: String?

override fun asValidObject(): ValidVerificationInfoMac? {
if (transactionID.isNullOrBlank() || keys.isNullOrBlank() || mac.isNullOrEmpty()) {
return null
}
return ValidVerificationInfoMac(
mac!!,
keys!!
)
}
}

internal interface VerificationInfoMacFactory {
fun create(tid: String, mac: Map<String, String>, keys: String) : VerificationInfoMac
fun create(tid: String, mac: Map<String, String>, keys: String): VerificationInfoMac
}

internal data class ValidVerificationInfoMac(
val mac: Map<String, String>,
val keys: String
)
Loading

0 comments on commit 7b5a50e

Please sign in to comment.