diff --git a/src/main/java/com/infomaniak/lib/core/api/ApiController.kt b/src/main/java/com/infomaniak/lib/core/api/ApiController.kt index 194ce7b0..3542d9c8 100644 --- a/src/main/java/com/infomaniak/lib/core/api/ApiController.kt +++ b/src/main/java/com/infomaniak/lib/core/api/ApiController.kt @@ -93,9 +93,9 @@ object ApiController { return toJson.toRequestBody(jsonMediaType) } - suspend fun refreshToken(refreshToken: String?, tokenInterceptorListener: TokenInterceptorListener): ApiToken { + suspend fun refreshToken(refreshToken: String, tokenInterceptorListener: TokenInterceptorListener): ApiToken { - if (refreshToken.isNullOrBlank()) { + if (refreshToken.isBlank()) { tokenInterceptorListener.onRefreshTokenError() throw RefreshTokenException() } diff --git a/src/main/java/com/infomaniak/lib/core/auth/TokenAuthenticator.kt b/src/main/java/com/infomaniak/lib/core/auth/TokenAuthenticator.kt index a7316b2e..0cad16b6 100644 --- a/src/main/java/com/infomaniak/lib/core/auth/TokenAuthenticator.kt +++ b/src/main/java/com/infomaniak/lib/core/auth/TokenAuthenticator.kt @@ -18,6 +18,7 @@ package com.infomaniak.lib.core.auth import com.infomaniak.lib.core.api.ApiController +import com.infomaniak.lib.core.utils.ApiTokenExt.isInfinite import com.infomaniak.lib.login.ApiToken import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.runBlocking @@ -44,10 +45,10 @@ class TokenAuthenticator( val hasUserChanged = userId != tokenInterceptorListener.getCurrentUserId() return@runBlocking when { - hasUserChanged -> null + hasUserChanged || apiToken.isInfinite -> null isAlreadyRefreshed -> changeAccessToken(request, apiToken) else -> { - val newToken = ApiController.refreshToken(apiToken.refreshToken, tokenInterceptorListener) + val newToken = ApiController.refreshToken(apiToken.refreshToken!!, tokenInterceptorListener) changeAccessToken(request, newToken) } } diff --git a/src/main/java/com/infomaniak/lib/core/networking/AccessTokenUsageInterceptor.kt b/src/main/java/com/infomaniak/lib/core/networking/AccessTokenUsageInterceptor.kt index d0291a92..07e1b190 100644 --- a/src/main/java/com/infomaniak/lib/core/networking/AccessTokenUsageInterceptor.kt +++ b/src/main/java/com/infomaniak/lib/core/networking/AccessTokenUsageInterceptor.kt @@ -18,6 +18,7 @@ package com.infomaniak.lib.core.networking import com.infomaniak.lib.core.auth.TokenInterceptorListener +import com.infomaniak.lib.core.utils.ApiTokenExt.isInfinite import io.sentry.Sentry import io.sentry.SentryLevel import kotlinx.coroutines.Dispatchers @@ -42,7 +43,7 @@ class AccessTokenUsageInterceptor( val apiToken = tokenInterceptorListener.getApiToken() ?: return@runBlocking // Only log api calls if we're not using refresh tokens - if (apiToken.refreshToken != null) return@runBlocking + if (!apiToken.isInfinite) return@runBlocking val currentApiCall = ApiCallRecord( accessToken = request.header("Authorization")?.replaceFirst("Bearer ", "") ?: return@runBlocking, diff --git a/src/main/java/com/infomaniak/lib/core/utils/ApiTokenExt.kt b/src/main/java/com/infomaniak/lib/core/utils/ApiTokenExt.kt new file mode 100644 index 00000000..2f21052f --- /dev/null +++ b/src/main/java/com/infomaniak/lib/core/utils/ApiTokenExt.kt @@ -0,0 +1,24 @@ +/* + * Infomaniak Core - Android + * Copyright (C) 2024 Infomaniak Network SA + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +package com.infomaniak.lib.core.utils + +import com.infomaniak.lib.login.ApiToken + +object ApiTokenExt { + val ApiToken.isInfinite get() = refreshToken == null +}