Skip to content

Commit

Permalink
MicrosoftAuth: 优化微软登录部分代码
Browse files Browse the repository at this point in the history
  • Loading branch information
purofle committed Jan 7, 2024
1 parent d1528ea commit 40826b4
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -51,24 +48,17 @@ object MicrosoftAuth {
}.body()
}

fun authorizationFlow(deviceCode: String): Flow<SuccessAuthentication> = flow {
var getToken = true
while (getToken) {
val result = authorizationDeviceCode(deviceCode)
runCatching {
val authError = result.toJsonObject<AuthorizationError>()
if (authError.error != PENDING) {
throw Exception(authError.error.toString())
}
}.onFailure {
if (it is CancellationException) {
throw it
}
emit(result.toJsonObject<SuccessAuthentication>())
getToken = false
}.onSuccess {
delay(5000)
suspend fun authorization(deviceCode: String): SuccessAuthentication {
val result = authorizationDeviceCode(deviceCode)
return try {
result.toJsonObject<SuccessAuthentication>()
} catch (e: Exception) {
val authError = result.toJsonObject<AuthorizationError>()
if (authError.error != PENDING) {
throw Exception(authError.error.toString())
}
delay(5000)
authorization(deviceCode)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
27 changes: 14 additions & 13 deletions src/main/kotlin/com/github/purofle/nmsl/pages/GamePage.kt
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ class GamePage : Page {
ExtendedFloatingActionButton(
{
scope.launch(Dispatchers.IO) {
val gameDownloader = getGameDownloader(selectedGame)
val gameDownloader = getGameDownloader(selectedGame)
startGame(selectedGame, gameDownloader.getLauncherArgument())
}

Expand Down Expand Up @@ -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") },
Expand Down

0 comments on commit 40826b4

Please sign in to comment.