From cb20a6d690b20dad9da7490de8f02c9c6696ffc9 Mon Sep 17 00:00:00 2001 From: David Perez Date: Thu, 15 Feb 2024 10:28:34 -0600 Subject: [PATCH] Update the TwoFactorRequired response to be able to parse multiple payload types (#1018) --- .../network/model/GetTokenResponseJson.kt | 3 ++- .../util/TwoFactorRequiredExtensions.kt | 4 +++ .../network/service/IdentityServiceTest.kt | 7 ++++-- .../util/TwoFactorRequiredExtensionTest.kt | 25 +++++++++++++------ .../auth/repository/AuthRepositoryTest.kt | 9 +++++-- .../TwoFactorLoginViewModelTest.kt | 9 +++++-- 6 files changed, 42 insertions(+), 15 deletions(-) diff --git a/app/src/main/java/com/x8bit/bitwarden/data/auth/datasource/network/model/GetTokenResponseJson.kt b/app/src/main/java/com/x8bit/bitwarden/data/auth/datasource/network/model/GetTokenResponseJson.kt index dba39b083..5b612ed85 100644 --- a/app/src/main/java/com/x8bit/bitwarden/data/auth/datasource/network/model/GetTokenResponseJson.kt +++ b/app/src/main/java/com/x8bit/bitwarden/data/auth/datasource/network/model/GetTokenResponseJson.kt @@ -2,6 +2,7 @@ package com.x8bit.bitwarden.data.auth.datasource.network.model import kotlinx.serialization.SerialName import kotlinx.serialization.Serializable +import kotlinx.serialization.json.JsonObject /** * Models response bodies from the get token request. @@ -121,7 +122,7 @@ sealed class GetTokenResponseJson { @Serializable data class TwoFactorRequired( @SerialName("TwoFactorProviders2") - val authMethodsData: Map?>, + val authMethodsData: Map, @SerialName("CaptchaBypassToken") val captchaToken: String?, diff --git a/app/src/main/java/com/x8bit/bitwarden/data/auth/datasource/network/util/TwoFactorRequiredExtensions.kt b/app/src/main/java/com/x8bit/bitwarden/data/auth/datasource/network/util/TwoFactorRequiredExtensions.kt index f8bb3877a..54691a0f4 100644 --- a/app/src/main/java/com/x8bit/bitwarden/data/auth/datasource/network/util/TwoFactorRequiredExtensions.kt +++ b/app/src/main/java/com/x8bit/bitwarden/data/auth/datasource/network/util/TwoFactorRequiredExtensions.kt @@ -2,6 +2,8 @@ package com.x8bit.bitwarden.data.auth.datasource.network.util import com.x8bit.bitwarden.data.auth.datasource.network.model.GetTokenResponseJson import com.x8bit.bitwarden.data.auth.datasource.network.model.TwoFactorAuthMethod +import kotlinx.serialization.json.contentOrNull +import kotlinx.serialization.json.jsonPrimitive /** * Return the list of two-factor auth methods available to the user. @@ -34,4 +36,6 @@ val GetTokenResponseJson.TwoFactorRequired?.twoFactorDisplayEmail: String ?.authMethodsData ?.get(TwoFactorAuthMethod.EMAIL) ?.get("Email") + ?.jsonPrimitive + ?.contentOrNull ?: "" diff --git a/app/src/test/java/com/x8bit/bitwarden/data/auth/datasource/network/service/IdentityServiceTest.kt b/app/src/test/java/com/x8bit/bitwarden/data/auth/datasource/network/service/IdentityServiceTest.kt index 9f7b873c3..1ca1fd490 100644 --- a/app/src/test/java/com/x8bit/bitwarden/data/auth/datasource/network/service/IdentityServiceTest.kt +++ b/app/src/test/java/com/x8bit/bitwarden/data/auth/datasource/network/service/IdentityServiceTest.kt @@ -18,6 +18,9 @@ import io.mockk.every import io.mockk.mockk import kotlinx.coroutines.test.runTest import kotlinx.serialization.json.Json +import kotlinx.serialization.json.JsonNull +import kotlinx.serialization.json.JsonObject +import kotlinx.serialization.json.JsonPrimitive import okhttp3.mockwebserver.MockResponse import org.junit.jupiter.api.Assertions.assertEquals import org.junit.jupiter.api.Assertions.assertTrue @@ -195,8 +198,8 @@ private const val TWO_FACTOR_BODY_JSON = """ """ private val TWO_FACTOR_BODY = GetTokenResponseJson.TwoFactorRequired( authMethodsData = mapOf( - TwoFactorAuthMethod.EMAIL to mapOf("Email" to "ex***@email.com"), - TwoFactorAuthMethod.AUTHENTICATOR_APP to mapOf("Email" to null), + TwoFactorAuthMethod.EMAIL to JsonObject(mapOf("Email" to JsonPrimitive("ex***@email.com"))), + TwoFactorAuthMethod.AUTHENTICATOR_APP to JsonObject(mapOf("Email" to JsonNull)), ), ssoToken = "exampleToken", captchaToken = "BWCaptchaBypass_ABCXYZ", diff --git a/app/src/test/java/com/x8bit/bitwarden/data/auth/datasource/network/util/TwoFactorRequiredExtensionTest.kt b/app/src/test/java/com/x8bit/bitwarden/data/auth/datasource/network/util/TwoFactorRequiredExtensionTest.kt index a149addcb..12455e695 100644 --- a/app/src/test/java/com/x8bit/bitwarden/data/auth/datasource/network/util/TwoFactorRequiredExtensionTest.kt +++ b/app/src/test/java/com/x8bit/bitwarden/data/auth/datasource/network/util/TwoFactorRequiredExtensionTest.kt @@ -2,16 +2,21 @@ package com.x8bit.bitwarden.data.auth.datasource.network.util import com.x8bit.bitwarden.data.auth.datasource.network.model.GetTokenResponseJson import com.x8bit.bitwarden.data.auth.datasource.network.model.TwoFactorAuthMethod -import org.junit.jupiter.api.Test +import kotlinx.serialization.json.JsonNull +import kotlinx.serialization.json.JsonObject +import kotlinx.serialization.json.JsonPrimitive import org.junit.jupiter.api.Assertions.assertEquals +import org.junit.jupiter.api.Test class TwoFactorRequiredExtensionTest { @Test fun `availableAuthMethods returns the expected value`() { val subject = GetTokenResponseJson.TwoFactorRequired( authMethodsData = mapOf( - TwoFactorAuthMethod.EMAIL to mapOf("Email" to "ex***@email.com"), - TwoFactorAuthMethod.AUTHENTICATOR_APP to mapOf("Email" to null), + TwoFactorAuthMethod.EMAIL to JsonObject( + mapOf("Email" to JsonPrimitive("ex***@email.com")), + ), + TwoFactorAuthMethod.AUTHENTICATOR_APP to JsonObject(mapOf("Email" to JsonNull)), ), captchaToken = null, ssoToken = null, @@ -30,8 +35,10 @@ class TwoFactorRequiredExtensionTest { fun `twoFactorDisplayEmail returns the expected value`() { val subject = GetTokenResponseJson.TwoFactorRequired( authMethodsData = mapOf( - TwoFactorAuthMethod.EMAIL to mapOf("Email" to "ex***@email.com"), - TwoFactorAuthMethod.AUTHENTICATOR_APP to mapOf("Email" to null), + TwoFactorAuthMethod.EMAIL to JsonObject( + mapOf("Email" to JsonPrimitive("ex***@email.com")), + ), + TwoFactorAuthMethod.AUTHENTICATOR_APP to JsonObject(mapOf("Email" to JsonNull)), ), captchaToken = null, ssoToken = null, @@ -43,7 +50,7 @@ class TwoFactorRequiredExtensionTest { fun `twoFactorDisplayEmail returns the expected value when null`() { val subject = GetTokenResponseJson.TwoFactorRequired( authMethodsData = mapOf( - TwoFactorAuthMethod.AUTHENTICATOR_APP to mapOf("Email" to null), + TwoFactorAuthMethod.AUTHENTICATOR_APP to JsonObject(mapOf("Email" to JsonNull)), ), captchaToken = null, ssoToken = null, @@ -55,8 +62,10 @@ class TwoFactorRequiredExtensionTest { fun `preferredAuthMethod returns the expected value`() { val subject = GetTokenResponseJson.TwoFactorRequired( authMethodsData = mapOf( - TwoFactorAuthMethod.EMAIL to mapOf("Email" to "ex***@email.com"), - TwoFactorAuthMethod.AUTHENTICATOR_APP to mapOf("Email" to null), + TwoFactorAuthMethod.EMAIL to JsonObject( + mapOf("Email" to JsonPrimitive("ex***@email.com")), + ), + TwoFactorAuthMethod.AUTHENTICATOR_APP to JsonObject(mapOf("Email" to JsonNull)), ), captchaToken = null, ssoToken = null, diff --git a/app/src/test/java/com/x8bit/bitwarden/data/auth/repository/AuthRepositoryTest.kt b/app/src/test/java/com/x8bit/bitwarden/data/auth/repository/AuthRepositoryTest.kt index 621cd17cf..3d8473464 100644 --- a/app/src/test/java/com/x8bit/bitwarden/data/auth/repository/AuthRepositoryTest.kt +++ b/app/src/test/java/com/x8bit/bitwarden/data/auth/repository/AuthRepositoryTest.kt @@ -104,6 +104,9 @@ import kotlinx.coroutines.flow.MutableStateFlow import kotlinx.coroutines.test.advanceTimeBy import kotlinx.coroutines.test.runTest import kotlinx.serialization.ExperimentalSerializationApi +import kotlinx.serialization.json.JsonNull +import kotlinx.serialization.json.JsonObject +import kotlinx.serialization.json.JsonPrimitive import kotlinx.serialization.json.buildJsonObject import kotlinx.serialization.json.put import org.junit.jupiter.api.AfterEach @@ -4151,8 +4154,10 @@ class AuthRepositoryTest { private const val USER_ID_2 = "b9d32ec0-6497-4582-9798-b350f53bfa02" private val ORGANIZATIONS = listOf(createMockOrganization(number = 0)) private val TWO_FACTOR_AUTH_METHODS_DATA = mapOf( - TwoFactorAuthMethod.EMAIL to mapOf("Email" to "ex***@email.com"), - TwoFactorAuthMethod.AUTHENTICATOR_APP to mapOf("Email" to null), + TwoFactorAuthMethod.EMAIL to JsonObject( + mapOf("Email" to JsonPrimitive("ex***@email.com")), + ), + TwoFactorAuthMethod.AUTHENTICATOR_APP to JsonObject(mapOf("Email" to JsonNull)), ) private val PRE_LOGIN_SUCCESS = PreLoginResponseJson( kdfParams = PreLoginResponseJson.KdfParams.Pbkdf2(iterations = 1u), diff --git a/app/src/test/java/com/x8bit/bitwarden/ui/auth/feature/twofactorlogin/TwoFactorLoginViewModelTest.kt b/app/src/test/java/com/x8bit/bitwarden/ui/auth/feature/twofactorlogin/TwoFactorLoginViewModelTest.kt index 55498c943..6ad5c30e0 100644 --- a/app/src/test/java/com/x8bit/bitwarden/ui/auth/feature/twofactorlogin/TwoFactorLoginViewModelTest.kt +++ b/app/src/test/java/com/x8bit/bitwarden/ui/auth/feature/twofactorlogin/TwoFactorLoginViewModelTest.kt @@ -22,6 +22,9 @@ import io.mockk.mockk import io.mockk.mockkStatic import io.mockk.unmockkStatic import kotlinx.coroutines.test.runTest +import kotlinx.serialization.json.JsonNull +import kotlinx.serialization.json.JsonObject +import kotlinx.serialization.json.JsonPrimitive import org.junit.jupiter.api.AfterEach import org.junit.jupiter.api.Assertions.assertEquals import org.junit.jupiter.api.BeforeEach @@ -417,8 +420,10 @@ class TwoFactorLoginViewModelTest : BaseViewModelTest() { companion object { private val TWO_FACTOR_AUTH_METHODS_DATA = mapOf( - TwoFactorAuthMethod.EMAIL to mapOf("Email" to "ex***@email.com"), - TwoFactorAuthMethod.AUTHENTICATOR_APP to mapOf("Email" to null), + TwoFactorAuthMethod.EMAIL to JsonObject( + mapOf("Email" to JsonPrimitive("ex***@email.com")), + ), + TwoFactorAuthMethod.AUTHENTICATOR_APP to JsonObject(mapOf("Email" to JsonNull)), ) private val TWO_FACTOR_RESPONSE = GetTokenResponseJson.TwoFactorRequired(