mirror of
https://github.com/bitwarden/android.git
synced 2026-06-07 23:58:03 -05:00
Add API for updating a cipher (#354)
This commit is contained in:
committed by
Álison Fernandes
parent
266db5cc04
commit
bfc0e9831c
@@ -4,6 +4,8 @@ import com.x8bit.bitwarden.data.vault.datasource.network.model.CipherJsonRequest
|
||||
import com.x8bit.bitwarden.data.vault.datasource.network.model.SyncResponseJson
|
||||
import retrofit2.http.Body
|
||||
import retrofit2.http.POST
|
||||
import retrofit2.http.PUT
|
||||
import retrofit2.http.Path
|
||||
|
||||
/**
|
||||
* Defines raw calls under the /ciphers API with authentication applied.
|
||||
@@ -15,4 +17,13 @@ interface CiphersApi {
|
||||
*/
|
||||
@POST("ciphers")
|
||||
suspend fun createCipher(@Body body: CipherJsonRequest): Result<SyncResponseJson.Cipher>
|
||||
|
||||
/**
|
||||
* Updates a cipher.
|
||||
*/
|
||||
@PUT("ciphers/{cipherId}")
|
||||
suspend fun updateCipher(
|
||||
@Path("cipherId") cipherId: String,
|
||||
@Body body: CipherJsonRequest,
|
||||
): Result<SyncResponseJson.Cipher>
|
||||
}
|
||||
|
||||
@@ -11,4 +11,12 @@ interface CiphersService {
|
||||
* Attempt to create a cipher.
|
||||
*/
|
||||
suspend fun createCipher(body: CipherJsonRequest): Result<SyncResponseJson.Cipher>
|
||||
|
||||
/**
|
||||
* Attempt to update a cipher.
|
||||
*/
|
||||
suspend fun updateCipher(
|
||||
cipherId: String,
|
||||
body: CipherJsonRequest,
|
||||
): Result<SyncResponseJson.Cipher>
|
||||
}
|
||||
|
||||
@@ -9,4 +9,13 @@ class CiphersServiceImpl constructor(
|
||||
) : CiphersService {
|
||||
override suspend fun createCipher(body: CipherJsonRequest): Result<SyncResponseJson.Cipher> =
|
||||
ciphersApi.createCipher(body = body)
|
||||
|
||||
override suspend fun updateCipher(
|
||||
cipherId: String,
|
||||
body: CipherJsonRequest,
|
||||
): Result<SyncResponseJson.Cipher> =
|
||||
ciphersApi.updateCipher(
|
||||
cipherId = cipherId,
|
||||
body = body,
|
||||
)
|
||||
}
|
||||
|
||||
@@ -6,6 +6,7 @@ import com.bitwarden.core.Kdf
|
||||
import com.x8bit.bitwarden.data.platform.repository.model.DataState
|
||||
import com.x8bit.bitwarden.data.vault.repository.model.CreateCipherResult
|
||||
import com.x8bit.bitwarden.data.vault.repository.model.SendData
|
||||
import com.x8bit.bitwarden.data.vault.repository.model.UpdateCipherResult
|
||||
import com.x8bit.bitwarden.data.vault.repository.model.VaultData
|
||||
import com.x8bit.bitwarden.data.vault.repository.model.VaultState
|
||||
import com.x8bit.bitwarden.data.vault.repository.model.VaultUnlockResult
|
||||
@@ -86,4 +87,12 @@ interface VaultRepository {
|
||||
* Attempt to create a cipher.
|
||||
*/
|
||||
suspend fun createCipher(cipherView: CipherView): CreateCipherResult
|
||||
|
||||
/**
|
||||
* Attempt to update a cipher.
|
||||
*/
|
||||
suspend fun updateCipher(
|
||||
cipherId: String,
|
||||
cipherView: CipherView,
|
||||
): UpdateCipherResult
|
||||
}
|
||||
|
||||
@@ -19,6 +19,7 @@ import com.x8bit.bitwarden.data.vault.datasource.network.service.SyncService
|
||||
import com.x8bit.bitwarden.data.vault.datasource.sdk.VaultSdkSource
|
||||
import com.x8bit.bitwarden.data.vault.repository.model.CreateCipherResult
|
||||
import com.x8bit.bitwarden.data.vault.repository.model.SendData
|
||||
import com.x8bit.bitwarden.data.vault.repository.model.UpdateCipherResult
|
||||
import com.x8bit.bitwarden.data.vault.repository.model.VaultData
|
||||
import com.x8bit.bitwarden.data.vault.repository.model.VaultState
|
||||
import com.x8bit.bitwarden.data.vault.repository.model.VaultUnlockResult
|
||||
@@ -256,6 +257,26 @@ class VaultRepositoryImpl constructor(
|
||||
},
|
||||
)
|
||||
|
||||
override suspend fun updateCipher(
|
||||
cipherId: String,
|
||||
cipherView: CipherView,
|
||||
): UpdateCipherResult =
|
||||
vaultSdkSource
|
||||
.encryptCipher(cipherView = cipherView)
|
||||
.flatMap { cipher ->
|
||||
ciphersService.updateCipher(
|
||||
cipherId = cipherId,
|
||||
body = cipher.toEncryptedNetworkCipher(),
|
||||
)
|
||||
}
|
||||
.fold(
|
||||
onFailure = { UpdateCipherResult.Error },
|
||||
onSuccess = {
|
||||
sync()
|
||||
UpdateCipherResult.Success
|
||||
},
|
||||
)
|
||||
|
||||
// TODO: This is temporary. Eventually this needs to be based on the presence of various
|
||||
// user keys but this will likely require SDK updates to support this (BIT-1190).
|
||||
private fun setVaultToUnlocked(userId: String) {
|
||||
|
||||
@@ -0,0 +1,17 @@
|
||||
package com.x8bit.bitwarden.data.vault.repository.model
|
||||
|
||||
/**
|
||||
* Models result of updating a cipher.
|
||||
*/
|
||||
sealed class UpdateCipherResult {
|
||||
|
||||
/**
|
||||
* Cipher updated successfully.
|
||||
*/
|
||||
data object Success : UpdateCipherResult()
|
||||
|
||||
/**
|
||||
* Generic error while updating cipher.
|
||||
*/
|
||||
data object Error : UpdateCipherResult()
|
||||
}
|
||||
Reference in New Issue
Block a user