Skip to content

Commit

Permalink
Replace isInfinite util method with ApiToken extension
Browse files Browse the repository at this point in the history
  • Loading branch information
LunarX committed Nov 12, 2024
1 parent 36e75b8 commit f438b8f
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
package com.infomaniak.lib.core.auth

import com.infomaniak.lib.core.api.ApiController
import com.infomaniak.lib.core.utils.TokenUtils
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,13 +44,12 @@ class TokenAuthenticator(

val isAlreadyRefreshed = apiToken.accessToken != authorization?.replaceFirst("Bearer ", "")
val hasUserChanged = userId != tokenInterceptorListener.getCurrentUserId()
val refreshToken = apiToken.refreshToken

return@runBlocking when {
hasUserChanged || TokenUtils.isInfinite(refreshToken) -> null
hasUserChanged || apiToken.isInfinite -> null
isAlreadyRefreshed -> changeAccessToken(request, apiToken)
else -> {
val newToken = ApiController.refreshToken(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,7 +18,7 @@
package com.infomaniak.lib.core.networking

import com.infomaniak.lib.core.auth.TokenInterceptorListener
import com.infomaniak.lib.core.utils.TokenUtils
import com.infomaniak.lib.core.utils.ApiTokenExt.isInfinite
import io.sentry.Sentry
import io.sentry.SentryLevel
import kotlinx.coroutines.Dispatchers
Expand All @@ -43,7 +43,7 @@ class AccessTokenUsageInterceptor(
val apiToken = tokenInterceptorListener.getApiToken() ?: return@runBlocking

// Only log api calls if we're not using refresh tokens
if (!TokenUtils.isInfinite(apiToken.refreshToken)) return@runBlocking
if (!apiToken.isInfinite) return@runBlocking

val currentApiCall = ApiCallRecord(
accessToken = request.header("Authorization")?.replaceFirst("Bearer ", "") ?: return@runBlocking,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,8 @@
*/
package com.infomaniak.lib.core.utils

import kotlin.contracts.ExperimentalContracts
import kotlin.contracts.contract
import com.infomaniak.lib.login.ApiToken

object TokenUtils {
@OptIn(ExperimentalContracts::class)
fun isInfinite(refreshToken: String?): Boolean {
contract {
returns(false) implies (refreshToken != null)
}
return refreshToken == null
}
object ApiTokenExt {
val ApiToken.isInfinite get() = refreshToken == null
}

0 comments on commit f438b8f

Please sign in to comment.