Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Stop throwing RefreshTokenException when the user has an infinite token #235

Merged
merged 4 commits into from
Nov 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/main/java/com/infomaniak/lib/core/api/ApiController.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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,
Expand Down
24 changes: 24 additions & 0 deletions src/main/java/com/infomaniak/lib/core/utils/ApiTokenExt.kt
Original file line number Diff line number Diff line change
@@ -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 <http://www.gnu.org/licenses/>.
*/
package com.infomaniak.lib.core.utils

import com.infomaniak.lib.login.ApiToken

object ApiTokenExt {
val ApiToken.isInfinite get() = refreshToken == null
}
Loading