Merge pull request #261 from vector-im/feature/e2e_file

Encrypt attachment in e2e rooms
This commit is contained in:
Benoit Marty
2019-07-02 16:44:17 +02:00
committed by GitHub
44 changed files with 941 additions and 161 deletions

View File

@ -228,4 +228,8 @@ data class Event(
}
}
/**
* Tells if the event is redacted
*/
fun isRedacted() = unsignedData?.redactedEvent != null
}

View File

@ -128,6 +128,7 @@ class CreateRoomParams {
contentMap["algorithm"] = algorithm
val algoEvent = Event(type = EventType.ENCRYPTION,
stateKey = "",
content = contentMap.toContent()
)
@ -161,6 +162,7 @@ class CreateRoomParams {
contentMap["history_visibility"] = historyVisibility
val historyVisibilityEvent = Event(type = EventType.STATE_HISTORY_VISIBILITY,
stateKey = "",
content = contentMap.toContent())
if (null == initialStates) {

View File

@ -21,7 +21,18 @@ import com.squareup.moshi.JsonClass
@JsonClass(generateAdapter = true)
data class AudioInfo(
/**
* The mimetype of the audio e.g. "audio/aac".
*/
@Json(name = "mimetype") val mimeType: String,
/**
* The size of the audio clip in bytes.
*/
@Json(name = "size") val size: Long = 0,
/**
* The duration of the audio in milliseconds.
*/
@Json(name = "duration") val duration: Int = 0
)

View File

@ -18,11 +18,32 @@ package im.vector.matrix.android.api.session.room.model.message
import com.squareup.moshi.Json
import com.squareup.moshi.JsonClass
import im.vector.matrix.android.internal.crypto.model.rest.EncryptedFileInfo
@JsonClass(generateAdapter = true)
data class FileInfo(
/**
* The mimetype of the file e.g. application/msword.
*/
@Json(name = "mimetype") val mimeType: String?,
/**
* The size of the file in bytes.
*/
@Json(name = "size") val size: Long = 0,
/**
* Metadata about the image referred to in thumbnail_url.
*/
@Json(name = "thumbnail_info") val thumbnailInfo: ThumbnailInfo? = null,
@Json(name = "thumbnail_url") val thumbnailUrl: String? = null
/**
* The URL to the thumbnail of the file. Only present if the thumbnail is unencrypted.
*/
@Json(name = "thumbnail_url") val thumbnailUrl: String? = null,
/**
* Information on the encrypted thumbnail file, as specified in End-to-end encryption. Only present if the thumbnail is encrypted.
*/
@Json(name = "thumbnail_file") val thumbnailFile: EncryptedFileInfo? = null
)

View File

@ -18,15 +18,52 @@ package im.vector.matrix.android.api.session.room.model.message
import com.squareup.moshi.Json
import com.squareup.moshi.JsonClass
import im.vector.matrix.android.internal.crypto.model.rest.EncryptedFileInfo
@JsonClass(generateAdapter = true)
data class ImageInfo(
/**
* The mimetype of the image, e.g. "image/jpeg".
*/
@Json(name = "mimetype") val mimeType: String?,
/**
* The intended display width of the image in pixels. This may differ from the intrinsic dimensions of the image file.
*/
@Json(name = "w") val width: Int = 0,
/**
* The intended display height of the image in pixels. This may differ from the intrinsic dimensions of the image file.
*/
@Json(name = "h") val height: Int = 0,
/**
* Size of the image in bytes.
*/
@Json(name = "size") val size: Int = 0,
/**
* Not documented
*/
@Json(name = "rotation") val rotation: Int = 0,
/**
* Not documented
*/
@Json(name = "orientation") val orientation: Int = 0,
/**
* Metadata about the image referred to in thumbnail_url.
*/
@Json(name = "thumbnail_info") val thumbnailInfo: ThumbnailInfo? = null,
@Json(name = "thumbnail_url") val thumbnailUrl: String? = null
/**
* The URL (typically MXC URI) to a thumbnail of the image. Only present if the thumbnail is unencrypted.
*/
@Json(name = "thumbnail_url") val thumbnailUrl: String? = null,
/**
* Information on the encrypted thumbnail file, as specified in End-to-end encryption. Only present if the thumbnail is encrypted.
*/
@Json(name = "thumbnail_file") val thumbnailFile: EncryptedFileInfo? = null
)

View File

@ -18,9 +18,22 @@ package im.vector.matrix.android.api.session.room.model.message
import com.squareup.moshi.Json
import com.squareup.moshi.JsonClass
import im.vector.matrix.android.internal.crypto.model.rest.EncryptedFileInfo
@JsonClass(generateAdapter = true)
data class LocationInfo(
/**
* The URL to the thumbnail of the file. Only present if the thumbnail is unencrypted.
*/
@Json(name = "thumbnail_url") val thumbnailUrl: String? = null,
@Json(name = "thumbnail_info") val thumbnailInfo: ThumbnailInfo? = null
/**
* Metadata about the image referred to in thumbnail_url.
*/
@Json(name = "thumbnail_info") val thumbnailInfo: ThumbnailInfo? = null,
/**
* Information on the encrypted thumbnail file, as specified in End-to-end encryption. Only present if the thumbnail is encrypted.
*/
@Json(name = "thumbnail_file") val thumbnailFile: EncryptedFileInfo? = null
)

View File

@ -20,13 +20,35 @@ import com.squareup.moshi.Json
import com.squareup.moshi.JsonClass
import im.vector.matrix.android.api.session.events.model.Content
import im.vector.matrix.android.api.session.room.model.relation.RelationDefaultContent
import im.vector.matrix.android.internal.crypto.model.rest.EncryptedFileInfo
@JsonClass(generateAdapter = true)
data class MessageAudioContent(
/**
* Not documented
*/
@Json(name = "msgtype") override val type: String,
/**
* Required. A description of the audio e.g. 'Bee Gees - Stayin' Alive', or some kind of content description for accessibility e.g. 'audio attachment'.
*/
@Json(name = "body") override val body: String,
@Json(name = "info") val info: AudioInfo? = null,
/**
* Metadata for the audio clip referred to in url.
*/
@Json(name = "info") val audioInfo: AudioInfo? = null,
/**
* Required. Required if the file is not encrypted. The URL (typically MXC URI) to the audio clip.
*/
@Json(name = "url") val url: String? = null,
@Json(name = "m.relates_to") override val relatesTo: RelationDefaultContent? = null,
@Json(name = "m.new_content") override val newContent: Content? = null
) : MessageContent
@Json(name = "m.new_content") override val newContent: Content? = null,
/**
* Required if the file is encrypted. Information on the encrypted file, as specified in End-to-end encryption.
*/
@Json(name = "file") override val encryptedFileInfo: EncryptedFileInfo? = null
) : MessageEncyptedContent

View File

@ -0,0 +1,27 @@
/*
* Copyright 2019 New Vector Ltd
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package im.vector.matrix.android.api.session.room.model.message
import im.vector.matrix.android.internal.crypto.model.rest.EncryptedFileInfo
/**
* Interface for message which can contains encrypted data
*/
interface MessageEncyptedContent : MessageContent {
val encryptedFileInfo: EncryptedFileInfo?
}

View File

@ -20,14 +20,37 @@ import com.squareup.moshi.Json
import com.squareup.moshi.JsonClass
import im.vector.matrix.android.api.session.events.model.Content
import im.vector.matrix.android.api.session.room.model.relation.RelationDefaultContent
import im.vector.matrix.android.internal.crypto.model.rest.EncryptedFileInfo
@JsonClass(generateAdapter = true)
data class MessageFileContent(
/**
* Not documented
*/
@Json(name = "msgtype") override val type: String,
/**
* Required. A human-readable description of the file. This is recommended to be the filename of the original upload.
*/
@Json(name = "body") override val body: String,
/**
* The original filename of the uploaded file.
*/
@Json(name = "filename") val filename: String? = null,
/**
* Information about the file referred to in url.
*/
@Json(name = "info") val info: FileInfo? = null,
/**
* Required. Required if the file is unencrypted. The URL (typically MXC URI) to the file.
*/
@Json(name = "url") val url: String? = null,
@Json(name = "m.relates_to") override val relatesTo: RelationDefaultContent? = null,
@Json(name = "m.new_content") override val newContent: Content? = null
) : MessageContent
@Json(name = "m.new_content") override val newContent: Content? = null,
@Json(name = "file") override val encryptedFileInfo: EncryptedFileInfo? = null
) : MessageEncyptedContent

View File

@ -20,13 +20,36 @@ import com.squareup.moshi.Json
import com.squareup.moshi.JsonClass
import im.vector.matrix.android.api.session.events.model.Content
import im.vector.matrix.android.api.session.room.model.relation.RelationDefaultContent
import im.vector.matrix.android.internal.crypto.model.rest.EncryptedFileInfo
@JsonClass(generateAdapter = true)
data class MessageImageContent(
/**
* Required. Must be 'm.image'.
*/
@Json(name = "msgtype") override val type: String,
/**
* Required. A textual representation of the image. This could be the alt text of the image, the filename of the image,
* or some kind of content description for accessibility e.g. 'image attachment'.
*/
@Json(name = "body") override val body: String,
/**
* Metadata about the image referred to in url.
*/
@Json(name = "info") val info: ImageInfo? = null,
/**
* Required. Required if the file is unencrypted. The URL (typically MXC URI) to the image.
*/
@Json(name = "url") val url: String? = null,
@Json(name = "m.relates_to") override val relatesTo: RelationDefaultContent? = null,
@Json(name = "m.new_content") override val newContent: Content? = null
) : MessageContent
@Json(name = "m.new_content") override val newContent: Content? = null,
/**
* Required if the file is encrypted. Information on the encrypted file, as specified in End-to-end encryption.
*/
@Json(name = "file") override val encryptedFileInfo: EncryptedFileInfo? = null
) : MessageEncyptedContent

