From f39e947ece13a36dc05056b19b3a514e05cbe74d Mon Sep 17 00:00:00 2001 From: Lorg0n Date: Thu, 17 Oct 2024 22:25:48 +0300 Subject: [PATCH] feat: more optimization Hikka authorization --- .../tachiyomi/data/track/hikka/Hikka.kt | 8 --- .../tachiyomi/data/track/hikka/HikkaApi.kt | 14 ++++- .../data/track/hikka/HikkaInterceptor.kt | 61 ++++--------------- .../tachiyomi/data/track/hikka/dto/HKOAuth.kt | 6 +- 4 files changed, 25 insertions(+), 64 deletions(-) diff --git a/app/src/main/java/eu/kanade/tachiyomi/data/track/hikka/Hikka.kt b/app/src/main/java/eu/kanade/tachiyomi/data/track/hikka/Hikka.kt index 4cdfa384a..7512728f5 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/data/track/hikka/Hikka.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/data/track/hikka/Hikka.kt @@ -151,14 +151,6 @@ class Hikka(id: Long) : BaseTracker(id, "Hikka"), DeletableTracker { interceptor.setAuth(null) } - fun getIfAuthExpired(): Boolean { - return trackPreferences.trackAuthExpired(this).get() - } - - fun setAuthExpired() { - trackPreferences.trackAuthExpired(this).set(true) - } - fun saveOAuth(oAuth: HKOAuth?) { trackPreferences.trackToken(this).set(json.encodeToString(oAuth)) } diff --git a/app/src/main/java/eu/kanade/tachiyomi/data/track/hikka/HikkaApi.kt b/app/src/main/java/eu/kanade/tachiyomi/data/track/hikka/HikkaApi.kt index c44d1e2cd..10104cead 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/data/track/hikka/HikkaApi.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/data/track/hikka/HikkaApi.kt @@ -174,12 +174,20 @@ class HikkaApi( .appendQueryParameter("scope", SCOPE) .build() - fun refreshTokenRequest(hkOAuth: HKOAuth): Request { + fun refreshTokenRequest(accessToken: String): Request { val headers = Headers.Builder() - .add("auth", hkOAuth.accessToken) + .add("auth", accessToken) .build() - return GET("$BASE_API_URL/user/me", headers = headers) + return GET("$BASE_API_URL/user/me", headers = headers) // Any request with auth + } + + fun authTokenInfo(accessToken: String): Request { + val headers = Headers.Builder() + .add("auth", accessToken) + .build() + + return GET("$BASE_API_URL/auth/token/info", headers = headers) } } } diff --git a/app/src/main/java/eu/kanade/tachiyomi/data/track/hikka/HikkaInterceptor.kt b/app/src/main/java/eu/kanade/tachiyomi/data/track/hikka/HikkaInterceptor.kt index 87e53c8fe..6b9e92d29 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/data/track/hikka/HikkaInterceptor.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/data/track/hikka/HikkaInterceptor.kt @@ -1,31 +1,33 @@ package eu.kanade.tachiyomi.data.track.hikka +import eu.kanade.tachiyomi.data.track.hikka.dto.HKAuthTokenInfo import eu.kanade.tachiyomi.data.track.hikka.dto.HKOAuth import kotlinx.serialization.json.Json import okhttp3.Interceptor import okhttp3.Response -import org.json.JSONObject import uy.kohesive.injekt.injectLazy -import java.io.IOException class HikkaInterceptor(private val hikka: Hikka) : Interceptor { private val json: Json by injectLazy() private var oauth: HKOAuth? = hikka.loadOAuth() - private val tokenExpired get() = hikka.getIfAuthExpired() override fun intercept(chain: Interceptor.Chain): Response { - if (tokenExpired) { - throw HKTokenExpired() - } val originalRequest = chain.request() - if (oauth?.isExpired() == true) { - refreshToken(chain) - } + val currAuth = oauth ?: throw Exception("Not authenticated with Hikka") - if (oauth == null) { - throw IOException("User is not authenticated") + if (currAuth.isExpired()) { + val refreshTokenResponse = chain.proceed(HikkaApi.refreshTokenRequest(currAuth.accessToken)) + if (!refreshTokenResponse.isSuccessful) + refreshTokenResponse.close() + + val authTokenInfoResponse = chain.proceed(HikkaApi.authTokenInfo(currAuth.accessToken)) + if (!authTokenInfoResponse.isSuccessful) + authTokenInfoResponse.close() + + val authTokenInfo = json.decodeFromString(authTokenInfoResponse.body.string()) + setAuth(HKOAuth(oauth!!.accessToken, authTokenInfo.expiration)) } val authRequest = originalRequest.newBuilder() @@ -40,41 +42,4 @@ class HikkaInterceptor(private val hikka: Hikka) : Interceptor { this.oauth = oauth hikka.saveOAuth(oauth) } - - private fun refreshToken(chain: Interceptor.Chain): HKOAuth = synchronized(this) { - if (tokenExpired) throw HKTokenExpired() - oauth?.takeUnless { it.isExpired() }?.let { return@synchronized it } - - val response = try { - chain.proceed(HikkaApi.refreshTokenRequest(oauth!!)) - } catch (_: Throwable) { - throw HKTokenRefreshFailed() - } - - if (response.code != 200) { - hikka.setAuthExpired() - throw HKTokenExpired() - } - - return runCatching { - if (response.isSuccessful && oauth != null) { - val responseBody = response.body?.string() ?: return@runCatching null - val jsonObject = JSONObject(responseBody) - - val secret = oauth!!.accessToken - val expiration = jsonObject.getLong("expiration") - - HKOAuth(secret, expiration) - } else { - response.close() - null - } - }.getOrNull()?.also { - this.oauth = it - hikka.saveOAuth(it) - } ?: throw HKTokenRefreshFailed() - } } - -class HKTokenRefreshFailed : IOException("Hikka: Failed to refresh account token") -class HKTokenExpired : IOException("Hikka: Login has expired") diff --git a/app/src/main/java/eu/kanade/tachiyomi/data/track/hikka/dto/HKOAuth.kt b/app/src/main/java/eu/kanade/tachiyomi/data/track/hikka/dto/HKOAuth.kt index f4822537e..69f34a74d 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/data/track/hikka/dto/HKOAuth.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/data/track/hikka/dto/HKOAuth.kt @@ -1,17 +1,13 @@ package eu.kanade.tachiyomi.data.track.hikka.dto -import kotlinx.serialization.SerialName import kotlinx.serialization.Serializable @Serializable data class HKOAuth( - @SerialName("secret") val accessToken: String, - - @SerialName("expiration") val expiration: Long, ) { fun isExpired(): Boolean { - return (expiration - 7200) < (System.currentTimeMillis() / 1000) + return (expiration - 43200) < (System.currentTimeMillis() / 1000) } }