Skip to content

Commit

Permalink
Merge pull request #235 from Infomaniak/infinite-token-refresh-exception
Browse files Browse the repository at this point in the history
Stop throwing RefreshTokenException when the user has an infinite token
  • Loading branch information
LunarX authored Nov 13, 2024
2 parents 6ffac22 + 568cf5e commit 23802ba
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 5 deletions.
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
}

0 comments on commit 23802ba

Please sign in to comment.