mirror of
https://github.com/bitwarden/android.git
synced 2025-02-16 20:09:59 +03:00
Add createAccountKeys API (#1240)
This commit is contained in:
parent
79f8703d9b
commit
8be7d0f8df
5 changed files with 53 additions and 0 deletions
|
@ -1,5 +1,6 @@
|
||||||
package com.x8bit.bitwarden.data.auth.datasource.network.api
|
package com.x8bit.bitwarden.data.auth.datasource.network.api
|
||||||
|
|
||||||
|
import com.x8bit.bitwarden.data.auth.datasource.network.model.CreateAccountKeysRequest
|
||||||
import com.x8bit.bitwarden.data.auth.datasource.network.model.DeleteAccountRequestJson
|
import com.x8bit.bitwarden.data.auth.datasource.network.model.DeleteAccountRequestJson
|
||||||
import com.x8bit.bitwarden.data.auth.datasource.network.model.ResetPasswordRequestJson
|
import com.x8bit.bitwarden.data.auth.datasource.network.model.ResetPasswordRequestJson
|
||||||
import com.x8bit.bitwarden.data.auth.datasource.network.model.SetPasswordRequestJson
|
import com.x8bit.bitwarden.data.auth.datasource.network.model.SetPasswordRequestJson
|
||||||
|
@ -11,6 +12,12 @@ import retrofit2.http.POST
|
||||||
* Defines raw calls under the /accounts API with authentication applied.
|
* Defines raw calls under the /accounts API with authentication applied.
|
||||||
*/
|
*/
|
||||||
interface AuthenticatedAccountsApi {
|
interface AuthenticatedAccountsApi {
|
||||||
|
/**
|
||||||
|
* Creates the keys for the current account.
|
||||||
|
*/
|
||||||
|
@POST("/accounts/keys")
|
||||||
|
suspend fun createAccountKeys(@Body body: CreateAccountKeysRequest): Result<Unit>
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Deletes the current account.
|
* Deletes the current account.
|
||||||
*/
|
*/
|
||||||
|
|
|
@ -0,0 +1,13 @@
|
||||||
|
package com.x8bit.bitwarden.data.auth.datasource.network.model
|
||||||
|
|
||||||
|
import kotlinx.serialization.SerialName
|
||||||
|
import kotlinx.serialization.Serializable
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Represents the request body used to create the keys for an account.
|
||||||
|
*/
|
||||||
|
@Serializable
|
||||||
|
data class CreateAccountKeysRequest(
|
||||||
|
@SerialName("PublicKey") val publicKey: String,
|
||||||
|
@SerialName("EncryptedPrivateKey") val encryptedPrivateKey: String,
|
||||||
|
)
|
|
@ -13,6 +13,11 @@ import com.x8bit.bitwarden.data.auth.datasource.network.model.SetPasswordRequest
|
||||||
*/
|
*/
|
||||||
interface AccountsService {
|
interface AccountsService {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Creates a new account's keys.
|
||||||
|
*/
|
||||||
|
suspend fun createAccountKeys(publicKey: String, encryptedPrivateKey: String): Result<Unit>
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Make delete account request.
|
* Make delete account request.
|
||||||
*/
|
*/
|
||||||
|
|
|
@ -2,6 +2,7 @@ package com.x8bit.bitwarden.data.auth.datasource.network.service
|
||||||
|
|
||||||
import com.x8bit.bitwarden.data.auth.datasource.network.api.AccountsApi
|
import com.x8bit.bitwarden.data.auth.datasource.network.api.AccountsApi
|
||||||
import com.x8bit.bitwarden.data.auth.datasource.network.api.AuthenticatedAccountsApi
|
import com.x8bit.bitwarden.data.auth.datasource.network.api.AuthenticatedAccountsApi
|
||||||
|
import com.x8bit.bitwarden.data.auth.datasource.network.model.CreateAccountKeysRequest
|
||||||
import com.x8bit.bitwarden.data.auth.datasource.network.model.DeleteAccountRequestJson
|
import com.x8bit.bitwarden.data.auth.datasource.network.model.DeleteAccountRequestJson
|
||||||
import com.x8bit.bitwarden.data.auth.datasource.network.model.PasswordHintRequestJson
|
import com.x8bit.bitwarden.data.auth.datasource.network.model.PasswordHintRequestJson
|
||||||
import com.x8bit.bitwarden.data.auth.datasource.network.model.PasswordHintResponseJson
|
import com.x8bit.bitwarden.data.auth.datasource.network.model.PasswordHintResponseJson
|
||||||
|
@ -22,6 +23,17 @@ class AccountsServiceImpl(
|
||||||
private val json: Json,
|
private val json: Json,
|
||||||
) : AccountsService {
|
) : AccountsService {
|
||||||
|
|
||||||
|
override suspend fun createAccountKeys(
|
||||||
|
publicKey: String,
|
||||||
|
encryptedPrivateKey: String,
|
||||||
|
): Result<Unit> =
|
||||||
|
authenticatedAccountsApi.createAccountKeys(
|
||||||
|
body = CreateAccountKeysRequest(
|
||||||
|
publicKey = publicKey,
|
||||||
|
encryptedPrivateKey = encryptedPrivateKey,
|
||||||
|
),
|
||||||
|
)
|
||||||
|
|
||||||
override suspend fun deleteAccount(masterPasswordHash: String): Result<Unit> =
|
override suspend fun deleteAccount(masterPasswordHash: String): Result<Unit> =
|
||||||
authenticatedAccountsApi.deleteAccount(DeleteAccountRequestJson(masterPasswordHash))
|
authenticatedAccountsApi.deleteAccount(DeleteAccountRequestJson(masterPasswordHash))
|
||||||
|
|
||||||
|
|
|
@ -32,6 +32,22 @@ class AccountsServiceTest : BaseServiceTest() {
|
||||||
},
|
},
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun `createAccountKeys with empty response is success`() = runTest {
|
||||||
|
val publicKey = "publicKey"
|
||||||
|
val encryptedPrivateKey = "encryptedPrivateKey"
|
||||||
|
val json = ""
|
||||||
|
val response = MockResponse().setBody(json)
|
||||||
|
server.enqueue(response)
|
||||||
|
|
||||||
|
val result = service.createAccountKeys(
|
||||||
|
publicKey = publicKey,
|
||||||
|
encryptedPrivateKey = encryptedPrivateKey,
|
||||||
|
)
|
||||||
|
|
||||||
|
assertTrue(result.isSuccess)
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
fun `deleteAccount with empty response is success`() = runTest {
|
fun `deleteAccount with empty response is success`() = runTest {
|
||||||
val masterPasswordHash = "37y4d8r379r4789nt387r39k3dr87nr93"
|
val masterPasswordHash = "37y4d8r379r4789nt387r39k3dr87nr93"
|
||||||
|
|
Loading…
Add table
Reference in a new issue