View File

@ -23,10 +23,26 @@ import im.vector.matrix.android.api.session.room.model.relation.RelationDefaultC
@JsonClass(generateAdapter = true)
data class MessageLocationContent(
/**
* Not documented
*/
@Json(name = "msgtype") override val type: String,
/**
* Required. A description of the location e.g. 'Big Ben, London, UK', or some kind of content description for accessibility e.g. 'location attachment'.
*/
@Json(name = "body") override val body: String,
/**
* Required. A geo URI representing this location.
*/
@Json(name = "geo_uri") val geoUri: String,
@Json(name = "info") val info: LocationInfo? = null,
/**
*
*/
@Json(name = "info") val locationInfo: LocationInfo? = null,
@Json(name = "m.relates_to") override val relatesTo: RelationDefaultContent? = null,
@Json(name = "m.new_content") override val newContent: Content? = null
) : MessageContent

View File

@ -20,13 +20,35 @@ import com.squareup.moshi.Json
import com.squareup.moshi.JsonClass
import im.vector.matrix.android.api.session.events.model.Content
import im.vector.matrix.android.api.session.room.model.relation.RelationDefaultContent
import im.vector.matrix.android.internal.crypto.model.rest.EncryptedFileInfo
@JsonClass(generateAdapter = true)
data class MessageVideoContent(
/**
* Required. Must be 'm.video'.
*/
@Json(name = "msgtype") override val type: String,
/**
* Required. A description of the video e.g. 'Gangnam style', or some kind of content description for accessibility e.g. 'video attachment'.
*/
@Json(name = "body") override val body: String,
@Json(name = "info") val info: VideoInfo? = null,
/**
* Metadata about the video clip referred to in url.
*/
@Json(name = "info") val videoInfo: VideoInfo? = null,
/**
* Required. Required if the file is unencrypted. The URL (typically MXC URI) to the video clip.
*/
@Json(name = "url") val url: String? = null,
@Json(name = "m.relates_to") override val relatesTo: RelationDefaultContent? = null,
@Json(name = "m.new_content") override val newContent: Content? = null
) : MessageContent
@Json(name = "m.new_content") override val newContent: Content? = null,
/**
* Required if the file is encrypted. Information on the encrypted file, as specified in End-to-end encryption.
*/
@Json(name = "file") override val encryptedFileInfo: EncryptedFileInfo? = null
) : MessageEncyptedContent

