From 08a6db7d6eb9d682126f06597a1ded6b41744fa3 Mon Sep 17 00:00:00 2001 From: arkon Date: Sat, 24 Apr 2021 16:30:53 -0400 Subject: [PATCH] Maybe better handle MAL token expiration --- .../data/track/myanimelist/MyAnimeListInterceptor.kt | 11 +++-------- .../kanade/tachiyomi/data/track/myanimelist/OAuth.kt | 3 ++- 2 files changed, 5 insertions(+), 9 deletions(-) diff --git a/app/src/main/java/eu/kanade/tachiyomi/data/track/myanimelist/MyAnimeListInterceptor.kt b/app/src/main/java/eu/kanade/tachiyomi/data/track/myanimelist/MyAnimeListInterceptor.kt index 2b401f0a5..50eb111a8 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/data/track/myanimelist/MyAnimeListInterceptor.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/data/track/myanimelist/MyAnimeListInterceptor.kt @@ -11,9 +11,6 @@ class MyAnimeListInterceptor(private val myanimelist: MyAnimeList, private var t private val json: Json by injectLazy() private var oauth: OAuth? = null - set(value) { - field = value?.copy(expires_in = System.currentTimeMillis() + (value.expires_in * 1000)) - } override fun intercept(chain: Interceptor.Chain): Response { val originalRequest = chain.request() @@ -24,21 +21,19 @@ class MyAnimeListInterceptor(private val myanimelist: MyAnimeList, private var t if (oauth == null) { oauth = myanimelist.loadOAuth() } - // Refresh access token if null or expired. - if (oauth!!.isExpired()) { + // Refresh access token if expired + if (oauth != null && oauth!!.isExpired()) { chain.proceed(MyAnimeListApi.refreshTokenRequest(oauth!!.refresh_token)).use { if (it.isSuccessful) { setAuth(json.decodeFromString(it.body!!.string())) } } } - - // Throw on null auth. if (oauth == null) { throw Exception("No authentication token") } - // Add the authorization header to the original request. + // Add the authorization header to the original request val authRequest = originalRequest.newBuilder() .addHeader("Authorization", "Bearer ${oauth!!.access_token}") .build() diff --git a/app/src/main/java/eu/kanade/tachiyomi/data/track/myanimelist/OAuth.kt b/app/src/main/java/eu/kanade/tachiyomi/data/track/myanimelist/OAuth.kt index 6f0a02d1a..590e6f225 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/data/track/myanimelist/OAuth.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/data/track/myanimelist/OAuth.kt @@ -7,8 +7,9 @@ data class OAuth( val refresh_token: String, val access_token: String, val token_type: String, + val created_at: Long = System.currentTimeMillis(), val expires_in: Long ) { - fun isExpired() = System.currentTimeMillis() > expires_in + fun isExpired() = System.currentTimeMillis() > created_at + (expires_in * 1000) }