Verification process for the current Session

This commit is contained in:
Maxime NATUREL 2022-09-14 15:50:11 +02:00
parent 5759a0f7da
commit 8cd7b0744a
4 changed files with 67 additions and 1 deletions

View File

@ -20,5 +20,6 @@ import im.vector.app.core.platform.VectorViewModelAction
import org.matrix.android.sdk.api.session.crypto.model.CryptoDeviceInfo
sealed class DevicesAction : VectorViewModelAction {
object VerifyCurrentSession : DevicesAction()
data class MarkAsManuallyVerified(val cryptoDeviceInfo: CryptoDeviceInfo) : DevicesAction()
}

View File

@ -25,6 +25,7 @@ import im.vector.app.core.di.ActiveSessionHolder
import im.vector.app.core.di.MavericksAssistedViewModelFactory
import im.vector.app.core.di.hiltMavericksViewModelFactory
import im.vector.app.features.settings.devices.v2.filter.DeviceManagerFilterType
import im.vector.app.features.settings.devices.v2.verification.CheckIfCurrentSessionCanBeVerifiedUseCase
import kotlinx.coroutines.flow.launchIn
import kotlinx.coroutines.flow.onEach
import kotlinx.coroutines.launch
@ -37,6 +38,7 @@ class DevicesViewModel @AssistedInject constructor(
private val getDeviceFullInfoListUseCase: GetDeviceFullInfoListUseCase,
private val refreshDevicesOnCryptoDevicesChangeUseCase: RefreshDevicesOnCryptoDevicesChangeUseCase,
refreshDevicesUseCase: RefreshDevicesUseCase,
private val checkIfCurrentSessionCanBeVerifiedUseCase: CheckIfCurrentSessionCanBeVerifiedUseCase,
) : VectorSessionsListViewModel<DevicesViewState, DevicesAction, DevicesViewEvent>(initialState, activeSessionHolder, refreshDevicesUseCase) {
@AssistedFactory
@ -94,10 +96,25 @@ class DevicesViewModel @AssistedInject constructor(
override fun handle(action: DevicesAction) {
when (action) {
is DevicesAction.VerifyCurrentSession -> handleVerifyCurrentSessionAction()
is DevicesAction.MarkAsManuallyVerified -> handleMarkAsManuallyVerifiedAction()
}
}
// TODO add unit tests
private fun handleVerifyCurrentSessionAction() {
viewModelScope.launch {
val currentSessionCanBeVerified = checkIfCurrentSessionCanBeVerifiedUseCase.execute()
if (currentSessionCanBeVerified) {
activeSessionHolder.getSafeActiveSession()?.let { session ->
_viewEvents.post(DevicesViewEvent.SelfVerification(session))
}
} else {
_viewEvents.post(DevicesViewEvent.PromptResetSecrets)
}
}
}
private fun handleMarkAsManuallyVerifiedAction() {
// TODO implement when needed
}

View File

@ -228,7 +228,7 @@ class VectorSettingsDevicesFragment :
currentDeviceInfo.deviceInfo.deviceId?.let { deviceId -> navigateToSessionOverview(deviceId) }
}
views.deviceListCurrentSession.viewVerifyButton.debouncedClicks {
// TODO show bottom Sheet verification process
viewModel.handle(DevicesAction.VerifyCurrentSession)
}
} ?: run {
hideCurrentSessionView()

View File

@ -0,0 +1,48 @@
/*
* Copyright (c) 2022 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.app.features.settings.devices.v2.verification
import im.vector.app.core.di.ActiveSessionHolder
import kotlinx.coroutines.flow.firstOrNull
import org.matrix.android.sdk.api.extensions.orFalse
import org.matrix.android.sdk.flow.flow
import timber.log.Timber
import javax.inject.Inject
// TODO add unit tests
class CheckIfCurrentSessionCanBeVerifiedUseCase @Inject constructor(
private val activeSessionHolder: ActiveSessionHolder,
) {
suspend fun execute(): Boolean {
val session = activeSessionHolder.getSafeActiveSession()
val cryptoSessionsCount = session?.flow()
?.liveUserCryptoDevices(session.myUserId)
?.firstOrNull()
?.size
?: 0
val hasOtherSessions = cryptoSessionsCount > 1
val isRecoverySetup = session
?.sharedSecretStorageService()
?.isRecoverySetup()
.orFalse()
Timber.d("hasOtherSessions=$hasOtherSessions (otherSessionsCount=$cryptoSessionsCount), isRecoverySetup=$isRecoverySetup")
return hasOtherSessions || isRecoverySetup
}
}