View File

@ -21,8 +21,23 @@ import com.squareup.moshi.JsonClass
@JsonClass(generateAdapter = true)
data class ThumbnailInfo(
/**
* The intended display width of the image in pixels. This may differ from the intrinsic dimensions of the image file.
*/
@Json(name = "w") val width: Int = 0,
/**
* The intended display height of the image in pixels. This may differ from the intrinsic dimensions of the image file.
*/
@Json(name = "h") val height: Int = 0,
/**
* Size of the image in bytes.
*/
@Json(name = "size") val size: Long = 0,
/**
* The mimetype of the image, e.g. "image/jpeg".
*/
@Json(name = "mimetype") val mimeType: String
)

View File

@ -18,14 +18,47 @@ package im.vector.matrix.android.api.session.room.model.message
import com.squareup.moshi.Json
import com.squareup.moshi.JsonClass
import im.vector.matrix.android.internal.crypto.model.rest.EncryptedFileInfo
@JsonClass(generateAdapter = true)
data class VideoInfo(
/**
* The mimetype of the video e.g. "video/mp4".
*/
@Json(name = "mimetype") val mimeType: String,
/**
* The width of the video in pixels.
*/
@Json(name = "w") val width: Int = 0,
/**
* The height of the video in pixels.
*/
@Json(name = "h") val height: Int = 0,
/**
* The size of the video in bytes.
*/
@Json(name = "size") val size: Long = 0,
/**
* The duration of the video in milliseconds.
*/
@Json(name = "duration") val duration: Int = 0,
/**
* Metadata about the image referred to in thumbnail_url.
*/
@Json(name = "thumbnail_info") val thumbnailInfo: ThumbnailInfo? = null,
@Json(name = "thumbnail_url") val thumbnailUrl: String? = null
/**
* The URL (typically MXC URI) to an image thumbnail of the video clip. Only present if the thumbnail is unencrypted.
*/
@Json(name = "thumbnail_url") val thumbnailUrl: String? = null,
/**
* Information on the encrypted thumbnail file, as specified in End-to-end encryption. Only present if the thumbnail is encrypted.
*/
@Json(name = "thumbnail_file") val thumbnailFile: EncryptedFileInfo? = null
)

View File

@ -0,0 +1,46 @@
/*
* Copyright 2016 OpenMarket Ltd
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package im.vector.matrix.android.internal.crypto.attachments
import android.os.Parcelable
import im.vector.matrix.android.internal.crypto.model.rest.EncryptedFileInfo
import kotlinx.android.parcel.Parcelize
fun EncryptedFileInfo.toElementToDecrypt(): ElementToDecrypt? {
// Check the validity of some fields
if (isValid()) {
return ElementToDecrypt(
iv = this.iv!!,
k = this.key!!.k!!,
sha256 = this.hashes!!["sha256"] ?: error("")
)
}
return null
}
/**
* Represent data to decode an attachment
*/
@Parcelize
data class ElementToDecrypt(
val iv: String,
val k: String,
val sha256: String
) : Parcelable

View File

