Skip to content
This repository has been archived by the owner on Jul 18, 2024. It is now read-only.

Commit

Permalink
Updater: Fix crash caused by incorrect login
Browse files Browse the repository at this point in the history
  • Loading branch information
YuKongA committed Jan 13, 2024
1 parent cd68e53 commit 4907af9
Show file tree
Hide file tree
Showing 7 changed files with 44 additions and 41 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,10 @@ import kotlinx.serialization.Serializable

@Serializable
data class AuthorizeInfoHelper(
val ssecurity: String,
val code: Int,
val passToken: String,
val description: String,
val securityStatus: Int,
val nonce: Long,
val userId: Long,
val cUserId: String,
val result: String,
val psecurity: String,
val captchaUrl: String?,
val location: String,
val pwd: Int,
val child: Int,
val desc: String,
val description: String? = null,
val location: String? = null,
val nonce: Long? = null,
val result: String? = null,
val ssecurity: String? = null,
val userId: Long? = null,
)
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ import kotlinx.serialization.Serializable

@Serializable
data class LoginInfoHelper(
val description: String,
val accountType: String,
val userId: String,
val authResult: String,
val description: String,
val ssecurity: String,
val serviceToken: String,
val authResult: String
val userId: String,
)
20 changes: 12 additions & 8 deletions app/src/main/kotlin/top/yukonga/update/logic/utils/LoginUtils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,13 @@ class LoginUtils {
private val mediaType = "application/x-www-form-urlencoded".toMediaType()

suspend fun login(context: Context, account: String, password: String, global: String): Boolean {
withContext(Dispatchers.Main) {
if (account.isEmpty() || password.isEmpty()) {
if (account.isEmpty() || password.isEmpty()) {
withContext(Dispatchers.Main) {
showStringToast(context, context.getString(R.string.account_or_password_empty), 0)
} else {
}
return false
} else {
withContext(Dispatchers.Main) {
showStringToast(context, context.getString(R.string.logging_in), 1)
}
}
Expand All @@ -56,7 +59,7 @@ class LoginUtils {
val authStr = response2.body!!.string().replace("&&&START&&&", "")
val authJson = json.decodeFromString<AuthorizeInfoHelper>(authStr)
val description = authJson.description
val nonce = authJson.nonce.toString()
val nonce = authJson.nonce
val ssecurity = authJson.ssecurity
val location = authJson.location
val userId = authJson.userId.toString()
Expand All @@ -65,13 +68,14 @@ class LoginUtils {

if (description != "成功") {
withContext(Dispatchers.Main) {
showStringToast(context, description, 0)
if (description == "登录验证失败") showStringToast(context, context.getString(R.string.login_error), 0)
else showStringToast(context, description, 0)
}
return false
}
if (nonce.isEmpty() || ssecurity.isEmpty() || location.isEmpty() || userId.isEmpty()) {
if (nonce == null || ssecurity == null || location == null || userId.isEmpty()) {
withContext(Dispatchers.Main) {
showStringToast(context, context.getString(R.string.unknown_error), 0)
showStringToast(context, context.getString(R.string.security_error), 0)
}
return false
}
Expand All @@ -85,7 +89,7 @@ class LoginUtils {
val cookies = response3.headers("Set-Cookie").joinToString("; ") { it.split(";")[0] }
val serviceToken = cookies.split("serviceToken=")[1].split(";")[0]

val loginInfo = LoginInfoHelper(description, accountType, userId, ssecurity, serviceToken, authResult)
val loginInfo = LoginInfoHelper(accountType, authResult, description, ssecurity, serviceToken, userId)

withContext(Dispatchers.Main) {
saveCookiesFile(context, Json.encodeToString(loginInfo))
Expand Down
7 changes: 6 additions & 1 deletion app/src/main/kotlin/top/yukonga/update/ui/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -164,9 +164,14 @@ class MainActivity : AppCompatActivity() {
loginIcon.setImageResource(R.drawable.ic_error)
loginTitle.text = getString(R.string.login_expired)
loginDesc.text = getString(R.string.login_expired_desc)
cookies.clear()
cookies["authResult"] = "-1"
FileUtils.saveCookiesFile(this@MainActivity, Json.encodeToString(cookies))
showStringToast(this@MainActivity, getString(R.string.login_expired_dialog), 0)
activityMainBinding.apply {
topAppBar.menu.findItem(R.id.login).isVisible = true
topAppBar.menu.findItem(R.id.logout).isVisible = false
}
}
}

Expand Down Expand Up @@ -496,7 +501,7 @@ class MainActivity : AppCompatActivity() {
val cookies = json.decodeFromString<MutableMap<String, String>>(cookiesFile)
val description = cookies["description"].toString()
val authResult = cookies["authResult"].toString()
if (description.isNotEmpty() && authResult == "-1") {
if (authResult == "-1") {
mainContentBinding.apply {
loginIcon.setImageResource(R.drawable.ic_error)
loginTitle.text = getString(R.string.login_expired)
Expand Down
5 changes: 3 additions & 2 deletions app/src/main/res/values-zh-rCN/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,14 @@
<string name="logging_in">登录中</string>
<string name="login_successful">登录成功</string>
<string name="request_sign_failed">请求 _sign 失败!</string>
<string name="account_or_password_empty">账号或密码为空</string>
<string name="account_or_password_empty">账号或密码为空</string>
<string name="no_account">未登录</string>
<string name="login_desc">正在使用 v1 接口</string>
<string name="using_v2">正在使用 v2 接口</string>
<string name="logged_in">已登录</string>
<string name="copy_button">复制</string>
<string name="download_button">下载</string>
<string name="unknown_error">获取密钥失败</string>
<string name="security_error">获取密钥失败</string>
<string name="logout">登出</string>
<string name="confirm">确定</string>
<string name="logout_successful">登出成功</string>
Expand All @@ -44,4 +44,5 @@
<string name="android_default">默认</string>
<string name="other">其它</string>
<string name="download_start">已开始下载</string>
<string name="login_error">登录验证失败!</string>
</resources>
19 changes: 10 additions & 9 deletions app/src/main/res/values-zh-rTW/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<string name="app_about">關於</string>
<string name="app_summary">取得 HyperOS/MIUI 更新信息</string>
<string name="app_github">在 &lt;a href="https://github.com/YuKongA/Updater">GitHub&lt;/a> 查看源码</string>
<string name="login">登陸</string>
<string name="login">登入</string>
<string name="code_name">設備代號</string>
<string name="device_name">設備名稱</string>
<string name="system_version">系統版本</string>
Expand All @@ -19,29 +19,30 @@
<string name="account">帳戶</string>
<string name="password">密碼</string>
<string name="cancel">取消</string>
<string name="logging_in">登陸中</string>
<string name="login_successful">登陸成功</string>
<string name="logging_in">登入中</string>
<string name="login_successful">登入成功</string>
<string name="request_sign_failed">請求 _sign 失敗!</string>
<string name="account_or_password_empty">帳號或密碼為空</string>
<string name="no_account">未登陸</string>
<string name="account_or_password_empty">帳號或密碼為空</string>
<string name="no_account">未登入</string>
<string name="login_desc">正在使用 v1 端口</string>
<string name="using_v2">正在使用 v2 端口</string>
<string name="logged_in">已登入</string>
<string name="copy_button">複製</string>
<string name="download_button">下載</string>
<string name="unknown_error">取得密鑰失敗</string>
<string name="security_error">取得密鑰失敗</string>
<string name="logout">登出</string>
<string name="confirm">確定</string>
<string name="logout_successful">登出成功</string>
<string name="logout_desc">確定要登出麼?</string>
<string name="regions_code">區域代號</string>
<string name="login_expired">登陸已過期</string>
<string name="login_expired_desc">建议重新登陸</string>
<string name="login_expired_dialog">偵測到登陸已過期,建議重新登陸。</string>
<string name="login_expired">登入已過期</string>
<string name="login_expired_desc">建议重新登入</string>
<string name="login_expired_dialog">偵測到登入已過期,建議重新登入。</string>
<string name="global">全球帳戶</string>
<string name="download_method">下載方式</string>
<string name="download_method_desc">選擇一個下載方式</string>
<string name="android_default">預設</string>
<string name="other">其它</string>
<string name="download_start">已開始下載</string>
<string name="login_error">登入验证失败!</string>
</resources>
7 changes: 4 additions & 3 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@
<string name="cancel">Cancel</string>
<string name="logging_in">Logging in</string>
<string name="login_successful">Login successful</string>
<string name="request_sign_failed">Request for _sign failed</string>
<string name="account_or_password_empty">Account or Password empty</string>
<string name="request_sign_failed">Request for _sign failed!</string>
<string name="account_or_password_empty">Account or Password empty!</string>
<string name="no_account">No account</string>
<string name="login_desc">Using v1 interface</string>
<string name="using_v2">Using v2 interface</string>
Expand All @@ -36,7 +36,7 @@
<string name="cdn_link" translatable="false">http://cdnorg.d.miui.com/%1$s/%2$s</string>
<string name="copy_button">Copy</string>
<string name="download_button">Download</string>
<string name="unknown_error">Failed to get security key</string>
<string name="security_error">Failed to get security key</string>
<string name="logout">Logout</string>
<string name="confirm">Confirm</string>
<string name="logout_successful">Logout successful</string>
Expand All @@ -51,4 +51,5 @@
<string name="android_default">Default</string>
<string name="other">Other</string>
<string name="download_start">Download has started</string>
<string name="login_error">Incorrect account or password!</string>
</resources>

0 comments on commit 4907af9

Please sign in to comment.