mirror of
https://github.com/bitwarden/android.git
synced 2026-04-29 20:38:41 -05:00
PM-24539: Prevent token refresh from looping (#5658)
This commit is contained in:
@@ -2,6 +2,7 @@ package com.x8bit.bitwarden.data.auth.datasource.disk.model
|
|||||||
|
|
||||||
import kotlinx.serialization.SerialName
|
import kotlinx.serialization.SerialName
|
||||||
import kotlinx.serialization.Serializable
|
import kotlinx.serialization.Serializable
|
||||||
|
import java.time.Instant
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Container for the user's API tokens.
|
* Container for the user's API tokens.
|
||||||
@@ -19,7 +20,7 @@ data class AccountTokensJson(
|
|||||||
val refreshToken: String?,
|
val refreshToken: String?,
|
||||||
|
|
||||||
@SerialName("expiresAtSec")
|
@SerialName("expiresAtSec")
|
||||||
val expiresAtSec: Long = Long.MAX_VALUE,
|
val expiresAtSec: Long = Instant.MAX.epochSecond,
|
||||||
) {
|
) {
|
||||||
/**
|
/**
|
||||||
* Returns `true` if the user is logged in, `false otherwise.
|
* Returns `true` if the user is logged in, `false otherwise.
|
||||||
|
|||||||
@@ -8,6 +8,7 @@ import okhttp3.Authenticator
|
|||||||
import okhttp3.Request
|
import okhttp3.Request
|
||||||
import okhttp3.Response
|
import okhttp3.Response
|
||||||
import okhttp3.Route
|
import okhttp3.Route
|
||||||
|
import timber.log.Timber
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* An authenticator used to refresh the access token when a 401 is returned from an API. Upon
|
* An authenticator used to refresh the access token when a 401 is returned from an API. Upon
|
||||||
@@ -21,6 +22,10 @@ internal class RefreshAuthenticator : Authenticator {
|
|||||||
route: Route?,
|
route: Route?,
|
||||||
response: Response,
|
response: Response,
|
||||||
): Request? {
|
): Request? {
|
||||||
|
if (response.shouldSkipAuthentication()) {
|
||||||
|
// If the same request keeps failing, let's just let the 401 pass through.
|
||||||
|
return null
|
||||||
|
}
|
||||||
val accessToken = requireNotNull(
|
val accessToken = requireNotNull(
|
||||||
response
|
response
|
||||||
.request
|
.request
|
||||||
@@ -34,6 +39,7 @@ internal class RefreshAuthenticator : Authenticator {
|
|||||||
}
|
}
|
||||||
|
|
||||||
else -> {
|
else -> {
|
||||||
|
Timber.d("Attempting to refresh token due to unauthorized")
|
||||||
refreshTokenProvider
|
refreshTokenProvider
|
||||||
?.refreshAccessTokenSynchronously(userId = userId)
|
?.refreshAccessTokenSynchronously(userId = userId)
|
||||||
?.fold(
|
?.fold(
|
||||||
@@ -52,4 +58,6 @@ internal class RefreshAuthenticator : Authenticator {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private fun Response.shouldSkipAuthentication(): Boolean = this.priorResponse != null
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user