@ -14,7 +14,7 @@
* limitations under the License.
*/
package im.vector.matrix.android.internal.crypto
package im.vector.matrix.android.internal.crypto.attachments
import android.text.TextUtils
import android.util.Base64
@ -42,7 +42,7 @@ object MXEncryptedAttachments {
*/
data class EncryptionResult(
var encryptedFileInfo: EncryptedFileInfo,
var encryptedStream: InputStream
var encryptedByteArray: ByteArray
)
/***
@ -112,7 +112,7 @@ object MXEncryptedAttachments {
hashes = mapOf("sha256" to base64ToUnpaddedBase64(Base64.encodeToString(messageDigest.digest(), Base64.DEFAULT))!!),
v = "v2"
),
encryptedStream = ByteArrayInputStream(outStream.toByteArray())
encryptedByteArray = outStream.toByteArray()
)
outStream.close()
@ -142,43 +142,37 @@ object MXEncryptedAttachments {
* @return the decrypted attachment stream
*/
fun decryptAttachment(attachmentStream: InputStream?, encryptedFileInfo: EncryptedFileInfo?): InputStream? {
if (encryptedFileInfo?.isValid() != true) {
Timber.e("## decryptAttachment() : some fields are not defined, or invalid key fields")
return null
}
val elementToDecrypt = encryptedFileInfo.toElementToDecrypt()
return decryptAttachment(attachmentStream, elementToDecrypt)
}
/**
* Decrypt an attachment
*
* @param attachmentStream the attachment stream
* @param elementToDecrypt the elementToDecrypt info
* @return the decrypted attachment stream
*/
fun decryptAttachment(attachmentStream: InputStream?, elementToDecrypt: ElementToDecrypt?): InputStream? {
// sanity checks
if (null == attachmentStream || null == encryptedFileInfo) {
Timber.e("## decryptAttachment() : null parameters")
if (null == attachmentStream || elementToDecrypt == null) {
Timber.e("## decryptAttachment() : null stream")
return null
}
if (TextUtils.isEmpty(encryptedFileInfo.iv)
|| null == encryptedFileInfo.key
|| null == encryptedFileInfo.hashes
|| !encryptedFileInfo.hashes.containsKey("sha256")) {
Timber.e("## decryptAttachment() : some fields are not defined")
return null
}
if (!TextUtils.equals(encryptedFileInfo.key!!.alg, "A256CTR")
|| !TextUtils.equals(encryptedFileInfo.key!!.kty, "oct")
|| TextUtils.isEmpty(encryptedFileInfo.key!!.k)) {
Timber.e("## decryptAttachment() : invalid key fields")
return null
}
// detect if there is no data to decrypt
try {
if (0 == attachmentStream.available()) {
return ByteArrayInputStream(ByteArray(0))
}
} catch (e: Exception) {
Timber.e(e, "Fail to retrieve the file size")
}
val t0 = System.currentTimeMillis()
val outStream = ByteArrayOutputStream()
try {
val key = Base64.decode(base64UrlToBase64(encryptedFileInfo.key!!.k), Base64.DEFAULT)
val initVectorBytes = Base64.decode(encryptedFileInfo.iv, Base64.DEFAULT)
val key = Base64.decode(base64UrlToBase64(elementToDecrypt.k), Base64.DEFAULT)
val initVectorBytes = Base64.decode(elementToDecrypt.iv, Base64.DEFAULT)
val decryptCipher = Cipher.getInstance(CIPHER_ALGORITHM)
val secretKeySpec = SecretKeySpec(key, SECRET_KEY_SPEC_ALGORITHM)
@ -205,7 +199,7 @@ object MXEncryptedAttachments {
val currentDigestValue = base64ToUnpaddedBase64(Base64.encodeToString(messageDigest.digest(), Base64.DEFAULT))
if (!TextUtils.equals(encryptedFileInfo.hashes["sha256"], currentDigestValue)) {
if (!TextUtils.equals(elementToDecrypt.sha256, currentDigestValue)) {
Timber.e("## decryptAttachment() : Digest value mismatch")
outStream.close()
return null

View File

@ -15,14 +15,75 @@
*/
package im.vector.matrix.android.internal.crypto.model.rest
import com.squareup.moshi.Json
import com.squareup.moshi.JsonClass
/**
* In Matrix specs: EncryptedFile
*/
@JsonClass(generateAdapter = true)
data class EncryptedFileInfo(
/**
* Required. The URL to the file.
*/
@Json(name = "url")
var url: String? = null,
var mimetype: String,
/**
* Not documented
*/
@Json(name = "mimetype")
var mimetype: String? = null,
/**
* Required. A JSON Web Key object.
*/
@Json(name = "key")
var key: EncryptedFileKey? = null,
var iv: String,
var hashes: Map<String, String>,
/**
* Required. The Initialisation Vector used by AES-CTR, encoded as unpadded base64.
*/
@Json(name = "iv")
var iv: String? = null,
/**
* Required. A map from an algorithm name to a hash of the ciphertext, encoded as unpadded base64.
* Clients should support the SHA-256 hash, which uses the key "sha256".
*/
@Json(name = "hashes")
var hashes: Map<String, String>? = null,
/**
* Required. Version of the encrypted attachments protocol. Must be "v2".
*/
@Json(name = "v")
var v: String? = null
)
) {
/**
* Check what the spec tells us
*/
fun isValid(): Boolean {
if (url.isNullOrBlank()) {
return false
}
if (key?.isValid() != true) {
return false
}
if (iv.isNullOrBlank()) {
return false
}
if (hashes?.containsKey("sha256") != true) {
return false
}
if (v != "v2") {
return false
}
return true
}
}

View File

@ -15,14 +15,66 @@
*/
package im.vector.matrix.android.internal.crypto.model.rest
import com.squareup.moshi.Json
import com.squareup.moshi.JsonClass
@JsonClass(generateAdapter = true)
data class EncryptedFileKey(
var alg: String,
var ext: Boolean? = null,
var key_ops: List<String>,
var kty: String,
var k: String
)
/**
* Required. Algorithm. Must be "A256CTR".
*/
@Json(name = "alg")
var alg: String? = null,
/**
* Required. Extractable. Must be true. This is a W3C extension.
*/
@Json(name = "ext")
var ext: Boolean? = null,
/**
* Required. Key operations. Must at least contain "encrypt" and "decrypt".
*/
@Json(name = "key_ops")
var key_ops: List<String>? = null,
/**
* Required. Key type. Must be "oct".
*/
@Json(name = "kty")
var kty: String? = null,
/**
* Required. The key, encoded as urlsafe unpadded base64.
*/
@Json(name = "k")
var k: String? = null
) {
/**
* Check what the spec tells us
*/
fun isValid(): Boolean {
if (alg != "A256CTR") {
return false
}
if (ext != true) {
return false
}
if (key_ops?.contains("encrypt") != true || key_ops?.contains("decrypt") != true) {
return false
}
if (kty != "oct") {
return false
}
if (k.isNullOrBlank()) {
return false
}
return true
}
}

View File

@ -54,7 +54,6 @@ internal class FileUploader @Inject constructor(@Authenticated
val uploadBody = RequestBody.create(MediaType.parse(mimeType), byteArray)
return upload(uploadBody, filename, progressListener)
}

View File

@ -25,13 +25,17 @@ import im.vector.matrix.android.api.session.events.model.Event
import im.vector.matrix.android.api.session.events.model.toContent
import im.vector.matrix.android.api.session.events.model.toModel
import im.vector.matrix.android.api.session.room.model.message.*
import im.vector.matrix.android.internal.crypto.attachments.MXEncryptedAttachments
import im.vector.matrix.android.internal.crypto.model.rest.EncryptedFileInfo
import im.vector.matrix.android.internal.network.ProgressRequestBody
import im.vector.matrix.android.internal.session.room.send.SendEventWorker
import im.vector.matrix.android.internal.worker.SessionWorkerParams
import im.vector.matrix.android.internal.worker.WorkerParamsFactory
import im.vector.matrix.android.internal.worker.getSessionComponent
import timber.log.Timber
import java.io.ByteArrayInputStream
import java.io.File
import java.io.FileInputStream
import javax.inject.Inject
@ -42,7 +46,9 @@ internal class UploadContentWorker(context: Context, params: WorkerParameters) :
override val userId: String,
val roomId: String,
val event: Event,
val attachment: ContentAttachmentData
val attachment: ContentAttachmentData,
val isRoomEncrypted: Boolean,
override var lastFailureMessage: String? = null
) : SessionWorkerParams
@Inject lateinit var fileUploader: FileUploader
@ -52,19 +58,41 @@ internal class UploadContentWorker(context: Context, params: WorkerParameters) :
val params = WorkerParamsFactory.fromData<Params>(inputData)
?: return Result.success()
if (params.lastFailureMessage != null) {
// Transmit the error
return Result.success(inputData)
}
val sessionComponent = getSessionComponent(params.userId) ?: return Result.success()
sessionComponent.inject(this)
val eventId = params.event.eventId ?: return Result.success()
val attachment = params.attachment
val isRoomEncrypted = params.isRoomEncrypted
val thumbnailData = ThumbnailExtractor.extractThumbnail(params.attachment)
val attachmentFile = createAttachmentFile(attachment) ?: return Result.failure()
var uploadedThumbnailUrl: String? = null
var uploadedThumbnailEncryptedFileInfo: EncryptedFileInfo? = null
if (thumbnailData != null) {
fileUploader
.uploadByteArray(thumbnailData.bytes, "thumb_${attachment.name}", thumbnailData.mimeType)
val contentUploadResponse = if (isRoomEncrypted) {
Timber.v("Encrypt thumbnail")
val encryptionResult = MXEncryptedAttachments.encryptAttachment(ByteArrayInputStream(thumbnailData.bytes), thumbnailData.mimeType)
?: return Result.failure()
uploadedThumbnailEncryptedFileInfo = encryptionResult.encryptedFileInfo
fileUploader
.uploadByteArray(encryptionResult.encryptedByteArray, "thumb_${attachment.name}", thumbnailData.mimeType)
} else {
fileUploader
.uploadByteArray(thumbnailData.bytes, "thumb_${attachment.name}", thumbnailData.mimeType)
}
contentUploadResponse
.fold(
{ Timber.e(it) },
{ uploadedThumbnailUrl = it.contentUri }
@ -76,11 +104,28 @@ internal class UploadContentWorker(context: Context, params: WorkerParameters) :
contentUploadStateTracker.setProgress(eventId, current, total)
}
}
return fileUploader
.uploadFile(attachmentFile, attachment.name, attachment.mimeType, progressListener)
var uploadedFileEncryptedFileInfo: EncryptedFileInfo? = null
val contentUploadResponse = if (isRoomEncrypted) {
Timber.v("Encrypt file")
val encryptionResult = MXEncryptedAttachments.encryptAttachment(FileInputStream(attachmentFile), attachment.mimeType)
?: return Result.failure()
uploadedFileEncryptedFileInfo = encryptionResult.encryptedFileInfo
fileUploader
.uploadByteArray(encryptionResult.encryptedByteArray, attachment.name, "application/octet-stream", progressListener)
} else {
fileUploader
.uploadFile(attachmentFile, attachment.name, attachment.mimeType, progressListener)
}
return contentUploadResponse
.fold(
{ handleFailure(params) },
{ handleSuccess(params, it.contentUri, uploadedThumbnailUrl) }
{ handleFailure(params, it) },
{ handleSuccess(params, it.contentUri, uploadedFileEncryptedFileInfo, uploadedThumbnailUrl, uploadedThumbnailEncryptedFileInfo) }
)
}
@ -93,46 +138,79 @@ internal class UploadContentWorker(context: Context, params: WorkerParameters) :
}
}
private fun handleFailure(params: Params): Result {
private fun handleFailure(params: Params, failure: Throwable): Result {
contentUploadStateTracker.setFailure(params.event.eventId!!)
return Result.success()
return Result.success(
WorkerParamsFactory.toData(
params.copy(
lastFailureMessage = failure.localizedMessage
)
)
)
}
private fun handleSuccess(params: Params,
attachmentUrl: String,
thumbnailUrl: String?): Result {
encryptedFileInfo: EncryptedFileInfo?,
thumbnailUrl: String?,
thumbnailEncryptedFileInfo: EncryptedFileInfo?): Result {
contentUploadStateTracker.setSuccess(params.event.eventId!!)
val event = updateEvent(params.event, attachmentUrl, thumbnailUrl)
val event = updateEvent(params.event, attachmentUrl, encryptedFileInfo, thumbnailUrl, thumbnailEncryptedFileInfo)
val sendParams = SendEventWorker.Params(params.userId, params.roomId, event)
return Result.success(WorkerParamsFactory.toData(sendParams))
}
private fun updateEvent(event: Event, url: String, thumbnailUrl: String? = null): Event {
private fun updateEvent(event: Event,
url: String,
encryptedFileInfo: EncryptedFileInfo?,
thumbnailUrl: String? = null,
thumbnailEncryptedFileInfo: EncryptedFileInfo?): Event {
val messageContent: MessageContent = event.content.toModel() ?: return event
val updatedContent = when (messageContent) {
is MessageImageContent -> messageContent.update(url)
is MessageVideoContent -> messageContent.update(url, thumbnailUrl)
is MessageFileContent -> messageContent.update(url)
is MessageAudioContent -> messageContent.update(url)
is MessageImageContent -> messageContent.update(url, encryptedFileInfo)
is MessageVideoContent -> messageContent.update(url, encryptedFileInfo, thumbnailUrl, thumbnailEncryptedFileInfo)
is MessageFileContent -> messageContent.update(url, encryptedFileInfo)
is MessageAudioContent -> messageContent.update(url, encryptedFileInfo)
else -> messageContent
}
return event.copy(content = updatedContent.toContent())
}
private fun MessageImageContent.update(url: String): MessageImageContent {
return copy(url = url)
private fun MessageImageContent.update(url: String,
encryptedFileInfo: EncryptedFileInfo?): MessageImageContent {
return copy(
url = if (encryptedFileInfo == null) url else null,
encryptedFileInfo = encryptedFileInfo?.copy(url = url)
)
}
private fun MessageVideoContent.update(url: String, thumbnailUrl: String?): MessageVideoContent {
return copy(url = url, info = info?.copy(thumbnailUrl = thumbnailUrl))
private fun MessageVideoContent.update(url: String,
encryptedFileInfo: EncryptedFileInfo?,
thumbnailUrl: String?,
thumbnailEncryptedFileInfo: EncryptedFileInfo?): MessageVideoContent {
return copy(
url = if (encryptedFileInfo == null) url else null,
videoInfo = videoInfo?.copy(
thumbnailUrl = if (thumbnailEncryptedFileInfo == null) thumbnailUrl else null,
thumbnailFile = thumbnailEncryptedFileInfo?.copy(url = url)
)
)
}
private fun MessageFileContent.update(url: String): MessageFileContent {
return copy(url = url)
private fun MessageFileContent.update(url: String,
encryptedFileInfo: EncryptedFileInfo?): MessageFileContent {
return copy(
url = if (encryptedFileInfo == null) url else null,
encryptedFileInfo = encryptedFileInfo?.copy(url = url)
)
}
private fun MessageAudioContent.update(url: String): MessageAudioContent {
return copy(url = url)
private fun MessageAudioContent.update(url: String,
encryptedFileInfo: EncryptedFileInfo?): MessageAudioContent {
return copy(
url = if (encryptedFileInfo == null) url else null,
encryptedFileInfo = encryptedFileInfo?.copy(url = url)
)
}
}

View File

@ -20,28 +20,26 @@ import android.content.Context
import androidx.work.CoroutineWorker
import androidx.work.WorkerParameters
import arrow.core.Try
import com.squareup.inject.assisted.Assisted
import com.squareup.inject.assisted.AssistedInject
import com.squareup.moshi.JsonClass
import im.vector.matrix.android.internal.worker.DelegateWorkerFactory
import im.vector.matrix.android.internal.worker.SessionWorkerParams
import im.vector.matrix.android.internal.worker.WorkerParamsFactory
import im.vector.matrix.android.internal.worker.getSessionComponent
import javax.inject.Inject
internal class GetGroupDataWorker (context: Context, params: WorkerParameters) : CoroutineWorker(context, params) {
internal class GetGroupDataWorker(context: Context, params: WorkerParameters) : CoroutineWorker(context, params) {
@JsonClass(generateAdapter = true)
internal data class Params(
override val userId: String,
val groupIds: List<String>
): SessionWorkerParams
val groupIds: List<String>,
override var lastFailureMessage: String? = null
) : SessionWorkerParams
@Inject lateinit var getGroupDataTask: GetGroupDataTask
override suspend fun doWork(): Result {
val params = WorkerParamsFactory.fromData<Params>(inputData)
?: return Result.failure()
?: return Result.failure()
val sessionComponent = getSessionComponent(params.userId) ?: return Result.success()
sessionComponent.inject(this)

View File

@ -31,10 +31,8 @@ import im.vector.matrix.android.api.util.Cancelable
import im.vector.matrix.android.internal.database.RealmLiveData
import im.vector.matrix.android.internal.database.helper.addSendingEvent
import im.vector.matrix.android.internal.database.mapper.asDomain
import im.vector.matrix.android.internal.database.model.ChunkEntity
import im.vector.matrix.android.internal.database.model.EventAnnotationsSummaryEntity
import im.vector.matrix.android.internal.database.model.RoomEntity
import im.vector.matrix.android.internal.database.query.findLastLiveChunkFromRoom
import im.vector.matrix.android.internal.database.query.where
import im.vector.matrix.android.internal.session.room.send.EncryptEventWorker
import im.vector.matrix.android.internal.session.room.send.LocalEchoEventFactory
@ -107,9 +105,12 @@ internal class DefaultRelationService @Inject constructor(private val context: C
//TODO duplicate with send service?
private fun createRedactEventWork(localEvent: Event, eventId: String, reason: String?): OneTimeWorkRequest {
val sendContentWorkerParams = RedactEventWorker.Params(credentials.userId, localEvent.eventId!!,
roomId, eventId, reason)
val sendContentWorkerParams = RedactEventWorker.Params(
credentials.userId,
localEvent.eventId!!,
roomId,
eventId,
reason)
val redactWorkData = WorkerParamsFactory.toData(sendContentWorkerParams)
return TimelineSendEventWorkCommon.createWork<RedactEventWorker>(redactWorkData)
}

View File

@ -39,7 +39,8 @@ internal class SendRelationWorker(context: Context, params: WorkerParameters) :
override val userId: String,
val roomId: String,
val event: Event,
val relationType: String? = null
val relationType: String? = null,
override var lastFailureMessage: String?
) : SessionWorkerParams
@Inject lateinit var roomAPI: RoomAPI
@ -48,6 +49,11 @@ internal class SendRelationWorker(context: Context, params: WorkerParameters) :
val params = WorkerParamsFactory.fromData<Params>(inputData)
?: return Result.failure()
if (params.lastFailureMessage != null) {
// Transmit the error
return Result.success(inputData)
}
val sessionComponent = getSessionComponent(params.userId) ?: return Result.success()
sessionComponent.inject(this)

View File

@ -101,13 +101,26 @@ internal class DefaultSendService @Inject constructor(private val context: Conte
val event = localEchoEventFactory.createMediaEvent(roomId, attachment).also {
saveLocalEcho(it)
}
val uploadWork = createUploadMediaWork(event, attachment)
val isRoomEncrypted = cryptoService.isRoomEncrypted(roomId)
val uploadWork = createUploadMediaWork(event, attachment, isRoomEncrypted)
val sendWork = createSendEventWork(event)
WorkManager.getInstance(context)
.beginUniqueWork(buildWorkIdentifier(UPLOAD_WORK), ExistingWorkPolicy.APPEND, uploadWork)
.then(sendWork)
.enqueue()
if (isRoomEncrypted) {
val encryptWork = createEncryptEventWork(event)
WorkManager.getInstance(context)
.beginUniqueWork(buildWorkIdentifier(UPLOAD_WORK), ExistingWorkPolicy.APPEND, uploadWork)
.then(encryptWork)
.then(sendWork)
.enqueue()
} else {
WorkManager.getInstance(context)
.beginUniqueWork(buildWorkIdentifier(UPLOAD_WORK), ExistingWorkPolicy.APPEND, uploadWork)
.then(sendWork)
.enqueue()
}
return CancelableWork(context, sendWork.id)
}
@ -148,8 +161,8 @@ internal class DefaultSendService @Inject constructor(private val context: Conte
return TimelineSendEventWorkCommon.createWork<RedactEventWorker>(redactWorkData)
}
private fun createUploadMediaWork(event: Event, attachment: ContentAttachmentData): OneTimeWorkRequest {
val uploadMediaWorkerParams = UploadContentWorker.Params(credentials.userId, roomId, event, attachment)
private fun createUploadMediaWork(event: Event, attachment: ContentAttachmentData, isRoomEncrypted: Boolean): OneTimeWorkRequest {
val uploadMediaWorkerParams = UploadContentWorker.Params(credentials.userId, roomId, event, attachment, isRoomEncrypted)
val uploadWorkData = WorkerParamsFactory.toData(uploadMediaWorkerParams)
return OneTimeWorkRequestBuilder<UploadContentWorker>()

View File

@ -41,7 +41,8 @@ internal class EncryptEventWorker(context: Context, params: WorkerParameters)
val roomId: String,
val event: Event,
/**Do not encrypt these keys, keep them as is in encrypted content (e.g. m.relates_to)*/
val keepKeys: List<String>? = null
val keepKeys: List<String>? = null,
override var lastFailureMessage: String? = null
) : SessionWorkerParams
@Inject lateinit var crypto: CryptoService
@ -52,6 +53,11 @@ internal class EncryptEventWorker(context: Context, params: WorkerParameters)
val params = WorkerParamsFactory.fromData<Params>(inputData)
?: return Result.success()
if (params.lastFailureMessage != null) {
// Transmit the error
return Result.success(inputData)
}
val sessionComponent = getSessionComponent(params.userId) ?: return Result.success()
sessionComponent.inject(this)
@ -105,16 +111,15 @@ internal class EncryptEventWorker(context: Context, params: WorkerParameters)
)
val nextWorkerParams = SendEventWorker.Params(params.userId, params.roomId, encryptedEvent)
return Result.success(WorkerParamsFactory.toData(nextWorkerParams))
} else {
val sendState = when (error) {
is Failure.CryptoError -> SendState.FAILED_UNKNOWN_DEVICES
else -> SendState.UNDELIVERED
}
localEchoUpdater.updateSendState(localEvent.eventId, sendState)
//always return success, or the chain will be stuck for ever!
val nextWorkerParams = SendEventWorker.Params(params.userId, params.roomId, localEvent, error?.localizedMessage ?: "Error")
return Result.success(WorkerParamsFactory.toData(nextWorkerParams))
}
val safeError = error
val sendState = when (safeError) {
is Failure.CryptoError -> SendState.FAILED_UNKNOWN_DEVICES
else -> SendState.UNDELIVERED
}
localEchoUpdater.updateSendState(localEvent.eventId, sendState)
//always return success, or the chain will be stuck for ever!
return Result.success()
}
}

View File

@ -179,7 +179,7 @@ internal class LocalEchoEventFactory @Inject constructor(private val credentials
val content = MessageVideoContent(
type = MessageType.MSGTYPE_VIDEO,
body = attachment.name ?: "video",
info = VideoInfo(
videoInfo = VideoInfo(
mimeType = attachment.mimeType,
width = width,
height = height,
@ -198,7 +198,7 @@ internal class LocalEchoEventFactory @Inject constructor(private val credentials
val content = MessageAudioContent(
type = MessageType.MSGTYPE_AUDIO,
body = attachment.name ?: "audio",
info = AudioInfo(
audioInfo = AudioInfo(
mimeType = attachment.mimeType ?: "audio/mpeg",
size = attachment.size
),

View File

@ -35,7 +35,8 @@ internal class RedactEventWorker(context: Context, params: WorkerParameters) : C
val txID: String,
val roomId: String,
val eventId: String,
val reason: String?
val reason: String?,
override var lastFailureMessage: String? = null
) : SessionWorkerParams
@Inject lateinit var roomAPI: RoomAPI
@ -44,6 +45,11 @@ internal class RedactEventWorker(context: Context, params: WorkerParameters) : C
val params = WorkerParamsFactory.fromData<Params>(inputData)
?: return Result.failure()
if (params.lastFailureMessage != null) {
// Transmit the error
return Result.success(inputData)
}
val sessionComponent = getSessionComponent(params.userId) ?: return Result.success()
sessionComponent.inject(this)
@ -62,7 +68,9 @@ internal class RedactEventWorker(context: Context, params: WorkerParameters) : C
else -> {
//TODO mark as failed to send?
//always return success, or the chain will be stuck for ever!
Result.success()
Result.success(WorkerParamsFactory.toData(params.copy(
lastFailureMessage = it.localizedMessage
)))
}
}
}, {

View File

@ -38,14 +38,14 @@ internal class SendEventWorker constructor(context: Context, params: WorkerParam
internal data class Params(
override val userId: String,
val roomId: String,
val event: Event
val event: Event,
override var lastFailureMessage: String? = null
) : SessionWorkerParams
@Inject lateinit var localEchoUpdater: LocalEchoUpdater
@Inject lateinit var roomAPI: RoomAPI
override suspend fun doWork(): Result {
val params = WorkerParamsFactory.fromData<Params>(inputData)
?: return Result.success()
@ -57,6 +57,13 @@ internal class SendEventWorker constructor(context: Context, params: WorkerParam
return Result.success()
}
if (params.lastFailureMessage != null) {
localEchoUpdater.updateSendState(event.eventId, SendState.UNDELIVERED)
// Transmit the error
return Result.success(inputData)
}
localEchoUpdater.updateSendState(event.eventId, SendState.SENDING)
val result = executeRequest<SendResponse> {
apiCall = roomAPI.send(

View File

@ -107,7 +107,7 @@ internal class InMemoryTimelineEventFactory @Inject constructor(private val room
senderRoomMember?.avatarUrl)
}
val event = eventEntity.asDomain()
if (event.getClearType() == EventType.ENCRYPTED) {
if (event.getClearType() == EventType.ENCRYPTED && !event.isRedacted()) {
handleEncryptedEvent(event, eventEntity.localId)
}
@ -141,6 +141,9 @@ internal class InMemoryTimelineEventFactory @Inject constructor(private val room
Timber.e(failure, "Encrypted event: decryption failed")
if (failure is MXDecryptionException) {
event.setCryptoError(failure.cryptoError)
} else {
// Other error
Timber.e("Other error, should be handled")
}
}
}

View File

@ -18,4 +18,7 @@ package im.vector.matrix.android.internal.worker
interface SessionWorkerParams {
val userId: String
}
// Null is no error occurs. When chaining Workers, first step is to check that there is no lastFailureMessage from the previous workers
var lastFailureMessage: String?
}