mirror of
https://github.com/mihonapp/mihon.git
synced 2024-11-29 06:23:17 -05:00
feat: changed authorization principle, removed dependence on third-party server
This commit is contained in:
parent
f480d4cb38
commit
e44666673f
5 changed files with 26 additions and 21 deletions
|
@ -128,9 +128,9 @@ class Hikka(id: Long) : BaseTracker(id, "Hikka"), DeletableTracker {
|
|||
|
||||
override suspend fun login(username: String, password: String) = login(password)
|
||||
|
||||
suspend fun login(code: String) {
|
||||
suspend fun login(reference: String) {
|
||||
try {
|
||||
val oauth = api.accessToken(code)
|
||||
val oauth = api.accessToken(reference)
|
||||
interceptor.setAuth(oauth)
|
||||
val user = api.getCurrentUser()
|
||||
saveCredentials(user.reference, oauth.accessToken)
|
||||
|
|
|
@ -3,7 +3,6 @@ package eu.kanade.tachiyomi.data.track.hikka
|
|||
import android.net.Uri
|
||||
import androidx.core.net.toUri
|
||||
import eu.kanade.tachiyomi.data.database.models.Track
|
||||
import eu.kanade.tachiyomi.data.track.hikka.dto.HKAuthTokenInfo
|
||||
import eu.kanade.tachiyomi.data.track.hikka.dto.HKManga
|
||||
import eu.kanade.tachiyomi.data.track.hikka.dto.HKMangaPagination
|
||||
import eu.kanade.tachiyomi.data.track.hikka.dto.HKOAuth
|
||||
|
@ -48,24 +47,16 @@ class HikkaApi(
|
|||
}
|
||||
}
|
||||
|
||||
private suspend fun getTokenInfo(accessToken: String): HKAuthTokenInfo {
|
||||
suspend fun accessToken(reference: String): HKOAuth {
|
||||
return withIOContext {
|
||||
with(json) {
|
||||
client.newCall(authTokenInfo(accessToken))
|
||||
client.newCall(authTokenCreate(reference))
|
||||
.awaitSuccess()
|
||||
.parseAs<HKAuthTokenInfo>()
|
||||
.parseAs<HKOAuth>()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
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()
|
||||
|
@ -189,10 +180,13 @@ class HikkaApi(
|
|||
const val BASE_API_URL = "https://hikka.io/api"
|
||||
const val BASE_URL = "https://hikka.io"
|
||||
private const val SCOPE = "readlist,read:user-details"
|
||||
private const val REFERENCE = "49eda83d-baa6-45f8-9936-b2a41d944da4"
|
||||
private const val CLIENT_REFERENCE = "49eda83d-baa6-45f8-9936-b2a41d944da4"
|
||||
private const val CLIENT_SECRET = "8Zxzs13Pvikx6m_4rwjF7t2BxxnEb0wWtXIRQ_68HyCvmdhGE9hdfz" +
|
||||
"SL1Pas4h927LaV2ocjVoc--S_vmorHEWWh42Z_z70j-wSFYsraQQ98" +
|
||||
"hiOeTH2BaDf77ZcA9W5Z"
|
||||
|
||||
fun authUrl(): Uri = "$BASE_URL/oauth".toUri().buildUpon()
|
||||
.appendQueryParameter("reference", REFERENCE)
|
||||
.appendQueryParameter("reference", CLIENT_REFERENCE)
|
||||
.appendQueryParameter("scope", SCOPE)
|
||||
.build()
|
||||
|
||||
|
@ -204,6 +198,14 @@ class HikkaApi(
|
|||
return GET("$BASE_API_URL/user/me", headers = headers) // Any request with auth
|
||||
}
|
||||
|
||||
fun authTokenCreate(reference: String): Request {
|
||||
val payload = buildJsonObject {
|
||||
put("request_reference", reference)
|
||||
put("client_secret", CLIENT_SECRET)
|
||||
}
|
||||
return POST("$BASE_API_URL/auth/token", body = payload.toString().toRequestBody(jsonMime))
|
||||
}
|
||||
|
||||
fun authTokenInfo(accessToken: String): Request {
|
||||
val headers = Headers.Builder()
|
||||
.add("auth", accessToken)
|
||||
|
|
|
@ -30,7 +30,7 @@ class HikkaInterceptor(private val hikka: Hikka) : Interceptor {
|
|||
}
|
||||
|
||||
val authTokenInfo = json.decodeFromString<HKAuthTokenInfo>(authTokenInfoResponse.body.string())
|
||||
setAuth(HKOAuth(oauth!!.accessToken, authTokenInfo.expiration))
|
||||
setAuth(HKOAuth(oauth!!.accessToken, authTokenInfo.expiration, authTokenInfo.created))
|
||||
}
|
||||
|
||||
val authRequest = originalRequest.newBuilder()
|
||||
|
|
|
@ -1,11 +1,14 @@
|
|||
package eu.kanade.tachiyomi.data.track.hikka.dto
|
||||
|
||||
import android.util.Log
|
||||
import kotlinx.serialization.SerialName
|
||||
import kotlinx.serialization.Serializable
|
||||
|
||||
@Serializable
|
||||
data class HKOAuth(
|
||||
val accessToken: String,
|
||||
@SerialName("secret") val accessToken: String,
|
||||
val expiration: Long,
|
||||
val created: Long,
|
||||
) {
|
||||
fun isExpired(): Boolean {
|
||||
return (expiration - 43200) < (System.currentTimeMillis() / 1000)
|
||||
|
|
|
@ -70,10 +70,10 @@ class TrackLoginActivity : BaseOAuthLoginActivity() {
|
|||
}
|
||||
|
||||
private fun handleHikka(data: Uri) {
|
||||
val code = data.getQueryParameter("code")
|
||||
if (code != null) {
|
||||
val reference = data.getQueryParameter("reference")
|
||||
if (reference != null) {
|
||||
lifecycleScope.launchIO {
|
||||
trackerManager.hikka.login(code)
|
||||
trackerManager.hikka.login(reference)
|
||||
returnToSettings()
|
||||
}
|
||||
} else {
|
||||
|
|
Loading…
Reference in a new issue