mirror of
https://github.com/mihonapp/mihon.git
synced 2024-12-25 18:38:23 +01:00
feat: improve Hikka authorization
This commit is contained in:
parent
32db1da753
commit
03f9ae8bb2
@ -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<TrackSearch> {
|
||||
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()
|
||||
}
|
||||
|
@ -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<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> {
|
||||
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)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -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")
|
||||
|
@ -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)
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user