Cleanup token authenticator, fixes ctd when opening tracking
This commit is contained in:
parent
7c3c452ac2
commit
858a5e6eee
@ -2,6 +2,7 @@ package exh.md.network
|
|||||||
|
|
||||||
import exh.log.xLogD
|
import exh.log.xLogD
|
||||||
import exh.log.xLogI
|
import exh.log.xLogI
|
||||||
|
import exh.log.xLogW
|
||||||
import exh.md.utils.MdUtil
|
import exh.md.utils.MdUtil
|
||||||
import kotlinx.coroutines.runBlocking
|
import kotlinx.coroutines.runBlocking
|
||||||
import okhttp3.Authenticator
|
import okhttp3.Authenticator
|
||||||
@ -14,15 +15,16 @@ class TokenAuthenticator(private val loginHelper: MangaDexLoginHelper) : Authent
|
|||||||
override fun authenticate(route: Route?, response: Response): Request? {
|
override fun authenticate(route: Route?, response: Response): Request? {
|
||||||
xLogI("Detected Auth error ${response.code} on ${response.request.url}")
|
xLogI("Detected Auth error ${response.code} on ${response.request.url}")
|
||||||
val token = refreshToken(loginHelper)
|
val token = refreshToken(loginHelper)
|
||||||
if (token.isEmpty()) {
|
return if (token != null) {
|
||||||
return null
|
response.request.newBuilder().header("Authorization", token).build()
|
||||||
throw Exception("Unable to authenticate request, please re login")
|
} else {
|
||||||
|
// throw Exception("Unable to authenticate request, please re login")
|
||||||
|
null
|
||||||
}
|
}
|
||||||
return response.request.newBuilder().header("Authorization", token).build()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Synchronized
|
@Synchronized
|
||||||
fun refreshToken(loginHelper: MangaDexLoginHelper): String {
|
fun refreshToken(loginHelper: MangaDexLoginHelper): String? {
|
||||||
var validated = false
|
var validated = false
|
||||||
|
|
||||||
runBlocking {
|
runBlocking {
|
||||||
@ -35,25 +37,32 @@ class TokenAuthenticator(private val loginHelper: MangaDexLoginHelper) : Authent
|
|||||||
)
|
)
|
||||||
)
|
)
|
||||||
} catch (e: NoSessionException) {
|
} catch (e: NoSessionException) {
|
||||||
xLogD("Session token does not exist")
|
this@TokenAuthenticator.xLogD("Session token does not exist")
|
||||||
false
|
false
|
||||||
}
|
}
|
||||||
|
|
||||||
if (checkToken) {
|
if (checkToken) {
|
||||||
xLogI("Token is valid, other thread must have refreshed it")
|
this@TokenAuthenticator.xLogI("Token is valid, other thread must have refreshed it")
|
||||||
validated = true
|
validated = true
|
||||||
}
|
}
|
||||||
if (validated.not()) {
|
if (validated.not()) {
|
||||||
xLogI("Token is invalid trying to refresh")
|
this@TokenAuthenticator.xLogI("Token is invalid trying to refresh")
|
||||||
validated = loginHelper.refreshToken(
|
val result = runCatching {
|
||||||
MdUtil.getAuthHeaders(
|
validated = loginHelper.refreshToken(
|
||||||
Headers.Builder().build(), loginHelper.preferences, loginHelper.mdList
|
MdUtil.getAuthHeaders(
|
||||||
|
Headers.Builder().build(), loginHelper.preferences, loginHelper.mdList
|
||||||
|
)
|
||||||
)
|
)
|
||||||
)
|
}
|
||||||
|
if (result.isFailure) {
|
||||||
|
result.exceptionOrNull()?.let {
|
||||||
|
this@TokenAuthenticator.xLogW("Error refreshing token", it)
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (validated.not()) {
|
if (validated.not()) {
|
||||||
xLogI("Did not refresh token, trying to login")
|
this@TokenAuthenticator.xLogI("Did not refresh token, trying to login")
|
||||||
val loginResult = loginHelper.login()
|
val loginResult = loginHelper.login()
|
||||||
validated = if (loginResult is MangaDexLoginHelper.LoginResult.Success) {
|
validated = if (loginResult is MangaDexLoginHelper.LoginResult.Success) {
|
||||||
MdUtil.updateLoginToken(
|
MdUtil.updateLoginToken(
|
||||||
@ -67,7 +76,7 @@ class TokenAuthenticator(private val loginHelper: MangaDexLoginHelper) : Authent
|
|||||||
}
|
}
|
||||||
return when {
|
return when {
|
||||||
validated -> "bearer: ${MdUtil.sessionToken(loginHelper.preferences, loginHelper.mdList)!!}"
|
validated -> "bearer: ${MdUtil.sessionToken(loginHelper.preferences, loginHelper.mdList)!!}"
|
||||||
else -> ""
|
else -> null
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user