feat: improve Hikka authorization

This commit is contained in:
Lorg0n 2024-10-17 20:32:47 +03:00
parent 32db1da753
commit 03f9ae8bb2
4 changed files with 22 additions and 25 deletions

View File

@ -117,10 +117,6 @@ class Hikka(id: Long) : BaseTracker(id, "Hikka"), DeletableTracker {
return update(track) 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<TrackSearch> { override suspend fun search(query: String): List<TrackSearch> {
return api.searchManga(query) return api.searchManga(query)
} }
@ -136,10 +132,10 @@ class Hikka(id: Long) : BaseTracker(id, "Hikka"), DeletableTracker {
suspend fun login(code: String) { suspend fun login(code: String) {
try { try {
val oauth = HKOAuth(code, System.currentTimeMillis() / 1000 + 30 * 60) val oauth = api.accessToken(code)
interceptor.setAuth(oauth) interceptor.setAuth(oauth)
val reference = api.getCurrentUser().reference val user = api.getCurrentUser()
saveCredentials(reference, oauth.accessToken) saveCredentials(user.reference, oauth.accessToken)
} catch (e: Throwable) { } catch (e: Throwable) {
logout() logout()
} }

View File

@ -48,20 +48,29 @@ class HikkaApi(
} }
} }
suspend fun getTokenInfo(): HKAuthTokenInfo { suspend fun getTokenInfo(accessToken: String): HKAuthTokenInfo {
return withIOContext { return withIOContext {
val request = Request.Builder() val request = Request.Builder()
.url("${BASE_API_URL}/auth/token/info") .url("${BASE_API_URL}/auth/token/info")
.header("auth", accessToken)
.get() .get()
.build() .build()
with(json) { with(json) {
authClient.newCall(request) client.newCall(request)
.awaitSuccess() .awaitSuccess()
.parseAs<HKAuthTokenInfo>() .parseAs<HKAuthTokenInfo>()
} }
} }
} }
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<TrackSearch> { suspend fun searchManga(query: String): List<TrackSearch> {
return withIOContext { return withIOContext {
val url = "$BASE_API_URL/manga".toUri().buildUpon() val url = "$BASE_API_URL/manga".toUri().buildUpon()
@ -165,13 +174,12 @@ class HikkaApi(
.appendQueryParameter("scope", SCOPE) .appendQueryParameter("scope", SCOPE)
.build() .build()
fun refreshTokenRequest(oauth: HKOAuth): Request { fun refreshTokenRequest(hkOAuth: HKOAuth): Request {
val headers = Headers.Builder() val headers = Headers.Builder()
.add("auth", oauth.accessToken) .add("auth", hkOAuth.accessToken)
.add("Cookie", "auth=${oauth.accessToken}")
.build() .build()
return GET("$BASE_API_URL/auth/token/info", headers = headers) return GET("$BASE_API_URL/user/me", headers = headers)
} }
} }
} }

View File

@ -26,24 +26,17 @@ class HikkaInterceptor(private val hikka: Hikka) : Interceptor {
} }
if (oauth == null) { if (oauth == null) {
throw IOException("Hikka.io: User is not authenticated") throw IOException("User is not authenticated")
} }
val authRequest = originalRequest.newBuilder() val authRequest = originalRequest.newBuilder()
.addHeader("auth", oauth!!.accessToken) .addHeader("auth", oauth!!.accessToken)
.addHeader("Cookie", "auth=${oauth!!.accessToken}")
.addHeader("accept", "application/json") .addHeader("accept", "application/json")
.build() .build()
Log.println(Log.WARN, "interceptor", "Set Auth Request Headers: " + authRequest.headers)
return chain.proceed(authRequest) 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?) { fun setAuth(oauth: HKOAuth?) {
this.oauth = oauth this.oauth = oauth
hikka.saveOAuth(oauth) hikka.saveOAuth(oauth)
@ -59,7 +52,7 @@ class HikkaInterceptor(private val hikka: Hikka) : Interceptor {
throw HKTokenRefreshFailed() throw HKTokenRefreshFailed()
} }
if (response.code == 401) { if (response.code != 200) {
hikka.setAuthExpired() hikka.setAuthExpired()
throw HKTokenExpired() throw HKTokenExpired()
} }
@ -84,5 +77,5 @@ class HikkaInterceptor(private val hikka: Hikka) : Interceptor {
} }
} }
class HKTokenRefreshFailed : IOException("Hikka.io: Failed to refresh account token") class HKTokenRefreshFailed : IOException("Hikka: Failed to refresh account token")
class HKTokenExpired : IOException("Hikka.io: Login has expired") class HKTokenExpired : IOException("Hikka: Login has expired")

View File

@ -12,6 +12,6 @@ data class HKOAuth(
val expiration: Long, val expiration: Long,
) { ) {
fun isExpired(): Boolean { fun isExpired(): Boolean {
return (expiration - 1000) < System.currentTimeMillis() / 1000 return (expiration - 7200) < (System.currentTimeMillis() / 1000)
} }
} }