BIT-329 Implement parsing and usage of kdf params (#112)

This commit is contained in:
Andrew Haisting
2023-10-13 14:05:55 -05:00
committed by GitHub
parent cbcb354a48
commit da6b40bc48
6 changed files with 212 additions and 40 deletions

View File

@@ -15,29 +15,85 @@ class AccountsServiceTest : BaseServiceTest() {
private val service = AccountsServiceImpl(accountsApi)
@Test
fun `preLogin should call API`() = runTest {
val response = MockResponse().setBody(PRE_LOGIN_RESPONSE_JSON)
fun `preLogin with unknown kdf type be failure`() = runTest {
val json = """
{
"kdf": 2,
"kdfIterations": 1,
}
"""
val response = MockResponse().setBody(json)
server.enqueue(response)
assertEquals(Result.success(PRE_LOGIN_RESPONSE), service.preLogin(EMAIL))
assert(service.preLogin(EMAIL).isFailure)
}
@Test
fun `preLogin Argon2 without memory property should be failure`() = runTest {
val json = """
{
"kdf": 1,
"kdfIterations": 1,
"kdfParallelism": 1
}
"""
val response = MockResponse().setBody(json)
server.enqueue(response)
assert(service.preLogin(EMAIL).isFailure)
}
@Test
fun `preLogin Argon2 without parallelism property should be failure`() = runTest {
val json = """
{
"kdf": 1,
"kdfIterations": 1,
"kdfMemory": 1
}
"""
val response = MockResponse().setBody(json)
server.enqueue(response)
assert(service.preLogin(EMAIL).isFailure)
}
@Test
fun `preLogin Argon2 should be success`() = runTest {
val json = """
{
"kdf": 1,
"kdfIterations": 1,
"kdfMemory": 1,
"kdfParallelism": 1
}
"""
val expectedResponse = PreLoginResponseJson(
kdfParams = PreLoginResponseJson.KdfParams.Argon2ID(
iterations = 1u,
memory = 1u,
parallelism = 1u,
),
)
val response = MockResponse().setBody(json)
server.enqueue(response)
assertEquals(Result.success(expectedResponse), service.preLogin(EMAIL))
}
@Test
fun `preLogin Pbkdf2 should be success`() = runTest {
val json = """
{
"kdf": 0,
"kdfIterations": 1
}
"""
val expectedResponse = PreLoginResponseJson(
kdfParams = PreLoginResponseJson.KdfParams.Pbkdf2(1u),
)
val response = MockResponse().setBody(json)
server.enqueue(response)
assertEquals(Result.success(expectedResponse), service.preLogin(EMAIL))
}
companion object {
private const val EMAIL = "email"
}
}
private const val PRE_LOGIN_RESPONSE_JSON = """
{
"kdf": 1,
"kdfIterations": 1,
"kdfMemory": 1,
"kdfParallelism": 1
}
"""
private val PRE_LOGIN_RESPONSE = PreLoginResponseJson(
kdf = 1,
kdfIterations = 1u,
kdfMemory = 1,
kdfParallelism = 1,
)

View File

@@ -1,7 +1,6 @@
package com.x8bit.bitwarden.data.auth.repository
import app.cash.turbine.test
import com.bitwarden.core.Kdf
import com.bitwarden.sdk.Client
import com.x8bit.bitwarden.data.auth.datasource.disk.AuthDiskSource
import com.x8bit.bitwarden.data.auth.datasource.network.model.AuthState
@@ -11,6 +10,7 @@ import com.x8bit.bitwarden.data.auth.datasource.network.model.PreLoginResponseJs
import com.x8bit.bitwarden.data.auth.datasource.network.service.AccountsService
import com.x8bit.bitwarden.data.auth.datasource.network.service.IdentityService
import com.x8bit.bitwarden.data.auth.datasource.network.util.CaptchaCallbackTokenResult
import com.x8bit.bitwarden.data.auth.util.toSdkParams
import com.x8bit.bitwarden.data.platform.datasource.network.interceptor.AuthTokenInterceptor
import io.mockk.clearMocks
import io.mockk.coEvery
@@ -35,7 +35,7 @@ class AuthRepositoryTest {
auth().hashPassword(
email = EMAIL,
password = PASSWORD,
kdfParams = Kdf.Pbkdf2(iterations = PRE_LOGIN_SUCCESS.kdfIterations),
kdfParams = PRE_LOGIN_SUCCESS.kdfParams.toSdkParams(),
)
} returns PASSWORD_HASH
}
@@ -209,10 +209,7 @@ class AuthRepositoryTest {
private const val ACCESS_TOKEN = "accessToken"
private const val CAPTCHA_KEY = "captcha"
private val PRE_LOGIN_SUCCESS = PreLoginResponseJson(
kdf = 1,
kdfIterations = 1u,
kdfMemory = null,
kdfParallelism = null,
kdfParams = PreLoginResponseJson.KdfParams.Pbkdf2(iterations = 1u),
)
}
}