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") },