Adding new "delete_devices" request API

This commit is contained in:
Maxime NATUREL 2022-10-24 16:12:32 +02:00
parent 7e836c0e97
commit bb262f0c41
7 changed files with 83 additions and 4 deletions

View File

@ -17,6 +17,7 @@
package org.matrix.android.sdk.api.session.crypto
import android.content.Context
import androidx.annotation.Size
import androidx.lifecycle.LiveData
import androidx.paging.PagedList
import org.matrix.android.sdk.api.MatrixCallback
@ -55,6 +56,8 @@ interface CryptoService {
fun deleteDevice(deviceId: String, userInteractiveAuthInterceptor: UserInteractiveAuthInterceptor, callback: MatrixCallback<Unit>)
fun deleteDevices(@Size(min = 1) deviceIds: List<String>, userInteractiveAuthInterceptor: UserInteractiveAuthInterceptor, callback: MatrixCallback<Unit>)
fun getCryptoVersion(context: Context, longFormat: Boolean): String
fun isCryptoEnabled(): Boolean

View File

@ -241,9 +241,15 @@ internal class DefaultCryptoService @Inject constructor(
.executeBy(taskExecutor)
}
// TODO add unit test
override fun deleteDevice(deviceId: String, userInteractiveAuthInterceptor: UserInteractiveAuthInterceptor, callback: MatrixCallback<Unit>) {
deleteDevices(listOf(deviceId), userInteractiveAuthInterceptor, callback)
}
// TODO add unit test
override fun deleteDevices(deviceIds: List<String>, userInteractiveAuthInterceptor: UserInteractiveAuthInterceptor, callback: MatrixCallback<Unit>) {
deleteDeviceTask
.configureWith(DeleteDeviceTask.Params(deviceId, userInteractiveAuthInterceptor, null)) {
.configureWith(DeleteDeviceTask.Params(deviceIds, userInteractiveAuthInterceptor, null)) {
this.executionThread = TaskThread.CRYPTO
this.callback = callback
}

View File

@ -18,6 +18,7 @@ package org.matrix.android.sdk.internal.crypto.api
import org.matrix.android.sdk.api.session.crypto.model.DeviceInfo
import org.matrix.android.sdk.api.session.crypto.model.DevicesListResponse
import org.matrix.android.sdk.internal.crypto.model.rest.DeleteDeviceParams
import org.matrix.android.sdk.internal.crypto.model.rest.DeleteDevicesParams
import org.matrix.android.sdk.internal.crypto.model.rest.KeyChangesResponse
import org.matrix.android.sdk.internal.crypto.model.rest.KeysClaimBody
import org.matrix.android.sdk.internal.crypto.model.rest.KeysClaimResponse
@ -136,6 +137,17 @@ internal interface CryptoApi {
@Body params: DeleteDeviceParams
)
/**
* Deletes the given devices, and invalidates any access token associated with them.
* Doc: https://spec.matrix.org/v1.4/client-server-api/#post_matrixclientv3delete_devices
*
* @param params the deletion parameters
*/
@POST(NetworkConstants.URI_API_PREFIX_PATH_V3 + "delete_devices")
suspend fun deleteDevices(
@Body params: DeleteDevicesParams
)
/**
* Update the device information.
* Doc: https://matrix.org/docs/spec/client_server/r0.4.0.html#put-matrix-client-r0-devices-deviceid

View File

@ -23,6 +23,9 @@ import com.squareup.moshi.JsonClass
*/
@JsonClass(generateAdapter = true)
internal data class DeleteDeviceParams(
/**
* Additional authentication information for the user-interactive authentication API.
*/
@Json(name = "auth")
val auth: Map<String, *>? = null
val auth: Map<String, *>? = null,
)

View File

@ -0,0 +1,37 @@
/*
* Copyright 2022 The Matrix.org Foundation C.I.C.
*
* 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 org.matrix.android.sdk.internal.crypto.model.rest
import com.squareup.moshi.Json
import com.squareup.moshi.JsonClass
/**
* This class provides the parameter to delete several devices.
*/
@JsonClass(generateAdapter = true)
internal data class DeleteDevicesParams(
/**
* Additional authentication information for the user-interactive authentication API.
*/
@Json(name = "auth")
val auth: Map<String, *>? = null,
/**
* Required: The list of device IDs to delete.
*/
@Json(name = "devices")
val deviceIds: List<String>,
)

View File

@ -22,6 +22,7 @@ import org.matrix.android.sdk.api.session.uia.UiaResult
import org.matrix.android.sdk.internal.auth.registration.handleUIA
import org.matrix.android.sdk.internal.crypto.api.CryptoApi
import org.matrix.android.sdk.internal.crypto.model.rest.DeleteDeviceParams
import org.matrix.android.sdk.internal.crypto.model.rest.DeleteDevicesParams
import org.matrix.android.sdk.internal.network.GlobalErrorReceiver
import org.matrix.android.sdk.internal.network.executeRequest
import org.matrix.android.sdk.internal.task.Task
@ -30,21 +31,37 @@ import javax.inject.Inject
internal interface DeleteDeviceTask : Task<DeleteDeviceTask.Params, Unit> {
data class Params(
val deviceId: String,
val deviceIds: List<String>,
val userInteractiveAuthInterceptor: UserInteractiveAuthInterceptor?,
val userAuthParam: UIABaseAuth?
)
}
// TODO add unit tests
internal class DefaultDeleteDeviceTask @Inject constructor(
private val cryptoApi: CryptoApi,
private val globalErrorReceiver: GlobalErrorReceiver
) : DeleteDeviceTask {
override suspend fun execute(params: DeleteDeviceTask.Params) {
require(params.deviceIds.isNotEmpty())
try {
executeRequest(globalErrorReceiver) {
cryptoApi.deleteDevice(params.deviceId, DeleteDeviceParams(params.userAuthParam?.asMap()))
val userAuthParam = params.userAuthParam?.asMap()
if (params.deviceIds.size == 1) {
cryptoApi.deleteDevice(
deviceId = params.deviceIds.first(),
DeleteDeviceParams(auth = userAuthParam)
)
} else {
cryptoApi.deleteDevices(
DeleteDevicesParams(
auth = userAuthParam,
deviceIds = params.deviceIds
)
)
}
}
} catch (throwable: Throwable) {
if (params.userInteractiveAuthInterceptor == null ||

View File

@ -22,6 +22,7 @@ internal object NetworkConstants {
const val URI_API_PREFIX_PATH_ = "$URI_API_PREFIX_PATH/"
const val URI_API_PREFIX_PATH_R0 = "$URI_API_PREFIX_PATH/r0/"
const val URI_API_PREFIX_PATH_V1 = "$URI_API_PREFIX_PATH/v1/"
const val URI_API_PREFIX_PATH_V3 = "$URI_API_PREFIX_PATH/v3/"
const val URI_API_PREFIX_PATH_UNSTABLE = "$URI_API_PREFIX_PATH/unstable/"
// Media