From 40826b4204aa1f6d122b861b37bcacdcd450af00 Mon Sep 17 00:00:00 2001 From: purofle Date: Sun, 7 Jan 2024 16:39:29 +0800 Subject: [PATCH] =?UTF-8?q?MicrosoftAuth:=20=E4=BC=98=E5=8C=96=E5=BE=AE?= =?UTF-8?q?=E8=BD=AF=E7=99=BB=E5=BD=95=E9=83=A8=E5=88=86=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../nmsl/auth/microsoft/MicrosoftAuth.kt | 30 +++++++------------ .../nmsl/auth/microsoft/DeviceCodeFlowTest.kt | 3 +- .../com/github/purofle/nmsl/pages/GamePage.kt | 27 +++++++++-------- 3 files changed, 25 insertions(+), 35 deletions(-) diff --git a/NMSLCore/src/main/kotlin/com/github/purofle/nmsl/auth/microsoft/MicrosoftAuth.kt b/NMSLCore/src/main/kotlin/com/github/purofle/nmsl/auth/microsoft/MicrosoftAuth.kt index e151374..dc4a1a6 100644 --- a/NMSLCore/src/main/kotlin/com/github/purofle/nmsl/auth/microsoft/MicrosoftAuth.kt +++ b/NMSLCore/src/main/kotlin/com/github/purofle/nmsl/auth/microsoft/MicrosoftAuth.kt @@ -8,9 +8,6 @@ import io.ktor.client.request.* import io.ktor.client.request.forms.* import io.ktor.http.* import kotlinx.coroutines.delay -import kotlinx.coroutines.flow.Flow -import kotlinx.coroutines.flow.flow -import java.util.concurrent.CancellationException object MicrosoftAuth { @@ -51,24 +48,17 @@ object MicrosoftAuth { }.body() } - fun authorizationFlow(deviceCode: String): Flow = flow { - var getToken = true - while (getToken) { - val result = authorizationDeviceCode(deviceCode) - runCatching { - val authError = result.toJsonObject() - if (authError.error != PENDING) { - throw Exception(authError.error.toString()) - } - }.onFailure { - if (it is CancellationException) { - throw it - } - emit(result.toJsonObject()) - getToken = false - }.onSuccess { - delay(5000) + suspend fun authorization(deviceCode: String): SuccessAuthentication { + val result = authorizationDeviceCode(deviceCode) + return try { + result.toJsonObject() + } catch (e: Exception) { + val authError = result.toJsonObject() + if (authError.error != PENDING) { + throw Exception(authError.error.toString()) } + delay(5000) + authorization(deviceCode) } } diff --git a/NMSLCore/src/test/kotlin/com/github/purofle/nmsl/auth/microsoft/DeviceCodeFlowTest.kt b/NMSLCore/src/test/kotlin/com/github/purofle/nmsl/auth/microsoft/DeviceCodeFlowTest.kt index 43f7919..ab82090 100644 --- a/NMSLCore/src/test/kotlin/com/github/purofle/nmsl/auth/microsoft/DeviceCodeFlowTest.kt +++ b/NMSLCore/src/test/kotlin/com/github/purofle/nmsl/auth/microsoft/DeviceCodeFlowTest.kt @@ -4,14 +4,13 @@ import com.github.purofle.nmsl.config.Config import com.github.purofle.nmsl.config.LauncherConfig import com.github.purofle.nmsl.config.Msa import com.github.purofle.nmsl.config.NmslConfig -import kotlinx.coroutines.flow.first import kotlinx.coroutines.runBlocking fun main() { runBlocking { val deviceFlow = MicrosoftAuth.getDeviceAuthorization() println(deviceFlow.message) - val auth = MicrosoftAuth.authorizationFlow(deviceFlow.deviceCode).first() + val auth = MicrosoftAuth.authorization(deviceFlow.deviceCode) // val auth = DeviceCodeFlow.authorizationRefreshToken(LauncherConfig.config.msa.refreshToken) diff --git a/src/main/kotlin/com/github/purofle/nmsl/pages/GamePage.kt b/src/main/kotlin/com/github/purofle/nmsl/pages/GamePage.kt index bc753e7..a8e9214 100644 --- a/src/main/kotlin/com/github/purofle/nmsl/pages/GamePage.kt +++ b/src/main/kotlin/com/github/purofle/nmsl/pages/GamePage.kt @@ -93,7 +93,7 @@ class GamePage : Page { ExtendedFloatingActionButton( { scope.launch(Dispatchers.IO) { - val gameDownloader = getGameDownloader(selectedGame) + val gameDownloader = getGameDownloader(selectedGame) startGame(selectedGame, gameDownloader.getLauncherArgument()) } @@ -181,22 +181,23 @@ fun MicrosoftLoginDialog(onDismissCallback: () -> Unit, callback: () -> Unit) { val deviceFlow = MicrosoftAuth.getDeviceAuthorization() deviceCode = deviceFlow.userCode - MicrosoftAuth.authorizationFlow(deviceFlow.deviceCode).collect { auth -> - Config.createConfig( - NmslConfig( - Msa( - accessToken = auth.accessToken, - refreshToken = auth.refreshToken, - expiresIn = auth.expiresIn - ), - MinecraftAuth.authenticate(auth.accessToken), - ) + MicrosoftAuth.authorization(deviceFlow.deviceCode).let { auth -> + Config.createConfig( + NmslConfig( + Msa( + accessToken = auth.accessToken, + refreshToken = auth.refreshToken, + expiresIn = auth.expiresIn + ), + MinecraftAuth.authenticate(auth.accessToken), ) - callback() + ) + callback() } } - val text = if (deviceCode.isNotEmpty()) "复制 $deviceCode 并打开 https://www.microsoft.com/link 来登录" else "请稍等..." + val text = + if (deviceCode.isNotEmpty()) "复制 $deviceCode 并打开 https://www.microsoft.com/link 来登录" else "请稍等..." AlertDialog( onDismissRequest = { onDismissCallback() }, title = { Text("Microsoft Login") },