Skip to content

Commit

Permalink
fixed #1017
Browse files Browse the repository at this point in the history
  • Loading branch information
Luke100000 committed Nov 11, 2024
1 parent 6b6de50 commit d315714
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 28 deletions.
7 changes: 6 additions & 1 deletion changelog.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# 7.5.21

* Fixed Library Auth when updating from older version

# 7.5.20

* Fixed Library Auth
Expand Down Expand Up @@ -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
Expand Down
44 changes: 17 additions & 27 deletions common/src/main/java/net/mca/client/gui/immersive_library/Auth.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
}
Expand All @@ -52,21 +50,15 @@ 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);
}
}

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) {
Expand All @@ -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 = """
Expand Down

0 comments on commit d315714

Please sign in to comment.