Merge pull request #955 from vector-im/feature/room_history_in_e2e

Feature/room history in e2e
This commit is contained in:
Benoit Marty 2020-02-04 21:18:26 +01:00 committed by GitHub
commit 062c4559a2
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
16 changed files with 111 additions and 63 deletions

View file

@ -12,6 +12,7 @@ Other changes:
Bugfix 🐛:
- Ask for permission before opening the camera (#934)
- Encrypt for invited users by default, if the room state allows it (#803)
Translations 🗣:
-

View file

@ -74,7 +74,7 @@ class CryptoTestHelper(val mTestHelper: CommonTestHelper) {
val room = aliceSession.getRoom(roomId!!)!!
val lock2 = CountDownLatch(1)
room.enableEncryptionWithAlgorithm(MXCRYPTO_ALGORITHM_MEGOLM, object : TestMatrixCallback<Unit>(lock2) {})
room.enableEncryption(callback = TestMatrixCallback(lock2))
mTestHelper.await(lock2)
return CryptoTestData(aliceSession, roomId!!)

View file

@ -23,6 +23,7 @@ import androidx.work.WorkManager
import com.zhuinden.monarchy.Monarchy
import im.vector.matrix.android.BuildConfig
import im.vector.matrix.android.api.auth.AuthenticationService
import im.vector.matrix.android.api.crypto.MXCryptoConfig
import im.vector.matrix.android.internal.SessionManager
import im.vector.matrix.android.internal.crypto.attachments.ElementToDecrypt
import im.vector.matrix.android.internal.crypto.attachments.MXEncryptedAttachments
@ -35,7 +36,8 @@ import java.util.concurrent.atomic.AtomicBoolean
import javax.inject.Inject
data class MatrixConfiguration(
val applicationFlavor: String = "Default-application-flavor"
val applicationFlavor: String = "Default-application-flavor",
val cryptoConfig: MXCryptoConfig = MXCryptoConfig()
) {
interface Provider {
@ -57,12 +59,11 @@ class Matrix private constructor(context: Context, matrixConfiguration: MatrixCo
init {
Monarchy.init(context)
DaggerMatrixComponent.factory().create(context).inject(this)
DaggerMatrixComponent.factory().create(context, matrixConfiguration).inject(this)
if (context.applicationContext !is Configuration.Provider) {
WorkManager.initialize(context, Configuration.Builder().build())
}
ProcessLifecycleOwner.get().lifecycle.addObserver(backgroundDetectionObserver)
userAgentHolder.setApplicationFlavor(matrixConfiguration.applicationFlavor)
}
fun getUserAgent() = userAgentHolder.userAgent

View file

@ -14,14 +14,14 @@
* limitations under the License.
*/
package im.vector.matrix.android.internal.crypto
package im.vector.matrix.android.api.crypto
/**
* Class to define the parameters used to customize or configure the end-to-end crypto.
*/
data class MXCryptoConfig(
// Tell whether the encryption of the event content is enabled for the invited members.
// By default, we encrypt messages only for the joined members.
// The encryption for the invited members will be blocked if the history visibility is "joined".
var enableEncryptionForInvitedMembers: Boolean = false
// SDK clients can disable this by settings it to false.
// Note that the encryption for the invited members will be blocked if the history visibility is "joined".
var enableEncryptionForInvitedMembers: Boolean = true
)

View file

@ -17,6 +17,7 @@
package im.vector.matrix.android.api.session.room.crypto
import im.vector.matrix.android.api.MatrixCallback
import im.vector.matrix.android.internal.crypto.MXCRYPTO_ALGORITHM_MEGOLM
interface RoomCryptoService {
@ -26,5 +27,9 @@ interface RoomCryptoService {
fun shouldEncryptForInvitedMembers(): Boolean
fun enableEncryptionWithAlgorithm(algorithm: String, callback: MatrixCallback<Unit>)
/**
* Enable encryption of the room
*/
fun enableEncryption(algorithm: String = MXCRYPTO_ALGORITHM_MEGOLM,
callback: MatrixCallback<Unit>)
}

View file

@ -18,9 +18,28 @@ package im.vector.matrix.android.api.session.room.model
import com.squareup.moshi.Json
/**
* Ref: https://matrix.org/docs/spec/client_server/latest#room-history-visibility
*/
enum class RoomHistoryVisibility {
/**
* All events while this is the m.room.history_visibility value may be shared by any
* participating homeserver with anyone, regardless of whether they have ever joined the room.
*/
@Json(name = "world_readable") WORLD_READABLE,
/**
* Previous events are always accessible to newly joined members. All events in the
* room are accessible, even those sent when the member was not a part of the room.
*/
@Json(name = "shared") SHARED,
/**
* Events are accessible to newly joined members from the point they were invited onwards.
* Events stop being accessible when the member's state changes to something other than invite or join.
*/
@Json(name = "invited") INVITED,
@Json(name = "joined") JOINED,
@Json(name = "world_readable") WORLD_READABLE
/**
* Events are accessible to newly joined members from the point they joined the room onwards.
* Events stop being accessible when the member's state changes to something other than join.
*/
@Json(name = "joined") JOINED
}

View file

@ -28,11 +28,6 @@ interface StateService {
*/
fun updateTopic(topic: String, callback: MatrixCallback<Unit>)
/**
* Enable encryption of the room
*/
fun enableEncryption(algorithm: String, callback: MatrixCallback<Unit>)
fun getStateEvent(eventType: String): Event?
fun getStateEventLive(eventType: String): LiveData<Optional<Event>>

View file

@ -159,13 +159,6 @@ internal abstract class CryptoModule {
fun providesRoomKeysAPI(retrofit: Retrofit): RoomKeysApi {
return retrofit.create(RoomKeysApi::class.java)
}
@JvmStatic
@Provides
@SessionScope
fun providesCryptoConfig(): MXCryptoConfig {
return MXCryptoConfig()
}
}
@Binds

View file

@ -27,6 +27,7 @@ import com.zhuinden.monarchy.Monarchy
import dagger.Lazy
import im.vector.matrix.android.api.MatrixCallback
import im.vector.matrix.android.api.auth.data.Credentials
import im.vector.matrix.android.api.crypto.MXCryptoConfig
import im.vector.matrix.android.api.failure.Failure
import im.vector.matrix.android.api.listeners.ProgressListener
import im.vector.matrix.android.api.session.crypto.CryptoService
@ -116,7 +117,7 @@ internal class DefaultCryptoService @Inject constructor(
// Olm device
private val olmDevice: MXOlmDevice,
// Set of parameters used to configure/customize the end-to-end crypto.
private val cryptoConfig: MXCryptoConfig = MXCryptoConfig(),
private val mxCryptoConfig: MXCryptoConfig,
// Device list manager
private val deviceListManager: DeviceListManager,
// The key backup service.
@ -399,6 +400,7 @@ internal class DefaultCryptoService @Inject constructor(
null
}
}
override fun getCryptoDeviceInfo(userId: String): List<CryptoDeviceInfo> {
return cryptoStore.getUserDevices(userId)?.map { it.value } ?: emptyList()
}
@ -545,8 +547,8 @@ internal class DefaultCryptoService @Inject constructor(
return cryptoStore.getUserDevices(userId)?.values?.toMutableList() ?: ArrayList()
}
fun isEncryptionEnabledForInvitedUser(): Boolean {
return cryptoConfig.enableEncryptionForInvitedMembers
private fun isEncryptionEnabledForInvitedUser(): Boolean {
return mxCryptoConfig.enableEncryptionForInvitedMembers
}
override fun getEncryptionAlgorithm(roomId: String): String? {
@ -779,7 +781,7 @@ internal class DefaultCryptoService @Inject constructor(
deviceListManager.startTrackingDeviceList(listOf(userId))
} else if (membership == Membership.INVITE
&& shouldEncryptForInvitedMembers(roomId)
&& cryptoConfig.enableEncryptionForInvitedMembers) {
&& isEncryptionEnabledForInvitedUser()) {
// track the deviceList for this invited user.
// Caution: there's a big edge case here in that federated servers do not
// know what other servers are in the room at the time they've been invited.

View file

@ -22,6 +22,7 @@ import com.squareup.moshi.Moshi
import dagger.BindsInstance
import dagger.Component
import im.vector.matrix.android.api.Matrix
import im.vector.matrix.android.api.MatrixConfiguration
import im.vector.matrix.android.api.auth.AuthenticationService
import im.vector.matrix.android.internal.SessionManager
import im.vector.matrix.android.internal.auth.AuthModule
@ -48,6 +49,8 @@ internal interface MatrixComponent {
fun context(): Context
fun matrixConfiguration(): MatrixConfiguration
fun resources(): Resources
fun olmManager(): OlmManager
@ -66,6 +69,7 @@ internal interface MatrixComponent {
@Component.Factory
interface Factory {
fun create(@BindsInstance context: Context): MatrixComponent
fun create(@BindsInstance context: Context,
@BindsInstance matrixConfiguration: MatrixConfiguration): MatrixComponent
}
}

View file

@ -18,18 +18,20 @@ package im.vector.matrix.android.internal.network
import android.content.Context
import im.vector.matrix.android.BuildConfig
import im.vector.matrix.android.api.MatrixConfiguration
import im.vector.matrix.android.internal.di.MatrixScope
import timber.log.Timber
import javax.inject.Inject
@MatrixScope
internal class UserAgentHolder @Inject constructor(private val context: Context) {
internal class UserAgentHolder @Inject constructor(private val context: Context,
matrixConfiguration: MatrixConfiguration) {
var userAgent: String = ""
private set
init {
setApplicationFlavor("NoFlavor")
setApplicationFlavor(matrixConfiguration.applicationFlavor)
}
/**
@ -38,7 +40,7 @@ internal class UserAgentHolder @Inject constructor(private val context: Context)
*
* @param flavorDescription the flavor description
*/
fun setApplicationFlavor(flavorDescription: String) {
private fun setApplicationFlavor(flavorDescription: String) {
var appName = ""
var appVersion = ""

View file

@ -23,10 +23,12 @@ import dagger.Lazy
import dagger.Module
import dagger.Provides
import dagger.multibindings.IntoSet
import im.vector.matrix.android.api.MatrixConfiguration
import im.vector.matrix.android.api.auth.data.Credentials
import im.vector.matrix.android.api.auth.data.HomeServerConnectionConfig
import im.vector.matrix.android.api.auth.data.SessionParams
import im.vector.matrix.android.api.auth.data.sessionId
import im.vector.matrix.android.api.crypto.MXCryptoConfig
import im.vector.matrix.android.api.session.InitialSyncProgressService
import im.vector.matrix.android.api.session.Session
import im.vector.matrix.android.api.session.homeserver.HomeServerCapabilitiesService
@ -34,7 +36,15 @@ import im.vector.matrix.android.api.session.securestorage.SecureStorageService
import im.vector.matrix.android.internal.crypto.verification.VerificationMessageLiveObserver
import im.vector.matrix.android.internal.database.LiveEntityObserver
import im.vector.matrix.android.internal.database.SessionRealmConfigurationFactory
import im.vector.matrix.android.internal.di.*
import im.vector.matrix.android.internal.di.Authenticated
import im.vector.matrix.android.internal.di.DeviceId
import im.vector.matrix.android.internal.di.SessionCacheDirectory
import im.vector.matrix.android.internal.di.SessionDatabase
import im.vector.matrix.android.internal.di.SessionFilesDirectory
import im.vector.matrix.android.internal.di.SessionId
import im.vector.matrix.android.internal.di.Unauthenticated
import im.vector.matrix.android.internal.di.UserId
import im.vector.matrix.android.internal.di.UserMd5
import im.vector.matrix.android.internal.network.AccessTokenInterceptor
import im.vector.matrix.android.internal.network.RetrofitFactory
import im.vector.matrix.android.internal.network.interceptors.CurlLoggingInterceptor
@ -178,6 +188,13 @@ internal abstract class SessionModule {
fun providesEventBus(): EventBus {
return EventBus.builder().build()
}
@JvmStatic
@Provides
@SessionScope
fun providesMxCryptoConfig(matrixConfiguration: MatrixConfiguration): MXCryptoConfig {
return matrixConfiguration.cryptoConfig
}
}
@Binds

View file

@ -21,6 +21,7 @@ import androidx.lifecycle.Transformations
import com.zhuinden.monarchy.Monarchy
import im.vector.matrix.android.api.MatrixCallback
import im.vector.matrix.android.api.session.crypto.CryptoService
import im.vector.matrix.android.api.session.events.model.EventType
import im.vector.matrix.android.api.session.room.Room
import im.vector.matrix.android.api.session.room.members.MembershipService
import im.vector.matrix.android.api.session.room.model.RoomSummary
@ -35,10 +36,15 @@ import im.vector.matrix.android.api.session.room.timeline.TimelineService
import im.vector.matrix.android.api.session.room.typing.TypingService
import im.vector.matrix.android.api.util.Optional
import im.vector.matrix.android.api.util.toOptional
import im.vector.matrix.android.internal.crypto.MXCRYPTO_ALGORITHM_MEGOLM
import im.vector.matrix.android.internal.database.mapper.RoomSummaryMapper
import im.vector.matrix.android.internal.database.model.RoomSummaryEntity
import im.vector.matrix.android.internal.database.model.RoomSummaryEntityFields
import im.vector.matrix.android.internal.database.query.where
import im.vector.matrix.android.internal.session.room.state.SendStateTask
import im.vector.matrix.android.internal.task.TaskExecutor
import im.vector.matrix.android.internal.task.configureWith
import java.security.InvalidParameterException
import javax.inject.Inject
internal class DefaultRoom @Inject constructor(override val roomId: String,
@ -54,7 +60,9 @@ internal class DefaultRoom @Inject constructor(override val roomId: String,
private val cryptoService: CryptoService,
private val relationService: RelationService,
private val roomMembersService: MembershipService,
private val roomPushRuleService: RoomPushRuleService) :
private val roomPushRuleService: RoomPushRuleService,
private val taskExecutor: TaskExecutor,
private val sendStateTask: SendStateTask) :
Room,
TimelineService by timelineService,
SendService by sendService,
@ -96,11 +104,27 @@ internal class DefaultRoom @Inject constructor(override val roomId: String,
return cryptoService.shouldEncryptForInvitedMembers(roomId)
}
override fun enableEncryptionWithAlgorithm(algorithm: String, callback: MatrixCallback<Unit>) {
if (isEncrypted()) {
callback.onFailure(IllegalStateException("Encryption is already enabled for this room"))
} else {
stateService.enableEncryption(algorithm, callback)
override fun enableEncryption(algorithm: String, callback: MatrixCallback<Unit>) {
when {
isEncrypted() -> {
callback.onFailure(IllegalStateException("Encryption is already enabled for this room"))
}
algorithm != MXCRYPTO_ALGORITHM_MEGOLM -> {
callback.onFailure(InvalidParameterException("Only MXCRYPTO_ALGORITHM_MEGOLM algorithm is supported"))
}
else -> {
val params = SendStateTask.Params(roomId,
EventType.STATE_ROOM_ENCRYPTION,
mapOf(
"algorithm" to algorithm
))
sendStateTask
.configureWith(params) {
this.callback = callback
}
.executeBy(taskExecutor)
}
}
}
}

View file

@ -29,8 +29,10 @@ import im.vector.matrix.android.internal.session.room.relation.DefaultRelationSe
import im.vector.matrix.android.internal.session.room.reporting.DefaultReportingService
import im.vector.matrix.android.internal.session.room.send.DefaultSendService
import im.vector.matrix.android.internal.session.room.state.DefaultStateService
import im.vector.matrix.android.internal.session.room.state.SendStateTask
import im.vector.matrix.android.internal.session.room.timeline.DefaultTimelineService
import im.vector.matrix.android.internal.session.room.typing.DefaultTypingService
import im.vector.matrix.android.internal.task.TaskExecutor
import javax.inject.Inject
internal interface RoomFactory {
@ -50,7 +52,9 @@ internal class DefaultRoomFactory @Inject constructor(private val monarchy: Mona
private val typingServiceFactory: DefaultTypingService.Factory,
private val relationServiceFactory: DefaultRelationService.Factory,
private val membershipServiceFactory: DefaultMembershipService.Factory,
private val roomPushRuleServiceFactory: DefaultRoomPushRuleService.Factory) :
private val roomPushRuleServiceFactory: DefaultRoomPushRuleService.Factory,
private val taskExecutor: TaskExecutor,
private val sendStateTask: SendStateTask) :
RoomFactory {
override fun create(roomId: String): Room {
@ -68,7 +72,9 @@ internal class DefaultRoomFactory @Inject constructor(private val monarchy: Mona
cryptoService,
relationServiceFactory.create(roomId),
membershipServiceFactory.create(roomId),
roomPushRuleServiceFactory.create(roomId)
roomPushRuleServiceFactory.create(roomId),
taskExecutor,
sendStateTask
)
}
}

View file

@ -27,7 +27,6 @@ import im.vector.matrix.android.api.session.events.model.EventType
import im.vector.matrix.android.api.session.room.state.StateService
import im.vector.matrix.android.api.util.Optional
import im.vector.matrix.android.api.util.toOptional
import im.vector.matrix.android.internal.crypto.MXCRYPTO_ALGORITHM_MEGOLM
import im.vector.matrix.android.internal.database.mapper.asDomain
import im.vector.matrix.android.internal.database.model.EventEntity
import im.vector.matrix.android.internal.database.query.descending
@ -36,7 +35,6 @@ import im.vector.matrix.android.internal.database.query.where
import im.vector.matrix.android.internal.task.TaskExecutor
import im.vector.matrix.android.internal.task.configureWith
import io.realm.Realm
import java.security.InvalidParameterException
internal class DefaultStateService @AssistedInject constructor(@Assisted private val roomId: String,
private val monarchy: Monarchy,
@ -57,7 +55,7 @@ internal class DefaultStateService @AssistedInject constructor(@Assisted private
override fun getStateEventLive(eventType: String): LiveData<Optional<Event>> {
val liveData = monarchy.findAllMappedWithChanges(
{ realm -> EventEntity.where(realm, roomId, eventType).descending() },
{ realm -> EventEntity.where(realm, roomId, eventType).descending() },
{ it.asDomain() }
)
return Transformations.map(liveData) { results ->
@ -78,22 +76,4 @@ internal class DefaultStateService @AssistedInject constructor(@Assisted private
}
.executeBy(taskExecutor)
}
override fun enableEncryption(algorithm: String, callback: MatrixCallback<Unit>) {
if (algorithm != MXCRYPTO_ALGORITHM_MEGOLM) {
callback.onFailure(InvalidParameterException("Only MXCRYPTO_ALGORITHM_MEGOLM algorithm is supported"))
} else {
val params = SendStateTask.Params(roomId,
EventType.STATE_ROOM_ENCRYPTION,
mapOf(
"algorithm" to algorithm
))
sendStateTask
.configureWith(params) {
this.callback = callback
}
.executeBy(taskExecutor)
}
}
}

View file

@ -23,7 +23,6 @@ import com.squareup.inject.assisted.Assisted
import com.squareup.inject.assisted.AssistedInject
import im.vector.matrix.android.api.MatrixCallback
import im.vector.matrix.android.api.session.Session
import im.vector.matrix.android.internal.crypto.MXCRYPTO_ALGORITHM_MEGOLM
import im.vector.matrix.rx.rx
import im.vector.matrix.rx.unwrap
import im.vector.riotx.core.platform.VectorViewModel
@ -71,7 +70,7 @@ class RoomSettingsViewModel @AssistedInject constructor(@Assisted initialState:
copy(isLoading = true)
}
room.enableEncryption(MXCRYPTO_ALGORITHM_MEGOLM, object : MatrixCallback<Unit> {
room.enableEncryption(callback = object : MatrixCallback<Unit> {
override fun onFailure(failure: Throwable) {
setState {
copy(isLoading = false)