Fix Bangumi tracker losing track of login expiration (#1681)

* Fix Bangumi tracking losing track of login state

kotlinx.serialization does NOT serialize default values (like
createdAt in BGMOAuth.kt), so every time the Bangumi tracker
deserialized the tracker OAuth, createdAt was set to the time of the
read, not the time of issuance.

Separately, BangumiInterceptor did correctly fetch new OAuth
credentials upon detected expiry of the stored credentials and saved
them, but did not use them for the current request (the new
credentials were used for all subsequent requests only). This led to
401 errors from Bangumi because the expired access_token was provided.
 A subsequent request using the newly acquired access_token would end
 up being successful.

* Add CHANGELOG.md entry

(cherry picked from commit dce6aacf02d07f3f123b19b1b74cbbe18c28852b)

# Conflicts:
#	CHANGELOG.md
This commit is contained in:
MajorTanya 2025-01-30 16:29:03 +01:00 committed by Jobobby04
parent b34f807d33
commit 1fa05703fa
2 changed files with 5 additions and 2 deletions

View File

@ -21,12 +21,13 @@ class BangumiInterceptor(private val bangumi: Bangumi) : Interceptor {
override fun intercept(chain: Interceptor.Chain): Response { override fun intercept(chain: Interceptor.Chain): Response {
val originalRequest = chain.request() val originalRequest = chain.request()
val currAuth = oauth ?: throw Exception("Not authenticated with Bangumi") var currAuth: BGMOAuth = oauth ?: throw Exception("Not authenticated with Bangumi")
if (currAuth.isExpired()) { if (currAuth.isExpired()) {
val response = chain.proceed(BangumiApi.refreshTokenRequest(currAuth.refreshToken!!)) val response = chain.proceed(BangumiApi.refreshTokenRequest(currAuth.refreshToken!!))
if (response.isSuccessful) { if (response.isSuccessful) {
newAuth(json.decodeFromString<BGMOAuth>(response.body.string())) currAuth = json.decodeFromString<BGMOAuth>(response.body.string())
newAuth(currAuth)
} else { } else {
response.close() response.close()
} }

View File

@ -1,5 +1,6 @@
package eu.kanade.tachiyomi.data.track.bangumi.dto package eu.kanade.tachiyomi.data.track.bangumi.dto
import kotlinx.serialization.EncodeDefault
import kotlinx.serialization.SerialName import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable import kotlinx.serialization.Serializable
@ -10,6 +11,7 @@ data class BGMOAuth(
@SerialName("token_type") @SerialName("token_type")
val tokenType: String, val tokenType: String,
@SerialName("created_at") @SerialName("created_at")
@EncodeDefault
val createdAt: Long = System.currentTimeMillis() / 1000, val createdAt: Long = System.currentTimeMillis() / 1000,
@SerialName("expires_in") @SerialName("expires_in")
val expiresIn: Long, val expiresIn: Long,