From d3157148246a2e8aedee8165973c989d270c7c15 Mon Sep 17 00:00:00 2001 From: Luke100000 Date: Mon, 11 Nov 2024 10:02:05 +0100 Subject: [PATCH] fixed #1017 --- changelog.md | 7 ++- .../client/gui/immersive_library/Auth.java | 44 +++++++------------ 2 files changed, 23 insertions(+), 28 deletions(-) diff --git a/changelog.md b/changelog.md index f558cd2aa1..00542dd9f9 100644 --- a/changelog.md +++ b/changelog.md @@ -1,3 +1,7 @@ +# 7.5.21 + +* Fixed Library Auth when updating from older version + # 7.5.20 * Fixed Library Auth @@ -31,7 +35,8 @@ # 7.5.15 -* Merged [Inworld integration](https://github.com/Luke100000/minecraft-comes-alive/wiki/GPT3-based-conversations) branch (thanks CSCMe!) +* Merged [Inworld integration](https://github.com/Luke100000/minecraft-comes-alive/wiki/GPT3-based-conversations) + branch (thanks CSCMe!) * Fixed incompatibility with Cobblemon (thanks Apion!) * Fixed AI issues with Grim Reapers, causing him to go much higher than intended * Fixed issues when using Arabic numerals diff --git a/common/src/main/java/net/mca/client/gui/immersive_library/Auth.java b/common/src/main/java/net/mca/client/gui/immersive_library/Auth.java index 2b1035ed06..cbbf53e576 100644 --- a/common/src/main/java/net/mca/client/gui/immersive_library/Auth.java +++ b/common/src/main/java/net/mca/client/gui/immersive_library/Auth.java @@ -6,12 +6,10 @@ import net.minecraft.util.Util; import javax.annotation.Nullable; -import java.io.BufferedWriter; -import java.io.File; -import java.io.FileWriter; import java.io.IOException; import java.nio.charset.StandardCharsets; import java.nio.file.Files; +import java.nio.file.Path; import java.nio.file.Paths; import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; @@ -32,7 +30,7 @@ private static String newToken() { @Nullable public static String loadToken() { try { - return Files.readString(Paths.get("./immersiveLibraryToken")); + return Files.readString(Paths.get("./immersiveLibraryToken_v2")); } catch (IOException e) { return null; } @@ -52,7 +50,7 @@ public static boolean hasToken() { public static void saveToken() { try { - Files.writeString(Paths.get("./immersiveLibraryToken"), currentToken); + Files.writeString(Paths.get("./immersiveLibraryToken_v2"), currentToken); } catch (IOException e) { MCA.LOGGER.error(e); } @@ -60,13 +58,7 @@ public static void saveToken() { public static void clearToken() { //noinspection ResultOfMethodCallIgnored - Paths.get("./immersiveLibraryToken").toFile().delete(); - } - - private static void write(String path, String content) throws IOException { - try (BufferedWriter writer = new BufferedWriter(new FileWriter(path))) { - writer.write(content); - } + Paths.get("./immersiveLibraryToken_v2").toFile().delete(); } public static String sha256(String input) { @@ -93,26 +85,24 @@ public static String createDataState(String username, String token) { } public static void authenticate(String username) { - try { - String tmpdir = Files.createTempDirectory("immersive_library").toFile().getAbsolutePath(); + // The unique, private token used to authenticate once authorized + currentToken = newToken(); - // The unique, private token used to authenticate once authorized - currentToken = newToken(); + // Inject token into request + String content = RES_PAGE; + content = content.replace("{URL}", Config.getInstance().immersiveLibraryUrl); + content = content.replace("{STATE}", createDataState(username, currentToken)); - // Store new token - saveToken(); - - // Inject token into request - String content = RES_PAGE; - content = content.replace("{URL}", Config.getInstance().immersiveLibraryUrl); - content = content.replace("{STATE}", createDataState(username, currentToken)); - write(tmpdir + "/page.html", content); - - // Open the authorization URL in the user's default web browser - Util.getOperatingSystem().open((new File(tmpdir + "/page.html")).toURI()); + // Save page + Path pageFile = Path.of("./immersiveLibraryAuthPage.html"); + try { + Files.writeString(pageFile, content); } catch (IOException e) { throw new RuntimeException(e); } + + // Open the authorization URL in the user's default web browser + Util.getOperatingSystem().open(pageFile.toUri()); } private static final String RES_PAGE = """