Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix xqt login api check #579

Merged
merged 1 commit into from
Dec 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package dev.dimension.flare.data.network.xqt

import dev.dimension.flare.data.network.authorization.BearerAuthorization
import dev.dimension.flare.data.network.ktorClient
import dev.dimension.flare.data.network.ktorfit
import dev.dimension.flare.data.network.xqt.api.DefaultApi
import dev.dimension.flare.data.network.xqt.api.GuestApi
Expand Down Expand Up @@ -28,9 +29,15 @@ import dev.dimension.flare.data.network.xqt.api.createV11PostApi
import dev.dimension.flare.data.network.xqt.api.createV20GetApi
import dev.dimension.flare.model.xqtHost
import io.ktor.client.HttpClient
import io.ktor.client.call.body
import io.ktor.client.plugins.BrowserUserAgent
import io.ktor.client.plugins.HttpClientPlugin
import io.ktor.client.plugins.defaultRequest
import io.ktor.client.request.HttpRequestPipeline
import io.ktor.client.request.accept
import io.ktor.client.request.get
import io.ktor.client.request.headers
import io.ktor.http.ContentType
import io.ktor.util.AttributeKey
import io.ktor.utils.io.KtorDsl

Expand Down Expand Up @@ -94,6 +101,29 @@ internal class XQTService(
// value.contains("gt=") &&
value.contains("ct0=") && value.contains("auth_token=")
}

suspend fun getInitialUserId(chocolate: String): String? {
val response =
ktorClient {
BrowserUserAgent()
defaultRequest {
headers {
append("Cookie", chocolate)
append("sec-fetch-dest", "document")
append("sec-fetch-mode", "navigate")
append("sec-fetch-site", "cross-site")
append("sec-gpc", "1")
append("upgrade-insecure-requests", "1")
append("accept-language", "en-US,en;q=0.9")
append("dnt", "1")
append("host", "x" + ".com")
}
accept(ContentType.Text.Html)
}
}.get("https://" + "x" + ".com" + "/home").body<String>()
val userIdRegex = "\"user_id\":\"([0-9]+)\"".toRegex()
return userIdRegex.find(response)?.groupValues?.get(1)
}
}

internal class XQTHeaderPlugin(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.runtime.setValue
import dev.dimension.flare.data.datasource.xqt.userByScreenName
import dev.dimension.flare.data.datasource.xqt.userById
import dev.dimension.flare.data.network.xqt.XQTService
import dev.dimension.flare.data.network.xqt.model.User
import dev.dimension.flare.data.repository.AccountRepository
Expand Down Expand Up @@ -61,11 +61,11 @@ class XQTLoginPresenter(
accountRepository: AccountRepository,
) {
val xqtService = XQTService(chocolate)
val accountSettings = xqtService.getAccountSettings()
requireNotNull(accountSettings.screenName)
val userId = xqtService.getInitialUserId(chocolate = chocolate)
requireNotNull(userId)
val account =
xqtService
.userByScreenName(accountSettings.screenName)
.userById(userId)
.body()
?.data
?.user
Expand Down
Loading