From 4985c69d81147cf337730a4078f8dc4f77a973bb Mon Sep 17 00:00:00 2001 From: MohamadJaara Date: Wed, 15 Nov 2023 16:41:07 +0100 Subject: [PATCH 1/4] fix: SessionManager missing session error --- .../wire/kalium/logic/feature/auth/LogoutUseCase.kt | 5 ++++- .../wire/kalium/logic/network/SessionManagerImpl.kt | 12 ++++-------- .../AuthenticatedNetworkContainer.kt | 6 +++--- .../com/wire/kalium/network/utils/CustomErrors.kt | 4 ++-- 4 files changed, 13 insertions(+), 14 deletions(-) diff --git a/logic/src/commonMain/kotlin/com/wire/kalium/logic/feature/auth/LogoutUseCase.kt b/logic/src/commonMain/kotlin/com/wire/kalium/logic/feature/auth/LogoutUseCase.kt index 608d74e2763..138881a36fb 100644 --- a/logic/src/commonMain/kotlin/com/wire/kalium/logic/feature/auth/LogoutUseCase.kt +++ b/logic/src/commonMain/kotlin/com/wire/kalium/logic/feature/auth/LogoutUseCase.kt @@ -77,7 +77,10 @@ internal class LogoutUseCaseImpl @Suppress("LongParameterList") constructor( endCallUseCase(it.conversationId) } - logoutRepository.logout() + if(reason != LogoutReason.SESSION_EXPIRED) { + logoutRepository.logout() + } + sessionRepository.logout(userId = userId, reason) logoutRepository.onLogout(reason) userSessionWorkScheduler.cancelScheduledSendingOfPendingMessages() diff --git a/logic/src/commonMain/kotlin/com/wire/kalium/logic/network/SessionManagerImpl.kt b/logic/src/commonMain/kotlin/com/wire/kalium/logic/network/SessionManagerImpl.kt index 89c6f01ccc4..d2ef0df39b3 100644 --- a/logic/src/commonMain/kotlin/com/wire/kalium/logic/network/SessionManagerImpl.kt +++ b/logic/src/commonMain/kotlin/com/wire/kalium/logic/network/SessionManagerImpl.kt @@ -66,18 +66,14 @@ class SessionManagerImpl internal constructor( private var serverConfig: ServerConfigDTO? = null - override suspend fun session(): SessionDTO = withContext(coroutineContext) { + override suspend fun session(): SessionDTO? = withContext(coroutineContext) { wrapStorageRequest { tokenStorage.getToken(userId.toDao()) } .map { sessionMapper.fromEntityToSessionDTO(it) } - .fold( + .nullableFold( { - error( - """SESSION MANAGER: - |"error": "missing user session", - |"cause": "$it" """.trimMargin() - ) + logout(LogoutReason.SESSION_EXPIRED) + null }, { session -> - kaliumLogger.i("_TOKEN_ FOUND SESSION = $session") session } ) diff --git a/network/src/commonMain/kotlin/com/wire/kalium/network/networkContainer/AuthenticatedNetworkContainer.kt b/network/src/commonMain/kotlin/com/wire/kalium/network/networkContainer/AuthenticatedNetworkContainer.kt index bca0beb3bc2..81aea5e7345 100644 --- a/network/src/commonMain/kotlin/com/wire/kalium/network/networkContainer/AuthenticatedNetworkContainer.kt +++ b/network/src/commonMain/kotlin/com/wire/kalium/network/networkContainer/AuthenticatedNetworkContainer.kt @@ -48,7 +48,6 @@ import com.wire.kalium.network.api.v2.authenticated.networkContainer.Authenticat import com.wire.kalium.network.api.v3.authenticated.networkContainer.AuthenticatedNetworkContainerV3 import com.wire.kalium.network.api.v4.authenticated.networkContainer.AuthenticatedNetworkContainerV4 import com.wire.kalium.network.api.v5.authenticated.networkContainer.AuthenticatedNetworkContainerV5 -import com.wire.kalium.network.kaliumLogger import com.wire.kalium.network.session.CertificatePinning import com.wire.kalium.network.session.SessionManager import com.wire.kalium.network.tools.ServerConfigDTO @@ -202,8 +201,9 @@ internal class AuthenticatedHttpClientProviderImpl( } private val loadToken: suspend () -> BearerTokens? = { - val session = sessionManager.session() ?: error("missing user session") - BearerTokens(accessToken = session.accessToken, refreshToken = session.refreshToken) + sessionManager.session()?.let {session -> + BearerTokens(accessToken = session.accessToken, refreshToken = session.refreshToken) + } } private val refreshToken: suspend RefreshTokensParams.() -> BearerTokens = { diff --git a/network/src/commonMain/kotlin/com/wire/kalium/network/utils/CustomErrors.kt b/network/src/commonMain/kotlin/com/wire/kalium/network/utils/CustomErrors.kt index 70e9b0882a8..e02d45e7282 100644 --- a/network/src/commonMain/kotlin/com/wire/kalium/network/utils/CustomErrors.kt +++ b/network/src/commonMain/kotlin/com/wire/kalium/network/utils/CustomErrors.kt @@ -27,7 +27,7 @@ object CustomErrors { NetworkResponse.Error( KaliumException.ServerError( ErrorResponse( - 500, + -1, "no cookie was found", NetworkErrorLabel.KaliumCustom.MISSING_REFRESH_TOKEN ) @@ -38,7 +38,7 @@ object CustomErrors { NetworkResponse.Error( KaliumException.ServerError( ErrorResponse( - 500, + -2, "no nonce found", NetworkErrorLabel.KaliumCustom.MISSING_NONCE ) From a5affd9906ebd1d6056cc8bfcae149b6df7111ec Mon Sep 17 00:00:00 2001 From: MohamadJaara Date: Wed, 15 Nov 2023 17:00:31 +0100 Subject: [PATCH 2/4] detekt --- .../kotlin/com/wire/kalium/network/utils/CustomErrors.kt | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/network/src/commonMain/kotlin/com/wire/kalium/network/utils/CustomErrors.kt b/network/src/commonMain/kotlin/com/wire/kalium/network/utils/CustomErrors.kt index e02d45e7282..be0961bac06 100644 --- a/network/src/commonMain/kotlin/com/wire/kalium/network/utils/CustomErrors.kt +++ b/network/src/commonMain/kotlin/com/wire/kalium/network/utils/CustomErrors.kt @@ -23,11 +23,15 @@ import com.wire.kalium.network.exceptions.KaliumException import com.wire.kalium.network.exceptions.NetworkErrorLabel object CustomErrors { + + private const val MISSING_REFRESH_TOKEN_CODE = -1 + private const val MISSING_NONCE_CODE = -2 + val MISSING_REFRESH_TOKEN = NetworkResponse.Error( KaliumException.ServerError( ErrorResponse( - -1, + MISSING_REFRESH_TOKEN_CODE, "no cookie was found", NetworkErrorLabel.KaliumCustom.MISSING_REFRESH_TOKEN ) @@ -38,10 +42,11 @@ object CustomErrors { NetworkResponse.Error( KaliumException.ServerError( ErrorResponse( - -2, + MISSING_NONCE_CODE, "no nonce found", NetworkErrorLabel.KaliumCustom.MISSING_NONCE ) ) ) + } From b4bd41a1ab6f5a624b2b96ccd1fb2d735f7e5198 Mon Sep 17 00:00:00 2001 From: MohamadJaara Date: Wed, 15 Nov 2023 17:34:01 +0100 Subject: [PATCH 3/4] add test --- .../logic/feature/auth/LogoutUseCaseTest.kt | 21 ++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) diff --git a/logic/src/commonTest/kotlin/com/wire/kalium/logic/feature/auth/LogoutUseCaseTest.kt b/logic/src/commonTest/kotlin/com/wire/kalium/logic/feature/auth/LogoutUseCaseTest.kt index 28ed81d2b83..65e6de3a923 100644 --- a/logic/src/commonTest/kotlin/com/wire/kalium/logic/feature/auth/LogoutUseCaseTest.kt +++ b/logic/src/commonTest/kotlin/com/wire/kalium/logic/feature/auth/LogoutUseCaseTest.kt @@ -82,9 +82,7 @@ class LogoutUseCaseTest { verify(arrangement.deregisterTokenUseCase) .suspendFunction(arrangement.deregisterTokenUseCase::invoke) .wasInvoked(exactly = once) - verify(arrangement.logoutRepository) - .suspendFunction(arrangement.logoutRepository::logout) - .wasInvoked(exactly = once) + verify(arrangement.sessionRepository) .suspendFunction(arrangement.sessionRepository::logout) .with(any(), eq(reason)) @@ -211,6 +209,10 @@ class LogoutUseCaseTest { logoutUseCase.invoke(reason) arrangement.globalTestScope.advanceUntilIdle() + verify(arrangement.logoutRepository) + .suspendFunction(arrangement.logoutRepository::logout) + .wasNotInvoked() + verify(arrangement.clearClientDataUseCase) .suspendFunction(arrangement.clearClientDataUseCase::invoke) .wasInvoked(exactly = once) @@ -238,9 +240,14 @@ class LogoutUseCaseTest { logoutUseCase.invoke(reason) arrangement.globalTestScope.advanceUntilIdle() + verify(arrangement.logoutRepository) + .suspendFunction(arrangement.logoutRepository::logout) + .wasInvoked(exactly = once) + verify(arrangement.clearClientDataUseCase) .suspendFunction(arrangement.clearClientDataUseCase::invoke) .wasInvoked(exactly = once) + verify(arrangement.clearUserDataUseCase) .suspendFunction(arrangement.clearUserDataUseCase::invoke) .wasNotInvoked() @@ -271,6 +278,10 @@ class LogoutUseCaseTest { logoutUseCase.invoke(reason) arrangement.globalTestScope.advanceUntilIdle() + verify(arrangement.logoutRepository) + .suspendFunction(arrangement.logoutRepository::logout) + .wasInvoked(exactly = once) + verify(arrangement.clearClientDataUseCase) .suspendFunction(arrangement.clearClientDataUseCase::invoke) .wasNotInvoked() @@ -307,6 +318,10 @@ class LogoutUseCaseTest { logoutUseCase.invoke(reason) arrangement.globalTestScope.advanceUntilIdle() + verify(arrangement.logoutRepository) + .suspendFunction(arrangement.logoutRepository::logout) + .wasInvoked(exactly = once) + verify(arrangement.observeEstablishedCallsUseCase) .suspendFunction(arrangement.observeEstablishedCallsUseCase::invoke) .wasInvoked(exactly = once) From 5dbadcf0b35155dffaad69c48fc3c15e8fc33f94 Mon Sep 17 00:00:00 2001 From: MohamadJaara Date: Wed, 15 Nov 2023 17:35:03 +0100 Subject: [PATCH 4/4] detekt --- .../kotlin/com/wire/kalium/logic/feature/auth/LogoutUseCase.kt | 2 +- .../network/networkContainer/AuthenticatedNetworkContainer.kt | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/logic/src/commonMain/kotlin/com/wire/kalium/logic/feature/auth/LogoutUseCase.kt b/logic/src/commonMain/kotlin/com/wire/kalium/logic/feature/auth/LogoutUseCase.kt index 138881a36fb..46aac8ba3e3 100644 --- a/logic/src/commonMain/kotlin/com/wire/kalium/logic/feature/auth/LogoutUseCase.kt +++ b/logic/src/commonMain/kotlin/com/wire/kalium/logic/feature/auth/LogoutUseCase.kt @@ -77,7 +77,7 @@ internal class LogoutUseCaseImpl @Suppress("LongParameterList") constructor( endCallUseCase(it.conversationId) } - if(reason != LogoutReason.SESSION_EXPIRED) { + if (reason != LogoutReason.SESSION_EXPIRED) { logoutRepository.logout() } diff --git a/network/src/commonMain/kotlin/com/wire/kalium/network/networkContainer/AuthenticatedNetworkContainer.kt b/network/src/commonMain/kotlin/com/wire/kalium/network/networkContainer/AuthenticatedNetworkContainer.kt index 81aea5e7345..67a9ca4bcb7 100644 --- a/network/src/commonMain/kotlin/com/wire/kalium/network/networkContainer/AuthenticatedNetworkContainer.kt +++ b/network/src/commonMain/kotlin/com/wire/kalium/network/networkContainer/AuthenticatedNetworkContainer.kt @@ -201,7 +201,7 @@ internal class AuthenticatedHttpClientProviderImpl( } private val loadToken: suspend () -> BearerTokens? = { - sessionManager.session()?.let {session -> + sessionManager.session()?.let { session -> BearerTokens(accessToken = session.accessToken, refreshToken = session.refreshToken) } }