mirror of
https://gitlab.bixilon.de/bixilon/minosoft.git
synced 2025-09-16 19:05:02 -04:00
refactor URL stream handlers
This commit is contained in:
parent
854e0bcd8f
commit
6520edf786
@ -24,6 +24,7 @@ import de.bixilon.minosoft.protocol.protocol.ProtocolDefinition;
|
||||
import de.bixilon.minosoft.util.logging.Log;
|
||||
import de.bixilon.minosoft.util.microsoft.MicrosoftOAuthUtils;
|
||||
import de.bixilon.minosoft.util.task.time.TimeWorker;
|
||||
import de.bixilon.minosoft.util.url.URLProtocolStreamHandlers;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.io.*;
|
||||
@ -288,7 +289,7 @@ public final class Util {
|
||||
|
||||
public static void initUtilClasses() {
|
||||
forceClassInit(Log.class);
|
||||
forceClassInit(ResourceURLHandler.class);
|
||||
forceClassInit(URLProtocolStreamHandlers.class);
|
||||
forceClassInit(MicrosoftOAuthUtils.class);
|
||||
forceClassInit(TimeWorker.class);
|
||||
}
|
||||
|
@ -18,18 +18,19 @@ import de.bixilon.minosoft.protocol.protocol.ProtocolDefinition
|
||||
import de.bixilon.minosoft.util.KUtil.asList
|
||||
import de.bixilon.minosoft.util.KUtil.toLong
|
||||
import de.bixilon.minosoft.util.KUtil.unsafeCast
|
||||
import de.bixilon.minosoft.util.Util
|
||||
import de.bixilon.minosoft.util.http.HTTP2.getJson
|
||||
import de.bixilon.minosoft.util.http.HTTP2.postJson
|
||||
import de.bixilon.minosoft.util.logging.Log
|
||||
import de.bixilon.minosoft.util.logging.LogLevels
|
||||
import de.bixilon.minosoft.util.logging.LogMessageType
|
||||
import de.bixilon.minosoft.util.nbt.tag.NBTUtil.asCompound
|
||||
import de.bixilon.minosoft.util.url.URLProtocolStreamHandlers
|
||||
import java.net.URL
|
||||
import java.net.URLConnection
|
||||
import java.net.URLStreamHandler
|
||||
|
||||
object MicrosoftOAuthUtils {
|
||||
val NULL_URL_CONNECTION: URLConnection = object : URLConnection(null) {
|
||||
override fun connect() {}
|
||||
}
|
||||
|
||||
fun loginToMicrosoftAccount(authorizationCode: String): MicrosoftAccount {
|
||||
Log.log(LogMessageType.AUTHENTICATION, LogLevels.INFO) { "Logging into microsoft account..." }
|
||||
@ -70,7 +71,7 @@ object MicrosoftOAuthUtils {
|
||||
|
||||
response.body!!
|
||||
if (response.statusCode != 200) {
|
||||
throw LoginException(response.statusCode, "Could not get authenticate against xbox live ", response.body.toString())
|
||||
throw LoginException(response.statusCode, "Could not authenticate with xbox live token", response.body.toString())
|
||||
}
|
||||
return Pair(response.body["Token"].unsafeCast(), response.body["DisplayClaims"].asCompound()["xui"].asList()[0].asCompound()["uhs"].unsafeCast())
|
||||
}
|
||||
@ -92,7 +93,7 @@ object MicrosoftOAuthUtils {
|
||||
2148916238 -> "This account is a child account!"
|
||||
else -> response.body["Message"].unsafeCast()
|
||||
}
|
||||
throw LoginException(response.statusCode, "Could not get authenticate against XSTS token ", errorMessage)
|
||||
throw LoginException(response.statusCode, "Could not get xsts token ", errorMessage)
|
||||
}
|
||||
return response.body["Token"].unsafeCast()
|
||||
}
|
||||
@ -128,17 +129,14 @@ object MicrosoftOAuthUtils {
|
||||
}
|
||||
|
||||
init {
|
||||
// ToDo
|
||||
// URL.setURLStreamHandlerFactory {
|
||||
// if (it == "ms-xal-" + ProtocolDefinition.MICROSOFT_ACCOUNT_APPLICATION_ID) {
|
||||
// return@setURLStreamHandlerFactory object : URLStreamHandler() {
|
||||
// override fun openConnection(url: URL): URLConnection {
|
||||
// loginToMicrosoftAccount(Util.urlQueryToMap(url.query)["code"]!!)
|
||||
// return NULL_URL_CONNECTION
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
// return@setURLStreamHandlerFactory null
|
||||
// }
|
||||
URLProtocolStreamHandlers.PROTOCOLS["ms-xal-" + ProtocolDefinition.MICROSOFT_ACCOUNT_APPLICATION_ID] = LoginURLHandler
|
||||
}
|
||||
|
||||
private object LoginURLHandler : URLStreamHandler() {
|
||||
|
||||
override fun openConnection(url: URL): URLConnection {
|
||||
loginToMicrosoftAccount(Util.urlQueryToMap(url.query)["code"]!!)
|
||||
return URLProtocolStreamHandlers.NULL_URL_CONNECTION
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -11,7 +11,7 @@
|
||||
* This software is not affiliated with Mojang AB, the original developer of Minecraft.
|
||||
*/
|
||||
|
||||
package de.bixilon.minosoft.util
|
||||
package de.bixilon.minosoft.util.url
|
||||
|
||||
import de.bixilon.minosoft.Minosoft
|
||||
import de.bixilon.minosoft.data.registries.ResourceLocation
|
||||
@ -19,33 +19,15 @@ import java.io.InputStream
|
||||
import java.net.URL
|
||||
import java.net.URLConnection
|
||||
import java.net.URLStreamHandler
|
||||
import java.net.URLStreamHandlerFactory
|
||||
|
||||
|
||||
object ResourceURLHandler {
|
||||
object ResourceURLHandler : URLStreamHandler() {
|
||||
|
||||
init {
|
||||
URL.setURLStreamHandlerFactory(ResourceURLStreamHandlerFactory)
|
||||
override fun openConnection(url: URL?): URLConnection {
|
||||
return ResourceURLConnection(url)
|
||||
}
|
||||
|
||||
|
||||
private object ResourceStreamHandler : URLStreamHandler() {
|
||||
override fun openConnection(url: URL?): URLConnection {
|
||||
return ResourceURLConnection(url)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private object ResourceURLStreamHandlerFactory : URLStreamHandlerFactory {
|
||||
|
||||
override fun createURLStreamHandler(protocol: String): URLStreamHandler? {
|
||||
if (protocol == "resource") {
|
||||
return ResourceStreamHandler
|
||||
}
|
||||
return null
|
||||
}
|
||||
}
|
||||
|
||||
private class ResourceURLConnection(url: URL?) : URLConnection(url) {
|
||||
override fun connect() {
|
||||
}
|
@ -0,0 +1,38 @@
|
||||
/*
|
||||
* Minosoft
|
||||
* Copyright (C) 2021 Moritz Zwerger
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*
|
||||
* This software is not affiliated with Mojang AB, the original developer of Minecraft.
|
||||
*/
|
||||
|
||||
package de.bixilon.minosoft.util.url
|
||||
|
||||
import java.net.URL
|
||||
import java.net.URLConnection
|
||||
import java.net.URLStreamHandler
|
||||
import java.net.URLStreamHandlerFactory
|
||||
|
||||
object URLProtocolStreamHandlers : URLStreamHandlerFactory {
|
||||
val PROTOCOLS: MutableMap<String, URLStreamHandler> = mutableMapOf(
|
||||
"resource" to ResourceURLHandler,
|
||||
)
|
||||
|
||||
init {
|
||||
URL.setURLStreamHandlerFactory(this)
|
||||
}
|
||||
|
||||
override fun createURLStreamHandler(protocol: String?): URLStreamHandler? {
|
||||
return PROTOCOLS[protocol]
|
||||
}
|
||||
|
||||
|
||||
val NULL_URL_CONNECTION: URLConnection = object : URLConnection(null) {
|
||||
override fun connect() {}
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user