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 efaaaa390..4cdfa384a 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 @@ -117,10 +117,6 @@ class Hikka(id: Long) : BaseTracker(id, "Hikka"), DeletableTracker { return update(track) } - private suspend fun add(track: eu.kanade.tachiyomi.data.database.models.Track): eu.kanade.tachiyomi.data.database.models.Track { - return api.addUserManga(track) - } - override suspend fun search(query: String): List { return api.searchManga(query) } @@ -136,10 +132,10 @@ class Hikka(id: Long) : BaseTracker(id, "Hikka"), DeletableTracker { suspend fun login(code: String) { try { - val oauth = HKOAuth(code, System.currentTimeMillis() / 1000 + 30 * 60) + val oauth = api.accessToken(code) interceptor.setAuth(oauth) - val reference = api.getCurrentUser().reference - saveCredentials(reference, oauth.accessToken) + val user = api.getCurrentUser() + saveCredentials(user.reference, oauth.accessToken) } catch (e: Throwable) { logout() } 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 f3bcc5874..c44d1e2cd 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 @@ -48,20 +48,29 @@ class HikkaApi( } } - suspend fun getTokenInfo(): HKAuthTokenInfo { + suspend fun getTokenInfo(accessToken: String): HKAuthTokenInfo { return withIOContext { val request = Request.Builder() .url("${BASE_API_URL}/auth/token/info") + .header("auth", accessToken) .get() .build() with(json) { - authClient.newCall(request) + client.newCall(request) .awaitSuccess() .parseAs() } } } + suspend fun accessToken(code: String): HKOAuth { + return withIOContext { + val tokenInfo = getTokenInfo(code) + val oauth = HKOAuth(code, tokenInfo.expiration) + oauth + } + } + suspend fun searchManga(query: String): List { return withIOContext { val url = "$BASE_API_URL/manga".toUri().buildUpon() @@ -165,13 +174,12 @@ class HikkaApi( .appendQueryParameter("scope", SCOPE) .build() - fun refreshTokenRequest(oauth: HKOAuth): Request { + fun refreshTokenRequest(hkOAuth: HKOAuth): Request { val headers = Headers.Builder() - .add("auth", oauth.accessToken) - .add("Cookie", "auth=${oauth.accessToken}") + .add("auth", hkOAuth.accessToken) .build() - return GET("$BASE_API_URL/auth/token/info", headers = headers) + return GET("$BASE_API_URL/user/me", 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 220b30224..8116bd7f0 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 @@ -26,24 +26,17 @@ class HikkaInterceptor(private val hikka: Hikka) : Interceptor { } if (oauth == null) { - throw IOException("Hikka.io: User is not authenticated") + throw IOException("User is not authenticated") } val authRequest = originalRequest.newBuilder() .addHeader("auth", oauth!!.accessToken) - .addHeader("Cookie", "auth=${oauth!!.accessToken}") .addHeader("accept", "application/json") .build() - Log.println(Log.WARN, "interceptor", "Set Auth Request Headers: " + authRequest.headers) - return chain.proceed(authRequest) } - /** - * Called when the user authenticates with MyAnimeList for the first time. Sets the refresh token - * and the oauth object. - */ fun setAuth(oauth: HKOAuth?) { this.oauth = oauth hikka.saveOAuth(oauth) @@ -59,7 +52,7 @@ class HikkaInterceptor(private val hikka: Hikka) : Interceptor { throw HKTokenRefreshFailed() } - if (response.code == 401) { + if (response.code != 200) { hikka.setAuthExpired() throw HKTokenExpired() } @@ -84,5 +77,5 @@ class HikkaInterceptor(private val hikka: Hikka) : Interceptor { } } -class HKTokenRefreshFailed : IOException("Hikka.io: Failed to refresh account token") -class HKTokenExpired : IOException("Hikka.io: Login has expired") +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 7b9caec5d..f4822537e 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 @@ -12,6 +12,6 @@ data class HKOAuth( val expiration: Long, ) { fun isExpired(): Boolean { - return (expiration - 1000) < System.currentTimeMillis() / 1000 + return (expiration - 7200) < (System.currentTimeMillis() / 1000) } }