Link new device using QrCode.
This commit is contained in:
parent
5ebb615751
commit
a073117d62
94 changed files with 4431 additions and 36 deletions
|
|
@ -25,6 +25,8 @@ import io.element.android.libraries.matrix.api.core.RoomIdOrAlias
|
|||
import io.element.android.libraries.matrix.api.core.UserId
|
||||
import io.element.android.libraries.matrix.api.createroom.CreateRoomParameters
|
||||
import io.element.android.libraries.matrix.api.createroom.RoomPreset
|
||||
import io.element.android.libraries.matrix.api.linknewdevice.LinkDesktopHandler
|
||||
import io.element.android.libraries.matrix.api.linknewdevice.LinkMobileHandler
|
||||
import io.element.android.libraries.matrix.api.media.MatrixMediaLoader
|
||||
import io.element.android.libraries.matrix.api.oidc.AccountManagementAction
|
||||
import io.element.android.libraries.matrix.api.room.BaseRoom
|
||||
|
|
@ -45,6 +47,8 @@ import io.element.android.libraries.matrix.api.user.MatrixSearchUserResults
|
|||
import io.element.android.libraries.matrix.api.user.MatrixUser
|
||||
import io.element.android.libraries.matrix.impl.encryption.RustEncryptionService
|
||||
import io.element.android.libraries.matrix.impl.exception.mapClientException
|
||||
import io.element.android.libraries.matrix.impl.linknewdevice.RustLinkDesktopHandler
|
||||
import io.element.android.libraries.matrix.impl.linknewdevice.RustLinkMobileHandler
|
||||
import io.element.android.libraries.matrix.impl.mapper.map
|
||||
import io.element.android.libraries.matrix.impl.media.RustMediaLoader
|
||||
import io.element.android.libraries.matrix.impl.media.RustMediaPreviewService
|
||||
|
|
@ -726,6 +730,34 @@ class RustMatrixClient(
|
|||
}
|
||||
}
|
||||
|
||||
override suspend fun canLinkNewDevice(): Result<Boolean> = withContext(sessionDispatcher) {
|
||||
runCatchingExceptions {
|
||||
innerClient.isLoginWithQrCodeSupported()
|
||||
}
|
||||
}
|
||||
|
||||
override fun createLinkMobileHandler(): Result<LinkMobileHandler> {
|
||||
return runCatchingExceptions {
|
||||
val handler = innerClient.newGrantLoginWithQrCodeHandler()
|
||||
RustLinkMobileHandler(
|
||||
inner = handler,
|
||||
sessionCoroutineScope = sessionCoroutineScope,
|
||||
sessionDispatcher = sessionDispatcher,
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
override fun createLinkDesktopHandler(): Result<LinkDesktopHandler> {
|
||||
return runCatchingExceptions {
|
||||
val handler = innerClient.newGrantLoginWithQrCodeHandler()
|
||||
RustLinkDesktopHandler(
|
||||
inner = handler,
|
||||
sessionCoroutineScope = sessionCoroutineScope,
|
||||
sessionDispatcher = sessionDispatcher,
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
override suspend fun markRoomAsFullyRead(roomId: RoomId, eventId: EventId): Result<Unit> = withContext(sessionDispatcher) {
|
||||
runCatchingExceptions {
|
||||
val room = innerClient.getRoom(roomId.value) ?: error("Could not fetch associated room")
|
||||
|
|
|
|||
|
|
@ -0,0 +1,21 @@
|
|||
/*
|
||||
* Copyright (c) 2025 Element Creations Ltd.
|
||||
*
|
||||
* SPDX-License-Identifier: AGPL-3.0-only OR LicenseRef-Element-Commercial.
|
||||
* Please see LICENSE files in the repository root for full details.
|
||||
*/
|
||||
|
||||
package io.element.android.libraries.matrix.impl.linknewdevice
|
||||
|
||||
import io.element.android.libraries.matrix.api.linknewdevice.ErrorType
|
||||
import org.matrix.rustcomponents.sdk.HumanQrGrantLoginException
|
||||
|
||||
internal fun HumanQrGrantLoginException.map() = when (this) {
|
||||
is HumanQrGrantLoginException.DeviceIdAlreadyInUse -> ErrorType.DeviceIdAlreadyInUse(message.orEmpty())
|
||||
is HumanQrGrantLoginException.InvalidCheckCode -> ErrorType.InvalidCheckCode(message.orEmpty())
|
||||
is HumanQrGrantLoginException.MissingSecretsBackup -> ErrorType.MissingSecretsBackup(message.orEmpty())
|
||||
is HumanQrGrantLoginException.NotFound -> ErrorType.NotFound(message.orEmpty())
|
||||
is HumanQrGrantLoginException.UnableToCreateDevice -> ErrorType.UnableToCreateDevice(message.orEmpty())
|
||||
is HumanQrGrantLoginException.Unknown -> ErrorType.Unknown(message.orEmpty())
|
||||
is HumanQrGrantLoginException.UnsupportedProtocol -> ErrorType.UnsupportedProtocol(message.orEmpty())
|
||||
}
|
||||
|
|
@ -0,0 +1,33 @@
|
|||
/*
|
||||
* Copyright (c) 2025 Element Creations Ltd.
|
||||
*
|
||||
* SPDX-License-Identifier: AGPL-3.0-only OR LicenseRef-Element-Commercial.
|
||||
* Please see LICENSE files in the repository root for full details.
|
||||
*/
|
||||
|
||||
package io.element.android.libraries.matrix.impl.linknewdevice
|
||||
|
||||
import io.element.android.libraries.core.extensions.runCatchingExceptions
|
||||
import io.element.android.libraries.matrix.api.linknewdevice.CheckCodeSender
|
||||
import kotlinx.coroutines.CoroutineDispatcher
|
||||
import kotlinx.coroutines.withContext
|
||||
import org.matrix.rustcomponents.sdk.CheckCodeSender as FfiCheckCodeSender
|
||||
|
||||
class RustCheckCodeSender(
|
||||
private val inner: FfiCheckCodeSender,
|
||||
private val sessionDispatcher: CoroutineDispatcher,
|
||||
) : CheckCodeSender {
|
||||
override suspend fun validate(code: UByte): Boolean = withContext(sessionDispatcher) {
|
||||
runCatchingExceptions {
|
||||
// TODO https://github.com/matrix-org/matrix-rust-sdk/pull/5957
|
||||
// inner.validate(code)
|
||||
true
|
||||
}.getOrNull() ?: true
|
||||
}
|
||||
|
||||
override suspend fun send(code: UByte): Result<Unit> = withContext(sessionDispatcher) {
|
||||
runCatchingExceptions {
|
||||
inner.send(code)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,82 @@
|
|||
/*
|
||||
* Copyright (c) 2025 Element Creations Ltd.
|
||||
*
|
||||
* SPDX-License-Identifier: AGPL-3.0-only OR LicenseRef-Element-Commercial.
|
||||
* Please see LICENSE files in the repository root for full details.
|
||||
*/
|
||||
|
||||
package io.element.android.libraries.matrix.impl.linknewdevice
|
||||
|
||||
import io.element.android.libraries.core.log.logger.LoggerTag
|
||||
import io.element.android.libraries.matrix.api.linknewdevice.LinkDesktopHandler
|
||||
import io.element.android.libraries.matrix.api.linknewdevice.LinkDesktopStep
|
||||
import io.element.android.libraries.matrix.api.logs.LoggerTags
|
||||
import io.element.android.libraries.matrix.impl.auth.qrlogin.QrErrorMapper
|
||||
import kotlinx.coroutines.CoroutineDispatcher
|
||||
import kotlinx.coroutines.CoroutineScope
|
||||
import kotlinx.coroutines.flow.MutableStateFlow
|
||||
import kotlinx.coroutines.flow.StateFlow
|
||||
import kotlinx.coroutines.flow.asStateFlow
|
||||
import kotlinx.coroutines.launch
|
||||
import kotlinx.coroutines.withContext
|
||||
import org.matrix.rustcomponents.sdk.GrantLoginWithQrCodeHandler
|
||||
import org.matrix.rustcomponents.sdk.GrantQrLoginProgress
|
||||
import org.matrix.rustcomponents.sdk.GrantQrLoginProgressListener
|
||||
import org.matrix.rustcomponents.sdk.HumanQrGrantLoginException
|
||||
import org.matrix.rustcomponents.sdk.QrCodeData
|
||||
import org.matrix.rustcomponents.sdk.QrCodeDecodeException
|
||||
import timber.log.Timber
|
||||
|
||||
private val tag = LoggerTag("RustLinkDesktopHandler", LoggerTags.linkNewDevice)
|
||||
|
||||
class RustLinkDesktopHandler(
|
||||
private val inner: GrantLoginWithQrCodeHandler,
|
||||
private val sessionCoroutineScope: CoroutineScope,
|
||||
private val sessionDispatcher: CoroutineDispatcher,
|
||||
) : LinkDesktopHandler {
|
||||
private val _linkDesktopStep = MutableStateFlow<LinkDesktopStep>(LinkDesktopStep.Uninitialized)
|
||||
override val linkDesktopStep: StateFlow<LinkDesktopStep> = _linkDesktopStep.asStateFlow()
|
||||
|
||||
override suspend fun handleScannedQrCode(data: ByteArray) = withContext(sessionDispatcher) {
|
||||
Timber.tag(tag.value).d("Emit Uninitialized")
|
||||
_linkDesktopStep.emit(LinkDesktopStep.Uninitialized)
|
||||
try {
|
||||
val qrCodeData = QrCodeData.fromBytes(data)
|
||||
inner.scan(
|
||||
qrCodeData = qrCodeData,
|
||||
progressListener = object : GrantQrLoginProgressListener {
|
||||
override fun onUpdate(state: GrantQrLoginProgress) {
|
||||
sessionCoroutineScope.launch {
|
||||
val mappedState = state.map()
|
||||
Timber.tag(tag.value).d("Emit ${mappedState::class.java.simpleName}")
|
||||
_linkDesktopStep.emit(mappedState)
|
||||
}
|
||||
}
|
||||
}
|
||||
)
|
||||
} catch (e: QrCodeDecodeException) {
|
||||
Timber.tag(tag.value).w(e, "Invalid QR code scanned")
|
||||
_linkDesktopStep.emit(
|
||||
LinkDesktopStep.InvalidQrCode(
|
||||
error = QrErrorMapper.map(e)
|
||||
)
|
||||
)
|
||||
} catch (e: HumanQrGrantLoginException) {
|
||||
Timber.tag(tag.value).w(e, "Error during QR login grant")
|
||||
_linkDesktopStep.emit(LinkDesktopStep.Error(e.map()))
|
||||
}
|
||||
}
|
||||
|
||||
private fun GrantQrLoginProgress.map() = when (this) {
|
||||
GrantQrLoginProgress.Done -> LinkDesktopStep.Done
|
||||
GrantQrLoginProgress.Starting -> LinkDesktopStep.Starting
|
||||
GrantQrLoginProgress.SyncingSecrets -> LinkDesktopStep.SyncingSecrets
|
||||
is GrantQrLoginProgress.WaitingForAuth -> LinkDesktopStep.WaitingForAuth(
|
||||
verificationUri = verificationUri,
|
||||
)
|
||||
is GrantQrLoginProgress.EstablishingSecureChannel -> LinkDesktopStep.EstablishingSecureChannel(
|
||||
checkCode = checkCode,
|
||||
checkCodeString = checkCodeString,
|
||||
)
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,75 @@
|
|||
/*
|
||||
* Copyright (c) 2025 Element Creations Ltd.
|
||||
*
|
||||
* SPDX-License-Identifier: AGPL-3.0-only OR LicenseRef-Element-Commercial.
|
||||
* Please see LICENSE files in the repository root for full details.
|
||||
*/
|
||||
|
||||
package io.element.android.libraries.matrix.impl.linknewdevice
|
||||
|
||||
import io.element.android.libraries.core.log.logger.LoggerTag
|
||||
import io.element.android.libraries.matrix.api.linknewdevice.LinkMobileHandler
|
||||
import io.element.android.libraries.matrix.api.linknewdevice.LinkMobileStep
|
||||
import io.element.android.libraries.matrix.api.logs.LoggerTags
|
||||
import kotlinx.coroutines.CoroutineDispatcher
|
||||
import kotlinx.coroutines.CoroutineScope
|
||||
import kotlinx.coroutines.flow.Flow
|
||||
import kotlinx.coroutines.flow.MutableStateFlow
|
||||
import kotlinx.coroutines.flow.asStateFlow
|
||||
import kotlinx.coroutines.launch
|
||||
import kotlinx.coroutines.withContext
|
||||
import org.matrix.rustcomponents.sdk.GrantGeneratedQrLoginProgress
|
||||
import org.matrix.rustcomponents.sdk.GrantGeneratedQrLoginProgressListener
|
||||
import org.matrix.rustcomponents.sdk.GrantLoginWithQrCodeHandler
|
||||
import org.matrix.rustcomponents.sdk.HumanQrGrantLoginException
|
||||
import timber.log.Timber
|
||||
|
||||
private val tag = LoggerTag("RustLinkMobileHandler", LoggerTags.linkNewDevice)
|
||||
|
||||
class RustLinkMobileHandler(
|
||||
private val inner: GrantLoginWithQrCodeHandler,
|
||||
private val sessionCoroutineScope: CoroutineScope,
|
||||
private val sessionDispatcher: CoroutineDispatcher,
|
||||
) : LinkMobileHandler {
|
||||
private val _linkMobileStep = MutableStateFlow<LinkMobileStep>(LinkMobileStep.Uninitialized)
|
||||
override val linkMobileStep: Flow<LinkMobileStep> = _linkMobileStep.asStateFlow()
|
||||
|
||||
override suspend fun start() = withContext(sessionDispatcher) {
|
||||
Timber.tag(tag.value).d("Emit Uninitialized")
|
||||
_linkMobileStep.emit(LinkMobileStep.Uninitialized)
|
||||
try {
|
||||
inner.generate(
|
||||
progressListener = object : GrantGeneratedQrLoginProgressListener {
|
||||
override fun onUpdate(state: GrantGeneratedQrLoginProgress) {
|
||||
sessionCoroutineScope.launch {
|
||||
val mappedState = state.map()
|
||||
Timber.tag(tag.value).d("Emit ${mappedState::class.java.simpleName}")
|
||||
_linkMobileStep.emit(mappedState)
|
||||
}
|
||||
}
|
||||
}
|
||||
)
|
||||
} catch (e: HumanQrGrantLoginException) {
|
||||
Timber.tag(tag.value).w(e, "Error during QR login grant")
|
||||
_linkMobileStep.emit(LinkMobileStep.Error(e.map()))
|
||||
}
|
||||
}
|
||||
|
||||
private fun GrantGeneratedQrLoginProgress.map(): LinkMobileStep {
|
||||
return when (this) {
|
||||
GrantGeneratedQrLoginProgress.Done -> LinkMobileStep.Done
|
||||
is GrantGeneratedQrLoginProgress.QrReady -> {
|
||||
LinkMobileStep.QrReady(String(qrCode.toBytes(), Charsets.ISO_8859_1))
|
||||
}
|
||||
is GrantGeneratedQrLoginProgress.QrScanned -> LinkMobileStep.QrScanned(
|
||||
RustCheckCodeSender(
|
||||
inner = checkCodeSender,
|
||||
sessionDispatcher = sessionDispatcher,
|
||||
)
|
||||
)
|
||||
GrantGeneratedQrLoginProgress.Starting -> LinkMobileStep.Starting
|
||||
GrantGeneratedQrLoginProgress.SyncingSecrets -> LinkMobileStep.SyncingSecrets
|
||||
is GrantGeneratedQrLoginProgress.WaitingForAuth -> LinkMobileStep.WaitingForAuth(verificationUri)
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue