Fix Mangadex Auth not properly handling bad responses

This commit is contained in:
Jobobby04 2024-01-13 00:10:10 -05:00
parent 14da34de64
commit efb95864cd

View File

@ -9,6 +9,7 @@ import exh.md.utils.MdUtil
import exh.util.nullIfBlank import exh.util.nullIfBlank
import okhttp3.Interceptor import okhttp3.Interceptor
import okhttp3.Response import okhttp3.Response
import tachiyomi.core.util.system.logcat
import java.io.IOException import java.io.IOException
class MangaDexAuthInterceptor( class MangaDexAuthInterceptor(
@ -50,11 +51,12 @@ class MangaDexAuthInterceptor(
// Retry the request once with a new token in case it was not already refreshed // Retry the request once with a new token in case it was not already refreshed
// by the is expired check before. // by the is expired check before.
if (response.code == 401 && tokenIsExpired) { if (response.code == 401 && tokenIsExpired) {
response.close()
val newToken = refreshToken(chain) val newToken = refreshToken(chain)
setAuth(newToken) setAuth(newToken)
newToken ?: return response
response.close()
val newRequest = originalRequest.newBuilder() val newRequest = originalRequest.newBuilder()
.addHeader("Authorization", "Bearer ${newToken.access_token}") .addHeader("Authorization", "Bearer ${newToken.access_token}")
.build() .build()
@ -75,7 +77,7 @@ class MangaDexAuthInterceptor(
MdUtil.saveOAuth(trackPreferences, mdList, oauth) MdUtil.saveOAuth(trackPreferences, mdList, oauth)
} }
private fun refreshToken(chain: Interceptor.Chain): OAuth { private fun refreshToken(chain: Interceptor.Chain): OAuth? {
val newOauth = runCatching { val newOauth = runCatching {
val oauthResponse = chain.proceed(MdUtil.refreshTokenRequest(oauth!!)) val oauthResponse = chain.proceed(MdUtil.refreshTokenRequest(oauth!!))
@ -87,10 +89,8 @@ class MangaDexAuthInterceptor(
} }
} }
if (newOauth.getOrNull() == null) { logcat(throwable = newOauth.exceptionOrNull()) { "Fetched new mangadex oauth" }
throw IOException("Failed to refresh the access token", newOauth.exceptionOrNull())
}
return newOauth.getOrNull()!! return newOauth.getOrNull()
} }
} }