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

Fix: Trying to retrieve token while logged out #191

Merged
merged 1 commit into from
May 29, 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
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,8 @@ abstract class CredentialManager {
user = getUserById(userId)
return user?.apiToken
}

override fun getCurrentUserId() = null
}

HttpClientConfig.apply { cacheDir?.let { cache(Cache(it, CACHE_SIZE_BYTES)) } }
Expand Down
28 changes: 17 additions & 11 deletions src/main/java/com/infomaniak/lib/core/auth/TokenAuthenticator.kt
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,8 @@
package com.infomaniak.lib.core.auth

import com.infomaniak.lib.core.api.ApiController
import com.infomaniak.lib.core.utils.SentryLog
import com.infomaniak.lib.login.ApiToken
import io.sentry.Sentry
import io.sentry.SentryLevel
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.runBlocking
import kotlinx.coroutines.sync.Mutex
Expand All @@ -34,21 +33,28 @@ class TokenAuthenticator(
private val tokenInterceptorListener: TokenInterceptorListener
) : Authenticator {

override fun authenticate(route: Route?, response: Response): Request {
private val userId = tokenInterceptorListener.getCurrentUserId()

override fun authenticate(route: Route?, response: Response): Request? {
return runBlocking(Dispatchers.IO) {
mutex.withLock {
val request = response.request
val authorization = request.header("Authorization")
var apiToken = tokenInterceptorListener.getApiToken() ?: run {
Sentry.captureMessage("Null ApiToken in TokenAuthenticator", SentryLevel.ERROR)
return@runBlocking request
val apiToken = tokenInterceptorListener.getApiToken() ?: run {
// The last user has been disconnected
SentryLog.e("TokenAuthenticator", "Null ApiToken in TokenAuthenticator")
return@runBlocking null
}
val isAlreadyRefreshed = apiToken.accessToken != authorization?.replaceFirst("Bearer ", "")
val hasUserChanged = userId != tokenInterceptorListener.getCurrentUserId()

if (apiToken.accessToken != authorization?.replaceFirst("Bearer ", "")) {
return@runBlocking changeAccessToken(request, apiToken)
} else {
apiToken = ApiController.refreshToken(apiToken.refreshToken, tokenInterceptorListener)
return@runBlocking changeAccessToken(request, apiToken)
return@runBlocking when {
hasUserChanged -> null
isAlreadyRefreshed -> changeAccessToken(request, apiToken)
else -> {
val newToken = ApiController.refreshToken(apiToken.refreshToken, tokenInterceptorListener)
changeAccessToken(request, newToken)
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,5 @@ interface TokenInterceptorListener {
suspend fun onRefreshTokenSuccess(apiToken: ApiToken)
suspend fun onRefreshTokenError()
suspend fun getApiToken(): ApiToken?
fun getCurrentUserId(): Int?
}
Loading