Add trusted device API (#1183)

This commit is contained in:
David Perez
2024-03-28 13:07:02 -05:00
committed by Álison Fernandes
parent 9253a7b682
commit de6f31775b
8 changed files with 144 additions and 10 deletions

View File

@@ -0,0 +1,18 @@
package com.x8bit.bitwarden.data.auth.datasource.network.api
import com.x8bit.bitwarden.data.auth.datasource.network.model.TrustedDeviceKeysRequestJson
import com.x8bit.bitwarden.data.auth.datasource.network.model.TrustedDeviceKeysResponseJson
import retrofit2.http.Body
import retrofit2.http.PUT
import retrofit2.http.Path
/**
* Defines raw calls under the /devices API that require authentication.
*/
interface AuthenticatedDevicesApi {
@PUT("/devices/{appId}/keys")
suspend fun updateTrustedDeviceKeys(
@Path(value = "appId") appId: String,
@Body request: TrustedDeviceKeysRequestJson,
): Result<TrustedDeviceKeysResponseJson>
}

View File

@@ -4,10 +4,9 @@ import retrofit2.http.GET
import retrofit2.http.Header
/**
* Defines raw calls under the /devices API.
* Defines raw calls under the /devices API that do not require authentication.
*/
interface DevicesApi {
interface UnauthenticatedDevicesApi {
@GET("/devices/knowndevice")
suspend fun getIsKnownDevice(
@Header(value = "X-Request-Email") emailAddress: String,

View File

@@ -54,7 +54,8 @@ object AuthNetworkModule {
fun providesDevicesService(
retrofits: Retrofits,
): DevicesService = DevicesServiceImpl(
devicesApi = retrofits.unauthenticatedApiRetrofit.create(),
authenticatedDevicesApi = retrofits.authenticatedApiRetrofit.create(),
unauthenticatedDevicesApi = retrofits.unauthenticatedApiRetrofit.create(),
)
@Provides

View File

@@ -0,0 +1,14 @@
package com.x8bit.bitwarden.data.auth.datasource.network.model
import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable
/**
* The request body for trusting a device.
*/
@Serializable
data class TrustedDeviceKeysRequestJson(
@SerialName("EncryptedUserKey") val encryptedUserKey: String,
@SerialName("EncryptedPublicKey") val encryptedDevicePublicKey: String,
@SerialName("EncryptedPrivateKey") val encryptedDevicePrivateKey: String,
)

View File

@@ -0,0 +1,18 @@
package com.x8bit.bitwarden.data.auth.datasource.network.model
import kotlinx.serialization.Contextual
import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable
import java.time.ZonedDateTime
/**
* The response body for trusting a device.
*/
@Serializable
data class TrustedDeviceKeysResponseJson(
@SerialName("id") val id: String,
@SerialName("name") val name: String,
@SerialName("identifier") val identifier: String,
@SerialName("type") val type: Int,
@Contextual @SerialName("creationDate") val creationDate: ZonedDateTime,
)

View File

@@ -1,5 +1,7 @@
package com.x8bit.bitwarden.data.auth.datasource.network.service
import com.x8bit.bitwarden.data.auth.datasource.network.model.TrustedDeviceKeysResponseJson
/**
* Provides an API for interacting with the /devices endpoints.
*/
@@ -11,4 +13,14 @@ interface DevicesService {
emailAddress: String,
deviceId: String,
): Result<Boolean>
/**
* Establishes trust with this device by storing the encrypted keys in the cloud.
*/
suspend fun trustDevice(
appId: String,
encryptedUserKey: String,
encryptedDevicePublicKey: String,
encryptedDevicePrivateKey: String,
): Result<TrustedDeviceKeysResponseJson>
}

View File

@@ -1,16 +1,34 @@
package com.x8bit.bitwarden.data.auth.datasource.network.service
import com.x8bit.bitwarden.data.auth.datasource.network.api.DevicesApi
import com.x8bit.bitwarden.data.auth.datasource.network.api.AuthenticatedDevicesApi
import com.x8bit.bitwarden.data.auth.datasource.network.api.UnauthenticatedDevicesApi
import com.x8bit.bitwarden.data.auth.datasource.network.model.TrustedDeviceKeysRequestJson
import com.x8bit.bitwarden.data.auth.datasource.network.model.TrustedDeviceKeysResponseJson
import com.x8bit.bitwarden.data.platform.datasource.network.util.base64UrlEncode
class DevicesServiceImpl(
private val devicesApi: DevicesApi,
private val authenticatedDevicesApi: AuthenticatedDevicesApi,
private val unauthenticatedDevicesApi: UnauthenticatedDevicesApi,
) : DevicesService {
override suspend fun getIsKnownDevice(
emailAddress: String,
deviceId: String,
): Result<Boolean> = devicesApi.getIsKnownDevice(
): Result<Boolean> = unauthenticatedDevicesApi.getIsKnownDevice(
emailAddress = emailAddress.base64UrlEncode(),
deviceId = deviceId,
)
override suspend fun trustDevice(
appId: String,
encryptedUserKey: String,
encryptedDevicePublicKey: String,
encryptedDevicePrivateKey: String,
): Result<TrustedDeviceKeysResponseJson> = authenticatedDevicesApi.updateTrustedDeviceKeys(
appId = appId,
request = TrustedDeviceKeysRequestJson(
encryptedUserKey = encryptedUserKey,
encryptedDevicePublicKey = encryptedDevicePublicKey,
encryptedDevicePrivateKey = encryptedDevicePrivateKey,
),
)
}